Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS: Dpi overhaul #997

Merged
merged 27 commits into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
da2611e
WIP - Make EL2 DPI changes and implement on Windows (#895)
Osspial Jun 19, 2019
c3c12f4
fix app_state errors
vbogaevsky Jun 24, 2019
fec2f80
fixes hidpi related errors in window_delegate
vbogaevsky Jun 25, 2019
73327e0
Merge branch 'dpi-overhaul' of github.com:vbogaevsky/winit into dpi-o…
vbogaevsky Jun 25, 2019
4213c7a
fix bad merge
vbogaevsky Jun 25, 2019
90ed1e4
dpi_factor edits in window_delegate
vbogaevsky Jun 26, 2019
607e6c3
fixes type and lifetime errors in window and window_delegate
vbogaevsky Jun 28, 2019
97ebda6
applies fmt
vbogaevsky Jun 28, 2019
7ac43a4
complies with @aleksijuvani requested changes
vbogaevsky Jun 30, 2019
a3ecb4b
modifies Handler lifetimes
vbogaevsky Jul 2, 2019
5d029ab
fixes lifetime isues, adds propper handling for HiDpiChanged
vbogaevsky Jul 9, 2019
cad8ae3
applies fmt
vbogaevsky Jul 9, 2019
f5a7f40
restore original lifetimes
vbogaevsky Jul 13, 2019
8b64e01
Merge branch 'dpi-overhaul' of github.com:rust-windowing/winit into d…
vbogaevsky Aug 14, 2019
00da630
solution is somewhere out there
vbogaevsky Aug 21, 2019
c7becf9
applies fmt
vbogaevsky Aug 21, 2019
8305206
pass as references
vbogaevsky Aug 21, 2019
189ca8c
resolves issue with HANDLER
vbogaevsky Aug 22, 2019
a76f8e7
crate visible type error
vbogaevsky Aug 23, 2019
c2d99b6
fixes visibility issues
vbogaevsky Aug 23, 2019
8ad50d5
applies fmt
vbogaevsky Aug 23, 2019
938da69
deals with warnings
vbogaevsky Aug 23, 2019
4fc725c
simplifies new_inner_size setting algorthm
vbogaevsky Aug 25, 2019
5aa604f
moves proxy instead of referencing it and removes double deref from p…
vbogaevsky Aug 25, 2019
7f0d3ea
makes @Osspial tests (https://github.com/rust-windowing/winit/pull/99…
vbogaevsky Aug 29, 2019
c6559d0
complies with @aleksijuvani suggested changes
vbogaevsky Aug 29, 2019
1101152
makes max window size std::f32::MAX
vbogaevsky Aug 30, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ core-foundation = "0.6"
core-graphics = "0.17.3"
core-video-sys = "0.1.2"
dispatch = "0.1.4"
objc = "0.2.3"
objc = "0.2.6"

[target.'cfg(target_os = "windows")'.dependencies]
bitflags = "1"
Expand Down
69 changes: 38 additions & 31 deletions src/platform_impl/macos/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ use crate::{
};

lazy_static! {
static ref HANDLER: Handler = Default::default();
static ref HANDLER: Handler<'static> = Default::default();
}

impl Event<Never> {
fn userify<T: 'static>(self) -> Event<T> {
impl<'a, Never> Event<'a, Never> {
fn userify<T: 'static>(self) -> Event<'a, T> {
self.map_nonuser_event()
// `Never` can't be constructed, so the `UserEvent` variant can't
// be present here.
Expand All @@ -33,7 +33,8 @@ impl Event<Never> {
}

pub trait EventHandler: Debug {
fn handle_nonuser_event(&mut self, event: Event<Never>, control_flow: &mut ControlFlow);
// Not sure probably it should accept Event<'static, Never>
fn handle_nonuser_event(&mut self, event: Event<'_, Never>, control_flow: &mut ControlFlow);
fn handle_user_events(&mut self, control_flow: &mut ControlFlow);
}

Expand All @@ -54,10 +55,10 @@ impl<F, T> Debug for EventLoopHandler<F, T> {

impl<F, T> EventHandler for EventLoopHandler<F, T>
where
F: 'static + FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
F: 'static + FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow),
T: 'static,
{
fn handle_nonuser_event(&mut self, event: Event<Never>, control_flow: &mut ControlFlow) {
fn handle_nonuser_event(&mut self, event: Event<'_, Never>, control_flow: &mut ControlFlow) {
(self.callback)(event.userify(), &self.window_target, control_flow);
self.will_exit |= *control_flow == ControlFlow::Exit;
if self.will_exit {
Expand All @@ -79,36 +80,38 @@ where
}

#[derive(Default)]
struct Handler {
struct Handler<'a> {
ready: AtomicBool,
in_callback: AtomicBool,
control_flow: Mutex<ControlFlow>,
control_flow_prev: Mutex<ControlFlow>,
start_time: Mutex<Option<Instant>>,
callback: Mutex<Option<Box<dyn EventHandler>>>,
pending_events: Mutex<VecDeque<Event<Never>>>,
deferred_events: Mutex<VecDeque<Event<Never>>>,
pending_events: Mutex<VecDeque<Event<'a, Never>>>,
vbogaevsky marked this conversation as resolved.
Show resolved Hide resolved
// We don't need it any more. See line 270
vbogaevsky marked this conversation as resolved.
Show resolved Hide resolved
// deferred_events: Mutex<VecDeque<Event<'a, Never>>>,
vbogaevsky marked this conversation as resolved.
Show resolved Hide resolved
pending_redraw: Mutex<Vec<WindowId>>,
waker: Mutex<EventLoopWaker>,
}

unsafe impl Send for Handler {}
unsafe impl Sync for Handler {}
unsafe impl<'a> Send for Handler<'a> {}
unsafe impl<'a> Sync for Handler<'a> {}

impl Handler {
fn events<'a>(&'a self) -> MutexGuard<'a, VecDeque<Event<Never>>> {
impl<'a> Handler<'a> {
fn events(&self) -> MutexGuard<'_, VecDeque<Event<'a, Never>>> {
self.pending_events.lock().unwrap()
}

fn deferred<'a>(&'a self) -> MutexGuard<'a, VecDeque<Event<Never>>> {
self.deferred_events.lock().unwrap()
}
// We don't need it any more.
// fn deferred(&self) -> MutexGuard<'_, VecDeque<Event<'a, Never>>> {
// self.deferred_events.lock().unwrap()
// }

fn redraw<'a>(&'a self) -> MutexGuard<'a, Vec<WindowId>> {
fn redraw(&self) -> MutexGuard<'_, Vec<WindowId>> {
self.pending_redraw.lock().unwrap()
}

fn waker<'a>(&'a self) -> MutexGuard<'a, EventLoopWaker> {
fn waker(&self) -> MutexGuard<'_, EventLoopWaker> {
self.waker.lock().unwrap()
}

Expand Down Expand Up @@ -144,13 +147,14 @@ impl Handler {
*self.start_time.lock().unwrap() = Some(Instant::now());
}

fn take_events(&self) -> VecDeque<Event<Never>> {
fn take_events(&self) -> VecDeque<Event<'_, Never>> {
mem::replace(&mut *self.events(), Default::default())
}

fn take_deferred(&self) -> VecDeque<Event<Never>> {
mem::replace(&mut *self.deferred(), Default::default())
}
// We don't need it any more.
// fn take_deferred(&self) -> VecDeque<Event<'_, Never>> {
// mem::replace(&mut *self.deferred(), Default::default())
// }

fn should_redraw(&self) -> Vec<WindowId> {
mem::replace(&mut *self.redraw(), Default::default())
Expand All @@ -164,7 +168,7 @@ impl Handler {
self.in_callback.store(in_callback, Ordering::Release);
}

fn handle_nonuser_event(&self, event: Event<Never>) {
fn handle_nonuser_event(&self, event: Event<'_, Never>) {
if let Some(ref mut callback) = *self.callback.lock().unwrap() {
callback.handle_nonuser_event(event, &mut *self.control_flow.lock().unwrap());
}
Expand All @@ -182,7 +186,7 @@ pub enum AppState {}
impl AppState {
pub fn set_callback<F, T>(callback: F, window_target: RootWindowTarget<T>)
where
F: 'static + FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
F: 'static + FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow),
T: 'static,
{
*HANDLER.callback.lock().unwrap() = Some(Box::new(EventLoopHandler {
Expand Down Expand Up @@ -245,30 +249,33 @@ impl AppState {
}
}

pub fn queue_event(event: Event<Never>) {
pub fn queue_event(event: Event<'static, Never>) {
if !unsafe { msg_send![class!(NSThread), isMainThread] } {
panic!("Event queued from different thread: {:#?}", event);
}
HANDLER.events().push_back(event);
}

pub fn queue_events(mut events: VecDeque<Event<Never>>) {
pub fn queue_events(mut events: VecDeque<Event<'static, Never>>) {
if !unsafe { msg_send![class!(NSThread), isMainThread] } {
panic!("Events queued from different thread: {:#?}", events);
}
HANDLER.events().append(&mut events);
}

pub fn send_event_immediately(event: Event<Never>) {
pub fn send_event_immediately(event: Event<'_, Never>) {
if !unsafe { msg_send![class!(NSThread), isMainThread] } {
panic!("Event sent from different thread: {:#?}", event);
}
HANDLER.deferred().push_back(event);
// HANDLER.deferred().push_back(event); // Commenting this out due to unnecessity
// We check that events are sent here from the same thread so we do not need to process
// asynchronously, thus we do not need a VecDeque here
if !HANDLER.get_in_callback() {
HANDLER.set_in_callback(true);
for event in HANDLER.take_deferred() {
HANDLER.handle_nonuser_event(event);
}
// Since we do not push_back, we don't need to take_deferred
// for event in HANDLER.take_deferred() {
HANDLER.handle_nonuser_event(event);
// }
HANDLER.set_in_callback(false);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/macos/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ pub unsafe fn modifier_event(
ns_event: id,
keymask: NSEventModifierFlags,
was_key_pressed: bool,
) -> Option<WindowEvent> {
) -> Option<WindowEvent<'static>> {
if !was_key_pressed && NSEvent::modifierFlags(ns_event).contains(keymask)
|| was_key_pressed && !NSEvent::modifierFlags(ns_event).contains(keymask)
{
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<T> EventLoop<T> {

pub fn run<F>(self, callback: F) -> !
where
F: 'static + FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
F: 'static + FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow),
{
unsafe {
let _pool = NSAutoreleasePool::new(nil);
Expand All @@ -98,7 +98,7 @@ impl<T> EventLoop<T> {

pub fn run_return<F>(&mut self, _callback: F)
where
F: FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
F: FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow),
{
unimplemented!();
}
Expand Down
Loading