2018-03-06 09:35:04 +01:00
|
|
|
use cocoa::appkit::NSScreen;
|
|
|
|
|
use cocoa::base::{id, nil};
|
|
|
|
|
use cocoa::foundation::{NSString, NSUInteger};
|
2017-10-31 12:03:18 +02:00
|
|
|
use core_graphics::display::{CGDirectDisplayID, CGDisplay};
|
2015-02-21 23:59:37 +11:00
|
|
|
use std::collections::VecDeque;
|
2018-05-14 08:14:57 -04:00
|
|
|
use std::fmt;
|
2017-09-01 11:04:57 +02:00
|
|
|
use super::EventsLoop;
|
2018-03-06 09:35:04 +01:00
|
|
|
use super::window::IdRef;
|
2014-11-04 18:03:38 +01:00
|
|
|
|
2018-04-18 02:07:54 +08:00
|
|
|
#[derive(Clone, PartialEq)]
|
2017-10-31 12:03:18 +02:00
|
|
|
pub struct MonitorId(CGDirectDisplayID);
|
2014-11-04 18:03:38 +01:00
|
|
|
|
2017-09-01 11:04:57 +02:00
|
|
|
impl EventsLoop {
|
|
|
|
|
pub fn get_available_monitors(&self) -> VecDeque<MonitorId> {
|
|
|
|
|
let mut monitors = VecDeque::new();
|
2017-10-31 12:03:18 +02:00
|
|
|
if let Ok(displays) = CGDisplay::active_displays() {
|
|
|
|
|
for d in displays {
|
|
|
|
|
monitors.push_back(MonitorId(d));
|
2017-09-01 11:04:57 +02:00
|
|
|
}
|
2014-11-04 18:03:38 +01:00
|
|
|
}
|
2017-09-01 11:04:57 +02:00
|
|
|
monitors
|
2014-11-04 18:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
2017-09-01 11:04:57 +02:00
|
|
|
#[inline]
|
|
|
|
|
pub fn get_primary_monitor(&self) -> MonitorId {
|
2017-10-31 12:03:18 +02:00
|
|
|
let id = MonitorId(CGDisplay::main().id);
|
2017-09-01 11:04:57 +02:00
|
|
|
id
|
|
|
|
|
}
|
2018-04-18 02:07:54 +08:00
|
|
|
|
|
|
|
|
pub fn make_monitor_from_display(id: CGDirectDisplayID) -> MonitorId {
|
|
|
|
|
let id = MonitorId(id);
|
|
|
|
|
id
|
|
|
|
|
}
|
2014-11-04 18:03:38 +01:00
|
|
|
}
|
|
|
|
|
|
2018-05-14 08:14:57 -04:00
|
|
|
impl fmt::Debug for MonitorId {
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct MonitorId {
|
|
|
|
|
name: Option<String>,
|
|
|
|
|
native_identifier: u32,
|
|
|
|
|
dimensions: (u32, u32),
|
|
|
|
|
position: &'static str,
|
|
|
|
|
hidpi_factor: f32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let monitor_id_proxy = MonitorId {
|
|
|
|
|
name: self.get_name(),
|
|
|
|
|
native_identifier: self.get_native_identifier(),
|
|
|
|
|
dimensions: self.get_dimensions(),
|
|
|
|
|
position: "WARNING: `MonitorId::get_position` is unimplemented on macOS!",
|
|
|
|
|
hidpi_factor: self.get_hidpi_factor(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
monitor_id_proxy.fmt(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-24 09:11:59 +02:00
|
|
|
impl MonitorId {
|
2014-11-04 18:03:38 +01:00
|
|
|
pub fn get_name(&self) -> Option<String> {
|
2015-09-24 09:11:59 +02:00
|
|
|
let MonitorId(display_id) = *self;
|
2017-10-31 12:03:18 +02:00
|
|
|
let screen_num = CGDisplay::new(display_id).model_number();
|
2014-11-04 18:03:38 +01:00
|
|
|
Some(format!("Monitor #{}", screen_num))
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-21 14:42:05 +02:00
|
|
|
#[inline]
|
2017-08-30 08:49:18 +02:00
|
|
|
pub fn get_native_identifier(&self) -> u32 {
|
|
|
|
|
self.0
|
2015-03-16 13:52:58 -07:00
|
|
|
}
|
|
|
|
|
|
2015-01-15 20:20:52 +01:00
|
|
|
pub fn get_dimensions(&self) -> (u32, u32) {
|
2015-09-24 09:11:59 +02:00
|
|
|
let MonitorId(display_id) = *self;
|
2017-10-31 12:03:18 +02:00
|
|
|
let display = CGDisplay::new(display_id);
|
|
|
|
|
let dimension = {
|
|
|
|
|
let height = display.pixels_high();
|
|
|
|
|
let width = display.pixels_wide();
|
2015-01-15 20:20:52 +01:00
|
|
|
(width as u32, height as u32)
|
2014-11-04 18:03:38 +01:00
|
|
|
};
|
|
|
|
|
dimension
|
|
|
|
|
}
|
2017-09-07 09:33:46 +01:00
|
|
|
|
|
|
|
|
#[inline]
|
2017-10-19 20:08:05 +03:00
|
|
|
pub fn get_position(&self) -> (i32, i32) {
|
2017-09-07 09:33:46 +01:00
|
|
|
unimplemented!()
|
|
|
|
|
}
|
2017-10-17 14:56:38 +03:00
|
|
|
|
|
|
|
|
pub fn get_hidpi_factor(&self) -> f32 {
|
2018-03-06 09:35:04 +01:00
|
|
|
let screen = match self.get_nsscreen() {
|
|
|
|
|
Some(screen) => screen,
|
|
|
|
|
None => return 1.0, // default to 1.0 when we can't find the screen
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
unsafe { NSScreen::backingScaleFactor(screen) as f32 }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn get_nsscreen(&self) -> Option<id> {
|
|
|
|
|
unsafe {
|
|
|
|
|
let native_id = self.get_native_identifier();
|
|
|
|
|
let screens = NSScreen::screens(nil);
|
|
|
|
|
let count: NSUInteger = msg_send![screens, count];
|
|
|
|
|
let key = IdRef::new(NSString::alloc(nil).init_str("NSScreenNumber"));
|
|
|
|
|
let mut matching_screen: Option<id> = None;
|
|
|
|
|
for i in 0..count {
|
|
|
|
|
let screen = msg_send![screens, objectAtIndex: i as NSUInteger];
|
|
|
|
|
let device_description = NSScreen::deviceDescription(screen);
|
|
|
|
|
let value: id = msg_send![device_description, objectForKey:*key];
|
|
|
|
|
if value != nil {
|
|
|
|
|
let screen_number: NSUInteger = msg_send![value, unsignedIntegerValue];
|
|
|
|
|
if screen_number as u32 == native_id {
|
|
|
|
|
matching_screen = Some(screen);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
matching_screen
|
|
|
|
|
}
|
2017-10-17 14:56:38 +03:00
|
|
|
}
|
2014-11-04 18:03:38 +01:00
|
|
|
}
|