From 6e6e0b9de8cfdf1c3866ba14621d39f0cff194d6 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 5 Dec 2019 19:59:11 +0100 Subject: [PATCH] WindowServer: Compute some layout rects in WSMenuManager up front Currently menu applets are laid out relative to the "audio rect" which is the rect of the little audio muted state icon thingy. There was an issue where applets would be placed at a negative X coord if they were added to the WindowServer before the first time drawing the menubar. --- Servers/WindowServer/WSMenuManager.cpp | 54 +++++++++++++----------- Servers/WindowServer/WSMenuManager.h | 5 +++ Servers/WindowServer/WSWindowManager.cpp | 11 ++--- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/Servers/WindowServer/WSMenuManager.cpp b/Servers/WindowServer/WSMenuManager.cpp index fdb7042feb1..a02598fa761 100644 --- a/Servers/WindowServer/WSMenuManager.cpp +++ b/Servers/WindowServer/WSMenuManager.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -34,6 +35,26 @@ WSMenuManager::WSMenuManager() last_update_time = now; } }); + + auto menubar_rect = this->menubar_rect(); + + int username_width = Font::default_bold_font().width(m_username); + m_username_rect = { + menubar_rect.right() - menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username), + menubar_rect.y(), + username_width, + menubar_rect.height() + }; + + int time_width = Font::default_font().width("2222-22-22 22:22:22"); + m_time_rect = { + m_username_rect.left() - menubar_menu_margin() / 2 - time_width, + menubar_rect.y(), + time_width, + menubar_rect.height() + }; + + m_audio_rect = { m_time_rect.right() - time_width - 20, m_time_rect.y() + 1, 12, 16 }; } WSMenuManager::~WSMenuManager() @@ -43,7 +64,7 @@ WSMenuManager::~WSMenuManager() void WSMenuManager::setup() { m_window = WSWindow::construct(*this, WSWindowType::Menubar); - m_window->set_rect(WSWindowManager::the().menubar_rect()); + m_window->set_rect(menubar_rect()); } bool WSMenuManager::is_open(const WSMenu& menu) const @@ -58,7 +79,7 @@ bool WSMenuManager::is_open(const WSMenu& menu) const void WSMenuManager::draw() { auto& wm = WSWindowManager::the(); - auto menubar_rect = wm.menubar_rect(); + auto menubar_rect = this->menubar_rect(); if (m_needs_window_resize) { m_window->set_rect(menubar_rect); @@ -87,16 +108,7 @@ void WSMenuManager::draw() return true; }); - int username_width = Font::default_bold_font().width(m_username); - - // FIXME: This rect should only be computed once. - Rect username_rect { - menubar_rect.right() - wm.menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username), - menubar_rect.y(), - username_width, - menubar_rect.height() - }; - painter.draw_text(username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, Color::Black); + painter.draw_text(m_username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, Color::Black); time_t now = time(nullptr); auto* tm = localtime(&now); @@ -107,20 +119,9 @@ void WSMenuManager::draw() tm->tm_hour, tm->tm_min, tm->tm_sec); - int time_width = wm.font().width(time_text); - // FIXME: This rect should only be computed once. - Rect time_rect { - username_rect.left() - wm.menubar_menu_margin() / 2 - time_width, - menubar_rect.y(), - time_width, - menubar_rect.height() - }; - painter.draw_text(time_rect, time_text, wm.font(), TextAlignment::CenterRight, Color::Black); - - // FIXME: This rect should only be computed once. - m_audio_rect = { time_rect.right() - wm.font().width(time_text) - 20, time_rect.y() + 1, 12, 16 }; + painter.draw_text(m_time_rect, time_text, wm.font(), TextAlignment::CenterRight, Color::Black); auto& audio_bitmap = m_audio_muted ? *m_muted_bitmap : *m_unmuted_bitmap; painter.blit(m_audio_rect.location(), audio_bitmap, audio_bitmap.rect()); @@ -322,3 +323,8 @@ void WSMenuManager::invalidate_applet(WSMenuApplet& applet, const Rect& rect) draw_applet(applet); window().invalidate(); } + +Rect WSMenuManager::menubar_rect() const +{ + return { 0, 0, WSScreen::the().rect().width(), 18 }; +} diff --git a/Servers/WindowServer/WSMenuManager.h b/Servers/WindowServer/WSMenuManager.h index 7569cd1674c..706c0fcc968 100644 --- a/Servers/WindowServer/WSMenuManager.h +++ b/Servers/WindowServer/WSMenuManager.h @@ -23,6 +23,9 @@ public: Vector>& open_menu_stack() { return m_open_menu_stack; } + Rect menubar_rect() const; + static int menubar_menu_margin() { return 16; } + void set_needs_window_resize(); WSMenu* current_menu() { return m_current_menu.ptr(); } @@ -64,6 +67,8 @@ private: OwnPtr m_audio_client; Rect m_audio_rect; + Rect m_username_rect; + Rect m_time_rect; bool m_needs_window_resize { false }; bool m_bar_open { false }; diff --git a/Servers/WindowServer/WSWindowManager.cpp b/Servers/WindowServer/WSWindowManager.cpp index c9433ac7796..4f20d54350f 100644 --- a/Servers/WindowServer/WSWindowManager.cpp +++ b/Servers/WindowServer/WSWindowManager.cpp @@ -241,11 +241,6 @@ void WSWindowManager::set_resolution(int width, int height) } } -int WSWindowManager::menubar_menu_margin() const -{ - return 16; -} - void WSWindowManager::set_current_menubar(WSMenuBar* menubar) { if (menubar) @@ -255,11 +250,11 @@ void WSWindowManager::set_current_menubar(WSMenuBar* menubar) #ifdef DEBUG_MENUS dbg() << "[WM] Current menubar is now " << menubar; #endif - Point next_menu_location { menubar_menu_margin() / 2, 0 }; + Point next_menu_location { WSMenuManager::menubar_menu_margin() / 2, 0 }; int index = 0; for_each_active_menubar_menu([&](WSMenu& menu) { int text_width = index == 1 ? Font::default_bold_font().width(menu.name()) : font().width(menu.name()); - menu.set_rect_in_menubar({ next_menu_location.x() - menubar_menu_margin() / 2, 0, text_width + menubar_menu_margin(), menubar_rect().height() - 1 }); + menu.set_rect_in_menubar({ next_menu_location.x() - WSMenuManager::menubar_menu_margin() / 2, 0, text_width + WSMenuManager::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; @@ -926,7 +921,7 @@ Rect WSWindowManager::menubar_rect() const { if (active_fullscreen_window()) return {}; - return { 0, 0, WSScreen::the().rect().width(), 18 }; + return m_menu_manager.menubar_rect(); } void WSWindowManager::draw_window_switcher()