diff --git a/WindowServer/WSMenu.cpp b/WindowServer/WSMenu.cpp index 0086c56834..505c4f35ab 100644 --- a/WindowServer/WSMenu.cpp +++ b/WindowServer/WSMenu.cpp @@ -107,7 +107,23 @@ void WSMenu::on_window_message(WSMessage& message) return; m_hovered_item = item; redraw(); + return; } + + if (message.type() == WSMessage::MouseUp) { + if (!m_hovered_item) + return; + did_activate(*m_hovered_item); + m_hovered_item = nullptr; + redraw(); + return; + } +} + +void WSMenu::did_activate(WSMenuItem& item) +{ + if (on_item_activation) + on_item_activation(item); } WSMenuItem* WSMenu::item_at(const Point& position) diff --git a/WindowServer/WSMenu.h b/WindowServer/WSMenu.h index 959dbfdf50..dc7adb4592 100644 --- a/WindowServer/WSMenu.h +++ b/WindowServer/WSMenu.h @@ -57,7 +57,11 @@ public: WSMenuItem* item_at(const Point&); void redraw(); + Function on_item_activation; + private: + void did_activate(WSMenuItem&); + String m_name; Rect m_rect_in_menubar; Rect m_text_rect_in_menubar; diff --git a/WindowServer/WSMenuItem.cpp b/WindowServer/WSMenuItem.cpp index 2a94937210..b0f2e3e03a 100644 --- a/WindowServer/WSMenuItem.cpp +++ b/WindowServer/WSMenuItem.cpp @@ -1,7 +1,8 @@ #include "WSMenuItem.h" -WSMenuItem::WSMenuItem(const String& text) +WSMenuItem::WSMenuItem(unsigned identifier, const String& text) : m_type(Text) + , m_identifier(identifier) , m_text(text) { } diff --git a/WindowServer/WSMenuItem.h b/WindowServer/WSMenuItem.h index 36ffd51e5f..0a21adb92d 100644 --- a/WindowServer/WSMenuItem.h +++ b/WindowServer/WSMenuItem.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include class WSMenuItem { @@ -11,7 +12,7 @@ public: Separator, }; - explicit WSMenuItem(const String& text); + explicit WSMenuItem(unsigned identifier, const String& text); explicit WSMenuItem(Type); ~WSMenuItem(); @@ -23,9 +24,12 @@ public: void set_rect(const Rect& rect) { m_rect = rect; } Rect rect() const { return m_rect; } + unsigned identifier() const { return m_identifier; } + private: Type m_type { None }; bool m_enabled { true }; + unsigned m_identifier { 0 }; String m_text; Rect m_rect; }; diff --git a/WindowServer/WSWindowManager.cpp b/WindowServer/WSWindowManager.cpp index 215b8a0285..d60e556891 100644 --- a/WindowServer/WSWindowManager.cpp +++ b/WindowServer/WSWindowManager.cpp @@ -185,19 +185,26 @@ WSWindowManager::WSWindowManager() { auto menu = make("Serenity"); - menu->add_item(make("Launch Terminal")); + menu->add_item(make(0, "Launch Terminal")); menu->add_item(make(WSMenuItem::Separator)); - menu->add_item(make("Hello again")); - menu->add_item(make("To all my friends")); - menu->add_item(make("Together we can play some rock&roll")); + menu->add_item(make(1, "Hello again")); + menu->add_item(make(2, "To all my friends")); + menu->add_item(make(3, "Together we can play some rock&roll")); menu->add_item(make(WSMenuItem::Separator)); - menu->add_item(make("About...")); + menu->add_item(make(4, "About...")); + menu->on_item_activation = [] (WSMenuItem& item) { + kprintf("WSMenu 1 item activated: '%s'\n", item.text().characters()); + }; menubar->add_menu(move(menu)); } { auto menu = make("Application"); - menu->add_item(make("Bar!")); - menu->add_item(make("Foo!")); + menu->add_item(make(5, "Foo.")); + menu->add_item(make(6, "Bar?")); + menu->add_item(make(7, "Baz!")); + menu->on_item_activation = [] (WSMenuItem& item) { + kprintf("WSMenu 2 item activated: '%s'\n", item.text().characters()); + }; menubar->add_menu(move(menu)); }