macOS: Fix Window::get_current_monitor (#521)

* macOS: Implement MonitorId::get_position

* macOS: Fix Window::get_current_monitor
This commit is contained in:
Francesca Frangipane 2018-05-16 09:41:45 -04:00 committed by GitHub
parent 87fa120ebb
commit 2464a135b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 18 deletions

View file

@ -9,6 +9,8 @@
- On X11, the primary monitor is detected correctly even when using versions of XRandR less than 1.5.
- `MonitorId` now implements `Debug`.
- Fixed bug on macOS where using `with_decorations(false)` would cause `set_decorations(true)` to produce a transparent titlebar with no title.
- Implemented `MonitorId::get_position` on macOS.
- On macOS, `Window::get_current_monitor` now returns accurate values.
# Version 0.14.0 (2018-05-09)

View file

@ -29,9 +29,9 @@ objc = "0.2"
[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
cocoa = "0.14"
core-foundation = "0.5"
core-graphics = "0.13"
cocoa = "0.15"
core-foundation = "0.6"
core-graphics = "0.14"
[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3"

View file

@ -1,7 +1,7 @@
use cocoa::appkit::NSScreen;
use cocoa::base::{id, nil};
use cocoa::foundation::{NSString, NSUInteger};
use core_graphics::display::{CGDirectDisplayID, CGDisplay};
use core_graphics::display::{CGDirectDisplayID, CGDisplay, CGDisplayBounds};
use std::collections::VecDeque;
use std::fmt;
use super::EventsLoop;
@ -40,7 +40,7 @@ impl fmt::Debug for MonitorId {
name: Option<String>,
native_identifier: u32,
dimensions: (u32, u32),
position: &'static str,
position: (i32, i32),
hidpi_factor: f32,
}
@ -48,7 +48,7 @@ impl fmt::Debug for MonitorId {
name: self.get_name(),
native_identifier: self.get_native_identifier(),
dimensions: self.get_dimensions(),
position: "WARNING: `MonitorId::get_position` is unimplemented on macOS!",
position: self.get_position(),
hidpi_factor: self.get_hidpi_factor(),
};
@ -81,7 +81,8 @@ impl MonitorId {
#[inline]
pub fn get_position(&self) -> (i32, i32) {
unimplemented!()
let bounds = unsafe { CGDisplayBounds(self.get_native_identifier()) };
(bounds.origin.x as i32, bounds.origin.y as i32)
}
pub fn get_hidpi_factor(&self) -> f32 {

View file

@ -326,7 +326,7 @@ impl WindowDelegate {
unsafe {
let state: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state as *mut DelegateState);
state.win_attribs.borrow_mut().fullscreen = Some(get_current_monitor());
state.win_attribs.borrow_mut().fullscreen = Some(get_current_monitor(*state.window));
state.handle_with_fullscreen = false;
}
@ -499,18 +499,13 @@ pub struct Window2 {
unsafe impl Send for Window2 {}
unsafe impl Sync for Window2 {}
/// Helpper funciton to convert NSScreen::mainScreen to MonitorId
unsafe fn get_current_monitor() -> RootMonitorId {
let screen = NSScreen::mainScreen(nil);
unsafe fn get_current_monitor(window: id) -> RootMonitorId {
let screen: id = msg_send![window, screen];
let desc = NSScreen::deviceDescription(screen);
let key = IdRef::new(NSString::alloc(nil).init_str("NSScreenNumber"));
let value = NSDictionary::valueForKey_(desc, *key);
let display_id = msg_send![value, unsignedIntegerValue];
RootMonitorId {
inner: EventsLoop::make_monitor_from_display(display_id),
}
RootMonitorId { inner: EventsLoop::make_monitor_from_display(display_id) }
}
impl Drop for Window2 {
@ -637,7 +632,7 @@ impl Window2 {
// Set fullscreen mode after we setup everything
if let Some(ref monitor) = win_attribs.fullscreen {
unsafe {
if monitor.inner != get_current_monitor().inner {
if monitor.inner != get_current_monitor(*window.window).inner {
unimplemented!();
}
}
@ -1082,7 +1077,7 @@ impl Window2 {
#[inline]
pub fn get_current_monitor(&self) -> RootMonitorId {
unsafe {
self::get_current_monitor()
self::get_current_monitor(*self.window)
}
}
}