From ef1bfcb9d881ba49cd0e2135375cf4cbb9e85a92 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 21 Jun 2019 15:02:11 +0200 Subject: [PATCH] WindowServer: Move some event code from WSWindowManager to WSMenuBarKeeper. --- Servers/WindowServer/WSMenuBarKeeper.cpp | 36 ++++++++++++++++++++++ Servers/WindowServer/WSMenuBarKeeper.h | 2 ++ Servers/WindowServer/WSWindowManager.cpp | 39 +----------------------- Servers/WindowServer/WSWindowManager.h | 4 +-- 4 files changed, 41 insertions(+), 40 deletions(-) diff --git a/Servers/WindowServer/WSMenuBarKeeper.cpp b/Servers/WindowServer/WSMenuBarKeeper.cpp index b6167995fc9..2ec0a8eb183 100644 --- a/Servers/WindowServer/WSMenuBarKeeper.cpp +++ b/Servers/WindowServer/WSMenuBarKeeper.cpp @@ -104,5 +104,41 @@ void WSMenuBarKeeper::refresh() void WSMenuBarKeeper::event(CEvent& event) { + if (event.type() == WSEvent::MouseMove || event.type() == WSEvent::MouseUp || event.type() == WSEvent::MouseDown || event.type() == WSEvent::MouseWheel) { + auto& mouse_event = static_cast(event); + WSWindowManager::the().for_each_active_menubar_menu([&](WSMenu& menu) { + if (menu.rect_in_menubar().contains(mouse_event.position())) { + handle_menu_mouse_event(menu, mouse_event); + return false; + } + return true; + }); + } return CObject::event(event); } + +void WSMenuBarKeeper::handle_menu_mouse_event(WSMenu& menu, const WSMouseEvent& event) +{ + auto& wm = WSWindowManager::the(); + bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove && wm.current_menu() && (wm.current_menu()->menubar() || wm.current_menu() == wm.system_menu()); + bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left; + bool should_open_menu = &menu != wm.current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button); + + if (should_open_menu) { + if (wm.current_menu() == &menu) + return; + wm.close_current_menu(); + if (!menu.is_empty()) { + auto& menu_window = menu.ensure_menu_window(); + menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 }); + menu_window.set_visible(true); + } + wm.set_current_menu(&menu); + refresh(); + return; + } + if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) { + wm.close_current_menu(); + return; + } +} diff --git a/Servers/WindowServer/WSMenuBarKeeper.h b/Servers/WindowServer/WSMenuBarKeeper.h index a0746f98a9d..8f34b78b60f 100644 --- a/Servers/WindowServer/WSMenuBarKeeper.h +++ b/Servers/WindowServer/WSMenuBarKeeper.h @@ -19,6 +19,8 @@ private: WSWindow& window() { return *m_window; } const WSWindow& window() const { return *m_window; } + void handle_menu_mouse_event(WSMenu&, const WSMouseEvent&); + void draw(); void tick_clock(); diff --git a/Servers/WindowServer/WSWindowManager.cpp b/Servers/WindowServer/WSWindowManager.cpp index 4714ed14b6c..541379ecfa9 100644 --- a/Servers/WindowServer/WSWindowManager.cpp +++ b/Servers/WindowServer/WSWindowManager.cpp @@ -385,31 +385,6 @@ void WSWindowManager::pick_new_active_window() }); } -void WSWindowManager::handle_menu_mouse_event(WSMenu& menu, const WSMouseEvent& event) -{ - bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove && m_current_menu && (m_current_menu->menubar() || m_current_menu == m_system_menu); - bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left; - bool should_open_menu = &menu != current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button); - - if (should_open_menu) { - if (current_menu() == &menu) - return; - close_current_menu(); - if (!menu.is_empty()) { - auto& menu_window = menu.ensure_menu_window(); - menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 }); - menu_window.set_visible(true); - } - m_current_menu = menu.make_weak_ptr(); - m_menubar_keeper.refresh(); - return; - } - if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) { - close_current_menu(); - return; - } -} - void WSWindowManager::close_current_menu() { if (m_current_menu && m_current_menu->menu_window()) @@ -418,17 +393,6 @@ void WSWindowManager::close_current_menu() m_menubar_keeper.refresh(); } -void WSWindowManager::handle_menubar_mouse_event(const WSMouseEvent& event) -{ - for_each_active_menubar_menu([&](WSMenu& menu) { - if (menu.rect_in_menubar().contains(event.position())) { - handle_menu_mouse_event(menu, event); - return false; - } - return true; - }); -} - void WSWindowManager::start_window_drag(WSWindow& window, const WSMouseEvent& event) { #ifdef DRAG_DEBUG @@ -718,10 +682,9 @@ void WSWindowManager::process_mouse_event(WSMouseEvent& event, WSWindow*& hovere } if (menubar_rect().contains(event.position())) { - handle_menubar_mouse_event(event); + m_menubar_keeper.event(event); return; } - if (m_current_menu && m_current_menu->menu_window()) { auto& window = *m_current_menu->menu_window(); bool event_is_inside_current_menu = window.rect().contains(event.position()); diff --git a/Servers/WindowServer/WSWindowManager.h b/Servers/WindowServer/WSWindowManager.h index 1d60e070023..d73fffb9714 100644 --- a/Servers/WindowServer/WSWindowManager.h +++ b/Servers/WindowServer/WSWindowManager.h @@ -80,6 +80,7 @@ public: void set_current_menubar(WSMenuBar*); WSMenu* current_menu() { return m_current_menu.ptr(); } void set_current_menu(WSMenu*); + WSMenu* system_menu() { return m_system_menu.ptr(); } const WSCursor& active_cursor() const; const WSCursor& arrow_cursor() const { return *m_arrow_cursor; } @@ -106,6 +107,7 @@ public: void close_menubar(WSMenuBar&); Color menu_selection_color() const { return m_menu_selection_color; } int menubar_menu_margin() const; + void close_current_menu(); void set_resolution(int width, int height); @@ -149,7 +151,6 @@ private: bool process_ongoing_window_resize(const WSMouseEvent&, WSWindow*& hovered_window); bool process_ongoing_window_drag(WSMouseEvent&, WSWindow*& hovered_window); void handle_menu_mouse_event(WSMenu&, const WSMouseEvent&); - void handle_menubar_mouse_event(const WSMouseEvent&); void handle_close_button_mouse_event(WSWindow&, const WSMouseEvent&); void start_window_drag(WSWindow&, const WSMouseEvent&); void handle_client_request(const WSAPIClientRequest&); @@ -167,7 +168,6 @@ private: template void for_each_window(Callback); - void close_current_menu(); virtual void event(CEvent&) override; void paint_window_frame(const WSWindow&); void tell_wm_listener_about_window(WSWindow& listener, WSWindow&);