1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:47:44 +00:00

PixelPaint: Scope tool actions to the containing window

We achieve this by deferring the construction of the tool buttons until
the toolbox widget has been added to a window.
This commit is contained in:
Andreas Kling 2020-07-23 19:53:48 +02:00
parent 4392413cd1
commit 7973f76790
2 changed files with 22 additions and 10 deletions

View file

@ -38,6 +38,7 @@
#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>
namespace PixelPaint { namespace PixelPaint {
@ -55,12 +56,14 @@ public:
builder.append(")"); builder.append(")");
set_tooltip(builder.to_string()); set_tooltip(builder.to_string());
m_action = GUI::Action::create_checkable(name, shortcut, [this](auto& action) { m_action = GUI::Action::create_checkable(
if (action.is_checked()) name, shortcut, [this](auto& action) {
m_toolbox.on_tool_selection(m_tool); if (action.is_checked())
else m_toolbox.on_tool_selection(m_tool);
m_toolbox.on_tool_selection(nullptr); else
}); m_toolbox.on_tool_selection(nullptr);
},
toolbox.window());
m_tool->set_action(m_action); m_tool->set_action(m_action);
set_action(*m_action); set_action(*m_action);
@ -101,6 +104,17 @@ 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);
deferred_invoke([this](auto&) {
setup_tools();
});
}
ToolboxWidget::~ToolboxWidget()
{
}
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 = [&](const StringView& name, const StringView& icon_name, const GUI::Shortcut& shortcut, NonnullOwnPtr<Tool> tool) -> ToolButton& {
m_tools.append(tool.ptr()); m_tools.append(tool.ptr());
auto& button = add<ToolButton>(*this, name, shortcut, move(tool)); auto& button = add<ToolButton>(*this, name, shortcut, move(tool));
@ -122,8 +136,4 @@ ToolboxWidget::ToolboxWidget()
add_tool("Ellipse", "circle", { Mod_Ctrl | Mod_Shift, Key_E }, make<EllipseTool>()); add_tool("Ellipse", "circle", { Mod_Ctrl | Mod_Shift, Key_E }, make<EllipseTool>());
} }
ToolboxWidget::~ToolboxWidget()
{
}
} }

View file

@ -50,6 +50,8 @@ public:
private: private:
friend class ToolButton; friend class ToolButton;
void setup_tools();
explicit ToolboxWidget(); explicit ToolboxWidget();
GUI::ActionGroup m_action_group; GUI::ActionGroup m_action_group;
Vector<Tool*> m_tools; Vector<Tool*> m_tools;