From 048ed64c260b404c07c3d795928633ed7b8ff692 Mon Sep 17 00:00:00 2001 From: networkException Date: Sat, 29 Jan 2022 18:38:53 +0100 Subject: [PATCH] LibGUI: Allow CommandPalette to distinguish radio buttons and checkboxes --- Userland/Libraries/LibGUI/CommandPalette.cpp | 48 ++++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibGUI/CommandPalette.cpp b/Userland/Libraries/LibGUI/CommandPalette.cpp index 8896afaaf7..4d2725cc2d 100644 --- a/Userland/Libraries/LibGUI/CommandPalette.cpp +++ b/Userland/Libraries/LibGUI/CommandPalette.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -20,22 +21,40 @@ namespace GUI { -class IconOrRadioButtonDelegate final : public GUI::TableCellPaintingDelegate { +enum class IconFlags : unsigned { + Checkable = 0, + Exclusive = 1, + Checked = 2, +}; + +AK_ENUM_BITWISE_OPERATORS(IconFlags); + +class ActionIconDelegate final : public GUI::TableCellPaintingDelegate { public: - virtual ~IconOrRadioButtonDelegate() override { } + virtual ~ActionIconDelegate() override { } bool should_paint(ModelIndex const& index) override { - return index.data().is_bool(); + return index.data().is_u32(); } virtual void paint(GUI::Painter& painter, Gfx::IntRect const& cell_rect, Gfx::Palette const& palette, ModelIndex const& index) override { - auto checked = index.data().as_bool(); + auto flags = static_cast(index.data().as_u32()); + VERIFY(has_flag(flags, IconFlags::Checkable)); - Gfx::IntRect radio_rect { 0, 0, 12, 12 }; - radio_rect.center_within(cell_rect); - Gfx::StylePainter::paint_radio_button(painter, radio_rect, palette, checked, false); + auto exclusive = has_flag(flags, IconFlags::Exclusive); + auto checked = has_flag(flags, IconFlags::Checked); + + if (exclusive) { + Gfx::IntRect radio_rect { 0, 0, 12, 12 }; + radio_rect.center_within(cell_rect); + Gfx::StylePainter::paint_radio_button(painter, radio_rect, palette, checked, false); + } else { + Gfx::IntRect radio_rect { 0, 0, 13, 13 }; + radio_rect.center_within(cell_rect); + Gfx::StylePainter::paint_check_box(painter, radio_rect, palette, true, checked, false); + } } }; @@ -85,8 +104,17 @@ public: case Column::Icon: if (action.icon()) return *action.icon(); - if (action.is_checkable()) - return action.is_checked(); + if (action.is_checkable()) { + auto flags = IconFlags::Checkable; + + if (action.is_checked()) + flags |= IconFlags::Checked; + + if (action.group() && action.group()->is_exclusive()) + flags |= IconFlags::Exclusive; + + return (u32)flags; + } return ""; case Column::Text: return Gfx::parse_ampersand_string(action.text()); @@ -135,7 +163,7 @@ CommandPalette::CommandPalette(GUI::Window& parent_window, ScreenPosition screen m_filter_model = MUST(GUI::FilteringProxyModel::create(*m_model)); m_filter_model->set_filter_term(""); - m_table_view->set_column_painting_delegate(0, make()); + m_table_view->set_column_painting_delegate(0, make()); m_table_view->set_model(*m_filter_model); m_text_box->on_change = [this] {