diff options
Diffstat (limited to 'WindowServer')
-rw-r--r-- | WindowServer/WSAPITypes.h | 2 | ||||
-rw-r--r-- | WindowServer/WSClientConnection.cpp | 3 | ||||
-rw-r--r-- | WindowServer/WSMenu.cpp | 12 | ||||
-rw-r--r-- | WindowServer/WSMenu.h | 1 | ||||
-rw-r--r-- | WindowServer/WSMenuItem.cpp | 3 | ||||
-rw-r--r-- | WindowServer/WSMenuItem.h | 4 | ||||
-rw-r--r-- | WindowServer/WSMessage.h | 5 | ||||
-rw-r--r-- | WindowServer/WSMessageLoop.cpp | 3 |
8 files changed, 25 insertions, 8 deletions
diff --git a/WindowServer/WSAPITypes.h b/WindowServer/WSAPITypes.h index e7fd570809..1818e91155 100644 --- a/WindowServer/WSAPITypes.h +++ b/WindowServer/WSAPITypes.h @@ -166,6 +166,8 @@ struct WSAPI_ClientMessage { int menubar_id; int menu_id; unsigned identifier; + char shortcut_text[32]; + int shortcut_text_length; } menu; struct { WSAPI_Rect rect; diff --git a/WindowServer/WSClientConnection.cpp b/WindowServer/WSClientConnection.cpp index 0cf421b0f4..71c4c8a076 100644 --- a/WindowServer/WSClientConnection.cpp +++ b/WindowServer/WSClientConnection.cpp @@ -210,14 +210,13 @@ void WSClientConnection::handle_request(WSAPIAddMenuItemRequest& request) { int menu_id = request.menu_id(); unsigned identifier = request.identifier(); - String text = request.text(); auto it = m_menus.find(menu_id); if (it == m_menus.end()) { post_error("Bad menu ID"); return; } auto& menu = *(*it).value; - menu.add_item(make<WSMenuItem>(identifier, move(text))); + menu.add_item(make<WSMenuItem>(identifier, request.text(), request.shortcut_text())); WSAPI_ServerMessage response; response.type = WSAPI_ServerMessage::Type::DidAddMenuItem; response.menu.menu_id = menu_id; diff --git a/WindowServer/WSMenu.cpp b/WindowServer/WSMenu.cpp index 79e3d2de9c..383cde2472 100644 --- a/WindowServer/WSMenu.cpp +++ b/WindowServer/WSMenu.cpp @@ -29,8 +29,13 @@ int WSMenu::width() const { int longest = 0; for (auto& item : m_items) { - if (item->type() == WSMenuItem::Text) - longest = max(longest, font().width(item->text())); + if (item->type() == WSMenuItem::Text) { + int item_width = font().width(item->text()); + if (!item->shortcut_text().is_empty()) + item_width += padding_between_text_and_shortcut() + font().width(item->shortcut_text()); + + longest = max(longest, item_width); + } } return max(longest, rect_in_menubar().width()) + horizontal_padding(); @@ -91,6 +96,9 @@ void WSMenu::draw() text_color = Color::White; } painter.draw_text(item->rect().translated(left_padding(), 0), item->text(), TextAlignment::CenterLeft, text_color); + if (!item->shortcut_text().is_empty()) { + painter.draw_text(item->rect().translated(-right_padding(), 0), item->shortcut_text(), TextAlignment::CenterRight, text_color); + } } else if (item->type() == WSMenuItem::Separator) { Point p1(1, item->rect().center().y()); Point p2(width() - 2, item->rect().center().y()); diff --git a/WindowServer/WSMenu.h b/WindowServer/WSMenu.h index f99f747d8c..107d6fc78f 100644 --- a/WindowServer/WSMenu.h +++ b/WindowServer/WSMenu.h @@ -73,6 +73,7 @@ public: void close(); private: + int padding_between_text_and_shortcut() const { return 50; } void did_activate(WSMenuItem&); WSClientConnection* m_client { nullptr }; int m_menu_id { 0 }; diff --git a/WindowServer/WSMenuItem.cpp b/WindowServer/WSMenuItem.cpp index b0f2e3e03a..49041e0034 100644 --- a/WindowServer/WSMenuItem.cpp +++ b/WindowServer/WSMenuItem.cpp @@ -1,9 +1,10 @@ #include "WSMenuItem.h" -WSMenuItem::WSMenuItem(unsigned identifier, const String& text) +WSMenuItem::WSMenuItem(unsigned identifier, const String& text, const String& shortcut_text) : m_type(Text) , m_identifier(identifier) , m_text(text) + , m_shortcut_text(shortcut_text) { } diff --git a/WindowServer/WSMenuItem.h b/WindowServer/WSMenuItem.h index 0a21adb92d..e3dce736f8 100644 --- a/WindowServer/WSMenuItem.h +++ b/WindowServer/WSMenuItem.h @@ -12,7 +12,7 @@ public: Separator, }; - explicit WSMenuItem(unsigned identifier, const String& text); + explicit WSMenuItem(unsigned identifier, const String& text, const String& shortcut_text = { }); explicit WSMenuItem(Type); ~WSMenuItem(); @@ -20,6 +20,7 @@ public: bool enabled() const { return m_enabled; } String text() const { return m_text; } + String shortcut_text() const { return m_shortcut_text; } void set_rect(const Rect& rect) { m_rect = rect; } Rect rect() const { return m_rect; } @@ -31,6 +32,7 @@ private: bool m_enabled { true }; unsigned m_identifier { 0 }; String m_text; + String m_shortcut_text; Rect m_rect; }; diff --git a/WindowServer/WSMessage.h b/WindowServer/WSMessage.h index 8cd7a6a627..125bd2ef90 100644 --- a/WindowServer/WSMessage.h +++ b/WindowServer/WSMessage.h @@ -189,22 +189,25 @@ private: class WSAPIAddMenuItemRequest : public WSAPIClientRequest { public: - WSAPIAddMenuItemRequest(int client_id, int menu_id, unsigned identifier, const String& text) + WSAPIAddMenuItemRequest(int client_id, int menu_id, unsigned identifier, const String& text, const String& shortcut_text) : WSAPIClientRequest(WSMessage::APIAddMenuItemRequest, client_id) , m_menu_id(menu_id) , m_identifier(identifier) , m_text(text) + , m_shortcut_text(shortcut_text) { } int menu_id() const { return m_menu_id; } unsigned identifier() const { return m_identifier; } String text() const { return m_text; } + String shortcut_text() const { return m_shortcut_text; } private: int m_menu_id { 0 }; unsigned m_identifier { 0 }; String m_text; + String m_shortcut_text; }; class WSAPIAddMenuSeparatorRequest : public WSAPIClientRequest { diff --git a/WindowServer/WSMessageLoop.cpp b/WindowServer/WSMessageLoop.cpp index 8e68215381..11fc7b1df4 100644 --- a/WindowServer/WSMessageLoop.cpp +++ b/WindowServer/WSMessageLoop.cpp @@ -294,7 +294,8 @@ void WSMessageLoop::on_receive_from_client(int client_id, const WSAPI_ClientMess break; case WSAPI_ClientMessage::Type::AddMenuItem: ASSERT(message.text_length < (ssize_t)sizeof(message.text)); - post_message(client, make<WSAPIAddMenuItemRequest>(client_id, message.menu.menu_id, message.menu.identifier, String(message.text, message.text_length))); + ASSERT(message.menu.shortcut_text_length < (ssize_t)sizeof(message.menu.shortcut_text)); + post_message(client, make<WSAPIAddMenuItemRequest>(client_id, message.menu.menu_id, message.menu.identifier, String(message.text, message.text_length), String(message.menu.shortcut_text, message.menu.shortcut_text_length))); break; case WSAPI_ClientMessage::Type::CreateWindow: ASSERT(message.text_length < (ssize_t)sizeof(message.text)); |