mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 06:17:34 +00:00
LibGUI+WindowServer: Make it possible to have checkable GActions.
They show up as checkable GButtons in GToolBar, and with (or without) check marks in menus. There are a bunch of places to make use of this. This patch only takes advantage of it in the FileManager for the view type actions.
This commit is contained in:
parent
9ff36afeaa
commit
8f81a3f9dd
15 changed files with 148 additions and 20 deletions
|
@ -246,6 +246,8 @@ struct WSAPI_ClientMessage {
|
|||
char shortcut_text[32];
|
||||
int shortcut_text_length;
|
||||
bool enabled;
|
||||
bool checkable;
|
||||
bool checked;
|
||||
WSAPI_Point position;
|
||||
bool top_anchored;
|
||||
} menu;
|
||||
|
|
|
@ -241,7 +241,7 @@ void WSClientConnection::handle_request(const WSAPIAddMenuItemRequest& request)
|
|||
return;
|
||||
}
|
||||
auto& menu = *(*it).value;
|
||||
menu.add_item(make<WSMenuItem>(menu, identifier, request.text(), request.shortcut_text(), request.is_enabled()));
|
||||
menu.add_item(make<WSMenuItem>(menu, identifier, request.text(), request.shortcut_text(), request.is_enabled(), request.is_checkable(), request.is_checked()));
|
||||
WSAPI_ServerMessage response;
|
||||
response.type = WSAPI_ServerMessage::Type::DidAddMenuItem;
|
||||
response.menu.menu_id = menu_id;
|
||||
|
@ -292,6 +292,9 @@ void WSClientConnection::handle_request(const WSAPIUpdateMenuItemRequest& reques
|
|||
menu_item->set_text(request.text());
|
||||
menu_item->set_shortcut_text(request.shortcut_text());
|
||||
menu_item->set_enabled(request.is_enabled());
|
||||
menu_item->set_checkable(request.is_checkable());
|
||||
if (request.is_checkable())
|
||||
menu_item->set_checked(request.is_checked());
|
||||
WSAPI_ServerMessage response;
|
||||
response.type = WSAPI_ServerMessage::Type::DidUpdateMenuItem;
|
||||
response.menu.menu_id = menu_id;
|
||||
|
|
|
@ -276,13 +276,15 @@ private:
|
|||
|
||||
class WSAPIAddMenuItemRequest : public WSAPIClientRequest {
|
||||
public:
|
||||
WSAPIAddMenuItemRequest(int client_id, int menu_id, unsigned identifier, const String& text, const String& shortcut_text, bool enabled)
|
||||
WSAPIAddMenuItemRequest(int client_id, int menu_id, unsigned identifier, const String& text, const String& shortcut_text, bool enabled, bool checkable, bool checked)
|
||||
: WSAPIClientRequest(WSEvent::APIAddMenuItemRequest, client_id)
|
||||
, m_menu_id(menu_id)
|
||||
, m_identifier(identifier)
|
||||
, m_text(text)
|
||||
, m_shortcut_text(shortcut_text)
|
||||
, m_enabled(enabled)
|
||||
, m_checkable(checkable)
|
||||
, m_checked(checked)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -291,24 +293,30 @@ public:
|
|||
String text() const { return m_text; }
|
||||
String shortcut_text() const { return m_shortcut_text; }
|
||||
bool is_enabled() const { return m_enabled; }
|
||||
bool is_checkable() const { return m_checkable; }
|
||||
bool is_checked() const { return m_checked; }
|
||||
|
||||
private:
|
||||
int m_menu_id { 0 };
|
||||
unsigned m_identifier { 0 };
|
||||
String m_text;
|
||||
String m_shortcut_text;
|
||||
bool m_enabled { true };
|
||||
bool m_enabled;
|
||||
bool m_checkable;
|
||||
bool m_checked;
|
||||
};
|
||||
|
||||
class WSAPIUpdateMenuItemRequest : public WSAPIClientRequest {
|
||||
public:
|
||||
WSAPIUpdateMenuItemRequest(int client_id, int menu_id, unsigned identifier, const String& text, const String& shortcut_text, bool enabled)
|
||||
WSAPIUpdateMenuItemRequest(int client_id, int menu_id, unsigned identifier, const String& text, const String& shortcut_text, bool enabled, bool checkable, bool checked)
|
||||
: WSAPIClientRequest(WSEvent::APIUpdateMenuItemRequest, client_id)
|
||||
, m_menu_id(menu_id)
|
||||
, m_identifier(identifier)
|
||||
, m_text(text)
|
||||
, m_shortcut_text(shortcut_text)
|
||||
, m_enabled(enabled)
|
||||
, m_checkable(checkable)
|
||||
, m_checked(checked)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -317,6 +325,8 @@ public:
|
|||
String text() const { return m_text; }
|
||||
String shortcut_text() const { return m_shortcut_text; }
|
||||
bool is_enabled() const { return m_enabled; }
|
||||
bool is_checkable() const { return m_checkable; }
|
||||
bool is_checked() const { return m_checked; }
|
||||
|
||||
private:
|
||||
int m_menu_id { 0 };
|
||||
|
@ -324,6 +334,8 @@ private:
|
|||
String m_text;
|
||||
String m_shortcut_text;
|
||||
bool m_enabled { true };
|
||||
bool m_checkable;
|
||||
bool m_checked;
|
||||
};
|
||||
|
||||
class WSAPIAddMenuSeparatorRequest : public WSAPIClientRequest {
|
||||
|
|
|
@ -170,12 +170,12 @@ bool WSEventLoop::on_receive_from_client(int client_id, const WSAPI_ClientMessag
|
|||
case WSAPI_ClientMessage::Type::AddMenuItem:
|
||||
ASSERT(message.text_length < (ssize_t)sizeof(message.text));
|
||||
ASSERT(message.menu.shortcut_text_length < (ssize_t)sizeof(message.menu.shortcut_text));
|
||||
post_event(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), message.menu.enabled));
|
||||
post_event(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), message.menu.enabled, message.menu.checkable, message.menu.checked));
|
||||
break;
|
||||
case WSAPI_ClientMessage::Type::UpdateMenuItem:
|
||||
ASSERT(message.text_length < (ssize_t)sizeof(message.text));
|
||||
ASSERT(message.menu.shortcut_text_length < (ssize_t)sizeof(message.menu.shortcut_text));
|
||||
post_event(client, make<WSAPIUpdateMenuItemRequest>(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), message.menu.enabled));
|
||||
post_event(client, make<WSAPIUpdateMenuItemRequest>(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), message.menu.enabled, message.menu.checkable, message.menu.checked));
|
||||
break;
|
||||
case WSAPI_ClientMessage::Type::AddMenuSeparator:
|
||||
post_event(client, make<WSAPIAddMenuSeparatorRequest>(client_id, message.menu.menu_id));
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "WSWindowManager.h"
|
||||
#include <WindowServer/WSAPITypes.h>
|
||||
#include <WindowServer/WSClientConnection.h>
|
||||
#include <SharedGraphics/CharacterBitmap.h>
|
||||
#include <SharedGraphics/Painter.h>
|
||||
#include <SharedGraphics/StylePainter.h>
|
||||
#include <SharedGraphics/Font.h>
|
||||
|
@ -26,6 +27,23 @@ const Font& WSMenu::font() const
|
|||
return Font::default_font();
|
||||
}
|
||||
|
||||
static const char* s_checked_bitmap_data = {
|
||||
" "
|
||||
" ## "
|
||||
" ## "
|
||||
" ## "
|
||||
" ## "
|
||||
" ## ## "
|
||||
" #### "
|
||||
" ## "
|
||||
" "
|
||||
};
|
||||
|
||||
static CharacterBitmap* s_checked_bitmap;
|
||||
static const int s_checked_bitmap_width = 9;
|
||||
static const int s_checked_bitmap_height = 9;
|
||||
static const int s_checked_bitmap_padding = 6;
|
||||
|
||||
int WSMenu::width() const
|
||||
{
|
||||
int longest = 0;
|
||||
|
@ -34,6 +52,8 @@ int WSMenu::width() const
|
|||
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());
|
||||
if (item->is_checkable())
|
||||
item_width += s_checked_bitmap_width + s_checked_bitmap_padding;
|
||||
|
||||
longest = max(longest, item_width);
|
||||
}
|
||||
|
@ -92,6 +112,9 @@ void WSMenu::draw()
|
|||
StylePainter::paint_menu_frame(painter, rect);
|
||||
int width = this->width();
|
||||
|
||||
if (!s_checked_bitmap)
|
||||
s_checked_bitmap = &CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
|
||||
|
||||
for (auto& item : m_items) {
|
||||
if (item->type() == WSMenuItem::Text) {
|
||||
Color text_color = Color::Black;
|
||||
|
@ -101,7 +124,16 @@ void WSMenu::draw()
|
|||
}
|
||||
if (!item->is_enabled())
|
||||
text_color = Color::MidGray;
|
||||
painter.draw_text(item->rect().translated(left_padding(), 0), item->text(), TextAlignment::CenterLeft, text_color);
|
||||
Rect text_rect = item->rect().translated(left_padding(), 0);
|
||||
if (item->is_checkable()) {
|
||||
if (item->is_checked()) {
|
||||
Rect checkmark_rect { text_rect.location().x(), 0, s_checked_bitmap_width, s_checked_bitmap_height };
|
||||
checkmark_rect.center_vertically_within(text_rect);
|
||||
painter.draw_bitmap(checkmark_rect.location(), *s_checked_bitmap, Color::Black);
|
||||
}
|
||||
text_rect.move_by(s_checked_bitmap_width + s_checked_bitmap_padding, 0);
|
||||
}
|
||||
painter.draw_text(text_rect, 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);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
#include "WSMenuItem.h"
|
||||
#include "WSMenu.h"
|
||||
|
||||
WSMenuItem::WSMenuItem(WSMenu& menu, unsigned identifier, const String& text, const String& shortcut_text, bool enabled)
|
||||
WSMenuItem::WSMenuItem(WSMenu& menu, unsigned identifier, const String& text, const String& shortcut_text, bool enabled, bool checkable, bool checked)
|
||||
: m_menu(menu)
|
||||
, m_type(Text)
|
||||
, m_enabled(enabled)
|
||||
, m_checkable(checkable)
|
||||
, m_checked(checked)
|
||||
, m_identifier(identifier)
|
||||
, m_text(text)
|
||||
, m_shortcut_text(shortcut_text)
|
||||
|
@ -28,3 +30,11 @@ void WSMenuItem::set_enabled(bool enabled)
|
|||
m_enabled = enabled;
|
||||
m_menu.redraw();
|
||||
}
|
||||
|
||||
void WSMenuItem::set_checked(bool checked)
|
||||
{
|
||||
if (m_checked == checked)
|
||||
return;
|
||||
m_checked = checked;
|
||||
m_menu.redraw();
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ public:
|
|||
Separator,
|
||||
};
|
||||
|
||||
WSMenuItem(WSMenu&, unsigned identifier, const String& text, const String& shortcut_text = { }, bool enabled = true);
|
||||
WSMenuItem(WSMenu&, unsigned identifier, const String& text, const String& shortcut_text = { }, bool enabled = true, bool checkable = false, bool checked = false);
|
||||
WSMenuItem(WSMenu&, Type);
|
||||
~WSMenuItem();
|
||||
|
||||
|
@ -23,6 +23,12 @@ public:
|
|||
bool is_enabled() const { return m_enabled; }
|
||||
void set_enabled(bool);
|
||||
|
||||
bool is_checkable() const { return m_checkable; }
|
||||
void set_checkable(bool checkable) { m_checkable = checkable; }
|
||||
|
||||
bool is_checked() const { return m_checked; }
|
||||
void set_checked(bool);
|
||||
|
||||
String text() const { return m_text; }
|
||||
void set_text(const String& text) { m_text = text; }
|
||||
|
||||
|
@ -38,6 +44,8 @@ private:
|
|||
WSMenu& m_menu;
|
||||
Type m_type { None };
|
||||
bool m_enabled { true };
|
||||
bool m_checkable { false };
|
||||
bool m_checked { false };
|
||||
unsigned m_identifier { 0 };
|
||||
String m_text;
|
||||
String m_shortcut_text;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue