From 89fcd44394e5e09d4e0694525ed90a566f7efdaf Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 20 May 2021 15:42:09 -0300 Subject: [PATCH] Select the initial ColorButton color when we go back to the button If start dragging from the ColorButton, we go outside the button, and then we go back to the same button, we should select the original color when the whole drag-and-drop process started. We have also improved the look of the button in that case: the original color is displayed (instead of the inverted color, which is when the button is selected, in this way is less confusing). --- src/app/ui/color_button.cpp | 19 +++++++++++++++++- src/app/ui/color_button.h | 4 ++++ src/ui/button.cpp | 40 +++++++++++++++++++++++++------------ src/ui/button.h | 3 +++ 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/app/ui/color_button.cpp b/src/app/ui/color_button.cpp index 4caba893f..c3299fdbe 100644 --- a/src/app/ui/color_button.cpp +++ b/src/app/ui/color_button.cpp @@ -160,9 +160,12 @@ bool ColorButton::onProcessMessage(Message* msg) Widget* picked = manager()->pickFromScreenPos(screenPos); if (picked == this) { - // Do nothing + setColor(m_startDragColor); break; } + else { + m_mouseLeft = true; + } IColorSource* colorSource = dynamic_cast(picked); if (colorSource) { @@ -302,6 +305,20 @@ void ColorButton::onClick(Event& ev) } } +void ColorButton::onStartDrag() +{ + m_startDragColor = m_color; + m_mouseLeft = false; +} + +void ColorButton::onSelectWhenDragging() +{ + if (m_mouseLeft) + setSelected(false); + else + ButtonBase::onSelectWhenDragging(); +} + void ColorButton::onLoadLayout(ui::LoadLayoutEvent& ev) { if (canPin()) { diff --git a/src/app/ui/color_button.h b/src/app/ui/color_button.h index 6fe9ba3e8..2400225df 100644 --- a/src/app/ui/color_button.h +++ b/src/app/ui/color_button.h @@ -58,6 +58,8 @@ namespace app { void onSizeHint(ui::SizeHintEvent& ev) override; void onPaint(ui::PaintEvent& ev) override; void onClick(ui::Event& ev) override; + void onStartDrag() override; + void onSelectWhenDragging() override; void onLoadLayout(ui::LoadLayoutEvent& ev) override; void onSaveLayout(ui::SaveLayoutEvent& ev) override; @@ -75,12 +77,14 @@ namespace app { gfx::Rect convertBounds(const gfx::Rect& bounds) const; app::Color m_color; + app::Color m_startDragColor; PixelFormat m_pixelFormat; ColorPopup* m_window; gfx::Rect m_windowDefaultBounds; gfx::Rect m_hiddenPopupBounds; bool m_desktopCoords; // True if m_windowDefault/hiddenPopupBounds are screen coordinates bool m_dependOnLayer; + bool m_mouseLeft; ColorButtonOptions m_options; }; diff --git a/src/ui/button.cpp b/src/ui/button.cpp index 1cc243806..40c5a8591 100644 --- a/src/ui/button.cpp +++ b/src/ui/button.cpp @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2019 Igara Studio S.A. +// Copyright (C) 2019-2021 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This file is released under the terms of the MIT license. @@ -165,6 +165,8 @@ bool ButtonBase::onProcessMessage(Message* msg) m_pressedStatus = isSelected(); captureMouse(); + + onStartDrag(); } return true; @@ -174,6 +176,8 @@ bool ButtonBase::onProcessMessage(Message* msg) m_pressedStatus = isSelected(); captureMouse(); + + onStartDrag(); } return true; @@ -186,6 +190,8 @@ bool ButtonBase::onProcessMessage(Message* msg) m_pressedStatus = isSelected(); captureMouse(); + + onStartDrag(); } } return true; @@ -231,19 +237,8 @@ bool ButtonBase::onProcessMessage(Message* msg) case kMouseMoveMessage: if (isEnabled() && hasCapture()) { - bool hasMouse = hasMouseOver(); - m_handleSelect = false; - - // Switch state when the mouse go out - if ((hasMouse && isSelected() != m_pressedStatus) || - (!hasMouse && isSelected() == m_pressedStatus)) { - if (hasMouse) - setSelected(m_pressedStatus); - else - setSelected(!m_pressedStatus); - } - + onSelectWhenDragging(); m_handleSelect = true; } break; @@ -269,6 +264,25 @@ void ButtonBase::generateButtonSelectSignal() onClick(ev); } +void ButtonBase::onStartDrag() +{ + // Do nothing +} + +void ButtonBase::onSelectWhenDragging() +{ + bool hasMouse = hasMouseOver(); + + // Switch state when the mouse go out + if ((hasMouse && isSelected() != m_pressedStatus) || + (!hasMouse && isSelected() == m_pressedStatus)) { + if (hasMouse) + setSelected(m_pressedStatus); + else + setSelected(!m_pressedStatus); + } +} + // ====================================================================== // Button class // ====================================================================== diff --git a/src/ui/button.h b/src/ui/button.h index 793bdc96c..d30af6e8a 100644 --- a/src/ui/button.h +++ b/src/ui/button.h @@ -1,4 +1,5 @@ // Aseprite UI Library +// Copyright (C) 2021 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This file is released under the terms of the MIT license. @@ -40,6 +41,8 @@ namespace ui { // New events virtual void onClick(Event& ev); + virtual void onStartDrag(); + virtual void onSelectWhenDragging(); private: void generateButtonSelectSignal();