1// Aseprite
2// Copyright (C) 2020-2021 Igara Studio S.A.
3// Copyright (C) 2001-2017 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/ui/popup_window_pin.h"
13
14#include "app/modules/gfx.h"
15#include "app/modules/gui.h"
16#include "app/ui/skin/skin_theme.h"
17#include "gfx/border.h"
18#include "gfx/size.h"
19#include "ui/ui.h"
20
21#include <vector>
22
23namespace app {
24
25using namespace app::skin;
26using namespace ui;
27
28PopupWindowPin::PopupWindowPin(const std::string& text,
29 const ClickBehavior clickBehavior,
30 const bool canPin)
31 : PopupWindow(text, clickBehavior,
32 EnterBehavior::CloseOnEnter, canPin)
33 , m_pinned(false)
34{
35}
36
37void PopupWindowPin::setPinned(const bool pinned)
38{
39 m_pinned = pinned;
40 if (m_pinned)
41 makeFloating();
42 else {
43 gfx::Rect rc = boundsOnScreen();
44 rc.enlarge(8 * guiscale());
45 setHotRegion(gfx::Region(rc));
46 makeFixed();
47 }
48}
49
50bool PopupWindowPin::onProcessMessage(Message* msg)
51{
52 switch (msg->type()) {
53
54 case kOpenMessage: {
55 if (!m_pinned)
56 setPinned(false);
57 break;
58 }
59
60 case kCloseMessage: {
61 // If the closer() wasn't the hot region or the window, it might
62 // be because the user pressed the close button.
63 if (closer() && closer() != this)
64 m_pinned = false;
65 break;
66 }
67
68 }
69
70 return PopupWindow::onProcessMessage(msg);
71}
72
73void PopupWindowPin::onWindowMovement()
74{
75 PopupWindow::onWindowMovement();
76
77 // If the window isn't pinned and we move it, we can automatically
78 // pin it.
79 if (!m_pinned)
80 setPinned(true);
81}
82
83} // namespace app
84