mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2025-01-11 21:31:29 +11:00
Implement MonitorId::get_hidpi_factor for macOS (#410)
* Implement MonitorId::get_hidpi_factor for macOS * Added changelog entry
This commit is contained in:
parent
4c62d71950
commit
f3d43016ad
|
@ -1,6 +1,8 @@
|
||||||
# Unreleased
|
# Unreleased
|
||||||
|
|
||||||
- Impl `Hash`, `PartialEq`, and `Eq` for `events::ModifiersState`.
|
- Impl `Hash`, `PartialEq`, and `Eq` for `events::ModifiersState`.
|
||||||
|
- Implement `MonitorId::get_hidpi_factor` for MacOS.
|
||||||
|
- Added method `os::macos::MonitorIdExt::get_nsscreen() -> *mut c_void` that gets a `NSScreen` object matching the monitor ID.
|
||||||
|
|
||||||
# Version 0.11.1 (2018-02-19)
|
# Version 0.11.1 (2018-02-19)
|
||||||
|
|
||||||
|
|
|
@ -86,6 +86,8 @@ impl WindowBuilderExt for WindowBuilder {
|
||||||
pub trait MonitorIdExt {
|
pub trait MonitorIdExt {
|
||||||
/// Returns the identifier of the monitor for Cocoa.
|
/// Returns the identifier of the monitor for Cocoa.
|
||||||
fn native_id(&self) -> u32;
|
fn native_id(&self) -> u32;
|
||||||
|
/// Returns a pointer to the NSScreen representing this monitor.
|
||||||
|
fn get_nsscreen(&self) -> Option<*mut c_void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MonitorIdExt for MonitorId {
|
impl MonitorIdExt for MonitorId {
|
||||||
|
@ -93,4 +95,8 @@ impl MonitorIdExt for MonitorId {
|
||||||
fn native_id(&self) -> u32 {
|
fn native_id(&self) -> u32 {
|
||||||
self.inner.get_native_identifier()
|
self.inner.get_native_identifier()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_nsscreen(&self) -> Option<*mut c_void> {
|
||||||
|
self.inner.get_nsscreen().map(|s| s as *mut c_void)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
|
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};
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use super::EventsLoop;
|
use super::EventsLoop;
|
||||||
|
use super::window::IdRef;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct MonitorId(CGDirectDisplayID);
|
pub struct MonitorId(CGDirectDisplayID);
|
||||||
|
@ -51,8 +55,35 @@ impl MonitorId {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn get_hidpi_factor(&self) -> f32 {
|
pub fn get_hidpi_factor(&self) -> f32 {
|
||||||
1.0
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ use objc::declare::ClassDecl;
|
||||||
|
|
||||||
use cocoa;
|
use cocoa;
|
||||||
use cocoa::base::{id, nil};
|
use cocoa::base::{id, nil};
|
||||||
use cocoa::foundation::{NSPoint, NSRect, NSSize, NSString, NSUInteger};
|
use cocoa::foundation::{NSPoint, NSRect, NSSize, NSString};
|
||||||
use cocoa::appkit::{self, NSApplication, NSColor, NSView, NSWindow, NSWindowStyleMask};
|
use cocoa::appkit::{self, NSApplication, NSColor, NSView, NSWindow, NSWindowStyleMask};
|
||||||
|
|
||||||
use core_graphics::display::CGDisplay;
|
use core_graphics::display::CGDisplay;
|
||||||
|
@ -389,27 +389,8 @@ impl Window2 {
|
||||||
unsafe {
|
unsafe {
|
||||||
let screen = match attrs.fullscreen {
|
let screen = match attrs.fullscreen {
|
||||||
Some(ref monitor_id) => {
|
Some(ref monitor_id) => {
|
||||||
let native_id = monitor_id.inner.get_native_identifier();
|
let monitor_screen = monitor_id.inner.get_nsscreen();
|
||||||
let matching_screen = {
|
Some(monitor_screen.unwrap_or(appkit::NSScreen::mainScreen(nil)))
|
||||||
let screens = appkit::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 = appkit::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
|
|
||||||
};
|
|
||||||
Some(matching_screen.unwrap_or(appkit::NSScreen::mainScreen(nil)))
|
|
||||||
},
|
},
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
@ -720,19 +701,19 @@ unsafe fn nswindow_set_max_dimensions<V: NSWindow + Copy>(
|
||||||
pub struct IdRef(id);
|
pub struct IdRef(id);
|
||||||
|
|
||||||
impl IdRef {
|
impl IdRef {
|
||||||
fn new(i: id) -> IdRef {
|
pub fn new(i: id) -> IdRef {
|
||||||
IdRef(i)
|
IdRef(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn retain(i: id) -> IdRef {
|
pub fn retain(i: id) -> IdRef {
|
||||||
if i != nil {
|
if i != nil {
|
||||||
let _: id = unsafe { msg_send![i, retain] };
|
let _: id = unsafe { msg_send![i, retain] };
|
||||||
}
|
}
|
||||||
IdRef(i)
|
IdRef(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn non_nil(self) -> Option<IdRef> {
|
pub fn non_nil(self) -> Option<IdRef> {
|
||||||
if self.0 == nil { None } else { Some(self) }
|
if self.0 == nil { None } else { Some(self) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue