Fix ColorButton color picking dragging the mouse without multiple displays

This commit is contained in:
David Capello 2021-04-20 13:01:49 -03:00
parent 0c932779fb
commit c1be8f6727

View File

@ -151,44 +151,38 @@ bool ColorButton::onProcessMessage(Message* msg)
gfx::Point mousePos = static_cast<MouseMessage*>(msg)->position();
app::Color color = m_color;
// Pick a color from a IColorSource
Widget* picked = window()->pick(mousePos);
// Try to pick a color from a IColorSource, then get the color
// from the display surface, and finally from the desktop. The
// desktop must be a last resource method, because in macOS it
// will ask for permissions to record the screen.
os::Window* nativeWindow = msg->display()->nativeWindow();
gfx::Point screenPos = nativeWindow->pointToScreen(mousePos);
Widget* picked = manager()->pickFromScreenPos(screenPos);
IColorSource* colorSource = (picked != this ? dynamic_cast<IColorSource*>(picked): nullptr);
if (colorSource) {
nativeWindow = picked->display()->nativeWindow();
mousePos = nativeWindow->pointFromScreen(screenPos);
}
else {
gfx::Color gfxColor = gfx::ColorNone;
// If there is no color source, get the color from the display
// surface, and finally from the desktop. The desktop must be
// a last resource method, because in macOS it will ask for
// permissions to record the screen.
if (!colorSource && get_multiple_displays()) {
os::Window* nativeWindow = display()->nativeWindow();
gfx::Point screenPos = nativeWindow->pointToScreen(mousePos);
picked = manager()->pickFromScreenPos(screenPos);
colorSource = (picked != this ? dynamic_cast<IColorSource*>(picked): nullptr);
if (colorSource) {
nativeWindow = picked->display()->nativeWindow();
// Get color from native window surface
if (nativeWindow->contentRect().contains(screenPos)) {
mousePos = nativeWindow->pointFromScreen(screenPos);
if (nativeWindow->surface()->bounds().contains(mousePos))
gfxColor = nativeWindow->surface()->getPixel(mousePos.x, mousePos.y);
}
else {
gfx::Color gfxColor = gfx::ColorNone;
// Get color from native window surface
if (nativeWindow->contentRect().contains(screenPos)) {
mousePos = nativeWindow->pointFromScreen(screenPos);
if (nativeWindow->surface()->bounds().contains(mousePos))
gfxColor = nativeWindow->surface()->getPixel(mousePos.x, mousePos.y);
}
// Or get the color from the screen
if (gfxColor == gfx::ColorNone) {
gfxColor = os::instance()->getColorFromScreen(screenPos);
}
color = app::Color::fromRgb(gfx::getr(gfxColor),
gfx::getg(gfxColor),
gfx::getb(gfxColor),
gfx::geta(gfxColor));
// Or get the color from the screen
if (gfxColor == gfx::ColorNone) {
gfxColor = os::instance()->getColorFromScreen(screenPos);
}
color = app::Color::fromRgb(gfx::getr(gfxColor),
gfx::getg(gfxColor),
gfx::getb(gfxColor),
gfx::geta(gfxColor));
}
if (colorSource) {