forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfit_bounds.cpp
More file actions
218 lines (189 loc) · 6.35 KB
/
fit_bounds.cpp
File metadata and controls
218 lines (189 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Aseprite UI Library
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ui/fit_bounds.h"
#include "gfx/rect.h"
#include "os/screen.h"
#include "os/system.h"
#include "ui/base.h"
#include "ui/display.h"
#include "ui/system.h"
#include "ui/window.h"
#include <algorithm>
#include <climits>
namespace ui {
// if the number of monitors changes during runtime this could break
static const std::vector<gfx::Rect> get_all_workareas()
{
std::vector<gfx::Rect> workareas;
os::ScreenList screens;
os::System::instance()->listScreens(screens);
for (const auto& screen : screens)
workareas.push_back(screen->workarea());
return workareas;
}
int fit_bounds(Display* display, int arrowAlign, const gfx::Rect& target, gfx::Rect& bounds)
{
bounds.x = target.x;
bounds.y = target.y;
int trycount = 0;
for (; trycount < 4; ++trycount) {
switch (arrowAlign) {
case TOP | LEFT:
bounds.x = target.x + target.w;
bounds.y = target.y + target.h;
break;
case TOP | RIGHT:
bounds.x = target.x - bounds.w;
bounds.y = target.y + target.h;
break;
case BOTTOM | LEFT:
bounds.x = target.x + target.w;
bounds.y = target.y - bounds.h;
break;
case BOTTOM | RIGHT:
bounds.x = target.x - bounds.w;
bounds.y = target.y - bounds.h;
break;
case TOP:
bounds.x = target.x + target.w / 2 - bounds.w / 2;
bounds.y = target.y + target.h;
break;
case BOTTOM:
bounds.x = target.x + target.w / 2 - bounds.w / 2;
bounds.y = target.y - bounds.h;
break;
case LEFT:
bounds.x = target.x + target.w;
bounds.y = target.y + target.h / 2 - bounds.h / 2;
break;
case RIGHT:
bounds.x = target.x - bounds.w;
bounds.y = target.y + target.h / 2 - bounds.h / 2;
break;
}
gfx::Size displaySize = display->size();
bounds.x = std::clamp(bounds.x, 0, displaySize.w - bounds.w);
bounds.y = std::clamp(bounds.y, 0, displaySize.h - bounds.h);
if (target.intersects(bounds)) {
switch (trycount) {
case 0:
case 2:
// Switch position
if (arrowAlign & (TOP | BOTTOM))
arrowAlign ^= TOP | BOTTOM;
if (arrowAlign & (LEFT | RIGHT))
arrowAlign ^= LEFT | RIGHT;
break;
case 1:
// Rotate positions
if (arrowAlign & (TOP | LEFT))
arrowAlign ^= TOP | LEFT;
if (arrowAlign & (BOTTOM | RIGHT))
arrowAlign ^= BOTTOM | RIGHT;
break;
}
}
else
break;
}
return arrowAlign;
}
void fit_bounds(const Display* parentDisplay,
Window* window,
const gfx::Rect& candidateBoundsRelativeToParentDisplay,
std::function<void(const gfx::Rect& workarea,
gfx::Rect& bounds,
std::function<gfx::Rect(Widget*)> getWidgetBounds)> fitLogic)
{
gfx::Point pos = candidateBoundsRelativeToParentDisplay.origin();
if (get_multiple_displays() && window->shouldCreateNativeWindow()) {
const os::Window* nativeWindow = const_cast<ui::Display*>(parentDisplay)->nativeWindow();
// Limit to the current screen workarea (instead of using all the
// available workarea between all monitors)
const gfx::Rect workarea = nativeWindow->screen()->workarea();
const int scale = nativeWindow->scale();
// Screen frame bounds
gfx::Rect frame(nativeWindow->pointToScreen(pos),
candidateBoundsRelativeToParentDisplay.size() * scale);
if (fitLogic)
fitLogic(workarea, frame, [](Widget* widget) { return widget->boundsOnScreen(); });
frame.x = std::clamp(frame.x, workarea.x, std::max(workarea.x, workarea.x2() - frame.w));
frame.y = std::clamp(frame.y, workarea.y, std::max(workarea.y, workarea.y2() - frame.h));
// Set frame bounds directly
window->setBounds(gfx::Rect(0, 0, frame.w / scale, frame.h / scale));
window->loadNativeFrame(frame);
if (window->isVisible()) {
if (window->ownDisplay())
window->display()->nativeWindow()->setFrame(frame);
}
}
else {
const gfx::Rect displayBounds(parentDisplay->size());
gfx::Rect frame(candidateBoundsRelativeToParentDisplay);
if (fitLogic)
fitLogic(displayBounds, frame, [](Widget* widget) { return widget->bounds(); });
frame.x = std::clamp(frame.x, 0, std::max(0, displayBounds.w - frame.w));
frame.y = std::clamp(frame.y, 0, std::max(0, displayBounds.h - frame.h));
window->setBounds(frame);
}
}
void limit_with_workarea(const gfx::Rect& workareaBounds, gfx::Rect& frame)
{
if (frame.x < workareaBounds.x)
frame.x = workareaBounds.x;
if (frame.y < workareaBounds.y)
frame.y = workareaBounds.y;
if (frame.x2() > workareaBounds.x2()) {
frame.x -= frame.x2() - workareaBounds.x2();
if (frame.x < workareaBounds.x) {
frame.x = workareaBounds.x;
frame.w = workareaBounds.w;
}
}
if (frame.y2() > workareaBounds.y2()) {
frame.y -= frame.y2() - workareaBounds.y2();
if (frame.y < workareaBounds.y) {
frame.y = workareaBounds.y;
frame.h = workareaBounds.h;
}
}
}
// since sqrt(a) < sqrt(b) for all a < b, we only have to square to compare
inline int distance_squared(const gfx::Rect& r1, const gfx::Rect& r2)
{
int dx = r1.x - r2.x;
int dy = r1.y - r2.y;
return dx * dx + dy * dy;
}
void limit_least(gfx::Rect& frame)
{
int min_distance = INT_MAX;
gfx::Rect candidate(frame); // it shouldn't be possible for there to be no workareas but set
// candidate to a valid Rect anyways
for (const auto& workarea : get_all_workareas()) {
// simulate clamping
gfx::Rect clone(frame);
limit_with_workarea(workarea, clone);
// find the least clamped clone
int distance = distance_squared(clone, frame);
if (distance < min_distance) {
min_distance = distance;
candidate.x = clone.x;
candidate.y = clone.y;
candidate.w = clone.w;
candidate.h = clone.h;
}
}
frame.x = candidate.x;
frame.y = candidate.y;
frame.w = candidate.w;
frame.h = candidate.h;
}
} // namespace ui