diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-25 17:20:23 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-25 17:20:23 +0200 |
commit | bd45b2b8d3a73cba825d8283091d2c14fa00a46f (patch) | |
tree | 70400504de9c071271a7fca4582609b0ecb35fbe | |
parent | dc6b61dbcce945166dfa244959c0053d0ee07efe (diff) | |
download | serenity-bd45b2b8d3a73cba825d8283091d2c14fa00a46f.zip |
Browser: Share one BookmarksBarWidget between all Tabs
-rw-r--r-- | Applications/Browser/BookmarksBarWidget.cpp | 12 | ||||
-rw-r--r-- | Applications/Browser/BookmarksBarWidget.h | 6 | ||||
-rw-r--r-- | Applications/Browser/Tab.cpp | 102 | ||||
-rw-r--r-- | Applications/Browser/Tab.h | 4 | ||||
-rw-r--r-- | Applications/Browser/WindowActions.cpp | 7 | ||||
-rw-r--r-- | Applications/Browser/WindowActions.h | 3 | ||||
-rw-r--r-- | Applications/Browser/main.cpp | 11 |
7 files changed, 94 insertions, 51 deletions
diff --git a/Applications/Browser/BookmarksBarWidget.cpp b/Applications/Browser/BookmarksBarWidget.cpp index 6e97b95c26..bda2edfee4 100644 --- a/Applications/Browser/BookmarksBarWidget.cpp +++ b/Applications/Browser/BookmarksBarWidget.cpp @@ -36,8 +36,18 @@ #include <LibGUI/Window.h> #include <LibGfx/Palette.h> +namespace Browser { + +static BookmarksBarWidget* s_the; + +BookmarksBarWidget& BookmarksBarWidget::the() +{ + return *s_the; +} + BookmarksBarWidget::BookmarksBarWidget(const String& bookmarks_file, bool enabled) { + s_the = this; set_layout<GUI::HorizontalBoxLayout>(); layout()->set_spacing(0); @@ -207,3 +217,5 @@ bool BookmarksBarWidget::add_bookmark(const String& url, const String& title) } return false; } + +} diff --git a/Applications/Browser/BookmarksBarWidget.h b/Applications/Browser/BookmarksBarWidget.h index 586192eea7..8637a80103 100644 --- a/Applications/Browser/BookmarksBarWidget.h +++ b/Applications/Browser/BookmarksBarWidget.h @@ -29,9 +29,13 @@ #include <LibGUI/Forward.h> #include <LibGUI/Widget.h> +namespace Browser { + class BookmarksBarWidget final : public GUI::Widget { C_OBJECT(BookmarksBarWidget) public: + static BookmarksBarWidget& the(); + virtual ~BookmarksBarWidget() override; void set_model(RefPtr<GUI::Model>); @@ -62,3 +66,5 @@ private: int m_last_visible_index { -1 }; }; + +} diff --git a/Applications/Browser/Tab.cpp b/Applications/Browser/Tab.cpp index f296eaebe9..d388408a06 100644 --- a/Applications/Browser/Tab.cpp +++ b/Applications/Browser/Tab.cpp @@ -26,9 +26,9 @@ #include "Tab.h" #include "BookmarksBarWidget.h" -#include "WindowActions.h" #include "History.h" #include "InspectorWidget.h" +#include "WindowActions.h" #include <LibGUI/Action.h> #include <LibGUI/Application.h> #include <LibGUI/BoxLayout.h> @@ -57,24 +57,16 @@ namespace Browser { static const char* home_url = "file:///home/anon/www/welcome.html"; -static const char* bookmarks_filename = "/home/anon/bookmarks.json"; Tab::Tab() { auto& widget = *this; set_layout<GUI::VerticalBoxLayout>(); - bool bookmarksbar_enabled = true; - - auto& toolbar_container = widget.add<GUI::ToolBarContainer>(); - auto& toolbar = toolbar_container.add<GUI::ToolBar>(); - m_bookmarks_bar = toolbar_container.add<BookmarksBarWidget>(bookmarks_filename, bookmarksbar_enabled); + m_toolbar_container = widget.add<GUI::ToolBarContainer>(); + auto& toolbar = m_toolbar_container->add<GUI::ToolBar>(); m_html_widget = widget.add<Web::HtmlView>(); - m_bookmarks_bar->on_bookmark_click = [this](auto&, auto& url) { - m_html_widget->load(url); - }; - m_go_back_action = GUI::CommonActions::make_go_back_action([this](auto&) { m_history.go_back(); update_actions(); @@ -115,10 +107,10 @@ Tab::Tab() m_bookmark_button->on_click = [this] { auto url = m_html_widget->main_frame().document()->url().to_string(); - if (m_bookmarks_bar->contains_bookmark(url)) { - m_bookmarks_bar->remove_bookmark(url); + if (BookmarksBarWidget::the().contains_bookmark(url)) { + BookmarksBarWidget::the().remove_bookmark(url); } else { - m_bookmarks_bar->add_bookmark(url, m_title); + BookmarksBarWidget::the().add_bookmark(url, m_title); } update_bookmark_button(url); }; @@ -171,10 +163,6 @@ Tab::Tab() m_statusbar->set_text(href); }; - m_bookmarks_bar->on_bookmark_hover = [this](auto&, auto& url) { - m_statusbar->set_text(url); - }; - Web::ResourceLoader::the().on_load_counter_change = [this] { if (Web::ResourceLoader::the().pending_loads() == 0) { m_statusbar->set_text(""); @@ -187,14 +175,18 @@ Tab::Tab() auto& app_menu = m_menubar->add_menu("Browser"); app_menu.add_action(WindowActions::the().create_new_tab_action()); - app_menu.add_action(GUI::Action::create("Close tab", { Mod_Ctrl, Key_W }, Gfx::Bitmap::load_from_file("/res/icons/16x16/close-tab.png"), [this](auto&) { - on_tab_close_request(*this); - }, this)); - - app_menu.add_action(GUI::Action::create("Reload", { Mod_None, Key_F5 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"), [this](auto&) { - TemporaryChange<bool> change(m_should_push_loads_to_history, false); - m_html_widget->reload(); - }, this)); + app_menu.add_action(GUI::Action::create( + "Close tab", { Mod_Ctrl, Key_W }, Gfx::Bitmap::load_from_file("/res/icons/16x16/close-tab.png"), [this](auto&) { + on_tab_close_request(*this); + }, + this)); + + app_menu.add_action(GUI::Action::create( + "Reload", { Mod_None, Key_F5 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"), [this](auto&) { + TemporaryChange<bool> change(m_should_push_loads_to_history, false); + m_html_widget->reload(); + }, + this)); app_menu.add_separator(); app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) { GUI::Application::the().quit(); @@ -234,32 +226,35 @@ Tab::Tab() })); auto& debug_menu = m_menubar->add_menu("Debug"); - debug_menu.add_action(GUI::Action::create("Dump DOM tree", [this](auto&) { - Web::dump_tree(*m_html_widget->document()); - }, this)); - debug_menu.add_action(GUI::Action::create("Dump Layout tree", [this](auto&) { - Web::dump_tree(*m_html_widget->document()->layout_node()); - }, this)); - debug_menu.add_action(GUI::Action::create("Dump Style sheets", [this](auto&) { - for (auto& sheet : m_html_widget->document()->stylesheets()) { - dump_sheet(sheet); - } - }, this)); + debug_menu.add_action(GUI::Action::create( + "Dump DOM tree", [this](auto&) { + Web::dump_tree(*m_html_widget->document()); + }, + this)); + debug_menu.add_action(GUI::Action::create( + "Dump Layout tree", [this](auto&) { + Web::dump_tree(*m_html_widget->document()->layout_node()); + }, + this)); + debug_menu.add_action(GUI::Action::create( + "Dump Style sheets", [this](auto&) { + for (auto& sheet : m_html_widget->document()->stylesheets()) { + dump_sheet(sheet); + } + }, + this)); debug_menu.add_separator(); - auto line_box_borders_action = GUI::Action::create_checkable("Line box borders", [this](auto& action) { - m_html_widget->set_should_show_line_box_borders(action.is_checked()); - m_html_widget->update(); - }, this); + auto line_box_borders_action = GUI::Action::create_checkable( + "Line box borders", [this](auto& action) { + m_html_widget->set_should_show_line_box_borders(action.is_checked()); + m_html_widget->update(); + }, + this); line_box_borders_action->set_checked(false); debug_menu.add_action(line_box_borders_action); auto& bookmarks_menu = m_menubar->add_menu("Bookmarks"); - auto show_bookmarksbar_action = GUI::Action::create_checkable("Show bookmarks bar", [this](auto& action) { - m_bookmarks_bar->set_visible(action.is_checked()); - m_bookmarks_bar->update(); - }, this); - show_bookmarksbar_action->set_checked(bookmarksbar_enabled); - bookmarks_menu.add_action(show_bookmarksbar_action); + bookmarks_menu.add_action(WindowActions::the().show_bookmarks_bar_action()); auto& help_menu = m_menubar->add_menu("Help"); help_menu.add_action(WindowActions::the().about_action()); @@ -282,7 +277,7 @@ void Tab::update_actions() void Tab::update_bookmark_button(const String& url) { - if (m_bookmarks_bar->contains_bookmark(url)) { + if (BookmarksBarWidget::the().contains_bookmark(url)) { m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-yellow.png")); } else { m_bookmark_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/star-contour.png")); @@ -291,6 +286,17 @@ void Tab::update_bookmark_button(const String& url) void Tab::did_become_active() { + BookmarksBarWidget::the().on_bookmark_click = [this](auto&, auto& url) { + m_html_widget->load(url); + }; + + BookmarksBarWidget::the().on_bookmark_hover = [this](auto&, auto& url) { + m_statusbar->set_text(url); + }; + + BookmarksBarWidget::the().remove_from_parent(); + m_toolbar_container->add_child(BookmarksBarWidget::the()); + GUI::Application::the().set_menubar(m_menubar); } diff --git a/Applications/Browser/Tab.h b/Applications/Browser/Tab.h index 9079716b32..f952895bb7 100644 --- a/Applications/Browser/Tab.h +++ b/Applications/Browser/Tab.h @@ -29,8 +29,6 @@ #include <LibGUI/Widget.h> #include <LibWeb/Forward.h> -class BookmarksBarWidget; - namespace Browser { class Tab final : public GUI::Widget { @@ -62,11 +60,11 @@ private: RefPtr<GUI::Action> m_go_back_action; RefPtr<GUI::Action> m_go_forward_action; RefPtr<GUI::TextBox> m_location_box; - RefPtr<BookmarksBarWidget> m_bookmarks_bar; RefPtr<GUI::Button> m_bookmark_button; RefPtr<GUI::Window> m_dom_inspector_window; RefPtr<GUI::StatusBar> m_statusbar; RefPtr<GUI::MenuBar> m_menubar; + RefPtr<GUI::ToolBarContainer> m_toolbar_container; String m_title; RefPtr<const Gfx::Bitmap> m_icon; diff --git a/Applications/Browser/WindowActions.cpp b/Applications/Browser/WindowActions.cpp index 000405478f..628ac5430c 100644 --- a/Applications/Browser/WindowActions.cpp +++ b/Applications/Browser/WindowActions.cpp @@ -43,6 +43,13 @@ WindowActions::WindowActions(GUI::Window& window) on_about(); }, &window); + m_show_bookmarks_bar_action = GUI::Action::create_checkable( + "Show bookmarks bar", + [this](auto& action) { + if (on_show_bookmarks_bar) + on_show_bookmarks_bar(action); + }, + &window); } } diff --git a/Applications/Browser/WindowActions.h b/Applications/Browser/WindowActions.h index e03233d349..ba564b56de 100644 --- a/Applications/Browser/WindowActions.h +++ b/Applications/Browser/WindowActions.h @@ -14,17 +14,20 @@ public: Function<void()> on_next_tab; Function<void()> on_previous_tab; Function<void()> on_about; + Function<void(GUI::Action&)> on_show_bookmarks_bar; GUI::Action& create_new_tab_action() { return *m_create_new_tab_action; } GUI::Action& next_tab_action() { return *m_next_tab_action; } GUI::Action& previous_tab_action() { return *m_previous_tab_action; } GUI::Action& about_action() { return *m_about_action; } + GUI::Action& show_bookmarks_bar_action() { return *m_show_bookmarks_bar_action; } private: RefPtr<GUI::Action> m_create_new_tab_action; RefPtr<GUI::Action> m_next_tab_action; RefPtr<GUI::Action> m_previous_tab_action; RefPtr<GUI::Action> m_about_action; + RefPtr<GUI::Action> m_show_bookmarks_bar_action; }; } diff --git a/Applications/Browser/main.cpp b/Applications/Browser/main.cpp index 3003e4823c..af0d5f03e2 100644 --- a/Applications/Browser/main.cpp +++ b/Applications/Browser/main.cpp @@ -24,6 +24,7 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "BookmarksBarWidget.h" #include "InspectorWidget.h" #include "Tab.h" #include "WindowActions.h" @@ -39,6 +40,8 @@ #include <stdio.h> #include <stdlib.h> +static const char* bookmarks_filename = "/home/anon/bookmarks.json"; + int main(int argc, char** argv) { if (getuid() == 0) { @@ -76,6 +79,9 @@ int main(int argc, char** argv) auto m_config = Core::ConfigFile::get_for_app("Browser"); auto home_url = m_config->read_entry("Preferences", "Home", "file:///home/anon/www/welcome.html"); + bool bookmarksbar_enabled = true; + auto bookmarks_bar = Browser::BookmarksBarWidget::construct(bookmarks_filename, bookmarksbar_enabled); + auto window = GUI::Window::construct(); window->set_rect(100, 100, 640, 480); window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png")); @@ -161,6 +167,11 @@ int main(int argc, char** argv) GUI::AboutDialog::show("Browser", Gfx::Bitmap::load_from_file("/res/icons/32x32/filetype-html.png"), window); }; + window_actions.on_show_bookmarks_bar = [&](auto& action) { + Browser::BookmarksBarWidget::the().set_visible(action.is_checked()); + }; + window_actions.show_bookmarks_bar_action().set_checked(bookmarksbar_enabled); + create_new_tab(default_url, true); window->show(); |