diff options
author | networkException <git@nwex.de> | 2022-01-28 17:22:17 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-29 11:22:02 +0100 |
commit | fcd97a3e19b7c58b1fe3d6f4cc127c93a9bf5a6c (patch) | |
tree | 8c20ac22de910159c23a297d70d2f6b6a81fe2e7 /Userland | |
parent | b67d4ab52fb5511685c8fc8dbb7592567b8690b0 (diff) | |
download | serenity-fcd97a3e19b7c58b1fe3d6f4cc127c93a9bf5a6c.zip |
LibGUI: Show radio buttons for checkable actions in CommandPalette
This patch adds a painting delegate to the icon column of CommandPalette
to show a radio button in case an action is checkable and does not
explicitly have an icon :^)
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibGUI/CommandPalette.cpp | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/CommandPalette.cpp b/Userland/Libraries/LibGUI/CommandPalette.cpp index 53e9d55f62..8896afaaf7 100644 --- a/Userland/Libraries/LibGUI/CommandPalette.cpp +++ b/Userland/Libraries/LibGUI/CommandPalette.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2022, Jakob-Niklas See <git@nwex.de> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -11,6 +12,7 @@ #include <LibGUI/CommandPalette.h> #include <LibGUI/FilteringProxyModel.h> #include <LibGUI/Model.h> +#include <LibGUI/Painter.h> #include <LibGUI/TableView.h> #include <LibGUI/TextBox.h> #include <LibGUI/Widget.h> @@ -18,6 +20,25 @@ namespace GUI { +class IconOrRadioButtonDelegate final : public GUI::TableCellPaintingDelegate { +public: + virtual ~IconOrRadioButtonDelegate() override { } + + bool should_paint(ModelIndex const& index) override + { + return index.data().is_bool(); + } + + 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(); + + 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); + } +}; + class ActionModel final : public GUI::Model { public: enum Column { @@ -64,6 +85,8 @@ public: case Column::Icon: if (action.icon()) return *action.icon(); + if (action.is_checkable()) + return action.is_checked(); return ""; case Column::Text: return Gfx::parse_ampersand_string(action.text()); @@ -112,6 +135,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<IconOrRadioButtonDelegate>()); m_table_view->set_model(*m_filter_model); m_text_box->on_change = [this] { |