From e9687ee50e3cdd3014f0364c96663b650f5ace63 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Tue, 10 Mar 2020 20:56:07 +1300 Subject: [PATCH] WindowServer: Control menu title font from menubar It makes a little more sense for the menubar to control what the font of the menu title is, as opposed to the menu manager. Menumanager now simply uses the font that the menu wants it to use. --- Servers/WindowServer/Menu.cpp | 10 ++++++++++ Servers/WindowServer/Menu.h | 5 +++++ Servers/WindowServer/MenuBar.cpp | 11 +++++++++++ Servers/WindowServer/MenuBar.h | 6 +----- Servers/WindowServer/MenuManager.cpp | 19 +++---------------- Servers/WindowServer/MenuManager.h | 3 --- 6 files changed, 30 insertions(+), 24 deletions(-) diff --git a/Servers/WindowServer/Menu.cpp b/Servers/WindowServer/Menu.cpp index 779012b8ec..e02d4a2031 100644 --- a/Servers/WindowServer/Menu.cpp +++ b/Servers/WindowServer/Menu.cpp @@ -56,6 +56,16 @@ Menu::~Menu() { } +void Menu::set_title_font(const Gfx::Font& font) +{ + m_title_font = &font; +} + +const Gfx::Font& Menu::title_font() const +{ + return *m_title_font; +} + const Gfx::Font& Menu::font() const { return Gfx::Font::default_font(); diff --git a/Servers/WindowServer/Menu.h b/Servers/WindowServer/Menu.h index 7cb3486d52..db4c76ea3b 100644 --- a/Servers/WindowServer/Menu.h +++ b/Servers/WindowServer/Menu.h @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -96,6 +97,8 @@ public: void draw(); const Gfx::Font& font() const; + const Gfx::Font& title_font() const; + void set_title_font(const Gfx::Font& font); MenuItem* item_with_identifier(unsigned); void redraw(); @@ -119,6 +122,8 @@ public: private: virtual void event(Core::Event&) override; + RefPtr m_title_font { &Gfx::Font::default_font() }; + void handle_mouse_move_event(const MouseEvent&); int visible_item_count() const; diff --git a/Servers/WindowServer/MenuBar.cpp b/Servers/WindowServer/MenuBar.cpp index f581ea51b7..27dbdcbebf 100644 --- a/Servers/WindowServer/MenuBar.cpp +++ b/Servers/WindowServer/MenuBar.cpp @@ -41,4 +41,15 @@ MenuBar::~MenuBar() { } +void MenuBar::add_menu(Menu& menu) +{ + menu.set_menubar(this); + + // NOTE: We assume that the first menu is the App menu, which has a bold font. + if (m_menus.is_empty()) + menu.set_title_font(Gfx::Font::default_bold_font()); + + m_menus.append(&menu); +} + } diff --git a/Servers/WindowServer/MenuBar.h b/Servers/WindowServer/MenuBar.h index e53ac5f76e..31dff9ac0f 100644 --- a/Servers/WindowServer/MenuBar.h +++ b/Servers/WindowServer/MenuBar.h @@ -41,11 +41,7 @@ public: ClientConnection& client() { return m_client; } const ClientConnection& client() const { return m_client; } int menubar_id() const { return m_menubar_id; } - void add_menu(Menu& menu) - { - menu.set_menubar(this); - m_menus.append(&menu); - } + void add_menu(Menu&); template void for_each_menu(Callback callback) diff --git a/Servers/WindowServer/MenuManager.cpp b/Servers/WindowServer/MenuManager.cpp index 8aca8bdcd3..39ae0f78d7 100644 --- a/Servers/WindowServer/MenuManager.cpp +++ b/Servers/WindowServer/MenuManager.cpp @@ -74,16 +74,6 @@ bool MenuManager::is_open(const Menu& menu) const return false; } -const Gfx::Font& MenuManager::menu_font() const -{ - return Gfx::Font::default_font(); -} - -const Gfx::Font& MenuManager::app_menu_font() const -{ - return Gfx::Font::default_bold_font(); -} - void MenuManager::draw() { auto& wm = WindowManager::the(); @@ -100,7 +90,7 @@ void MenuManager::draw() painter.fill_rect(menubar_rect, palette.window()); painter.draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, palette.threed_shadow1()); - int index = 0; + for_each_active_menubar_menu([&](Menu& menu) { Color text_color = palette.window_text(); if (is_open(menu)) { @@ -111,10 +101,9 @@ void MenuManager::draw() painter.draw_text( menu.text_rect_in_menubar(), menu.name(), - index == 1 ? app_menu_font() : menu_font(), + menu.title_font(), Gfx::TextAlignment::CenterLeft, text_color); - ++index; return IterationDecision::Continue; }); @@ -382,13 +371,11 @@ void MenuManager::set_current_menubar(MenuBar* menubar) dbg() << "[WM] Current menubar is now " << menubar; #endif Gfx::Point next_menu_location { MenuManager::menubar_menu_margin() / 2, 0 }; - int index = 0; for_each_active_menubar_menu([&](Menu& menu) { - int text_width = index == 1 ? Gfx::Font::default_bold_font().width(menu.name()) : Gfx::Font::default_font().width(menu.name()); + int text_width = menu.title_font().width(menu.name()); menu.set_rect_in_menubar({ next_menu_location.x() - MenuManager::menubar_menu_margin() / 2, 0, text_width + MenuManager::menubar_menu_margin(), menubar_rect().height() - 1 }); menu.set_text_rect_in_menubar({ next_menu_location, { text_width, menubar_rect().height() } }); next_menu_location.move_by(menu.rect_in_menubar().width(), 0); - ++index; return IterationDecision::Continue; }); refresh(); diff --git a/Servers/WindowServer/MenuManager.h b/Servers/WindowServer/MenuManager.h index 41df33b232..f4d5d5aad8 100644 --- a/Servers/WindowServer/MenuManager.h +++ b/Servers/WindowServer/MenuManager.h @@ -95,9 +95,6 @@ public: void did_change_theme(); private: - const Gfx::Font& menu_font() const; - const Gfx::Font& app_menu_font() const; - void close_menus(const Vector&); const Window& window() const { return *m_window; }