1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:28:12 +00:00

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 :^)
This commit is contained in:
networkException 2022-01-28 17:22:17 +01:00 committed by Andreas Kling
parent b67d4ab52f
commit fcd97a3e19

View file

@ -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] {