1// Aseprite UI Library
2// Copyright (C) 2019-2022 Igara Studio S.A.
3// Copyright (C) 2001-2016 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "ui/fit_bounds.h"
13
14#include "gfx/rect.h"
15#include "os/screen.h"
16#include "os/system.h"
17#include "ui/base.h"
18#include "ui/display.h"
19#include "ui/system.h"
20#include "ui/window.h"
21
22#include <algorithm>
23
24namespace ui {
25
26#if 0 // TODO unused function, referenced in a comment in this file
27static gfx::Region get_workarea_region()
28{
29 // Returns the union of the workarea of all available screens
30 gfx::Region wa;
31 os::ScreenList screens;
32 os::instance()->listScreens(screens);
33 for (const auto& screen : screens)
34 wa |= gfx::Region(screen->workarea());
35 return wa;
36}
37#endif
38
39int fit_bounds(Display* display, int arrowAlign, const gfx::Rect& target, gfx::Rect& bounds)
40{
41 bounds.x = target.x;
42 bounds.y = target.y;
43
44 int trycount = 0;
45 for (; trycount < 4; ++trycount) {
46 switch (arrowAlign) {
47 case TOP | LEFT:
48 bounds.x = target.x + target.w;
49 bounds.y = target.y + target.h;
50 break;
51 case TOP | RIGHT:
52 bounds.x = target.x - bounds.w;
53 bounds.y = target.y + target.h;
54 break;
55 case BOTTOM | LEFT:
56 bounds.x = target.x + target.w;
57 bounds.y = target.y - bounds.h;
58 break;
59 case BOTTOM | RIGHT:
60 bounds.x = target.x - bounds.w;
61 bounds.y = target.y - bounds.h;
62 break;
63 case TOP:
64 bounds.x = target.x + target.w/2 - bounds.w/2;
65 bounds.y = target.y + target.h;
66 break;
67 case BOTTOM:
68 bounds.x = target.x + target.w/2 - bounds.w/2;
69 bounds.y = target.y - bounds.h;
70 break;
71 case LEFT:
72 bounds.x = target.x + target.w;
73 bounds.y = target.y + target.h/2 - bounds.h/2;
74 break;
75 case RIGHT:
76 bounds.x = target.x - bounds.w;
77 bounds.y = target.y + target.h/2 - bounds.h/2;
78 break;
79 }
80
81 gfx::Size displaySize = display->size();
82 bounds.x = std::clamp(bounds.x, 0, displaySize.w-bounds.w);
83 bounds.y = std::clamp(bounds.y, 0, displaySize.h-bounds.h);
84
85 if (target.intersects(bounds)) {
86 switch (trycount) {
87 case 0:
88 case 2:
89 // Switch position
90 if (arrowAlign & (TOP | BOTTOM)) arrowAlign ^= TOP | BOTTOM;
91 if (arrowAlign & (LEFT | RIGHT)) arrowAlign ^= LEFT | RIGHT;
92 break;
93 case 1:
94 // Rotate positions
95 if (arrowAlign & (TOP | LEFT)) arrowAlign ^= TOP | LEFT;
96 if (arrowAlign & (BOTTOM | RIGHT)) arrowAlign ^= BOTTOM | RIGHT;
97 break;
98 }
99 }
100 else
101 break;
102 }
103
104 return arrowAlign;
105}
106
107void fit_bounds(const Display* parentDisplay,
108 Window* window,
109 const gfx::Rect& candidateBoundsRelativeToParentDisplay,
110 std::function<void(const gfx::Rect& workarea,
111 gfx::Rect& bounds,
112 std::function<gfx::Rect(Widget*)> getWidgetBounds)> fitLogic)
113{
114 gfx::Point pos = candidateBoundsRelativeToParentDisplay.origin();
115
116 if (get_multiple_displays() && window->shouldCreateNativeWindow()) {
117 const os::Window* nativeWindow = const_cast<ui::Display*>(parentDisplay)->nativeWindow();
118 // Limit to the current screen workarea (instead of using all the
119 // available workarea between all monitors, get_workarea_region())
120 const gfx::Rect workarea = nativeWindow->screen()->workarea();
121 const int scale = nativeWindow->scale();
122
123 // Screen frame bounds
124 gfx::Rect frame(
125 nativeWindow->pointToScreen(pos),
126 candidateBoundsRelativeToParentDisplay.size() * scale);
127
128 if (fitLogic)
129 fitLogic(workarea, frame, [](Widget* widget){ return widget->boundsOnScreen(); });
130
131 frame.x = std::clamp(frame.x, workarea.x, workarea.x2() - frame.w);
132 frame.y = std::clamp(frame.y, workarea.y, workarea.y2() - frame.h);
133
134 // Set frame bounds directly
135 window->setBounds(gfx::Rect(0, 0, frame.w / scale, frame.h / scale));
136 window->loadNativeFrame(frame);
137
138 if (window->isVisible()) {
139 if (window->ownDisplay())
140 window->display()->nativeWindow()->setFrame(frame);
141 }
142 }
143 else {
144 const gfx::Rect displayBounds(parentDisplay->size());
145 gfx::Rect frame(candidateBoundsRelativeToParentDisplay);
146
147 if (fitLogic)
148 fitLogic(displayBounds, frame, [](Widget* widget){ return widget->bounds(); });
149
150 frame.x = std::clamp(frame.x, 0, displayBounds.w - frame.w);
151 frame.y = std::clamp(frame.y, 0, displayBounds.h - frame.h);
152
153 window->setBounds(frame);
154 }
155}
156
157// Limit window position using the union of all workareas
158//
159// TODO at least the title bar should be visible so we can
160// resize it, because workareas can form an irregular shape
161// (not rectangular) the calculation is a little more
162// complex
163void limit_with_workarea(Display* parentDisplay, gfx::Rect& frame)
164{
165 if (!get_multiple_displays())
166 return;
167
168 ASSERT(parentDisplay);
169
170 gfx::Rect waBounds = parentDisplay->nativeWindow()->screen()->workarea();
171 if (frame.x < waBounds.x) frame.x = waBounds.x;
172 if (frame.y < waBounds.y) frame.y = waBounds.y;
173 if (frame.x2() > waBounds.x2()) {
174 frame.x -= frame.x2() - waBounds.x2();
175 if (frame.x < waBounds.x) {
176 frame.x = waBounds.x;
177 frame.w = waBounds.w;
178 }
179 }
180 if (frame.y2() > waBounds.y2()) {
181 frame.y -= frame.y2() - waBounds.y2();
182 if (frame.y < waBounds.y) {
183 frame.y = waBounds.y;
184 frame.h = waBounds.h;
185 }
186 }
187}
188
189} // namespace ui
190