Wired everything and improved the TextControlCompleter behavior a bit with

regards to eating the B_RETURN key before we can dispatch in BrowserWindow. So
autocompletion for URLs basically works. What's missing is:

* Much better grouping of matches.
* Fix the delay when the BrowsingHistory is first accessed (lazy loads itself
  from disk just then, ought to do it in the application thread after startup,
  which probably makes it unnoticable before the user starts typing a URL).

git-svn-id: http://svn.haiku-os.org/webpositive/webkit/trunk@268 94f232f2-1747-11df-bad5-a5bfde151594
This commit is contained in:
stippi
2012-07-03 15:32:58 +02:00
committed by Alexandre Deckner
parent fd5768c40d
commit 1a52acbc07
8 changed files with 108 additions and 7 deletions
+68
View File
@@ -37,6 +37,7 @@
#include "BrowserApp.h" #include "BrowserApp.h"
#include "BrowsingHistory.h" #include "BrowsingHistory.h"
#include "IconButton.h" #include "IconButton.h"
#include "TextControlCompleter.h"
#include "WebPage.h" #include "WebPage.h"
#include "WebTabView.h" #include "WebTabView.h"
#include "WebView.h" #include "WebView.h"
@@ -93,6 +94,66 @@ layoutItemFor(BView* view)
} }
class BrowsingHistoryChoiceModel : public BAutoCompleter::ChoiceModel {
virtual void FetchChoicesFor(const BString& pattern)
{
int32 count = CountChoices();
for (int32 i = 0; i < count; i++) {
delete reinterpret_cast<BAutoCompleter::Choice*>(
fChoices.ItemAtFast(i));
}
fChoices.MakeEmpty();
// Search through BrowsingHistory for any matches.
BrowsingHistory* history = BrowsingHistory::defaultInstance();
if (!history->Lock())
return;
count = history->countItems();
for (int32 i = 0; i < count; i++) {
BrowsingHistoryItem item = history->historyItemAt(i);
const BString& choiceText = item.url();
int32 matchPos = choiceText.IFindFirst(pattern);
if (matchPos < 0)
continue;
fChoices.AddItem(new BAutoCompleter::Choice(choiceText,
choiceText, matchPos, pattern.Length()));
}
history->Unlock();
}
virtual int32 CountChoices() const
{
return fChoices.CountItems();
}
virtual const BAutoCompleter::Choice* ChoiceAt(int32 index) const
{
return reinterpret_cast<BAutoCompleter::Choice*>(
fChoices.ItemAt(index));
}
private:
BList fChoices;
};
class BrowsingHistoryPatternSelector : public BAutoCompleter::PatternSelector {
virtual void SelectPatternBounds(const BString& text, int32 caretPos,
int32* start, int32* length)
{
if (!start || !length)
return;
*start = 0;
*length = text.Length();
}
};
// #pragma mark - BrowserWindow
BrowserWindow::BrowserWindow(BRect frame, const BMessenger& downloadListener, BrowserWindow::BrowserWindow(BRect frame, const BMessenger& downloadListener,
ToolbarPolicy toolbarPolicy) ToolbarPolicy toolbarPolicy)
: BWebWindow(frame, kApplicationName, : BWebWindow(frame, kApplicationName,
@@ -228,6 +289,10 @@ BrowserWindow::BrowserWindow(BRect frame, const BMessenger& downloadListener,
fURLTextControl->MakeFocus(true); fURLTextControl->MakeFocus(true);
fURLAutoCompleter = new TextControlCompleter(fURLTextControl,
new BrowsingHistoryChoiceModel(),
new BrowsingHistoryPatternSelector());
fFindGroup = layoutItemFor(findGroup); fFindGroup = layoutItemFor(findGroup);
fTabGroup = layoutItemFor(fTabManager->TabGroup()); fTabGroup = layoutItemFor(fTabManager->TabGroup());
} else { } else {
@@ -236,6 +301,7 @@ BrowserWindow::BrowserWindow(BRect frame, const BMessenger& downloadListener,
fStopButton = 0; fStopButton = 0;
fGoButton = 0; fGoButton = 0;
fURLTextControl = 0; fURLTextControl = 0;
fURLAutoCompleter = 0;
fStatusText = 0; fStatusText = 0;
fLoadingProgressBar = 0; fLoadingProgressBar = 0;
@@ -275,6 +341,8 @@ BrowserWindow::BrowserWindow(BRect frame, const BMessenger& downloadListener,
BrowserWindow::~BrowserWindow() BrowserWindow::~BrowserWindow()
{ {
delete fURLAutoCompleter;
delete fTabManager;
} }
+2
View File
@@ -43,6 +43,7 @@ class BStringView;
class BTextControl; class BTextControl;
class IconButton; class IconButton;
class TabManager; class TabManager;
class TextControlCompleter;
class BWebView; class BWebView;
enum ToolbarPolicy { enum ToolbarPolicy {
@@ -125,6 +126,7 @@ private:
IconButton* fStopButton; IconButton* fStopButton;
BButton* fGoButton; BButton* fGoButton;
BTextControl* fURLTextControl; BTextControl* fURLTextControl;
TextControlCompleter* fURLAutoCompleter;
BStringView* fStatusText; BStringView* fStatusText;
BStatusBar* fLoadingProgressBar; BStatusBar* fLoadingProgressBar;
BLayoutItem* fFindGroup; BLayoutItem* fFindGroup;
@@ -148,6 +148,16 @@ BAutoCompleter::SelectPrevious(bool wrap)
} }
bool
BAutoCompleter::IsChoiceSelected() const
{
if (fCompletionStyle)
return fCompletionStyle->IsChoiceSelected();
else
return false;
}
void void
BAutoCompleter::ApplyChoice(bool hideChoices) BAutoCompleter::ApplyChoice(bool hideChoices)
{ {
@@ -210,3 +220,4 @@ BAutoCompleter::SetCompletionStyle(CompletionStyle* style)
delete fCompletionStyle; delete fCompletionStyle;
fCompletionStyle = style; fCompletionStyle = style;
} }
@@ -97,6 +97,7 @@ public:
virtual bool Select(int32 index) = 0; virtual bool Select(int32 index) = 0;
virtual bool SelectNext(bool wrap = false) = 0; virtual bool SelectNext(bool wrap = false) = 0;
virtual bool SelectPrevious(bool wrap = false) = 0; virtual bool SelectPrevious(bool wrap = false) = 0;
virtual bool IsChoiceSelected() const = 0;
virtual void ApplyChoice(bool hideChoices = true) = 0; virtual void ApplyChoice(bool hideChoices = true) = 0;
virtual void CancelChoice() = 0; virtual void CancelChoice() = 0;
@@ -135,6 +136,7 @@ protected:
bool Select(int32 index); bool Select(int32 index);
bool SelectNext(bool wrap = false); bool SelectNext(bool wrap = false);
bool SelectPrevious(bool wrap = false); bool SelectPrevious(bool wrap = false);
bool IsChoiceSelected() const;
void ApplyChoice(bool hideChoices = true); void ApplyChoice(bool hideChoices = true);
void CancelChoice(); void CancelChoice();
@@ -96,6 +96,13 @@ BDefaultCompletionStyle::SelectPrevious(bool wrap)
} }
bool
BDefaultCompletionStyle::IsChoiceSelected() const
{
return fSelectedIndex >= 0;
}
void void
BDefaultCompletionStyle::ApplyChoice(bool hideChoices) BDefaultCompletionStyle::ApplyChoice(bool hideChoices)
{ {
@@ -112,8 +119,10 @@ BDefaultCompletionStyle::ApplyChoice(bool hideChoices)
fEditView->SetEditViewState(completedText, fEditView->SetEditViewState(completedText,
fPatternStartPos+choiceStr.Length()); fPatternStartPos+choiceStr.Length());
if (hideChoices) if (hideChoices) {
fChoiceView->HideChoices(); fChoiceView->HideChoices();
Select(-1);
}
} }
@@ -35,6 +35,7 @@ public:
virtual bool Select(int32 index); virtual bool Select(int32 index);
virtual bool SelectNext(bool wrap = false); virtual bool SelectNext(bool wrap = false);
virtual bool SelectPrevious(bool wrap = false); virtual bool SelectPrevious(bool wrap = false);
virtual bool IsChoiceSelected() const;
virtual void ApplyChoice(bool hideChoices = true); virtual void ApplyChoice(bool hideChoices = true);
virtual void CancelChoice(); virtual void CancelChoice();
@@ -10,6 +10,7 @@
#include <Looper.h> #include <Looper.h>
#include <TextControl.h> #include <TextControl.h>
#include <stdio.h>
#include "AutoCompleterDefaultImpl.h" #include "AutoCompleterDefaultImpl.h"
@@ -58,7 +59,7 @@ TextControlCompleter::TextControlWrapper::GetAdjustmentFrame()
{ {
BRect frame = fTextControl->TextView()->Bounds(); BRect frame = fTextControl->TextView()->Bounds();
frame = fTextControl->TextView()->ConvertToScreen(frame); frame = fTextControl->TextView()->ConvertToScreen(frame);
frame.InsetBy(-1, -3); frame.InsetBy(0, -3);
return frame; return frame;
} }
@@ -68,14 +69,16 @@ TextControlCompleter::TextControlCompleter(BTextControl* textControl,
: :
BAutoCompleter(new TextControlWrapper(textControl), model, BAutoCompleter(new TextControlWrapper(textControl), model,
new BDefaultChoiceView(), patternSelector), new BDefaultChoiceView(), patternSelector),
BMessageFilter(B_KEY_DOWN) BMessageFilter(B_KEY_DOWN),
fTextControl(textControl)
{ {
textControl->TextView()->AddFilter(this); fTextControl->TextView()->AddFilter(this);
} }
TextControlCompleter::~TextControlCompleter() TextControlCompleter::~TextControlCompleter()
{ {
fTextControl->TextView()->RemoveFilter(this);
} }
@@ -99,9 +102,12 @@ TextControlCompleter::Filter(BMessage* message, BHandler** target)
CancelChoice(); CancelChoice();
return B_SKIP_MESSAGE; return B_SKIP_MESSAGE;
case B_RETURN: case B_RETURN:
ApplyChoice(); if (IsChoiceSelected()) {
EditViewStateChanged(); ApplyChoice();
return B_SKIP_MESSAGE; EditViewStateChanged();
} else
CancelChoice();
return B_DISPATCH_MESSAGE;
case B_TAB: { case B_TAB: {
// make sure that the choices-view is closed when tabbing out: // make sure that the choices-view is closed when tabbing out:
CancelChoice(); CancelChoice();
@@ -36,6 +36,8 @@ private:
private: private:
BTextControl* fTextControl; BTextControl* fTextControl;
}; };
private:
BTextControl* fTextControl;
}; };
#endif // TEXT_CONTROL_COMPLETER_H #endif // TEXT_CONTROL_COMPLETER_H