Fixed (again) the way URL locking works.

URL input is considered locked when it is focused and original text was
changed. Pressing ESC will now change URL input's text to the current URL.

Signed-off-by: Adrien Destugues <[email protected]>
This commit is contained in:
Victor
2017-06-25 07:40:35 +02:00
committed by Adrien Destugues
parent 4c9ec29000
commit af5001925e
3 changed files with 44 additions and 72 deletions
+22 -8
View File
@@ -735,12 +735,10 @@ BrowserWindow::DispatchMessage(BMessage* message, BHandler* target)
// Do it in such a way that the user sees the Go-button go down. // Do it in such a way that the user sees the Go-button go down.
_InvokeButtonVisibly(fURLInputGroup->GoButton()); _InvokeButtonVisibly(fURLInputGroup->GoButton());
return; return;
} } else if (bytes[0] == B_ESCAPE) {
// Lock the URL text control to prevent changes while user is // Replace edited text with the current URL.
// typing and set a timer to unlock it after a set period fURLInputGroup->LockURLInput(false);
// of time. fURLInputGroup->SetText(CurrentWebView()->MainFrameURL());
else {
fURLInputGroup->LockURLInput();
} }
} else if (target == fFindTextControl->TextView()) { } else if (target == fFindTextControl->TextView()) {
// Handle B_RETURN when the find text control has focus. // Handle B_RETURN when the find text control has focus.
@@ -1314,6 +1312,10 @@ BrowserWindow::SetCurrentWebView(BWebView* webView)
} else } else
webView->MakeFocus(true); webView->MakeFocus(true);
bool state = fURLInputGroup->IsURLInputLocked();
fURLInputGroup->LockURLInput(false);
// Unlock it so the following code can update the URL
if (userData != NULL) { if (userData != NULL) {
fURLInputGroup->SetPageIcon(userData->PageIcon()); fURLInputGroup->SetPageIcon(userData->PageIcon());
if (userData->URLInputContents().Length()) if (userData->URLInputContents().Length())
@@ -1330,6 +1332,9 @@ BrowserWindow::SetCurrentWebView(BWebView* webView)
fURLInputGroup->SetText(webView->MainFrameURL()); fURLInputGroup->SetText(webView->MainFrameURL());
} }
fURLInputGroup->LockURLInput(state);
// Restore the state
// Trigger update of the interface to the new page, by requesting // Trigger update of the interface to the new page, by requesting
// to resend all notifications. // to resend all notifications.
webView->WebPage()->ResendNotifications(); webView->WebPage()->ResendNotifications();
@@ -1474,8 +1479,15 @@ BrowserWindow::CloseWindowRequested(BWebView* view)
void void
BrowserWindow::LoadNegotiating(const BString& url, BWebView* view) BrowserWindow::LoadNegotiating(const BString& url, BWebView* view)
{ {
if (view != CurrentWebView()) if (view != CurrentWebView()) {
return; // Update the userData contents instead so the user sees
// the correct URL when they switch back to that tab.
PageUserData* userData = static_cast<PageUserData*>(
view->GetUserData());
if (userData != NULL && userData->URLInputContents().Length() == 0) {
userData->SetURLInputContents(url);
}
}
fURLInputGroup->SetText(url.String()); fURLInputGroup->SetText(url.String());
@@ -1729,6 +1741,8 @@ void
BrowserWindow::UpdateGlobalHistory(const BString& url) BrowserWindow::UpdateGlobalHistory(const BString& url)
{ {
BrowsingHistory::DefaultInstance()->AddItem(BrowsingHistoryItem(url)); BrowsingHistory::DefaultInstance()->AddItem(BrowsingHistoryItem(url));
fURLInputGroup->SetText(CurrentWebView()->MainFrameURL());
} }
+21 -56
View File
@@ -151,7 +151,6 @@ public:
virtual void MouseDown(BPoint where); virtual void MouseDown(BPoint where);
virtual void KeyDown(const char* bytes, int32 numBytes); virtual void KeyDown(const char* bytes, int32 numBytes);
virtual void MakeFocus(bool focused = true); virtual void MakeFocus(bool focused = true);
virtual void SetPreviousText(const char* text);
virtual BSize MinSize(); virtual BSize MinSize();
virtual BSize MaxSize(); virtual BSize MaxSize();
@@ -170,7 +169,6 @@ private:
private: private:
URLInputGroup* fURLInputGroup; URLInputGroup* fURLInputGroup;
TextViewCompleter* fURLAutoCompleter; TextViewCompleter* fURLAutoCompleter;
BString fPreviousText;
bool fUpdateAutoCompleterChoices; bool fUpdateAutoCompleterChoices;
}; };
@@ -181,7 +179,6 @@ URLInputGroup::URLTextView::URLTextView(URLInputGroup* parent)
fURLInputGroup(parent), fURLInputGroup(parent),
fURLAutoCompleter(new TextViewCompleter(this, fURLAutoCompleter(new TextViewCompleter(this,
new BrowsingHistoryChoiceModel())), new BrowsingHistoryChoiceModel())),
fPreviousText(""),
fUpdateAutoCompleterChoices(true) fUpdateAutoCompleterChoices(true)
{ {
MakeResizable(true); MakeResizable(true);
@@ -285,8 +282,8 @@ URLInputGroup::URLTextView::KeyDown(const char* bytes, int32 numBytes)
break; break;
case B_ESCAPE: case B_ESCAPE:
// Revert to text as it was when we received keyboard focus. // Text already unlocked && replaced in BrowserWindow,
SetText(fPreviousText.String()); // now select it.
SelectAll(); SelectAll();
break; break;
@@ -295,40 +292,37 @@ URLInputGroup::URLTextView::KeyDown(const char* bytes, int32 numBytes)
break; break;
default: default:
{
BString currentText = Text();
BTextView::KeyDown(bytes, numBytes); BTextView::KeyDown(bytes, numBytes);
// Lock the URL input if it was modified
if (!fURLInputGroup->IsURLInputLocked()
&& Text() != currentText)
fURLInputGroup->LockURLInput();
break; break;
}
} }
} }
void void
URLInputGroup::URLTextView::MakeFocus(bool focus) URLInputGroup::URLTextView::MakeFocus(bool focus)
{ {
// Unlock the URL input ahead of time if focus was lost. // Unlock the URL input if focus was lost.
if (!focus) { if (!focus)
fURLInputGroup->LockURLInput(false); fURLInputGroup->LockURLInput(false);
}
if (focus == IsFocus()) if (focus == IsFocus())
return; return;
BTextView::MakeFocus(focus); BTextView::MakeFocus(focus);
if (focus) { if (focus)
fPreviousText = Text();
SelectAll(); SelectAll();
}
fURLInputGroup->Invalidate(); fURLInputGroup->Invalidate();
} }
void
URLInputGroup::URLTextView::SetPreviousText(const char* text)
{
fPreviousText = text;
}
BSize BSize
URLInputGroup::URLTextView::MinSize() URLInputGroup::URLTextView::MinSize()
{ {
@@ -596,7 +590,6 @@ private:
URLInputGroup::URLInputGroup(BMessage* goMessage) URLInputGroup::URLInputGroup(BMessage* goMessage)
: :
BGroupView(B_HORIZONTAL, 0.0), BGroupView(B_HORIZONTAL, 0.0),
fURLLockTimeout(NULL),
fWindowActive(false), fWindowActive(false),
fURLLocked(false) fURLLocked(false)
{ {
@@ -629,7 +622,6 @@ URLInputGroup::URLInputGroup(BMessage* goMessage)
URLInputGroup::~URLInputGroup() URLInputGroup::~URLInputGroup()
{ {
delete fURLLockTimeout;
} }
@@ -683,14 +675,9 @@ URLInputGroup::TextView() const
void void
URLInputGroup::SetText(const char* text) URLInputGroup::SetText(const char* text)
{ {
// Ignore setting the text, if the user edited the URL in the last // Ignore setting the text, if the input is locked.
// couple of seconds. Instead set the previous text in the text view, if (fURLLocked)
// so if the user presses ESC the input will update to show the new
// text.
if (fURLLocked) {
fTextView->SetPreviousText(text);
return; return;
}
if (!text || !Text() || strcmp(Text(), text) != 0) { if (!text || !Text() || strcmp(Text(), text) != 0) {
fTextView->SetUpdateAutoCompleterChoices(false); fTextView->SetUpdateAutoCompleterChoices(false);
@@ -721,37 +708,15 @@ URLInputGroup::SetPageIcon(const BBitmap* icon)
} }
bool
URLInputGroup::IsURLInputLocked() const
{
return fURLLocked;
}
void void
URLInputGroup::LockURLInput(bool lock) URLInputGroup::LockURLInput(bool lock)
{ {
fURLLocked = lock; fURLLocked = lock;
if (lock) {
if (fURLLockTimeout == NULL) {
fURLLockTimeout = new BMessageRunner(this,
new BMessage(MSG_LOCK_TIMEOUT), LOCK_TIMEOUT, 1);
} else {
fURLLockTimeout->SetInterval(LOCK_TIMEOUT);
}
}
} }
void
URLInputGroup::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_LOCK_TIMEOUT:
{
delete fURLLockTimeout;
fURLLockTimeout = NULL;
LockURLInput(false);
break;
}
default:
{
BGroupView(message);
break;
}
}
}
+1 -8
View File
@@ -6,18 +6,12 @@
#define URL_INPUT_GROUP_H #define URL_INPUT_GROUP_H
#include <GroupView.h> #include <GroupView.h>
#include <MessageRunner.h>
class BButton; class BButton;
class BTextView; class BTextView;
class URLInputGroup : public BGroupView { class URLInputGroup : public BGroupView {
private:
static const uint32 MSG_LOCK_TIMEOUT = 'loti';
static const bigtime_t LOCK_TIMEOUT = 1000000;
// Lock will timeout in one second
public: public:
URLInputGroup(BMessage* goMessage); URLInputGroup(BMessage* goMessage);
virtual ~URLInputGroup(); virtual ~URLInputGroup();
@@ -35,14 +29,13 @@ public:
void SetPageIcon(const BBitmap* icon); void SetPageIcon(const BBitmap* icon);
bool IsURLInputLocked() const;
virtual void LockURLInput(bool lock = true); virtual void LockURLInput(bool lock = true);
virtual void MessageReceived(BMessage* message);
private: private:
class PageIconView; class PageIconView;
class URLTextView; class URLTextView;
BMessageRunner* fURLLockTimeout;
PageIconView* fIconView; PageIconView* fIconView;
URLTextView* fTextView; URLTextView* fTextView;
BButton* fGoButton; BButton* fGoButton;