summaryrefslogtreecommitdiff
path: root/WindowServer
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-02-11 10:55:02 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-02-11 10:55:02 +0100
commit145aa27b8fc28dd9fc81244b76e75fda4ab8192e (patch)
tree115d09a8c2895e79d030bceeaf1d6af454a51c93 /WindowServer
parent78fc7a9ef252a6ab44c4ca1fffc5b8849209c924 (diff)
downloadserenity-145aa27b8fc28dd9fc81244b76e75fda4ab8192e.zip
WindowServer: Give menu items an identifier field and add a simple callback.
Eventually these identifiers will be sent to the userspace client who created the menu. None of that is hooked up yet though.
Diffstat (limited to 'WindowServer')
-rw-r--r--WindowServer/WSMenu.cpp16
-rw-r--r--WindowServer/WSMenu.h4
-rw-r--r--WindowServer/WSMenuItem.cpp3
-rw-r--r--WindowServer/WSMenuItem.h6
-rw-r--r--WindowServer/WSWindowManager.cpp21
5 files changed, 41 insertions, 9 deletions
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<void(WSMenuItem&)> 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 <AK/AKString.h>
+#include <AK/Function.h>
#include <SharedGraphics/Rect.h>
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<WSMenu>("Serenity");
- menu->add_item(make<WSMenuItem>("Launch Terminal"));
+ menu->add_item(make<WSMenuItem>(0, "Launch Terminal"));
menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
- menu->add_item(make<WSMenuItem>("Hello again"));
- menu->add_item(make<WSMenuItem>("To all my friends"));
- menu->add_item(make<WSMenuItem>("Together we can play some rock&roll"));
+ menu->add_item(make<WSMenuItem>(1, "Hello again"));
+ menu->add_item(make<WSMenuItem>(2, "To all my friends"));
+ menu->add_item(make<WSMenuItem>(3, "Together we can play some rock&roll"));
menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
- menu->add_item(make<WSMenuItem>("About..."));
+ menu->add_item(make<WSMenuItem>(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<WSMenu>("Application");
- menu->add_item(make<WSMenuItem>("Bar!"));
- menu->add_item(make<WSMenuItem>("Foo!"));
+ menu->add_item(make<WSMenuItem>(5, "Foo."));
+ menu->add_item(make<WSMenuItem>(6, "Bar?"));
+ menu->add_item(make<WSMenuItem>(7, "Baz!"));
+ menu->on_item_activation = [] (WSMenuItem& item) {
+ kprintf("WSMenu 2 item activated: '%s'\n", item.text().characters());
+ };
menubar->add_menu(move(menu));
}