From 4935236b41f096c485ed21308b37e1b64eaebcba Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 4 Jun 2014 22:10:59 +0100 Subject: [PATCH] Workaround to avoid problems if OSX fails to return any monitor sizes while monitors are being re-arranged. --- .../components/juce_Desktop.cpp | 1 + .../native/juce_mac_Windowing.mm | 64 ++++++++++++------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/modules/juce_gui_basics/components/juce_Desktop.cpp b/modules/juce_gui_basics/components/juce_Desktop.cpp index 5dedd4295f..b4db565b76 100644 --- a/modules/juce_gui_basics/components/juce_Desktop.cpp +++ b/modules/juce_gui_basics/components/juce_Desktop.cpp @@ -329,6 +329,7 @@ void Desktop::Displays::refresh() oldDisplays.swapWith (displays); init (Desktop::getInstance()); + jassert (displays.size() > 0); if (oldDisplays != displays) { diff --git a/modules/juce_gui_basics/native/juce_mac_Windowing.mm b/modules/juce_gui_basics/native/juce_mac_Windowing.mm index 7744d03879..9ad430097f 100644 --- a/modules/juce_gui_basics/native/juce_mac_Windowing.mm +++ b/modules/juce_gui_basics/native/juce_mac_Windowing.mm @@ -354,6 +354,41 @@ static Rectangle convertDisplayRect (NSRect r, CGFloat mainScreenBottom) return convertToRectInt (r); } +static Desktop::Displays::Display getDisplayFromScreen (NSScreen* s, CGFloat& mainScreenBottom, const float masterScale) +{ + Desktop::Displays::Display d; + + d.isMain = (mainScreenBottom == 0); + + if (d.isMain) + mainScreenBottom = [s frame].size.height; + + d.userArea = convertDisplayRect ([s visibleFrame], mainScreenBottom) / masterScale; + d.totalArea = convertDisplayRect ([s frame], mainScreenBottom) / masterScale; + d.scale = masterScale; + + #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7) + if ([s respondsToSelector: @selector (backingScaleFactor)]) + d.scale *= s.backingScaleFactor; + #endif + + NSSize dpi = [[[s deviceDescription] objectForKey: NSDeviceResolution] sizeValue]; + d.dpi = (dpi.width + dpi.height) / 2.0; + + return d; +} + +static Desktop::Displays::Display getDummyScreen (const float masterScale) +{ + Desktop::Displays::Display d; + d.isMain = true; + d.scale = masterScale; + d.dpi = 0; + d.userArea = d.totalArea = Rectangle (0, 0, 1024, 800); + return d; +} + + void Desktop::Displays::findDisplays (const float masterScale) { JUCE_AUTORELEASEPOOL @@ -363,29 +398,14 @@ void Desktop::Displays::findDisplays (const float masterScale) CGFloat mainScreenBottom = 0; for (NSScreen* s in [NSScreen screens]) + displays.add (getDisplayFromScreen (s, mainScreenBottom, masterScale)); + + if (displays.size() == 0) // this can apparently happen while rearranging monitors { - Display d; - d.isMain = false; - - if (mainScreenBottom == 0) - { - mainScreenBottom = [s frame].size.height; - d.isMain = true; - } - - d.userArea = convertDisplayRect ([s visibleFrame], mainScreenBottom) / masterScale; - d.totalArea = convertDisplayRect ([s frame], mainScreenBottom) / masterScale; - d.scale = masterScale; - - #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7) - if ([s respondsToSelector: @selector (backingScaleFactor)]) - d.scale *= s.backingScaleFactor; - #endif - - NSSize dpi = [[[s deviceDescription] objectForKey: NSDeviceResolution] sizeValue]; - d.dpi = (dpi.width + dpi.height) / 2.0; - - displays.add (d); + if (NSScreen* s = [NSScreen mainScreen]) + displays.add (getDisplayFromScreen (s, mainScreenBottom, masterScale)); + else + displays.add (getDummyScreen (masterScale)); } } }