1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:47:34 +00:00

LibGUI+Browser: Add UrlBox class

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.
This commit is contained in:
Rob Ryan 2021-08-15 15:48:53 +10:00 committed by Andreas Kling
parent fbf824a50f
commit 34a64ed25b
7 changed files with 68 additions and 8 deletions

View file

@ -142,7 +142,7 @@ Tab::Tab(BrowserWindow& window, Type type)
toolbar.add_action(window.go_home_action()); toolbar.add_action(window.go_home_action());
toolbar.add_action(window.reload_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->set_placeholder("Address");
m_location_box->on_return_pressed = [this] { m_location_box->on_return_pressed = [this] {
@ -153,7 +153,7 @@ Tab::Tab(BrowserWindow& window, Type type)
auto url = url_from_user_input(m_location_box->text()); auto url = url_from_user_input(m_location_box->text());
load(url); load(url);
view().set_focus(true); view();
}; };
m_location_box->add_custom_context_menu_action(GUI::Action::create("Paste && Go", [this](auto&) { m_location_box->add_custom_context_menu_action(GUI::Action::create("Paste && Go", [this](auto&) {
@ -312,8 +312,8 @@ Tab::Tab(BrowserWindow& window, Type type)
auto focus_location_box_action = GUI::Action::create( auto focus_location_box_action = GUI::Action::create(
"Focus location box", { Mod_Ctrl, Key_L }, Key_F6, [this](auto&) { "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->set_focus(true);
m_location_box->select_current_line();
}, },
this); this);
@ -371,6 +371,8 @@ void Tab::load(const URL& url, LoadType load_type)
m_page_view->load(url); m_page_view->load(url);
else else
m_web_content_view->load(url); m_web_content_view->load(url);
m_location_box->set_focus(false);
} }
URL Tab::url() const URL Tab::url() const

View file

@ -89,7 +89,7 @@ private:
RefPtr<Web::InProcessWebView> m_page_view; RefPtr<Web::InProcessWebView> m_page_view;
RefPtr<Web::OutOfProcessWebView> m_web_content_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::Button> m_bookmark_button;
RefPtr<GUI::Window> m_dom_inspector_window; RefPtr<GUI::Window> m_dom_inspector_window;
RefPtr<GUI::Window> m_console_window; RefPtr<GUI::Window> m_console_window;

View file

@ -66,6 +66,7 @@ class Statusbar;
class TabWidget; class TabWidget;
class TableView; class TableView;
class TextBox; class TextBox;
class UrlBox;
class TextDocument; class TextDocument;
class TextDocumentLine; class TextDocumentLine;
struct TextDocumentSpan; struct TextDocumentSpan;

View file

@ -8,6 +8,7 @@
REGISTER_WIDGET(GUI, TextBox) REGISTER_WIDGET(GUI, TextBox)
REGISTER_WIDGET(GUI, PasswordBox) REGISTER_WIDGET(GUI, PasswordBox)
REGISTER_WIDGET(GUI, UrlBox)
namespace GUI { namespace GUI {
@ -80,4 +81,38 @@ PasswordBox::PasswordBox()
set_text_is_secret(true); 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);
}
}
} }

View file

@ -43,4 +43,20 @@ public:
PasswordBox(); 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 };
};
} }

View file

@ -256,10 +256,7 @@ void TextEditor::mousedown_event(MouseEvent& event)
if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) { if (m_triple_click_timer.is_valid() && m_triple_click_timer.elapsed() < 250) {
m_triple_click_timer = Core::ElapsedTimer(); m_triple_click_timer = Core::ElapsedTimer();
m_selection = document().range_for_entire_line(m_cursor.line()); select_current_line();
set_cursor(m_selection.end());
update();
did_update_selection();
return; return;
} }
@ -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() void TextEditor::automatic_selection_scroll_timer_fired()
{ {
if (!m_in_drag_select) { if (!m_in_drag_select) {

View file

@ -142,6 +142,7 @@ public:
void delete_previous_char(); void delete_previous_char();
void delete_from_line_start_to_cursor(); void delete_from_line_start_to_cursor();
void select_all(); void select_all();
void select_current_line();
virtual void undo(); virtual void undo();
virtual void redo(); virtual void redo();