From 93b4113386d67ddf6688b38e862c10ebd1d3c68f Mon Sep 17 00:00:00 2001
From: Wiktor <vikkindhart@gmail.com>
Date: Sun, 11 Jun 2017 11:14:17 +0200
Subject: [PATCH] Changed URL input locking to use BMessageRunner.
In the current state, opening a link in a new tab will cause URL input to
become empty. This fixes the issue by changing the way locking works and
implementing a lock timeout. The text passed to SetText() will be instead
stored as previous text, so if the user presses ESC after editing the URL,
URL input will update to the latest sent text (i. e. current URL). Also
fixed two lines the style checker was complaining about.
Fixes #13548.
---
src/apps/webpositive/BrowserWindow.cpp | 12 +++++--
src/apps/webpositive/URLInputGroup.cpp | 62 ++++++++++++++++++++++++++++++++--
src/apps/webpositive/URLInputGroup.h | 11 ++++++
3 files changed, 79 insertions(+), 6 deletions(-)
diff --git a/src/apps/webpositive/BrowserWindow.cpp b/src/apps/webpositive/BrowserWindow.cpp
index 49eb6f6..e899593 100644
a
|
b
|
BrowserWindow::DispatchMessage(BMessage* message, BHandler* target)
|
736 | 736 | _InvokeButtonVisibly(fURLInputGroup->GoButton()); |
737 | 737 | return; |
738 | 738 | } |
| 739 | // Lock the URL text control to prevent changes while user is |
| 740 | // typing and set a timer to unlock it after a set period |
| 741 | // of time. |
| 742 | else { |
| 743 | fURLInputGroup->LockURLInput(); |
| 744 | } |
739 | 745 | } else if (target == fFindTextControl->TextView()) { |
740 | 746 | // Handle B_RETURN when the find text control has focus. |
741 | 747 | if (bytes[0] == B_RETURN) { |
… |
… |
BrowserWindow::MessageReceived(BMessage* message)
|
844 | 850 | entry_ref ref; |
845 | 851 | BString name; |
846 | 852 | |
847 | | if (message->FindRef("directory", &ref) == B_OK && |
848 | | message->FindString("name", &name) == B_OK) { |
| 853 | if (message->FindRef("directory", &ref) == B_OK |
| 854 | && message->FindString("name", &name) == B_OK) { |
849 | 855 | BDirectory dir(&ref); |
850 | 856 | BFile output(&dir, name, |
851 | 857 | B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); |
… |
… |
BrowserWindow::_EncodeURIComponent(const BString& search)
|
2448 | 2454 | void |
2449 | 2455 | BrowserWindow::_VisitURL(const BString& url) |
2450 | 2456 | { |
2451 | | //fURLInputGroup->TextView()->SetText(url); |
| 2457 | // fURLInputGroup->TextView()->SetText(url); |
2452 | 2458 | CurrentWebView()->LoadURL(url.String()); |
2453 | 2459 | } |
2454 | 2460 | |
diff --git a/src/apps/webpositive/URLInputGroup.cpp b/src/apps/webpositive/URLInputGroup.cpp
index 45c083f..5f6b2a9 100644
a
|
b
|
public:
|
151 | 151 | virtual void MouseDown(BPoint where); |
152 | 152 | virtual void KeyDown(const char* bytes, int32 numBytes); |
153 | 153 | virtual void MakeFocus(bool focused = true); |
| 154 | virtual void SetPreviousText(const char* text); |
154 | 155 | |
155 | 156 | virtual BSize MinSize(); |
156 | 157 | virtual BSize MaxSize(); |
… |
… |
URLInputGroup::URLTextView::KeyDown(const char* bytes, int32 numBytes)
|
302 | 303 | void |
303 | 304 | URLInputGroup::URLTextView::MakeFocus(bool focus) |
304 | 305 | { |
| 306 | // Unlock the URL input ahead of time if focus was lost. |
| 307 | if (!focus) { |
| 308 | fURLInputGroup->LockURLInput(false); |
| 309 | } |
| 310 | |
305 | 311 | if (focus == IsFocus()) |
306 | 312 | return; |
307 | 313 | |
… |
… |
URLInputGroup::URLTextView::MakeFocus(bool focus)
|
316 | 322 | } |
317 | 323 | |
318 | 324 | |
| 325 | void |
| 326 | URLInputGroup::URLTextView::SetPreviousText(const char* text) |
| 327 | { |
| 328 | fPreviousText = text; |
| 329 | } |
| 330 | |
| 331 | |
319 | 332 | BSize |
320 | 333 | URLInputGroup::URLTextView::MinSize() |
321 | 334 | { |
… |
… |
private:
|
583 | 596 | URLInputGroup::URLInputGroup(BMessage* goMessage) |
584 | 597 | : |
585 | 598 | BGroupView(B_HORIZONTAL, 0.0), |
586 | | fWindowActive(false) |
| 599 | fURLLockTimeout(NULL), |
| 600 | fWindowActive(false), |
| 601 | fURLLocked(false) |
587 | 602 | { |
588 | 603 | GroupLayout()->SetInsets(2, 2, 2, 2); |
589 | 604 | |
… |
… |
URLInputGroup::URLInputGroup(BMessage* goMessage)
|
608 | 623 | |
609 | 624 | SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH, |
610 | 625 | B_ALIGN_VERTICAL_CENTER)); |
| 626 | |
611 | 627 | } |
612 | 628 | |
613 | 629 | |
… |
… |
URLInputGroup::TextView() const
|
666 | 682 | void |
667 | 683 | URLInputGroup::SetText(const char* text) |
668 | 684 | { |
669 | | // Ignore setting the text, if the user is currently editing the URL. |
670 | | if (fWindowActive && fTextView->IsFocus()) |
| 685 | // Ignore setting the text, if the user edited the URL in the last |
| 686 | // couple of seconds. Instead set the previous text in the text view, |
| 687 | // so if the user presses ESC the input will update to show the new |
| 688 | // text. |
| 689 | if (fURLLocked) { |
| 690 | fTextView->SetPreviousText(text); |
671 | 691 | return; |
| 692 | } |
672 | 693 | |
673 | 694 | if (!text || !Text() || strcmp(Text(), text) != 0) { |
674 | 695 | fTextView->SetUpdateAutoCompleterChoices(false); |
… |
… |
URLInputGroup::SetPageIcon(const BBitmap* icon)
|
698 | 719 | fIconView->SetIcon(icon); |
699 | 720 | } |
700 | 721 | |
| 722 | |
| 723 | void |
| 724 | URLInputGroup::LockURLInput(bool lock) |
| 725 | { |
| 726 | fURLLocked = lock; |
| 727 | if (lock) { |
| 728 | if (fURLLockTimeout == NULL) { |
| 729 | fURLLockTimeout = new BMessageRunner(this, |
| 730 | new BMessage(MSG_LOCK_TIMEOUT), LOCK_TIMEOUT, 1); |
| 731 | } else { |
| 732 | fURLLockTimeout->SetInterval(LOCK_TIMEOUT); |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | |
| 738 | void |
| 739 | URLInputGroup::MessageReceived(BMessage* message) |
| 740 | { |
| 741 | switch (message->what) { |
| 742 | case MSG_LOCK_TIMEOUT: |
| 743 | { |
| 744 | delete fURLLockTimeout; |
| 745 | fURLLockTimeout = NULL; |
| 746 | LockURLInput(false); |
| 747 | break; |
| 748 | } |
| 749 | default: |
| 750 | { |
| 751 | BGroupView(message); |
| 752 | break; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
diff --git a/src/apps/webpositive/URLInputGroup.h b/src/apps/webpositive/URLInputGroup.h
index d71f713..2e5bd72 100644
a
|
b
|
|
6 | 6 | #define URL_INPUT_GROUP_H |
7 | 7 | |
8 | 8 | #include <GroupView.h> |
| 9 | #include <MessageRunner.h> |
9 | 10 | |
10 | 11 | class BButton; |
11 | 12 | class BTextView; |
12 | 13 | |
13 | 14 | |
14 | 15 | class URLInputGroup : public BGroupView { |
| 16 | private: |
| 17 | static const uint32 MSG_LOCK_TIMEOUT = 'loti'; |
| 18 | static const bigtime_t LOCK_TIMEOUT = 1000000; |
| 19 | // Lock will timeout in one second |
| 20 | |
15 | 21 | public: |
16 | 22 | URLInputGroup(BMessage* goMessage); |
17 | 23 | virtual ~URLInputGroup(); |
… |
… |
public:
|
29 | 35 | |
30 | 36 | void SetPageIcon(const BBitmap* icon); |
31 | 37 | |
| 38 | virtual void LockURLInput(bool lock = true); |
| 39 | virtual void MessageReceived(BMessage* message); |
| 40 | |
32 | 41 | private: |
33 | 42 | class PageIconView; |
34 | 43 | class URLTextView; |
35 | 44 | |
| 45 | BMessageRunner* fURLLockTimeout; |
36 | 46 | PageIconView* fIconView; |
37 | 47 | URLTextView* fTextView; |
38 | 48 | BButton* fGoButton; |
39 | 49 | bool fWindowActive; |
| 50 | bool fURLLocked; |
40 | 51 | }; |
41 | 52 | |
42 | 53 | #endif // URL_INPUT_GROUP_H |