2016-09-15 14:07:36 +00:00
|
|
|
// Aseprite UI Library
|
2021-03-19 21:57:56 +00:00
|
|
|
// Copyright (C) 2019-2021 Igara Studio S.A.
|
2016-09-15 14:07:36 +00:00
|
|
|
// 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
|
|
|
|
|
2021-02-18 15:30:14 +00:00
|
|
|
#include "ui/fit_bounds.h"
|
|
|
|
|
2019-12-20 18:08:34 +00:00
|
|
|
#include "base/clamp.h"
|
2016-09-15 14:07:36 +00:00
|
|
|
#include "gfx/rect.h"
|
|
|
|
#include "ui/base.h"
|
2021-02-18 15:30:14 +00:00
|
|
|
#include "ui/display.h"
|
2016-09-15 14:07:36 +00:00
|
|
|
#include "ui/system.h"
|
2021-03-19 21:57:56 +00:00
|
|
|
#include "ui/window.h"
|
2016-09-15 14:07:36 +00:00
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2021-02-18 15:30:14 +00:00
|
|
|
int fit_bounds(Display* display, int arrowAlign, const gfx::Rect& target, gfx::Rect& bounds)
|
2016-09-15 14:07:36 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-18 15:30:14 +00:00
|
|
|
gfx::Size displaySize = display->size();
|
|
|
|
bounds.x = base::clamp(bounds.x, 0, displaySize.w-bounds.w);
|
|
|
|
bounds.y = base::clamp(bounds.y, 0, displaySize.h-bounds.h);
|
2016-09-15 14:07:36 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-03-19 21:57:56 +00:00
|
|
|
void fit_bounds(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 = parentDisplay->nativeWindow();
|
|
|
|
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 = base::clamp(frame.x, workarea.x, workarea.x2() - frame.w);
|
|
|
|
frame.y = base::clamp(frame.y, 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())
|
|
|
|
window->expandWindow(frame.size() / scale);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const gfx::Rect displayBounds(parentDisplay->size());
|
|
|
|
gfx::Rect frame(candidateBoundsRelativeToParentDisplay);
|
|
|
|
|
|
|
|
if (fitLogic)
|
|
|
|
fitLogic(displayBounds, frame, [](Widget* widget){ return widget->bounds(); });
|
|
|
|
|
|
|
|
frame.x = base::clamp(frame.x, 0, displayBounds.w - frame.w);
|
|
|
|
frame.y = base::clamp(frame.y, 0, displayBounds.h - frame.h);
|
|
|
|
|
|
|
|
window->setBounds(frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 14:07:36 +00:00
|
|
|
} // namespace ui
|