浏览代码

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 :^)
networkException 3 年之前
父节点
当前提交
fcd97a3e19
共有 1 个文件被更改,包括 24 次插入0 次删除
  1. 24 0
      Userland/Libraries/LibGUI/CommandPalette.cpp

+ 24 - 0
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] {