summaryrefslogtreecommitdiff
path: root/Servers/WindowServer/WSMenu.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-08-26 18:54:44 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-08-26 18:54:44 +0200
commit1e604b7984290694cefefc3ccf70be86c44395ba (patch)
treef4af2fc7e98906395dd6c633bd3aef1d019dadce /Servers/WindowServer/WSMenu.cpp
parentd522a6fe4c6dae3d36ca7f77c957be7073b27d9c (diff)
downloadserenity-1e604b7984290694cefefc3ccf70be86c44395ba.zip
WindowServer+LibGUI: Show action icons in the menus when possible
Any GAction that has an icon assigned will now show up with that icon when added to a menu as well. I made the menu items 2px taller to accomodate the icons. I think this turned out quite nice as well :^)
Diffstat (limited to 'Servers/WindowServer/WSMenu.cpp')
-rw-r--r--Servers/WindowServer/WSMenu.cpp23
1 files changed, 16 insertions, 7 deletions
diff --git a/Servers/WindowServer/WSMenu.cpp b/Servers/WindowServer/WSMenu.cpp
index cc9bb91e19..2a8c983578 100644
--- a/Servers/WindowServer/WSMenu.cpp
+++ b/Servers/WindowServer/WSMenu.cpp
@@ -7,6 +7,7 @@
#include "WSWindowManager.h"
#include <LibDraw/CharacterBitmap.h>
#include <LibDraw/Font.h>
+#include <LibDraw/GraphicsBitmap.h>
#include <LibDraw/Painter.h>
#include <LibDraw/StylePainter.h>
#include <WindowServer/WSAPITypes.h>
@@ -44,7 +45,8 @@ 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;
+static const int s_item_icon_width = 16;
+static const int s_checkbox_or_icon_padding = 6;
int WSMenu::width() const
{
@@ -58,8 +60,8 @@ int WSMenu::width() const
int shortcut_width = font().width(item.shortcut_text());
widest_shortcut = max(shortcut_width, widest_shortcut);
}
- if (item.is_checkable())
- text_width += s_checked_bitmap_width + s_checked_bitmap_padding;
+ if (item.is_checkable() || item.icon())
+ text_width += s_item_icon_width + s_checkbox_or_icon_padding;
widest_text = max(widest_text, text_width);
}
@@ -125,8 +127,11 @@ void WSMenu::draw()
s_checked_bitmap = &CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
bool has_checkable_items = false;
- for (auto& item : m_items)
+ bool has_items_with_icon = false;
+ for (auto& item : m_items) {
has_checkable_items = has_checkable_items | item.is_checkable();
+ has_items_with_icon = has_items_with_icon | !!item.icon();
+ }
for (auto& item : m_items) {
if (item.type() == WSMenuItem::Text) {
@@ -139,7 +144,7 @@ void WSMenu::draw()
text_color = Color::MidGray;
Rect text_rect = item.rect().translated(left_padding(), 0);
if (item.is_checkable()) {
- Rect checkmark_rect { text_rect.location().x(), 0, s_checked_bitmap_width, s_checked_bitmap_height };
+ Rect checkmark_rect { text_rect.location().x() + 2, 0, s_checked_bitmap_width, s_checked_bitmap_height };
checkmark_rect.center_vertically_within(text_rect);
Rect checkbox_rect = checkmark_rect.inflated(4, 4);
painter.fill_rect(checkbox_rect, Color::White);
@@ -147,9 +152,13 @@ void WSMenu::draw()
if (item.is_checked()) {
painter.draw_bitmap(checkmark_rect.location(), *s_checked_bitmap, Color::Black);
}
+ } else if (item.icon()) {
+ Rect icon_rect { text_rect.location().x() - 2, 0, s_item_icon_width, s_item_icon_width };
+ icon_rect.center_vertically_within(text_rect);
+ painter.blit(icon_rect.location(), *item.icon(), item.icon()->rect());
}
- if (has_checkable_items)
- text_rect.move_by(s_checked_bitmap_width + s_checked_bitmap_padding, 0);
+ if (has_checkable_items || has_items_with_icon)
+ text_rect.move_by(s_item_icon_width + s_checkbox_or_icon_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);