Skip to content

Commit

Permalink
Fix several issues locating windows with multiple UI windows
Browse files Browse the repository at this point in the history
We've refactored the code to support locating windows (and popups
windows + hot regions) correctly in both modes: with one ui::Display
and with multiple ui::Displays.

For this we've added a new ui::fit_bounds() function that works in
both modes.
  • Loading branch information
dacap committed Mar 19, 2021
1 parent 7079801 commit bcd6949
Show file tree
Hide file tree
Showing 48 changed files with 517 additions and 263 deletions.
13 changes: 9 additions & 4 deletions src/app/commands/cmd_canvas_size.cpp
Expand Up @@ -342,12 +342,17 @@ void CanvasSizeCommand::onExecute(Context* context)

// Find best position for the window on the editor
if (DocView* docView = static_cast<UIContext*>(context)->activeView()) {
window->positionWindow(
docView->bounds().x2() - window->bounds().w,
docView->bounds().y);
Display* display = ui::Manager::getDefault()->display();
ui::fit_bounds(display,
window.get(),
gfx::Rect(docView->bounds().x2() - window->bounds().w,
docView->bounds().y,
window->bounds().w,
window->bounds().h));
}
else
else {
window->centerWindow();
}

load_window_pos(window.get(), "CanvasSize");
window->setVisible(true);
Expand Down
7 changes: 2 additions & 5 deletions src/app/commands/cmd_change_pixel_format.cpp
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2019-2021 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
Expand Down Expand Up @@ -246,10 +246,7 @@ class ColorModeWindow : public app::gen::ColorMode {
advancedCheck()->Click.connect(
[this](ui::Event&){
advanced()->setVisible(advancedCheck()->isSelected());

const gfx::Rect origBounds = bounds();
setBounds(gfx::Rect(bounds().origin(), sizeHint()));
manager()->invalidateRect(origBounds);
expandWindow(sizeHint());
});
}
else {
Expand Down
15 changes: 12 additions & 3 deletions src/app/commands/cmd_keyboard_shortcuts.cpp
Expand Up @@ -32,6 +32,7 @@
#include "base/string.h"
#include "fmt/format.h"
#include "ui/alert.h"
#include "ui/fit_bounds.h"
#include "ui/graphics.h"
#include "ui/listitem.h"
#include "ui/message.h"
Expand Down Expand Up @@ -882,11 +883,19 @@ void KeyboardShortcutsCommand::onExecute(Context* context)
std::string neededSearchCopy = m_search;
KeyboardShortcutsWindow window(keys, menuKeys, neededSearchCopy);

gfx::Size displaySize = ui::get_desktop_size();
window.setBounds(gfx::Rect(0, 0, displaySize.w*3/4, displaySize.h*3/4));
ui::Display* mainDisplay = Manager::getDefault()->display();
ui::fit_bounds(mainDisplay, &window,
gfx::Rect(mainDisplay->size()),
[](const gfx::Rect& workarea,
gfx::Rect& bounds,
std::function<gfx::Rect(Widget*)> getWidgetBounds) {
gfx::Point center = bounds.center();
bounds.setSize(workarea.size()*3/4);
bounds.setOrigin(center - gfx::Point(bounds.size()/2));
});

window.loadLayout();

window.centerWindow();
window.setVisible(true);
window.openWindowInForeground();

Expand Down
6 changes: 1 addition & 5 deletions src/app/commands/cmd_paste_text.cpp
Expand Up @@ -124,11 +124,7 @@ class PasteTextWindow : public app::gen::PasteText {
}

if (!m_fontPopup->isVisible()) {
gfx::Size displaySize = manager()->display()->size();
gfx::Rect bounds = fontFace()->bounds();
m_fontPopup->showPopup(
gfx::Rect(bounds.x, bounds.y+bounds.h,
displaySize.w/2, displaySize.h/2));
m_fontPopup->showPopup(display(), fontFace()->bounds());
}
else {
m_fontPopup->closeWindow(NULL);
Expand Down
17 changes: 9 additions & 8 deletions src/app/console.cpp
Expand Up @@ -71,14 +71,15 @@ class Console::ConsoleWindow : public Window {
void centerConsole() {
initTheme();

remapWindow();

// TODO center to main window or screen workspace
gfx::Size displaySize = manager()->display()->size();
setBounds(gfx::Rect(0, 0, displaySize.w*9/10, displaySize.h*6/10));

centerWindow();
invalidate();
Display* display = ui::Manager::getDefault()->display();
const gfx::Rect displayRc = display->bounds();
gfx::Rect rc;
rc.w = displayRc.w*9/10;
rc.h = displayRc.h*6/10;
rc.x = displayRc.x + displayRc.w/2 - rc.w/2;
rc.y = displayRc.y + displayRc.h/2 - rc.h/2;

ui::fit_bounds(display, this, rc);
}

private:
Expand Down
12 changes: 8 additions & 4 deletions src/app/modules/gui.cpp
Expand Up @@ -693,11 +693,15 @@ void CustomizedGuiManager::onInitTheme(InitThemeEvent& ev)
void CustomizedGuiManager::onNewDisplayConfiguration(Display* display)
{
Manager::onNewDisplayConfiguration(display);
save_gui_config();

// TODO Should we provide a more generic way for all ui::Window to
// detect the os::Window (or UI Screen Scaling) change?
Console::notifyNewDisplayConfiguration();
// Only whne the main display/window is modified
if (display == this->display()) {
save_gui_config();

// TODO Should we provide a more generic way for all ui::Window to
// detect the os::Window (or UI Screen Scaling) change?
Console::notifyNewDisplayConfiguration();
}
}

std::string CustomizedGuiManager::loadLayout(Widget* widget)
Expand Down
2 changes: 1 addition & 1 deletion src/app/ui/browser_view.cpp
Expand Up @@ -589,7 +589,7 @@ void BrowserView::onTabPopup(Workspace* workspace)
if (!menu)
return;

menu->showPopup(mousePosInDisplay());
menu->showPopup(mousePosInDisplay(), display());
}

} // namespace app
35 changes: 22 additions & 13 deletions src/app/ui/brush_popup.cpp
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
Expand Down Expand Up @@ -36,6 +36,7 @@
#include "os/surface.h"
#include "os/system.h"
#include "ui/button.h"
#include "ui/fit_bounds.h"
#include "ui/link_label.h"
#include "ui/listitem.h"
#include "ui/menu.h"
Expand All @@ -52,22 +53,24 @@ using namespace ui;

namespace {

void show_popup_menu(PopupWindow* popupWindow, Menu* popupMenu,
const gfx::Point& pt)
void show_popup_menu(PopupWindow* popupWindow,
Menu* popupMenu,
const gfx::Point& pt,
Display* display)
{
// Here we make the popup window temporaly floating, so it's
// not closed by the popup menu.
popupWindow->makeFloating();

popupMenu->showPopup(pt);
popupMenu->showPopup(pt, display);

// Add the menu popup region to the window popup hot region so it's
// not closed after we close the menu.
popupWindow->makeFixed();

gfx::Region rgn;
rgn.createUnion(gfx::Region(popupWindow->bounds()),
gfx::Region(popupMenu->bounds()));
rgn.createUnion(gfx::Region(popupWindow->boundsOnScreen()),
gfx::Region(popupMenu->boundsOnScreen()));
popupWindow->setHotRegion(rgn);
}

Expand Down Expand Up @@ -194,7 +197,8 @@ class BrushOptionsItem : public ButtonSet::Item {

m_changeFlags = true;
show_popup_menu(m_popup, &menu,
gfx::Point(origin().x, origin().y+bounds().h));
gfx::Point(origin().x, origin().y+bounds().h),
display());

if (m_changeFlags) {
brush = m_brushes.getBrushSlot(m_slot);
Expand Down Expand Up @@ -299,8 +303,10 @@ class NewBrushOptionsItem : public ButtonSet::Item {
params.shade()->setSelected(saveBrush.shade());
params.pixelPerfect()->setSelected(saveBrush.pixelPerfect());

show_popup_menu(static_cast<PopupWindow*>(window()), &menu,
gfx::Point(origin().x, origin().y+bounds().h));
show_popup_menu(static_cast<PopupWindow*>(window()),
&menu,
gfx::Point(origin().x, origin().y+bounds().h),
display());

// Save preferences
if (saveBrush.brushType() != params.brushType()->isSelected())
Expand Down Expand Up @@ -384,7 +390,8 @@ void BrushPopup::setBrush(Brush* brush)
}
}

void BrushPopup::regenerate(const gfx::Rect& box)
void BrushPopup::regenerate(ui::Display* display,
const gfx::Point& pos)
{
auto& brushSlots = App::instance()->brushes().getBrushSlots();

Expand Down Expand Up @@ -425,8 +432,8 @@ void BrushPopup::regenerate(const gfx::Rect& box)
m_box.addChild(m_customBrushes);

// Resize the window and change the hot region.
setBounds(gfx::Rect(box.origin(), sizeHint()));
setHotRegion(gfx::Region(bounds()));
fit_bounds(display, this, gfx::Rect(pos, sizeHint()));
setHotRegion(gfx::Region(boundsOnScreen()));
}

void BrushPopup::onBrushChanges()
Expand All @@ -435,7 +442,9 @@ void BrushPopup::onBrushChanges()
gfx::Region rgn;
getDrawableRegion(rgn, kCutTopWindowsAndUseChildArea);

regenerate(bounds());
Display* mainDisplay = manager()->display();
regenerate(mainDisplay,
mainDisplay->nativeWindow()->pointFromScreen(boundsOnScreen().origin()));
invalidate();

parent()->invalidateRegion(rgn);
Expand Down
5 changes: 3 additions & 2 deletions src/app/ui/brush_popup.h
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2018-2020 Igara Studio S.A.
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2001-2015 David Capello
//
// This program is distributed under the terms of
Expand All @@ -21,7 +21,8 @@ namespace app {
BrushPopup();

void setBrush(doc::Brush* brush);
void regenerate(const gfx::Rect& box);
void regenerate(ui::Display* display,
const gfx::Point& pos);

static os::SurfaceRef createSurfaceForBrush(const doc::BrushRef& brush);

Expand Down
11 changes: 4 additions & 7 deletions src/app/ui/color_bar.cpp
Expand Up @@ -1868,7 +1868,7 @@ void ColorBar::showPaletteSortOptions()
asc.Click.connect([this]{ setAscending(true); });
des.Click.connect([this]{ setAscending(false); });

menu.showPopup(gfx::Point(bounds.x, bounds.y+bounds.h));
menu.showPopup(gfx::Point(bounds.x, bounds.y2()), display());
}

void ColorBar::showPalettePresets()
Expand All @@ -1884,16 +1884,13 @@ void ColorBar::showPalettePresets()
}

if (!m_palettePopup->isVisible()) {
gfx::Size displaySize = ui::get_desktop_size();
gfx::Rect bounds = m_buttons.getItem(
static_cast<int>(PalButton::PRESETS))->bounds();

m_palettePopup->showPopup(
gfx::Rect(bounds.x, bounds.y+bounds.h,
displaySize.w/2, displaySize.h*3/4));
m_palettePopup->showPopup(display(), bounds);
}
else {
m_palettePopup->closeWindow(NULL);
m_palettePopup->closeWindow(nullptr);
}
}

Expand All @@ -1904,7 +1901,7 @@ void ColorBar::showPaletteOptions()
gfx::Rect bounds = m_buttons.getItem(
static_cast<int>(PalButton::OPTIONS))->bounds();

menu->showPopup(gfx::Point(bounds.x, bounds.y+bounds.h));
menu->showPopup(gfx::Point(bounds.x, bounds.y2()), display());
}
}

Expand Down

0 comments on commit bcd6949

Please sign in to comment.