From ad2752276a3aaaec5292ce38fe03ae23136c26bb Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 15 May 2021 23:08:17 +0200 Subject: PixelPaint: Use GUI::Toolbar inside the toolbox widget We don't need to implement our own toolbar and tool button classes when the ones from LibGUI work just fine. :^) --- Userland/Applications/PixelPaint/ToolboxWidget.cpp | 79 ++++++---------------- Userland/Applications/PixelPaint/ToolboxWidget.h | 15 ++-- 2 files changed, 29 insertions(+), 65 deletions(-) diff --git a/Userland/Applications/PixelPaint/ToolboxWidget.cpp b/Userland/Applications/PixelPaint/ToolboxWidget.cpp index b45280308b..69f403eb03 100644 --- a/Userland/Applications/PixelPaint/ToolboxWidget.cpp +++ b/Userland/Applications/PixelPaint/ToolboxWidget.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -16,65 +16,18 @@ #include "RectangleTool.h" #include "SprayTool.h" #include "ZoomTool.h" -#include #include #include #include -#include +#include namespace PixelPaint { -class ToolButton final : public GUI::Button { - C_OBJECT(ToolButton) -public: - ToolButton(ToolboxWidget& toolbox, const String& name, const GUI::Shortcut& shortcut, OwnPtr tool) - : m_toolbox(toolbox) - , m_tool(move(tool)) - { - StringBuilder builder; - builder.append(name); - builder.append(" ("); - builder.append(shortcut.to_string()); - builder.append(")"); - set_tooltip(builder.to_string()); - - m_action = GUI::Action::create_checkable( - name, shortcut, [this](auto& action) { - if (action.is_checked()) - m_toolbox.on_tool_selection(m_tool); - else - m_toolbox.on_tool_selection(nullptr); - }, - toolbox.window()); - - m_tool->set_action(m_action); - set_action(*m_action); - m_toolbox.m_action_group.add_action(*m_action); - } - - const Tool& tool() const { return *m_tool; } - Tool& tool() { return *m_tool; } - - virtual bool is_uncheckable() const override { return false; } - - virtual void context_menu_event(GUI::ContextMenuEvent& event) override - { - m_action->activate(); - m_tool->on_tool_button_contextmenu(event); - } - -private: - ToolboxWidget& m_toolbox; - OwnPtr m_tool; - RefPtr m_action; -}; - ToolboxWidget::ToolboxWidget() { set_fill_with_background_color(true); - set_frame_thickness(0); - set_fixed_width(28); + set_fixed_width(26); set_layout(); layout()->set_spacing(0); @@ -83,6 +36,7 @@ ToolboxWidget::ToolboxWidget() m_action_group.set_exclusive(true); m_action_group.set_unchecking_allowed(false); + m_toolbar = add(Gfx::Orientation::Vertical); setup_tools(); } @@ -92,15 +46,22 @@ ToolboxWidget::~ToolboxWidget() void ToolboxWidget::setup_tools() { - auto add_tool = [&](const StringView& name, const StringView& icon_name, const GUI::Shortcut& shortcut, NonnullOwnPtr tool) -> ToolButton& { - m_tools.append(tool.ptr()); - auto& button = add(*this, name, shortcut, move(tool)); - button.set_focus_policy(GUI::FocusPolicy::TabFocus); - button.set_fixed_size(24, 24); - button.set_checkable(true); - button.set_button_style(Gfx::ButtonStyle::Coolbar); - button.set_icon(Gfx::Bitmap::load_from_file(String::formatted("/res/icons/pixelpaint/{}.png", icon_name))); - return button; + auto add_tool = [&](String name, StringView const& icon_name, GUI::Shortcut const& shortcut, NonnullOwnPtr tool) { + auto action = GUI::Action::create_checkable(move(name), shortcut, Gfx::Bitmap::load_from_file(String::formatted("/res/icons/pixelpaint/{}.png", icon_name)), + [this, tool = tool.ptr()](auto& action) { + if (action.is_checked()) + on_tool_selection(tool); + else + on_tool_selection(nullptr); + }); + m_action_group.add_action(action); + auto& button = m_toolbar->add_action(action); + button.on_context_menu_request = [action = action.ptr(), tool = tool.ptr()](auto& event) { + action->activate(); + tool->on_tool_button_contextmenu(event); + }; + tool->set_action(action); + m_tools.append(move(tool)); }; add_tool("Move", "move", { 0, Key_M }, make()); diff --git a/Userland/Applications/PixelPaint/ToolboxWidget.h b/Userland/Applications/PixelPaint/ToolboxWidget.h index 5e9f8faeaf..c4de0ffa77 100644 --- a/Userland/Applications/PixelPaint/ToolboxWidget.h +++ b/Userland/Applications/PixelPaint/ToolboxWidget.h @@ -1,20 +1,22 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once +#include #include -#include +#include namespace PixelPaint { class Tool; -class ToolboxWidget final : public GUI::Frame { - C_OBJECT(ToolboxWidget) +class ToolboxWidget final : public GUI::Widget { + C_OBJECT(ToolboxWidget); + public: virtual ~ToolboxWidget() override; @@ -24,7 +26,7 @@ public: void for_each_tool(Callback callback) { for (auto& tool : m_tools) - callback(*tool); + callback(tool); } private: @@ -33,8 +35,9 @@ private: void setup_tools(); explicit ToolboxWidget(); + RefPtr m_toolbar; GUI::ActionGroup m_action_group; - Vector m_tools; + NonnullOwnPtrVector m_tools; }; } -- cgit v1.2.3