Skip to content

Truly independent web browser

UI/Qt: Show the bookmarks bar shortcut in its visibility menu

Timothy Flynn979ab7bb58e3
diff --git a/UI/Qt/Actions.cpp b/UI/Qt/Actions.cpp
index f54fc1d0035..a14a415f2ff 100644
--- a/UI/Qt/Actions.cpp
+++ b/UI/Qt/Actions.cpp
@@ -70,16 +70,6 @@ void Action::set_engaged(bool engaged)
observer->on_engaged_state_changed(*this);
}
-void Action::set_busy(bool busy)
-{
- if (m_busy == busy)
- return;
- m_busy = busy;
-
- for (auto& observer : m_observers)
- observer->on_busy_state_changed(*this);
-}
-
void Action::set_checked(bool checked)
{
set_checked_internal(checked);
@@ -104,6 +94,26 @@ void Action::set_checked_internal(bool checked)
observer->on_checked_state_changed(*this);
}
+void Action::set_busy(bool busy)
+{
+ if (m_busy == busy)
+ return;
+ m_busy = busy;
+
+ for (auto& observer : m_observers)
+ observer->on_busy_state_changed(*this);
+}
+
+void Action::set_shortcut_hint(Optional<ActionID> shortcut_hint)
+{
+ if (m_shortcut_hint == shortcut_hint)
+ return;
+ m_shortcut_hint = shortcut_hint;
+
+ for (auto& observer : m_observers)
+ observer->on_shortcut_hint_changed(*this);
+}
+
void Action::add_observer(NonnullOwnPtr<Observer> observer)
{
observer->on_text_changed(*this);
@@ -115,6 +125,7 @@ void Action::add_observer(NonnullOwnPtr<Observer> observer)
observer->on_busy_state_changed(*this);
if (is_checkable())
observer->on_checked_state_changed(*this);
+ observer->on_shortcut_hint_changed(*this);
m_observers.append(move(observer));
}
diff --git a/UI/Qt/Actions.h b/UI/Qt/Actions.h
index f3ae04c7406..082057b53ec 100644
--- a/UI/Qt/Actions.h
+++ b/UI/Qt/Actions.h
@@ -189,13 +189,17 @@ public:
bool engaged() const { return m_engaged; }
void set_engaged(bool);
+ bool is_checkable() const { return m_checked.has_value(); }
+ bool checked() const { return *m_checked; }
+ void set_checked(bool);
+
// Work the action started is under way, e.g. a download, which its button shows by pulsing.
bool busy() const { return m_busy; }
void set_busy(bool);
- bool is_checkable() const { return m_checked.has_value(); }
- bool checked() const { return *m_checked; }
- void set_checked(bool);
+ // Show another action's shortcut as a menu hint.
+ Optional<ActionID> shortcut_hint() const { return m_shortcut_hint; }
+ void set_shortcut_hint(Optional<ActionID>);
struct Observer {
AK_ALLOC_WITH_KMALLOC;
@@ -209,6 +213,7 @@ public:
virtual void on_engaged_state_changed(Action&) { }
virtual void on_busy_state_changed(Action&) { }
virtual void on_checked_state_changed(Action&) { }
+ virtual void on_shortcut_hint_changed(Action&) { }
};
void add_observer(NonnullOwnPtr<Observer>);
@@ -238,6 +243,7 @@ private:
bool m_engaged { false };
bool m_busy { false };
Optional<bool> m_checked;
+ Optional<ActionID> m_shortcut_hint;
Function<void()> m_action;
Vector<NonnullOwnPtr<Observer>, 1> m_observers;
diff --git a/UI/Qt/Bookmarks.cpp b/UI/Qt/Bookmarks.cpp
index 88670726ce1..1aca44b644d 100644
--- a/UI/Qt/Bookmarks.cpp
+++ b/UI/Qt/Bookmarks.cpp
@@ -69,7 +69,6 @@ void Bookmarks::create_menus()
appearance.show_bookmarks_bar = value;
Application::settings().set_appearance(appearance);
});
- action->set_checked(Application::settings().appearance().show_bookmarks_bar == value);
m_show_bookmarks_bar_menu->add_action(action);
return action;
};
@@ -78,6 +77,8 @@ void Bookmarks::create_menus()
m_show_bookmarks_bar_always_action = add_show_bookmarks_bar_option("Always"sv, WebView::ShowBookmarksBar::Always);
m_show_bookmarks_bar_never_action = add_show_bookmarks_bar_option("Never"sv, WebView::ShowBookmarksBar::Never);
m_show_bookmarks_bar_on_hover_action = add_show_bookmarks_bar_option("Only When Hovering the Navigation Toolbar"sv, WebView::ShowBookmarksBar::OnHover);
+ update_show_bookmarks_bar_actions();
+
m_menu->add_submenu(*m_show_bookmarks_bar_menu);
m_menu->add_separator();
@@ -102,12 +103,11 @@ void Bookmarks::create_menus()
});
}));
- m_toggle_bookmark_bar_action = Action::create("Show Bookmarks Bar"sv, ActionID::ToggleBookmarksBar, [] {
- auto appearance = Application::settings().appearance();
- appearance.show_bookmarks_bar = appearance.show_bookmarks_bar == WebView::ShowBookmarksBar::Always
- ? WebView::ShowBookmarksBar::Never
- : WebView::ShowBookmarksBar::Always;
- Application::settings().set_appearance(appearance);
+ m_toggle_bookmark_bar_action = Action::create("Show Bookmarks Bar"sv, ActionID::ToggleBookmarksBar, [this] {
+ if (Application::settings().appearance().show_bookmarks_bar == WebView::ShowBookmarksBar::Always)
+ m_show_bookmarks_bar_never_action->activate();
+ else
+ m_show_bookmarks_bar_always_action->activate();
});
m_menu->add_separator();
@@ -288,6 +288,10 @@ void Bookmarks::create_menu_items(Optional<MenuData> data)
void Bookmarks::update_show_bookmarks_bar_actions()
{
+ auto show_always = Application::settings().appearance().show_bookmarks_bar == WebView::ShowBookmarksBar::Always;
+ m_show_bookmarks_bar_always_action->set_shortcut_hint(show_always ? OptionalNone {} : Optional<ActionID> { ActionID::ToggleBookmarksBar });
+ m_show_bookmarks_bar_never_action->set_shortcut_hint(show_always ? Optional<ActionID> { ActionID::ToggleBookmarksBar } : OptionalNone {});
+
switch (Application::settings().appearance().show_bookmarks_bar) {
case WebView::ShowBookmarksBar::Always:
m_show_bookmarks_bar_always_action->set_checked(true);
diff --git a/UI/Qt/Menu.cpp b/UI/Qt/Menu.cpp
index aa3bbb794b7..700c7fc8699 100644
--- a/UI/Qt/Menu.cpp
+++ b/UI/Qt/Menu.cpp
@@ -26,6 +26,8 @@ namespace Ladybird {
static constexpr auto DYNAMIC_HISTORY_MENU_ITEM_PROPERTY = "LadybirdDynamicHistoryMenuItem";
static constexpr size_t RECENT_HISTORY_MENU_ITEM_LIMIT = 15;
+static void set_action_shortcuts(QAction&, ActionID);
+
class ActionObserver final : public Action::Observer {
public:
static NonnullOwnPtr<ActionObserver> create(Action& action, QAction& qaction, IncludeActionIcon include_action_icon)
@@ -105,6 +107,12 @@ public:
}
}
+ virtual void on_checked_state_changed(Action& action) override
+ {
+ if (m_action)
+ m_action->setChecked(action.checked());
+ }
+
// QAction has no busy state, so it's a property, and the buttons that show it learn of a change as of any other.
virtual void on_busy_state_changed(Action& action) override
{
@@ -114,10 +122,16 @@ public:
Q_EMIT m_action->changed();
}
- virtual void on_checked_state_changed(Action& action) override
+ virtual void on_shortcut_hint_changed(Action& action) override
{
- if (m_action)
- m_action->setChecked(action.checked());
+ if (!m_action)
+ return;
+
+ auto hint = action.shortcut_hint();
+ set_action_shortcuts(*m_action, hint.value_or(action.id()));
+
+ // A hint leaves the window shortcut with the action that owns it.
+ m_action->setShortcutContext(hint.has_value() ? Qt::WidgetShortcut : Qt::WindowShortcut);
}
private:
@@ -182,24 +196,18 @@ static void add_properties(QObject& object, T& menu_or_action)
object.setProperty(key.to_byte_string().characters(), qstring_from_ak_string(value));
}
-static Action::Observer& initialize_native_control(Action& action, QAction& qaction, QPalette const& palette, IncludeActionIcon include_action_icon)
+static void set_action_shortcuts(QAction& qaction, ActionID id)
{
- static constexpr int const MENU_ICON_SIZE = 16;
+ qaction.setShortcuts({});
- switch (action.id()) {
+ switch (id) {
case ActionID::NavigateBack:
- if (include_action_icon == IncludeActionIcon::Yes)
- qaction.setIcon(create_chrome_icon(ChromeIcon::Back, palette));
qaction.setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Back));
break;
case ActionID::NavigateForward:
- if (include_action_icon == IncludeActionIcon::Yes)
- qaction.setIcon(create_chrome_icon(ChromeIcon::Forward, palette));
qaction.setShortcuts(QKeySequence::keyBindings(QKeySequence::StandardKey::Forward));
break;
case ActionID::Reload:
- if (include_action_icon == IncludeActionIcon::Yes)
- qaction.setIcon(create_chrome_icon(ChromeIcon::Reload, palette));
qaction.setShortcuts({ QKeySequence(Qt::CTRL | Qt::Key_R), QKeySequence(Qt::Key_F5) });
break;
case ActionID::ReloadBypassingCache:
@@ -210,8 +218,6 @@ static Action::Observer& initialize_native_control(Action& action, QAction& qact
#endif
break;
case ActionID::ViewDownloads:
- if (include_action_icon == IncludeActionIcon::Yes)
- qaction.setIcon(create_chrome_icon(ChromeIcon::Download, palette));
#if defined(AK_OS_MACOS)
qaction.setShortcut(QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_L));
#else
@@ -246,21 +252,11 @@ static Action::Observer& initialize_native_control(Action& action, QAction& qact
break;
case ActionID::ToggleBookmark:
- if (include_action_icon == IncludeActionIcon::Yes)
- qaction.setIcon(create_chrome_icon(action.engaged() ? ChromeIcon::StarFilled : ChromeIcon::Star, palette));
qaction.setShortcut(QKeySequence(Qt::CTRL | Qt::Key_D));
break;
- case ActionID::ToggleBookmarkViaToolbar:
- if (include_action_icon == IncludeActionIcon::Yes)
- qaction.setIcon(create_chrome_icon(action.engaged() ? ChromeIcon::StarFilled : ChromeIcon::Star, palette));
- break;
case ActionID::ToggleBookmarksBar:
qaction.setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_B));
break;
- case ActionID::TranslatePage:
- if (include_action_icon == IncludeActionIcon::Yes)
- qaction.setIcon(create_translate_icon(palette, action.engaged()));
- break;
case ActionID::ToggleVerticalTabsExpanded:
#if defined(AK_OS_MACOS)
qaction.setShortcut(QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_S));
@@ -271,13 +267,6 @@ static Action::Observer& initialize_native_control(Action& action, QAction& qact
case ActionID::AddBookmarkAllTabs:
qaction.setShortcut(QKeySequence(Qt::CTRL | Qt::SHIFT | Qt::Key_D));
break;
- case ActionID::BookmarkItem:
- if (auto icon = action.png_icon(); icon.has_value())
- qaction.setIcon(favicon_with_rounded_corners(icon_from_png(icon->bytes(), MENU_ICON_SIZE), MENU_ICON_SIZE));
- else
- qaction.setIcon(create_chrome_icon(ChromeIcon::Globe, palette));
- break;
-
case ActionID::ViewHistory:
#if defined(AK_OS_MACOS)
qaction.setShortcut(QKeySequence(Qt::CTRL | Qt::Key_Y));
@@ -344,6 +333,47 @@ static Action::Observer& initialize_native_control(Action& action, QAction& qact
default:
break;
}
+}
+
+static Action::Observer& initialize_native_control(Action& action, QAction& qaction, QPalette const& palette, IncludeActionIcon include_action_icon)
+{
+ static constexpr int const MENU_ICON_SIZE = 16;
+
+ switch (action.id()) {
+ case ActionID::NavigateBack:
+ if (include_action_icon == IncludeActionIcon::Yes)
+ qaction.setIcon(create_chrome_icon(ChromeIcon::Back, palette));
+ break;
+ case ActionID::NavigateForward:
+ if (include_action_icon == IncludeActionIcon::Yes)
+ qaction.setIcon(create_chrome_icon(ChromeIcon::Forward, palette));
+ break;
+ case ActionID::Reload:
+ if (include_action_icon == IncludeActionIcon::Yes)
+ qaction.setIcon(create_chrome_icon(ChromeIcon::Reload, palette));
+ break;
+ case ActionID::ViewDownloads:
+ if (include_action_icon == IncludeActionIcon::Yes)
+ qaction.setIcon(create_chrome_icon(ChromeIcon::Download, palette));
+ break;
+ case ActionID::ToggleBookmark:
+ case ActionID::ToggleBookmarkViaToolbar:
+ if (include_action_icon == IncludeActionIcon::Yes)
+ qaction.setIcon(create_chrome_icon(action.engaged() ? ChromeIcon::StarFilled : ChromeIcon::Star, palette));
+ break;
+ case ActionID::TranslatePage:
+ if (include_action_icon == IncludeActionIcon::Yes)
+ qaction.setIcon(create_translate_icon(palette, action.engaged()));
+ break;
+ case ActionID::BookmarkItem:
+ if (auto icon = action.png_icon(); icon.has_value())
+ qaction.setIcon(favicon_with_rounded_corners(icon_from_png(icon->bytes(), MENU_ICON_SIZE), MENU_ICON_SIZE));
+ else
+ qaction.setIcon(create_chrome_icon(ChromeIcon::Globe, palette));
+ break;
+ default:
+ break;
+ }
qaction.setShortcutVisibleInContextMenu(true);