mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 05:32:44 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									78fc7a9ef2
								
							
						
					
					
						commit
						145aa27b8f
					
				
					 5 changed files with 41 additions and 9 deletions
				
			
		|  | @ -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) | ||||
|  |  | |||
|  | @ -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; | ||||
|  |  | |||
|  | @ -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) | ||||
| { | ||||
| } | ||||
|  |  | |||
|  | @ -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; | ||||
| }; | ||||
|  |  | |||
|  | @ -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)); | ||||
|         } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling