1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:17:45 +00:00

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. :^)
This commit is contained in:
Andreas Kling 2021-05-15 23:08:17 +02:00
parent 0ee7991dca
commit ad2752276a
2 changed files with 29 additions and 65 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -16,65 +16,18 @@
#include "RectangleTool.h" #include "RectangleTool.h"
#include "SprayTool.h" #include "SprayTool.h"
#include "ZoomTool.h" #include "ZoomTool.h"
#include <AK/StringBuilder.h>
#include <LibGUI/Action.h> #include <LibGUI/Action.h>
#include <LibGUI/BoxLayout.h> #include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h> #include <LibGUI/Button.h>
#include <LibGUI/Window.h> #include <LibGUI/Toolbar.h>
namespace PixelPaint { namespace PixelPaint {
class ToolButton final : public GUI::Button {
C_OBJECT(ToolButton)
public:
ToolButton(ToolboxWidget& toolbox, const String& name, const GUI::Shortcut& shortcut, OwnPtr<Tool> 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<Tool> m_tool;
RefPtr<GUI::Action> m_action;
};
ToolboxWidget::ToolboxWidget() ToolboxWidget::ToolboxWidget()
{ {
set_fill_with_background_color(true); set_fill_with_background_color(true);
set_frame_thickness(0); set_fixed_width(26);
set_fixed_width(28);
set_layout<GUI::VerticalBoxLayout>(); set_layout<GUI::VerticalBoxLayout>();
layout()->set_spacing(0); layout()->set_spacing(0);
@ -83,6 +36,7 @@ ToolboxWidget::ToolboxWidget()
m_action_group.set_exclusive(true); m_action_group.set_exclusive(true);
m_action_group.set_unchecking_allowed(false); m_action_group.set_unchecking_allowed(false);
m_toolbar = add<GUI::Toolbar>(Gfx::Orientation::Vertical);
setup_tools(); setup_tools();
} }
@ -92,15 +46,22 @@ ToolboxWidget::~ToolboxWidget()
void ToolboxWidget::setup_tools() void ToolboxWidget::setup_tools()
{ {
auto add_tool = [&](const StringView& name, const StringView& icon_name, const GUI::Shortcut& shortcut, NonnullOwnPtr<Tool> tool) -> ToolButton& { auto add_tool = [&](String name, StringView const& icon_name, GUI::Shortcut const& shortcut, NonnullOwnPtr<Tool> tool) {
m_tools.append(tool.ptr()); auto action = GUI::Action::create_checkable(move(name), shortcut, Gfx::Bitmap::load_from_file(String::formatted("/res/icons/pixelpaint/{}.png", icon_name)),
auto& button = add<ToolButton>(*this, name, shortcut, move(tool)); [this, tool = tool.ptr()](auto& action) {
button.set_focus_policy(GUI::FocusPolicy::TabFocus); if (action.is_checked())
button.set_fixed_size(24, 24); on_tool_selection(tool);
button.set_checkable(true); else
button.set_button_style(Gfx::ButtonStyle::Coolbar); on_tool_selection(nullptr);
button.set_icon(Gfx::Bitmap::load_from_file(String::formatted("/res/icons/pixelpaint/{}.png", icon_name))); });
return button; 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<MoveTool>()); add_tool("Move", "move", { 0, Key_M }, make<MoveTool>());

View file

@ -1,20 +1,22 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#pragma once #pragma once
#include <AK/NonnullOwnPtrVector.h>
#include <LibGUI/ActionGroup.h> #include <LibGUI/ActionGroup.h>
#include <LibGUI/Frame.h> #include <LibGUI/Widget.h>
namespace PixelPaint { namespace PixelPaint {
class Tool; class Tool;
class ToolboxWidget final : public GUI::Frame { class ToolboxWidget final : public GUI::Widget {
C_OBJECT(ToolboxWidget) C_OBJECT(ToolboxWidget);
public: public:
virtual ~ToolboxWidget() override; virtual ~ToolboxWidget() override;
@ -24,7 +26,7 @@ public:
void for_each_tool(Callback callback) void for_each_tool(Callback callback)
{ {
for (auto& tool : m_tools) for (auto& tool : m_tools)
callback(*tool); callback(tool);
} }
private: private:
@ -33,8 +35,9 @@ private:
void setup_tools(); void setup_tools();
explicit ToolboxWidget(); explicit ToolboxWidget();
RefPtr<GUI::Toolbar> m_toolbar;
GUI::ActionGroup m_action_group; GUI::ActionGroup m_action_group;
Vector<Tool*> m_tools; NonnullOwnPtrVector<Tool> m_tools;
}; };
} }