Skip to content

Commit

Permalink
LibGUI+Browser: Add UrlBox class
Browse files Browse the repository at this point in the history
This allows the address bar to "select all" when initially gaining focus
as Firefox and Chrome do. A future improvement on this would be for the
Widget class to mange and provide focus transition as part of the events
instead of the UrlBox class. Currently focus is updated before the event
is provided to the UrlBox class.
  • Loading branch information
robryanx authored and awesomekling committed Aug 18, 2021
1 parent fbf824a commit 34a64ed
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 8 deletions.
8 changes: 5 additions & 3 deletions Userland/Applications/Browser/Tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Tab::Tab(BrowserWindow& window, Type type)
toolbar.add_action(window.go_home_action());
toolbar.add_action(window.reload_action());

m_location_box = toolbar.add<GUI::TextBox>();
m_location_box = toolbar.add<GUI::UrlBox>();
m_location_box->set_placeholder("Address");

m_location_box->on_return_pressed = [this] {
Expand All @@ -153,7 +153,7 @@ Tab::Tab(BrowserWindow& window, Type type)

auto url = url_from_user_input(m_location_box->text());
load(url);
view().set_focus(true);
view();
};

m_location_box->add_custom_context_menu_action(GUI::Action::create("Paste && Go", [this](auto&) {
Expand Down Expand Up @@ -312,8 +312,8 @@ Tab::Tab(BrowserWindow& window, Type type)

auto focus_location_box_action = GUI::Action::create(
"Focus location box", { Mod_Ctrl, Key_L }, Key_F6, [this](auto&) {
m_location_box->select_all();
m_location_box->set_focus(true);
m_location_box->select_current_line();
},
this);

Expand Down Expand Up @@ -371,6 +371,8 @@ void Tab::load(const URL& url, LoadType load_type)
m_page_view->load(url);
else
m_web_content_view->load(url);

m_location_box->set_focus(false);
}

URL Tab::url() const
Expand Down
2 changes: 1 addition & 1 deletion Userland/Applications/Browser/Tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Tab final : public GUI::Widget {
RefPtr<Web::InProcessWebView> m_page_view;
RefPtr<Web::OutOfProcessWebView> m_web_content_view;

RefPtr<GUI::TextBox> m_location_box;
RefPtr<GUI::UrlBox> m_location_box;
RefPtr<GUI::Button> m_bookmark_button;
RefPtr<GUI::Window> m_dom_inspector_window;
RefPtr<GUI::Window> m_console_window;
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibGUI/Forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Statusbar;
class TabWidget;
class TableView;
class TextBox;
class UrlBox;
class TextDocument;
class TextDocumentLine;
struct TextDocumentSpan;
Expand Down
35 changes: 35 additions & 0 deletions Userland/Libraries/LibGUI/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

REGISTER_WIDGET(GUI, TextBox)
REGISTER_WIDGET(GUI, PasswordBox)
REGISTER_WIDGET(GUI, UrlBox)

namespace GUI {

Expand Down Expand Up @@ -80,4 +81,38 @@ PasswordBox::PasswordBox()
set_text_is_secret(true);
}

UrlBox::UrlBox()
: TextBox()
{
set_auto_focusable(false);
}

UrlBox::~UrlBox()
{
}

void UrlBox::focusout_event(GUI::FocusEvent& event)
{
set_focus_transition(true);

TextBox::focusout_event(event);
}

void UrlBox::mousedown_event(GUI::MouseEvent& event)
{
if (is_displayonly())
return;

if (event.button() != MouseButton::Left)
return;

if (is_focus_transition()) {
TextBox::select_current_line();

set_focus_transition(false);
} else {
TextBox::mousedown_event(event);
}
}

}
16 changes: 16 additions & 0 deletions Userland/Libraries/LibGUI/TextBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,20 @@ class PasswordBox : public TextBox {
PasswordBox();
};

class UrlBox : public TextBox {
C_OBJECT(UrlBox)
public:
UrlBox();
virtual ~UrlBox() override;

void set_focus_transition(bool focus_transition) { m_focus_transition = focus_transition; }
bool is_focus_transition() const { return m_focus_transition; }

private:
virtual void mousedown_event(GUI::MouseEvent&) override;
virtual void focusout_event(GUI::FocusEvent&) override;

bool m_focus_transition { true };
};

}
13 changes: 9 additions & 4 deletions Userland/Libraries/LibGUI/TextEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,7 @@ void TextEditor::mousedown_event(MouseEvent& event)

if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) {
m_triple_click_timer = Core::ElapsedTimer();
m_selection = document().range_for_entire_line(m_cursor.line());
set_cursor(m_selection.end());
update();
did_update_selection();
select_current_line();
return;
}

Expand Down Expand Up @@ -310,6 +307,14 @@ void TextEditor::mousemove_event(MouseEvent& event)
}
}

void TextEditor::select_current_line()
{
m_selection = document().range_for_entire_line(m_cursor.line());
set_cursor(m_selection.end());
update();
did_update_selection();
}

void TextEditor::automatic_selection_scroll_timer_fired()
{
if (!m_in_drag_select) {
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibGUI/TextEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class TextEditor
void delete_previous_char();
void delete_from_line_start_to_cursor();
void select_all();
void select_current_line();
virtual void undo();
virtual void redo();

Expand Down

0 comments on commit 34a64ed

Please sign in to comment.