Refactor removing duplicated code with new WindowWithHand

FilterWindow and DialogWindow (for scripts) share this code to enable
the Hand tool in the active Editor.
This commit is contained in:
David Capello 2024-11-13 14:36:23 -03:00
parent 699342fe9d
commit 4d7b28bfb6
6 changed files with 127 additions and 91 deletions

View File

@ -673,6 +673,7 @@ target_sources(app-lib PRIVATE
ui/timeline/timeline.cpp
ui/toolbar.cpp
ui/user_data_view.cpp
ui/window_with_hand.cpp
ui/workspace.cpp
ui/workspace_panel.cpp
ui/workspace_tabs.cpp

View File

@ -17,10 +17,6 @@
#include "app/ini_file.h"
#include "app/modules/gui.h"
#include "app/pref/preferences.h"
#include "app/tools/tool_box.h"
#include "app/ui/editor/editor.h"
#include "app/ui/context_bar.h"
#include "app/ui/toolbar.h"
namespace app {
@ -32,7 +28,7 @@ FilterWindow::FilterWindow(const char* title, const char* cfgSection,
WithChannels withChannels,
WithTiled withTiled,
TiledMode tiledMode)
: Window(WithTitleBar, title)
: WindowWithHand(WithTitleBar, title)
, m_cfgSection(cfgSection)
, m_filterMgr(filterMgr)
, m_hbox(HORIZONTAL)
@ -46,8 +42,6 @@ FilterWindow::FilterWindow(const char* title, const char* cfgSection,
, m_tiledCheck(withTiled == WithTiledCheckBox ?
new CheckBox(Strings::filters_tiled()) :
nullptr)
, m_editor(nullptr)
, m_oldTool(nullptr)
{
m_okButton.processMnemonicFromText();
m_cancelButton.processMnemonicFromText();
@ -91,20 +85,12 @@ FilterWindow::FilterWindow(const char* title, const char* cfgSection,
// OK is magnetic (the default button)
m_okButton.setFocusMagnet(true);
if (Editor::activeEditor()) {
m_editor = Editor::activeEditor();
m_editor->add_observer(this);
m_oldTool = m_editor->getCurrentEditorTool();
tools::Tool* hand = App::instance()->toolBox()->getToolById(tools::WellKnownTools::Hand);
ToolBar::instance()->selectTool(hand);
}
// Enable the Hand tool in the active editor.
enableHandTool(true);
}
FilterWindow::~FilterWindow()
{
if (m_oldTool)
ToolBar::instance()->selectTool(m_oldTool);
// Save window configuration
save_window_pos(this, m_cfgSection);
@ -113,9 +99,6 @@ FilterWindow::~FilterWindow()
// Save cels target button
Preferences::instance().filters.celsTarget(m_targetButton.celsTarget());
if (m_editor)
m_editor->remove_observer(this);
}
bool FilterWindow::doModal()
@ -150,18 +133,6 @@ bool FilterWindow::doModal()
return result;
}
void FilterWindow::onBroadcastMouseMessage(const gfx::Point& screenPos,
ui::WidgetsList& targets) {
// Add the Filter Window as receptor of mouse events.
targets.push_back(this);
// Also add the editor
if (m_editor)
targets.push_back(ui::View::getView(m_editor));
// and add the context bar.
if (App::instance()->contextBar())
targets.push_back(App::instance()->contextBar());
}
void FilterWindow::restartPreview()
{
bool state = m_showPreview.isSelected();

View File

@ -11,21 +11,21 @@
#include "app/commands/filters/filter_preview.h"
#include "app/commands/filters/filter_target_buttons.h"
#include "app/ui/editor/editor.h"
#include "app/ui/window_with_hand.h"
#include "filters/tiled_mode.h"
#include "ui/box.h"
#include "ui/button.h"
#include "ui/window.h"
namespace app {
class Editor;
class FilterManagerImpl;
using namespace filters;
// A generic window to show parameters for a Filter with integrated
// preview in the current editor.
class FilterWindow : public ui::Window,
public EditorObserver {
class FilterWindow : public WindowWithHand {
public:
enum WithChannels { WithChannelsSelector, WithoutChannelsSelector };
enum WithTiled { WithTiledCheckBox, WithoutTiledCheckBox };
@ -51,9 +51,6 @@ namespace app {
// which specified different targets for each matrix.
void setNewTarget(Target target);
void onBroadcastMouseMessage(const gfx::Point& screenPos,
ui::WidgetsList& targets) override;
// Returns the container where derived classes should put controls.
ui::Widget* getContainer() { return &m_container; }
@ -87,8 +84,6 @@ namespace app {
FilterTargetButtons m_targetButton;
ui::CheckBox m_showPreview;
ui::CheckBox* m_tiledCheck;
Editor* m_editor;
tools::Tool* m_oldTool;
};
} // namespace app

View File

@ -22,13 +22,11 @@
#include "app/ui/button_set.h"
#include "app/ui/color_button.h"
#include "app/ui/color_shades.h"
#include "app/ui/editor/editor.h"
#include "app/ui/expr_entry.h"
#include "app/ui/filename_field.h"
#include "app/ui/main_window.h"
#include "app/ui/editor/editor.h"
#include "app/ui/toolbar.h"
#include "app/ui/context_bar.h"
#include "app/tools/tool_box.h"
#include "app/ui/window_with_hand.h"
#include "base/paths.h"
#include "base/remove_from_container.h"
#include "ui/box.h"
@ -47,7 +45,6 @@
#include "ui/slider.h"
#include "ui/system.h"
#include "ui/view.h"
#include "ui/window.h"
#include <map>
#include <stack>
@ -63,24 +60,14 @@ using namespace ui;
namespace {
class DialogWindow : public ui::Window,
public EditorObserver {
class DialogWindow : public WindowWithHand {
public:
DialogWindow(Type type, const std::string& text)
: Window(type, text)
, m_editor(nullptr)
, m_oldTool(nullptr)
: WindowWithHand(type, text)
, m_handTool(false)
{
}
~DialogWindow() {
if (m_editor)
m_editor->remove_observer(this);
if (m_oldTool)
ToolBar::instance()->selectTool(m_oldTool);
}
// Enables the Hand tool in the active editor.
void setHandTool(const bool flag) {
m_handTool = flag;
@ -89,47 +76,16 @@ public:
protected:
void onOpen(Event& ev) override {
if (m_handTool && Editor::activeEditor()) {
m_editor = Editor::activeEditor();
m_editor->add_observer(this);
m_oldTool = m_editor->getCurrentEditorTool();
tools::Tool* hand = App::instance()->toolBox()->getToolById(tools::WellKnownTools::Hand);
ToolBar::instance()->selectTool(hand);
enableHandTool(true);
}
}
void onBeforeClose(CloseEvent& ev) override {
// unset references in case the same dialog is opened again
if (m_editor) {
m_editor->remove_observer(this);
m_editor = nullptr;
}
if (m_oldTool) {
ToolBar::instance()->selectTool(m_oldTool);
m_oldTool = nullptr;
}
}
void onBroadcastMouseMessage(const gfx::Point& screenPos, ui::WidgetsList& targets) override {
if (m_handTool) {
// Same impl as in FilterWindow::onBroadcastMouseMessage():
// Add this Window as receptor of mouse events.
targets.push_back(this);
// Add also the editor as receptor of mouse events.
if (m_editor)
targets.push_back(ui::View::getView(m_editor));
// and add the context bar.
if (App::instance()->contextBar())
targets.push_back(App::instance()->contextBar());
}
else {
Window::onBroadcastMouseMessage(screenPos, targets);
}
if (isHandToolEnabled())
enableHandTool(false);
}
private:
Editor* m_editor;
tools::Tool* m_oldTool;
bool m_handTool;
};

View File

@ -0,0 +1,70 @@
// Aseprite
// Copyright (C) 2024 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/ui/window_with_hand.h"
#include "app/app.h"
#include "app/tools/tool_box.h"
#include "app/ui/context_bar.h"
#include "app/ui/editor/editor.h"
#include "app/ui/toolbar.h"
namespace app {
WindowWithHand::WindowWithHand(Type type, const std::string& text)
: Window(type, text)
{
}
WindowWithHand::~WindowWithHand()
{
enableHandTool(false);
}
void WindowWithHand::enableHandTool(const bool state)
{
if (m_editor) {
m_editor->remove_observer(this);
m_editor = nullptr;
}
if (m_oldTool) {
ToolBar::instance()->selectTool(m_oldTool);
m_oldTool = nullptr;
}
auto* editor = Editor::activeEditor();
if (state && editor) {
m_editor = editor;
m_editor->add_observer(this);
m_oldTool = m_editor->getCurrentEditorTool();
tools::Tool* hand = App::instance()->toolBox()->getToolById(tools::WellKnownTools::Hand);
ToolBar::instance()->selectTool(hand);
}
}
void WindowWithHand::onBroadcastMouseMessage(const gfx::Point& screenPos,
ui::WidgetsList& targets)
{
if (m_editor) {
// Add this window as receptor of mouse events.
targets.push_back(this);
// Also add the editor
if (m_editor)
targets.push_back(ui::View::getView(m_editor));
// and add the context bar.
if (App::instance()->contextBar())
targets.push_back(App::instance()->contextBar());
}
else {
Window::onBroadcastMouseMessage(screenPos, targets);
}
}
} // namespace app

View File

@ -0,0 +1,43 @@
// Aseprite
// Copyright (C) 2024 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifndef APP_UI_WINDOW_WITH_HAND_H_INCLUDED
#define APP_UI_WINDOW_WITH_HAND_H_INCLUDED
#pragma once
#include "app/ui/editor/editor_observer.h"
#include "ui/window.h"
namespace app {
namespace tools {
class Tool;
}
// A Window that enables the possibility to scroll/zoom the active
// editor with the Hand tool.
class WindowWithHand : public ui::Window,
public EditorObserver {
public:
WindowWithHand(Type type, const std::string& text);
~WindowWithHand();
// Enables the Hand tool in the active editor.
void enableHandTool(bool state);
bool isHandToolEnabled() const { return m_editor != nullptr; }
protected:
void onBroadcastMouseMessage(const gfx::Point& screenPos,
ui::WidgetsList& targets) override;
private:
Editor* m_editor = nullptr;
tools::Tool* m_oldTool = nullptr;
};
} // namespace app
#endif