From 3919a1dcc065b4c1a741ae9c2d7b8cd7ad2bb875 Mon Sep 17 00:00:00 2001 From: Dylan Katz Date: Mon, 10 Jan 2022 19:06:11 -0800 Subject: [PATCH] Browser: Load icons at start of program Previously, Browser loaded icons from the disk every time an icon was set. In addition to making more calls to the disk and decoding more images, this makes error propagation impossible. This change moves all icon loading to the start of the program. --- .../Browser/BookmarksBarWidget.cpp | 11 +++---- Userland/Applications/Browser/Browser.h | 2 ++ .../Applications/Browser/BrowserWindow.cpp | 16 ++++----- Userland/Applications/Browser/CMakeLists.txt | 1 + .../Applications/Browser/ConsoleWidget.cpp | 3 +- Userland/Applications/Browser/IconBag.cpp | 33 +++++++++++++++++++ Userland/Applications/Browser/IconBag.h | 32 ++++++++++++++++++ Userland/Applications/Browser/Tab.cpp | 22 +++++-------- .../Applications/Browser/WindowActions.cpp | 3 +- Userland/Applications/Browser/main.cpp | 13 +++++--- 10 files changed, 101 insertions(+), 35 deletions(-) create mode 100644 Userland/Applications/Browser/IconBag.cpp create mode 100644 Userland/Applications/Browser/IconBag.h diff --git a/Userland/Applications/Browser/BookmarksBarWidget.cpp b/Userland/Applications/Browser/BookmarksBarWidget.cpp index 2b2223d32b..92a00b70f8 100644 --- a/Userland/Applications/Browser/BookmarksBarWidget.cpp +++ b/Userland/Applications/Browser/BookmarksBarWidget.cpp @@ -4,7 +4,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "BookmarksBarWidget.h" +#include +#include #include #include #include @@ -198,7 +199,7 @@ void BookmarksBarWidget::model_did_update(unsigned) button.set_button_style(Gfx::ButtonStyle::Coolbar); button.set_text(title); - button.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-html.png").release_value_but_fixme_should_propagate_errors()); + button.set_icon(g_icon_bag.filetype_html); button.set_fixed_size(font().width(title) + 32, 20); button.set_relative_rect(rect); button.set_focus_policy(GUI::FocusPolicy::TabFocus); @@ -249,11 +250,7 @@ void BookmarksBarWidget::update_content_size() for (size_t i = m_last_visible_index; i < m_bookmarks.size(); ++i) { auto& bookmark = m_bookmarks.at(i); bookmark.set_visible(false); - m_additional_menu->add_action(GUI::Action::create(bookmark.text(), - Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-html.png").release_value_but_fixme_should_propagate_errors(), - [&](auto&) { - bookmark.on_click(0); - })); + m_additional_menu->add_action(GUI::Action::create(bookmark.text(), g_icon_bag.filetype_html, [&](auto&) { bookmark.on_click(0); })); } } } diff --git a/Userland/Applications/Browser/Browser.h b/Userland/Applications/Browser/Browser.h index fade37a00b..db773c58f2 100644 --- a/Userland/Applications/Browser/Browser.h +++ b/Userland/Applications/Browser/Browser.h @@ -7,11 +7,13 @@ #pragma once #include +#include namespace Browser { extern String g_home_url; extern String g_search_engine; extern Vector g_content_filters; +extern IconBag g_icon_bag; } diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index 32e95e7127..5076e62d9d 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -197,21 +197,21 @@ void BrowserWindow::build_menus() }); m_view_source_action = GUI::Action::create( - "View &Source", { Mod_Ctrl, Key_U }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/code.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) { + "View &Source", { Mod_Ctrl, Key_U }, g_icon_bag.code, [this](auto&) { active_tab().m_web_content_view->get_source(); }, this); m_view_source_action->set_status_tip("View source code of the current page"); m_inspect_dom_tree_action = GUI::Action::create( - "Inspect &DOM Tree", { Mod_None, Key_F12 }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/tree.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) { + "Inspect &DOM Tree", { Mod_None, Key_F12 }, g_icon_bag.tree, [this](auto&) { active_tab().show_inspector_window(Tab::InspectorTarget::Document); }, this); m_inspect_dom_tree_action->set_status_tip("Open inspector window for this page"); m_inspect_dom_node_action = GUI::Action::create( - "&Inspect Element", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspect.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) { + "&Inspect Element", g_icon_bag.inspect, [this](auto&) { active_tab().show_inspector_window(Tab::InspectorTarget::HoveredElement); }, this); @@ -222,7 +222,7 @@ void BrowserWindow::build_menus() inspect_menu.add_action(*m_inspect_dom_tree_action); auto js_console_action = GUI::Action::create( - "Open &JS Console", { Mod_Ctrl, Key_I }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-javascript.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) { + "Open &JS Console", { Mod_Ctrl, Key_I }, g_icon_bag.filetype_javascript, [this](auto&) { active_tab().show_console_window(); }, this); @@ -249,7 +249,7 @@ void BrowserWindow::build_menus() m_search_engine_actions.set_exclusive(true); auto& search_engine_menu = settings_menu.add_submenu("&Search Engine"); - search_engine_menu.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png").release_value_but_fixme_should_propagate_errors()); + search_engine_menu.set_icon(g_icon_bag.find); bool search_engine_set = false; m_disable_search_engine_action = GUI::Action::create_checkable( @@ -318,7 +318,7 @@ void BrowserWindow::build_menus() } auto& color_scheme_menu = settings_menu.add_submenu("&Color Scheme"); - color_scheme_menu.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/color-chooser.png").release_value_but_fixme_should_propagate_errors()); + color_scheme_menu.set_icon(g_icon_bag.color_chooser); { auto current_setting = Web::CSS::preferred_color_scheme_from_string(Config::read_string("Browser", "Preferences", "ColorScheme", "auto")); m_color_scheme_actions.set_exclusive(true); @@ -343,7 +343,7 @@ void BrowserWindow::build_menus() auto& debug_menu = add_menu("&Debug"); debug_menu.add_action(GUI::Action::create( - "Dump &DOM Tree", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/tree.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) { + "Dump &DOM Tree", g_icon_bag.tree, [this](auto&) { active_tab().m_web_content_view->debug_request("dump-dom-tree"); }, this)); @@ -360,7 +360,7 @@ void BrowserWindow::build_menus() debug_menu.add_action(GUI::Action::create("Dump &History", { Mod_Ctrl, Key_H }, [this](auto&) { active_tab().m_history.dump(); })); - debug_menu.add_action(GUI::Action::create("Dump C&ookies", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/cookie.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) { + debug_menu.add_action(GUI::Action::create("Dump C&ookies", g_icon_bag.cookie, [this](auto&) { auto& tab = active_tab(); if (tab.on_dump_cookies) tab.on_dump_cookies(); diff --git a/Userland/Applications/Browser/CMakeLists.txt b/Userland/Applications/Browser/CMakeLists.txt index f46be1c7c8..90e59b3ae9 100644 --- a/Userland/Applications/Browser/CMakeLists.txt +++ b/Userland/Applications/Browser/CMakeLists.txt @@ -18,6 +18,7 @@ set(SOURCES DownloadWidget.cpp EditBookmarkGML.h History.cpp + IconBag.cpp InspectorWidget.cpp Tab.cpp TabGML.h diff --git a/Userland/Applications/Browser/ConsoleWidget.cpp b/Userland/Applications/Browser/ConsoleWidget.cpp index c52d4f8646..9f46618ce4 100644 --- a/Userland/Applications/Browser/ConsoleWidget.cpp +++ b/Userland/Applications/Browser/ConsoleWidget.cpp @@ -8,6 +8,7 @@ #include "ConsoleWidget.h" #include +#include #include #include #include @@ -60,7 +61,7 @@ ConsoleWidget::ConsoleWidget() auto& clear_button = bottom_container.add(); clear_button.set_fixed_size(22, 22); - clear_button.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/delete.png").release_value_but_fixme_should_propagate_errors()); + clear_button.set_icon(g_icon_bag.delete_icon); clear_button.set_tooltip("Clear the console output"); clear_button.on_click = [this](auto) { clear_output(); diff --git a/Userland/Applications/Browser/IconBag.cpp b/Userland/Applications/Browser/IconBag.cpp new file mode 100644 index 0000000000..d8c4b7a61c --- /dev/null +++ b/Userland/Applications/Browser/IconBag.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022, Dylan Katz + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +namespace Browser { +ErrorOr IconBag::try_create() +{ + IconBag icon_bag; + + icon_bag.filetype_html = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-html.png")); + icon_bag.filetype_text = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-text.png")); + icon_bag.filetype_javascript = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-javascript.png")); + icon_bag.bookmark_contour = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/bookmark-contour.png")); + icon_bag.bookmark_filled = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/bookmark-filled.png")); + icon_bag.inspector_object = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object.png")); + icon_bag.find = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png")); + icon_bag.color_chooser = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/color-chooser.png")); + icon_bag.delete_icon = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/delete.png")); + icon_bag.new_tab = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new-tab.png")); + icon_bag.duplicate_tab = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/duplicate-tab.png")); + icon_bag.code = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/code.png")); + icon_bag.tree = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/tree.png")); + icon_bag.inspect = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspect.png")); + icon_bag.cookie = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/cookie.png")); + + return icon_bag; +} +} diff --git a/Userland/Applications/Browser/IconBag.h b/Userland/Applications/Browser/IconBag.h new file mode 100644 index 0000000000..bf33803b79 --- /dev/null +++ b/Userland/Applications/Browser/IconBag.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022, Dylan Katz + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Browser { +struct IconBag final { + static ErrorOr try_create(); + + RefPtr filetype_html { nullptr }; + RefPtr filetype_text { nullptr }; + RefPtr filetype_javascript { nullptr }; + RefPtr bookmark_contour { nullptr }; + RefPtr bookmark_filled { nullptr }; + RefPtr inspector_object { nullptr }; + RefPtr find { nullptr }; + RefPtr color_chooser { nullptr }; + RefPtr delete_icon { nullptr }; + RefPtr new_tab { nullptr }; + RefPtr duplicate_tab { nullptr }; + RefPtr code { nullptr }; + RefPtr tree { nullptr }; + RefPtr inspect { nullptr }; + RefPtr cookie { nullptr }; +}; +} diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index a347db0906..40ff08a91d 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -74,7 +74,7 @@ void Tab::view_source(const URL& url, const String& source) editor.set_ruler_visible(true); window->resize(640, 480); window->set_title(url.to_string()); - window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-text.png").release_value_but_fixme_should_propagate_errors()); + window->set_icon(g_icon_bag.filetype_text); window->show(); } @@ -98,9 +98,7 @@ Tab::Tab(BrowserWindow& window) m_go_back_context_menu = GUI::Menu::construct(); for (auto& url : m_history.get_back_title_history()) { i++; - m_go_back_context_menu->add_action(GUI::Action::create(url.to_string(), - Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-html.png").release_value_but_fixme_should_propagate_errors(), - [this, i](auto&) { go_back(i); })); + m_go_back_context_menu->add_action(GUI::Action::create(url.to_string(), g_icon_bag.filetype_html, [this, i](auto&) { go_back(i); })); } m_go_back_context_menu->popup(context_menu_event.screen_position()); }; @@ -113,9 +111,7 @@ Tab::Tab(BrowserWindow& window) m_go_forward_context_menu = GUI::Menu::construct(); for (auto& url : m_history.get_forward_title_history()) { i++; - m_go_forward_context_menu->add_action(GUI::Action::create(url.to_string(), - Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-html.png").release_value_but_fixme_should_propagate_errors(), - [this, i](auto&) { go_forward(i); })); + m_go_forward_context_menu->add_action(GUI::Action::create(url.to_string(), g_icon_bag.filetype_html, [this, i](auto&) { go_forward(i); })); } m_go_forward_context_menu->popup(context_menu_event.screen_position()); }; @@ -150,7 +146,7 @@ Tab::Tab(BrowserWindow& window) m_bookmark_button = toolbar.add(); m_bookmark_button->set_button_style(Gfx::ButtonStyle::Coolbar); m_bookmark_button->set_focus_policy(GUI::FocusPolicy::TabFocus); - m_bookmark_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/bookmark-contour.png").release_value_but_fixme_should_propagate_errors()); + m_bookmark_button->set_icon(g_icon_bag.bookmark_contour); m_bookmark_button->set_fixed_size(22, 22); m_bookmark_button->on_click = [this](auto) { @@ -332,7 +328,7 @@ Tab::Tab(BrowserWindow& window) m_tab_context_menu->add_action(GUI::CommonActions::make_close_tab_action([this](auto&) { on_tab_close_request(*this); })); - m_tab_context_menu->add_action(GUI::Action::create("&Duplicate Tab", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/duplicate-tab.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) { + m_tab_context_menu->add_action(GUI::Action::create("&Duplicate Tab", g_icon_bag.duplicate_tab, [this](auto&) { on_tab_open_request(url()); })); m_tab_context_menu->add_action(GUI::Action::create("Close &Other Tabs", [this](auto&) { @@ -413,10 +409,10 @@ void Tab::bookmark_current_url() void Tab::update_bookmark_button(const String& url) { if (BookmarksBarWidget::the().contains_bookmark(url)) { - m_bookmark_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/bookmark-filled.png").release_value_but_fixme_should_propagate_errors()); + m_bookmark_button->set_icon(g_icon_bag.bookmark_filled); m_bookmark_button->set_tooltip("Remove Bookmark"); } else { - m_bookmark_button->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/bookmark-contour.png").release_value_but_fixme_should_propagate_errors()); + m_bookmark_button->set_icon(g_icon_bag.bookmark_contour); m_bookmark_button->set_tooltip("Add Bookmark"); } } @@ -485,7 +481,7 @@ void Tab::show_inspector_window(Browser::Tab::InspectorTarget inspector_target) auto window = GUI::Window::construct(&this->window()); window->resize(300, 500); window->set_title("Inspector"); - window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object.png").release_value_but_fixme_should_propagate_errors()); + window->set_icon(g_icon_bag.inspector_object); window->on_close = [&]() { m_web_content_view->clear_inspected_dom_node(); }; @@ -514,7 +510,7 @@ void Tab::show_console_window() auto console_window = GUI::Window::construct(&window()); console_window->resize(500, 300); console_window->set_title("JS Console"); - console_window->set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-javascript.png").release_value_but_fixme_should_propagate_errors()); + console_window->set_icon(g_icon_bag.filetype_javascript); m_console_widget = console_window->set_main_widget(); m_console_widget->on_js_input = [this](String const& js_source) { m_web_content_view->js_console_input(js_source); diff --git a/Userland/Applications/Browser/WindowActions.cpp b/Userland/Applications/Browser/WindowActions.cpp index d91f7cfb16..dc65b0ea94 100644 --- a/Userland/Applications/Browser/WindowActions.cpp +++ b/Userland/Applications/Browser/WindowActions.cpp @@ -5,6 +5,7 @@ */ #include "WindowActions.h" +#include #include #include #include @@ -24,7 +25,7 @@ WindowActions::WindowActions(GUI::Window& window) VERIFY(!s_the); s_the = this; m_create_new_tab_action = GUI::Action::create( - "&New Tab", { Mod_Ctrl, Key_T }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/new-tab.png").release_value_but_fixme_should_propagate_errors(), [this](auto&) { + "&New Tab", { Mod_Ctrl, Key_T }, g_icon_bag.new_tab, [this](auto&) { if (on_create_new_tab) on_create_new_tab(); }, diff --git a/Userland/Applications/Browser/main.cpp b/Userland/Applications/Browser/main.cpp index ccbd85693b..33de4d1258 100644 --- a/Userland/Applications/Browser/main.cpp +++ b/Userland/Applications/Browser/main.cpp @@ -4,12 +4,12 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "Browser.h" -#include "BrowserWindow.h" -#include "CookieJar.h" -#include "Tab.h" -#include "WindowActions.h" #include +#include +#include +#include +#include +#include #include #include #include @@ -29,6 +29,7 @@ namespace Browser { String g_search_engine; String g_home_url; Vector g_content_filters; +IconBag g_icon_bag; } @@ -70,6 +71,8 @@ ErrorOr serenity_main(Main::Arguments arguments) Browser::g_home_url = Config::read_string("Browser", "Preferences", "Home", "file:///res/html/misc/welcome.html"); Browser::g_search_engine = Config::read_string("Browser", "Preferences", "SearchEngine", {}); + Browser::g_icon_bag = TRY(Browser::IconBag::try_create()); + auto ad_filter_list_or_error = Core::File::open(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::OpenMode::ReadOnly); if (!ad_filter_list_or_error.is_error()) { auto& ad_filter_list = *ad_filter_list_or_error.value();