Skip to content

Truly independent web browser

UI/Qt: Keep each tab's omnibox editing state across tab switches

Andreas Kling53480a2ee562
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index d102fad8b52..39c2372b423 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -244,7 +244,9 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow
set_current_tab(tab);
if (tab) {
QWidget* focus_widget = &tab->view();
- if (auto* tab_focus_widget = tab->focusWidget(); tab_focus_widget && tab->isAncestorOf(tab_focus_widget))
+ if (auto* location_edit = tab->location_edit_with_parked_editing())
+ focus_widget = location_edit;
+ else if (auto* tab_focus_widget = tab->focusWidget(); tab_focus_widget && tab->isAncestorOf(tab_focus_widget))
focus_widget = tab_focus_widget;
#if defined(AK_OS_MACOS)
make_appkit_window_first_responder(*focus_widget);
diff --git a/UI/Qt/LocationEdit.cpp b/UI/Qt/LocationEdit.cpp
index ff69b1dea84..84547964235 100644
--- a/UI/Qt/LocationEdit.cpp
+++ b/UI/Qt/LocationEdit.cpp
@@ -378,8 +378,48 @@ void LocationEdit::contextMenuEvent(QContextMenuEvent* event)
delete menu;
}
+// Leaving a tab parks its omnibox's editing session: the suggestion popup closes, but everything else is kept,
+// including an inline completion and the suggestions, and the session picks up where it left off when the omnibox
+// gets focus again.
+void LocationEdit::park_editing_session()
+{
+ if (!hasFocus() || m_parked_editing_session.has_value())
+ return;
+
+ // NB: Only the popup's window closes. Dismissing the popup would end the omnibox's popup session, which drops an
+ // inline completion from the text.
+ m_autocomplete->close();
+ m_omnibox.set_suspended(true);
+
+ m_parked_editing_session = ParkedEditingSession {
+ .cursor_position = cursorPosition(),
+ .selection_start = selectionStart(),
+ .selection_length = static_cast<int>(selectedText().length()),
+ };
+}
+
void LocationEdit::focusInEvent(QFocusEvent* event)
{
+ if (auto parked = m_parked_editing_session; parked.has_value()) {
+ m_parked_editing_session.clear();
+ NativeTextEditingLineEdit::focusInEvent(event);
+ m_omnibox.set_suspended(false);
+
+ // Restore the selection with the cursor at the end it was on.
+ if (parked->selection_length > 0) {
+ if (parked->cursor_position == parked->selection_start)
+ setSelection(parked->selection_start + parked->selection_length, -parked->selection_length);
+ else
+ setSelection(parked->selection_start, parked->selection_length);
+ } else {
+ setCursorPosition(parked->cursor_position);
+ }
+ if (m_omnibox.is_popup_visible())
+ m_autocomplete->show_with_suggestions(m_omnibox.suggestions(), m_omnibox.selected_suggestion());
+ animate_focus_glow(58);
+ return;
+ }
+
if (event->reason() == Qt::PopupFocusReason) {
NativeTextEditingLineEdit::focusInEvent(event);
m_omnibox.set_suspended(false);
@@ -414,6 +454,9 @@ void LocationEdit::focusOutEvent(QFocusEvent* event)
{
QLineEdit::focusOutEvent(event);
+ if (m_parked_editing_session.has_value())
+ return;
+
// A popup (for example the context menu) borrows focus without ending the editing session; late
// suggestion deliveries must not rewrite the text or raise the suggestion popup while it is open.
if (event->reason() == Qt::PopupFocusReason) {
@@ -803,6 +846,9 @@ void LocationEdit::highlight_location()
void LocationEdit::set_text_and_restart_editing(QString const& text)
{
setText(text);
+ // The parked cursor and selection belonged to the text that was just replaced.
+ if (m_parked_editing_session.has_value())
+ m_parked_editing_session = ParkedEditingSession {};
if (!m_omnibox.is_editing())
return;
@@ -821,7 +867,9 @@ void LocationEdit::set_url(Optional<URL::URL> url)
if (!m_has_user_edited_hidden_url)
set_text_and_restart_editing({});
} else if (m_url.has_value()) {
- set_text_and_restart_editing(hasFocus() ? serialized_url() : display_url());
+ // NB: A parked editing session still owns the omnibox, so it gets the text a focused omnibox would.
+ auto is_editing = hasFocus() || m_parked_editing_session.has_value();
+ set_text_and_restart_editing(is_editing ? serialized_url() : display_url());
setCursorPosition(0);
}
diff --git a/UI/Qt/LocationEdit.h b/UI/Qt/LocationEdit.h
index 242ac393255..4b5ed886254 100644
--- a/UI/Qt/LocationEdit.h
+++ b/UI/Qt/LocationEdit.h
@@ -47,6 +47,8 @@ public:
bool url_is_hidden() const { return m_url_is_hidden; }
void set_url_is_hidden(bool);
void show_autocomplete();
+ void park_editing_session();
+ bool has_parked_editing_session() const { return m_parked_editing_session.has_value(); }
Function<void(String, Optional<URL::URL>, WebView::OmniboxDestinationKind)> on_navigation;
@@ -98,6 +100,13 @@ private:
Optional<URL::URL> m_url;
bool m_url_is_hidden { false };
+
+ struct ParkedEditingSession {
+ int cursor_position { 0 };
+ int selection_start { -1 };
+ int selection_length { 0 };
+ };
+ Optional<ParkedEditingSession> m_parked_editing_session;
bool m_has_user_edited_hidden_url { false };
bool m_is_updating_chrome_style { false };
bool m_is_updating_field_hover { false };
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index 98f0a06ca20..58eb036622e 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -1215,6 +1215,11 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> travers
Tab::~Tab() = default;
+void Tab::park_location_editing()
+{
+ m_location_edit->park_editing_session();
+}
+
void Tab::focus_location_editor()
{
#if defined(AK_OS_MACOS)
diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.h
index c1fa7574b47..f38eaceff77 100644
--- a/UI/Qt/Tab.h
+++ b/UI/Qt/Tab.h
@@ -125,6 +125,8 @@ public:
public slots:
void focus_location_editor();
+ void park_location_editing();
+ LocationEdit* location_edit_with_parked_editing() const { return m_location_edit->has_parked_editing_session() ? m_location_edit : nullptr; }
signals:
void title_changed(int id, QString const&);
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index 162899ddda1..90a6cb644f3 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -1800,11 +1800,15 @@ TabWidget::TabWidget(QWidget* parent)
update_chrome_style();
connect(m_tab_bar, &QTabBar::currentChanged, this, [this](int index) {
+ // The tab being left keeps its omnibox's editing session for when it is shown again.
+ if (auto* previous_tab = qobject_cast<Tab*>(m_stacked_widget->currentWidget()); previous_tab && m_stacked_widget->indexOf(previous_tab) != index)
+ previous_tab->park_location_editing();
+
if (index >= 0 && index < m_stacked_widget->count())
m_stacked_widget->setCurrentIndex(index);
- emit current_tab_changed(index);
m_toolbar_container->setCurrentIndex(index);
+ emit current_tab_changed(index);
update_vertical_tabs_overlay_geometry();
update_vertical_tabs_resize_handle();
update_vertical_tabs_content_overlay();