LibWebView+UI: Reduce boilerplate to update the bookmarks bar display
We currently use LibWebView's Application as the entry point to learn about the bookmarks bar being shown/hidden, and propagate that through virtual methods. At the time this was added, AppKit's Tab window and Qt's BrowserWindow did not have a settings observer. They do now, so let's skip a couple of middle-men. For AppKit, we change the settings observer to just (weakly) store the Tab instance so that we don't have to add callback functions for each setting.
This commit is contained in:
parent
83105b8349
commit
7fc71e563b
13 changed files with 27 additions and 73 deletions
|
|
@ -68,11 +68,6 @@ struct ApplicationSettingsObserver final : public SettingsObserver {
|
|||
Application::the().tab_settings_changed({});
|
||||
}
|
||||
|
||||
virtual void show_bookmarks_bar_changed() override
|
||||
{
|
||||
Application::the().show_bookmarks_bar_changed({});
|
||||
}
|
||||
|
||||
virtual void browsing_data_settings_changed() override
|
||||
{
|
||||
auto const& browsing_data_settings = Application::settings().browsing_data_settings();
|
||||
|
|
@ -1415,11 +1410,11 @@ void Application::initialize_actions()
|
|||
m_bookmarks_menu->add_action(*m_toggle_bookmark_action);
|
||||
update_bookmark_action_for_current_web_view();
|
||||
|
||||
m_toggle_bookmark_bar_action = Action::create("Toggle Bookmarks Bar"sv, ActionID::ToggleBookmarksBar, [this]() {
|
||||
m_toggle_bookmark_bar_action = Action::create_checkable("Show Bookmarks Bar"sv, ActionID::ToggleBookmarksBar, [this]() {
|
||||
m_settings.set_show_bookmarks_bar(!m_settings.show_bookmarks_bar());
|
||||
});
|
||||
m_toggle_bookmark_bar_action->set_checked(m_settings.show_bookmarks_bar());
|
||||
m_bookmarks_menu->add_action(*m_toggle_bookmark_bar_action);
|
||||
update_bookmarks_bar_action();
|
||||
|
||||
m_bookmarks_menu->add_separator();
|
||||
m_bookmarks_menu_static_size = m_bookmarks_menu->size();
|
||||
|
|
@ -1653,17 +1648,6 @@ void Application::bookmarks_changed(Badge<ApplicationBookmarkStoreObserver>)
|
|||
rebuild_bookmarks_menu();
|
||||
}
|
||||
|
||||
void Application::update_bookmarks_bar_action()
|
||||
{
|
||||
m_toggle_bookmark_bar_action->set_text(m_settings.show_bookmarks_bar() ? "Hide Bookmark Bar"sv : "Show Bookmark Bar"sv);
|
||||
}
|
||||
|
||||
void Application::show_bookmarks_bar_changed(Badge<ApplicationSettingsObserver>)
|
||||
{
|
||||
update_bookmarks_bar_action();
|
||||
update_bookmarks_bar_display(m_settings.show_bookmarks_bar());
|
||||
}
|
||||
|
||||
void Application::create_bookmark_menu_items(Optional<MenuData> data)
|
||||
{
|
||||
auto const& [menu, items, target_folder_id] = data.ensure([&]() -> MenuData {
|
||||
|
|
|
|||
|
|
@ -220,7 +220,6 @@ protected:
|
|||
virtual void update_tabs_display() const { }
|
||||
|
||||
virtual void rebuild_bookmarks_menu() const { }
|
||||
virtual void update_bookmarks_bar_display([[maybe_unused]] bool show_bookmarks_bar) const { }
|
||||
virtual void on_recently_closed_entries_changed() const { }
|
||||
|
||||
struct BookmarkID {
|
||||
|
|
@ -256,9 +255,7 @@ private:
|
|||
ErrorOr<void> load_content_blocker_lists();
|
||||
|
||||
void initialize_actions();
|
||||
|
||||
void update_vertical_tabs_action();
|
||||
void update_bookmarks_bar_action();
|
||||
|
||||
struct MenuData {
|
||||
Menu& menu;
|
||||
|
|
|
|||
|
|
@ -33,7 +33,6 @@ private:
|
|||
virtual void insert_clipboard_entry(Web::Clipboard::SystemClipboardRepresentation) override;
|
||||
|
||||
virtual void rebuild_bookmarks_menu() const override;
|
||||
virtual void update_bookmarks_bar_display(bool) const override;
|
||||
virtual void show_bookmark_context_menu(Gfx::IntPoint, Optional<WebView::BookmarkItem const&>, Optional<String const&> target_folder_id) override;
|
||||
virtual Optional<BookmarkID> bookmark_item_id_for_context_menu() const override;
|
||||
virtual NonnullRefPtr<BookmarkPromise> display_add_bookmark_dialog() const override;
|
||||
|
|
|
|||
|
|
@ -170,12 +170,6 @@ void Application::rebuild_bookmarks_menu() const
|
|||
[delegate rebuildBookmarksMenu];
|
||||
}
|
||||
|
||||
void Application::update_bookmarks_bar_display(bool show_bookmarks_bar) const
|
||||
{
|
||||
ApplicationDelegate* delegate = [NSApp delegate];
|
||||
[delegate updateBookmarksBarDisplay:show_bookmarks_bar];
|
||||
}
|
||||
|
||||
void Application::show_bookmark_context_menu(Gfx::IntPoint content_position, Optional<WebView::BookmarkItem const&> item, Optional<String const&> target_folder_id)
|
||||
{
|
||||
ApplicationDelegate* delegate = [NSApp delegate];
|
||||
|
|
|
|||
|
|
@ -38,7 +38,6 @@
|
|||
- (void)removeTab:(nonnull TabController*)controller;
|
||||
|
||||
- (void)rebuildBookmarksMenu;
|
||||
- (void)updateBookmarksBarDisplay:(bool)show_bookmarks_bar;
|
||||
|
||||
- (void)onDevtoolsEnabled;
|
||||
- (void)onDevtoolsDisabled;
|
||||
|
|
|
|||
|
|
@ -146,15 +146,6 @@
|
|||
}
|
||||
}
|
||||
|
||||
- (void)updateBookmarksBarDisplay:(bool)show_bookmarks_bar
|
||||
{
|
||||
for (TabController* controller in self.managed_tabs) {
|
||||
if (auto* tab = (Tab*)[controller window]; ([tab styleMask] & NSWindowStyleMaskFullScreen) == 0) {
|
||||
[tab updateBookmarksBarDisplay:show_bookmarks_bar];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)onDevtoolsEnabled
|
||||
{
|
||||
if (!self.info_bar) {
|
||||
|
|
|
|||
|
|
@ -33,18 +33,17 @@ static constexpr NSUInteger const TAB_LOADING_SPINNER_SEGMENT_COUNT = 12;
|
|||
|
||||
class TabSettingsObserver final : public WebView::SettingsObserver {
|
||||
public:
|
||||
explicit TabSettingsObserver(Function<void(WebView::ConfigVariableID)> callback)
|
||||
: m_callback(move(callback))
|
||||
explicit TabSettingsObserver(Tab* tab)
|
||||
: m_tab(tab)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void config_variable_changed(WebView::ConfigVariableID variable) override
|
||||
{
|
||||
m_callback(variable);
|
||||
}
|
||||
|
||||
private:
|
||||
Function<void(WebView::ConfigVariableID)> m_callback;
|
||||
// These are forward-declared so that they may access non-public Tab methods.
|
||||
virtual void show_bookmarks_bar_changed() override;
|
||||
virtual void config_variable_changed(WebView::ConfigVariableID variable) override;
|
||||
|
||||
__weak Tab* m_tab { nil };
|
||||
};
|
||||
|
||||
static NSImage* tab_loading_spinner_icon(NSUInteger frame)
|
||||
|
|
@ -141,20 +140,13 @@ static NSImage* tab_loading_spinner_icon(NSUInteger frame)
|
|||
|
||||
self.favicon = [Tab defaultFavicon];
|
||||
self.title = @"New Tab";
|
||||
__weak Tab* weak_self = self;
|
||||
m_settings_observer = make<TabSettingsObserver>([weak_self](WebView::ConfigVariableID variable) {
|
||||
if (variable != WebView::ConfigVariableID::ShowWebContentProcessIDInTabTitle)
|
||||
return;
|
||||
Tab* strong_self = weak_self;
|
||||
if (strong_self == nil)
|
||||
return;
|
||||
[strong_self updateTabTitleAndFavicon];
|
||||
});
|
||||
[self updateTabTitleAndFavicon];
|
||||
|
||||
[self setTitleVisibility:NSWindowTitleHidden];
|
||||
[self setIsVisible:YES];
|
||||
|
||||
m_settings_observer = make<TabSettingsObserver>(self);
|
||||
|
||||
auto* bookmarks_bar = [[BookmarksBar alloc] init];
|
||||
self.bookmarks_bar_controller = [[NSTitlebarAccessoryViewController alloc] init];
|
||||
[self.bookmarks_bar_controller setView:bookmarks_bar];
|
||||
|
|
@ -477,3 +469,14 @@ static NSImage* tab_loading_spinner_icon(NSUInteger frame)
|
|||
}
|
||||
|
||||
@end
|
||||
|
||||
void TabSettingsObserver::show_bookmarks_bar_changed()
|
||||
{
|
||||
[m_tab updateBookmarksBarDisplay:WebView::Application::settings().show_bookmarks_bar()];
|
||||
}
|
||||
|
||||
void TabSettingsObserver::config_variable_changed(WebView::ConfigVariableID variable)
|
||||
{
|
||||
if (variable == WebView::ConfigVariableID::ShowWebContentProcessIDInTabTitle)
|
||||
[m_tab updateTabTitleAndFavicon];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -287,10 +287,6 @@ void Application::rebuild_bookmarks_menu() const
|
|||
{
|
||||
}
|
||||
|
||||
void Application::update_bookmarks_bar_display(bool) const
|
||||
{
|
||||
}
|
||||
|
||||
void Application::on_devtools_enabled() const
|
||||
{
|
||||
WebView::Application::on_devtools_enabled();
|
||||
|
|
|
|||
|
|
@ -60,7 +60,6 @@ private:
|
|||
virtual bool should_capture_web_content_output() const override { return false; }
|
||||
|
||||
virtual void rebuild_bookmarks_menu() const override;
|
||||
virtual void update_bookmarks_bar_display(bool) const override;
|
||||
|
||||
virtual void on_devtools_enabled() const override;
|
||||
virtual void on_devtools_disabled() const override;
|
||||
|
|
|
|||
|
|
@ -319,14 +319,6 @@ void Application::rebuild_bookmarks_menu() const
|
|||
}
|
||||
}
|
||||
|
||||
void Application::update_bookmarks_bar_display(bool show_bookmarks_bar) const
|
||||
{
|
||||
for (auto* widget : QApplication::topLevelWidgets()) {
|
||||
if (auto* window = as_if<BrowserWindow>(widget))
|
||||
window->update_bookmarks_bar_display(show_bookmarks_bar);
|
||||
}
|
||||
}
|
||||
|
||||
void Application::update_reopen_recently_closed_actions() const
|
||||
{
|
||||
for (auto* widget : QApplication::topLevelWidgets()) {
|
||||
|
|
|
|||
|
|
@ -64,7 +64,6 @@ private:
|
|||
virtual void update_tabs_display() const override;
|
||||
|
||||
virtual void rebuild_bookmarks_menu() const override;
|
||||
virtual void update_bookmarks_bar_display(bool) const override;
|
||||
virtual void show_bookmark_context_menu(Gfx::IntPoint, Optional<WebView::BookmarkItem const&>, Optional<String const&> target_folder_id) override;
|
||||
virtual Optional<BookmarkID> bookmark_item_id_for_context_menu() const override;
|
||||
virtual NonnullRefPtr<BookmarkPromise> display_add_bookmark_dialog() const override;
|
||||
|
|
|
|||
|
|
@ -512,11 +512,12 @@ void BrowserWindow::rebuild_bookmarks_menu()
|
|||
});
|
||||
}
|
||||
|
||||
void BrowserWindow::update_bookmarks_bar_display(bool show_bookmarks_bar)
|
||||
void BrowserWindow::show_bookmarks_bar_changed()
|
||||
{
|
||||
auto show_bookmarks_bar = WebView::Application::settings().show_bookmarks_bar();
|
||||
|
||||
for_each_tab([&](Tab& tab) {
|
||||
if (tab.view().is_fullscreen() == Web::ViewportIsFullscreen::No)
|
||||
tab.bookmarks_bar().setVisible(show_bookmarks_bar);
|
||||
tab.bookmarks_bar().setVisible(show_bookmarks_bar);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -114,7 +114,6 @@ public:
|
|||
void update_tabs_display();
|
||||
|
||||
void rebuild_bookmarks_menu();
|
||||
void update_bookmarks_bar_display(bool show_bookmarks_bar);
|
||||
void update_reopen_recently_closed_action();
|
||||
void detach_tab_to_new_window(int index, QPoint global_position);
|
||||
void move_tab_to_window(int index, BrowserWindow& target_window, int target_index);
|
||||
|
|
@ -158,6 +157,7 @@ private:
|
|||
virtual void closeEvent(QCloseEvent*) override;
|
||||
|
||||
virtual void show_menu_bar_changed() override;
|
||||
virtual void show_bookmarks_bar_changed() override;
|
||||
virtual void config_variable_changed(WebView::ConfigVariableID) override;
|
||||
|
||||
Tab& create_new_tab(Web::HTML::ActivateTab, Tab& parent, Optional<u64> page_index);
|
||||
|
|
|
|||
Loading…
Reference in a new issue