Skip to content

Commit 262bf7d

Browse files
vbogaevskyOsspial
authored andcommitted
macOS: Dpi overhaul (#997) (and rebase changes) * WIP - Make EL2 DPI changes and implement on Windows (#895) * Modify DPI API publicly and on Windows * Add generic Position and make dpi creation functions const * Make examples work * Fix fullscreen windows not appearing * Replace Logical coordinates in window events with Physical coordinates * Update HiDpiFactorChanged * Document to_static * fix app_state errors * fixes hidpi related errors in window_delegate * fix bad merge * dpi_factor edits in window_delegate * fixes type and lifetime errors in window and window_delegate * applies fmt * complies with @aleksijuvani requested changes * modifies Handler lifetimes * fixes lifetime isues, adds propper handling for HiDpiChanged * applies fmt * restore original lifetimes * solution is somewhere out there * applies fmt * pass as references * resolves issue with HANDLER * crate visible type error * fixes visibility issues * applies fmt * deals with warnings * simplifies new_inner_size setting algorthm * moves proxy instead of referencing it and removes double deref from proxy.ns_window * makes @Osspial tests (https://github.com/rust-windowing/winit/pull/997\#discussion_r301852354) pass * complies with @aleksijuvani suggested changes * makes max window size std::f32::MAX Changes from rebasing: * fixes compile errors * applies fmt * reimplements HiDpiFactorChanged after #1173 merge * uses EventWrappers
1 parent 202232f commit 262bf7d

19 files changed

+304
-193
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ cocoa = "0.18.4"
3636
core-foundation = "0.6"
3737
core-graphics = "0.17.3"
3838
dispatch = "0.1.4"
39-
objc = "0.2.3"
39+
objc = "0.2.6"
4040

4141
[target.'cfg(target_os = "macos")'.dependencies.core-video-sys]
4242
version = "0.1.3"

examples/multithreaded.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ extern crate env_logger;
22
use std::{collections::HashMap, sync::mpsc, thread, time::Duration};
33

44
use winit::{
5-
dpi::{PhysicalPosition, PhysicalSize},
5+
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
66
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
77
event_loop::{ControlFlow, EventLoop},
88
window::{CursorIcon, Fullscreen, WindowBuilder},
99
};
1010

1111
const WINDOW_COUNT: usize = 3;
12-
const WINDOW_SIZE: PhysicalSize = PhysicalSize::new(600, 400);
12+
const WINDOW_SIZE: Size = Size::Physical(PhysicalSize::new(600, 400));
1313

1414
fn main() {
1515
env_logger::init();
@@ -114,18 +114,30 @@ fn main() {
114114
Q => window.request_redraw(),
115115
R => window.set_resizable(state),
116116
S => window.set_inner_size(match state {
117-
true => PhysicalSize::new(
118-
WINDOW_SIZE.width + 100,
119-
WINDOW_SIZE.height + 100,
120-
),
117+
true => {
118+
if let Size::Physical(size) = WINDOW_SIZE {
119+
Size::Physical(PhysicalSize::new(
120+
size.width + 100,
121+
size.height + 100,
122+
))
123+
} else {
124+
WINDOW_SIZE
125+
}
126+
}
121127
false => WINDOW_SIZE,
122128
}),
123-
W => window
124-
.set_cursor_position(PhysicalPosition::new(
125-
WINDOW_SIZE.width as f64 / 2.0,
126-
WINDOW_SIZE.height as f64 / 2.0,
127-
))
128-
.unwrap(),
129+
W => {
130+
if let Size::Physical(size) = WINDOW_SIZE {
131+
window
132+
.set_cursor_position(Position::Physical(
133+
PhysicalPosition::new(
134+
size.width as f64 / 2.0,
135+
size.height as f64 / 2.0,
136+
),
137+
))
138+
.unwrap()
139+
}
140+
}
129141
Z => {
130142
window.set_visible(false);
131143
thread::sleep(Duration::from_secs(1));

src/event.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'a> WindowEvent<'a> {
246246
ReceivedCharacter(c) => Some(ReceivedCharacter(c)),
247247
Focused(focused) => Some(Focused(focused)),
248248
KeyboardInput { device_id, input } => Some(KeyboardInput { device_id, input }),
249-
ModifiersChanged { modifiers } => Some(ModifiersChanged{ modifiers }),
249+
ModifiersChanged { modifiers } => Some(ModifiersChanged { modifiers }),
250250
CursorMoved {
251251
device_id,
252252
position,

src/platform_impl/ios/view.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,9 @@ pub unsafe fn create_window(
500500
let () = msg_send![uiscreen, setCurrentMode: video_mode.video_mode.screen_mode];
501501
msg_send![window, setScreen:video_mode.monitor().ui_screen()]
502502
}
503-
Some(Fullscreen::Borderless(ref monitor)) => msg_send![window, setScreen:monitor.ui_screen()],
503+
Some(Fullscreen::Borderless(ref monitor)) => {
504+
msg_send![window, setScreen:monitor.ui_screen()]
505+
}
504506
None => (),
505507
}
506508

src/platform_impl/linux/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use raw_window_handle::RawWindowHandle;
77
use smithay_client_toolkit::reexports::client::ConnectError;
88

99
pub use self::x11::XNotSupported;
10-
use self::x11::{
11-
ffi::XVisualInfo, util::WindowType as XWindowType, XConnection, XError,
12-
};
10+
use self::x11::{ffi::XVisualInfo, util::WindowType as XWindowType, XConnection, XError};
1311
use crate::{
1412
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
1513
error::{ExternalError, NotSupportedError, OsError as RootOsError},
@@ -588,9 +586,7 @@ impl<T: 'static> EventLoop<T> {
588586
pub fn primary_monitor(&self) -> MonitorHandle {
589587
match *self {
590588
EventLoop::Wayland(ref evlp) => MonitorHandle::Wayland(evlp.primary_monitor()),
591-
EventLoop::X(ref evlp) => {
592-
MonitorHandle::X(evlp.x_connection().primary_monitor())
593-
}
589+
EventLoop::X(ref evlp) => MonitorHandle::X(evlp.x_connection().primary_monitor()),
594590
}
595591
}
596592

src/platform_impl/linux/wayland/event_loop.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ use smithay_client_toolkit::reexports::client::protocol::{
2727

2828
use crate::{
2929
dpi::{LogicalSize, PhysicalPosition, PhysicalSize},
30-
event::{DeviceEvent, DeviceId as RootDeviceId, Event, ModifiersState, StartCause, WindowEvent},
30+
event::{
31+
DeviceEvent, DeviceId as RootDeviceId, Event, ModifiersState, StartCause, WindowEvent,
32+
},
3133
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootELW},
3234
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
3335
platform_impl::platform::{
34-
sticky_exit_callback,
35-
DeviceId as PlatformDeviceId,
36-
MonitorHandle as PlatformMonitorHandle,
37-
VideoMode as PlatformVideoMode,
38-
WindowId as PlatformWindowId,
36+
sticky_exit_callback, DeviceId as PlatformDeviceId, MonitorHandle as PlatformMonitorHandle,
37+
VideoMode as PlatformVideoMode, WindowId as PlatformWindowId,
3938
},
4039
window::{CursorIcon, WindowId as RootWindowId},
4140
};
@@ -473,12 +472,7 @@ impl<T: 'static> EventLoop<T> {
473472

474473
while let Ok(event) = self.kbd_channel.try_recv() {
475474
let event = event.map_nonuser_event().unwrap();
476-
sticky_exit_callback(
477-
event,
478-
&self.window_target,
479-
&mut control_flow,
480-
&mut callback,
481-
);
475+
sticky_exit_callback(event, &self.window_target, &mut control_flow, &mut callback);
482476
}
483477

484478
while let Ok(event) = self.user_channel.try_recv() {

src/platform_impl/linux/wayland/keyboard.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ pub fn init_keyboard(
8585
*modifiers_tracker.lock().unwrap() = modifiers;
8686

8787
if let Some(wid) = *target.lock().unwrap() {
88-
my_sink
89-
.send_window_event(WindowEvent::ModifiersChanged { modifiers }, wid);
88+
my_sink.send_window_event(WindowEvent::ModifiersChanged { modifiers }, wid);
9089
}
9190
}
9291
}

src/platform_impl/linux/wayland/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
target_os = "netbsd", target_os = "openbsd"))]
33

44
pub use self::{
5-
event_loop::{
6-
EventLoop, EventLoopProxy, EventLoopWindowTarget, MonitorHandle, VideoMode,
7-
},
5+
event_loop::{EventLoop, EventLoopProxy, EventLoopWindowTarget, MonitorHandle, VideoMode},
86
window::Window,
97
};
108

src/platform_impl/linux/wayland/pointer.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,11 @@ pub fn implement_relative_pointer(
243243
) -> Result<ZwpRelativePointerV1, ()> {
244244
manager.get_relative_pointer(pointer, |rel_pointer| {
245245
rel_pointer.implement_closure(
246-
move |evt, _rel_pointer| {
247-
match evt {
248-
Event::RelativeMotion { dx, dy, .. } => sink
249-
.send_device_event(DeviceEvent::MouseMotion { delta: (dx, dy) }, DeviceId),
250-
_ => unreachable!(),
246+
move |evt, _rel_pointer| match evt {
247+
Event::RelativeMotion { dx, dy, .. } => {
248+
sink.send_device_event(DeviceEvent::MouseMotion { delta: (dx, dy) }, DeviceId)
251249
}
250+
_ => unreachable!(),
252251
},
253252
(),
254253
)

src/platform_impl/macos/app.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use objc::{
1111

1212
use crate::{
1313
event::{DeviceEvent, ElementState, Event},
14-
platform_impl::platform::{app_state::AppState, util, DEVICE_ID},
14+
platform_impl::platform::{app_state::AppState, event::EventWrapper, util, DEVICE_ID},
1515
};
1616

1717
pub struct AppClass(pub *const Class);
@@ -71,59 +71,59 @@ unsafe fn maybe_dispatch_device_event(event: id) {
7171
let delta_y = event.deltaY() as f64;
7272

7373
if delta_x != 0.0 {
74-
events.push_back(Event::DeviceEvent {
74+
events.push_back(EventWrapper::StaticEvent(Event::DeviceEvent {
7575
device_id: DEVICE_ID,
7676
event: DeviceEvent::Motion {
7777
axis: 0,
7878
value: delta_x,
7979
},
80-
});
80+
}));
8181
}
8282

8383
if delta_y != 0.0 {
84-
events.push_back(Event::DeviceEvent {
84+
events.push_back(EventWrapper::StaticEvent(Event::DeviceEvent {
8585
device_id: DEVICE_ID,
8686
event: DeviceEvent::Motion {
8787
axis: 1,
8888
value: delta_y,
8989
},
90-
});
90+
}));
9191
}
9292

9393
if delta_x != 0.0 || delta_y != 0.0 {
94-
events.push_back(Event::DeviceEvent {
94+
events.push_back(EventWrapper::StaticEvent(Event::DeviceEvent {
9595
device_id: DEVICE_ID,
9696
event: DeviceEvent::MouseMotion {
9797
delta: (delta_x, delta_y),
9898
},
99-
});
99+
}));
100100
}
101101

102102
AppState::queue_events(events);
103103
}
104104
appkit::NSLeftMouseDown | appkit::NSRightMouseDown | appkit::NSOtherMouseDown => {
105105
let mut events = VecDeque::with_capacity(1);
106106

107-
events.push_back(Event::DeviceEvent {
107+
events.push_back(EventWrapper::StaticEvent(Event::DeviceEvent {
108108
device_id: DEVICE_ID,
109109
event: DeviceEvent::Button {
110110
button: event.buttonNumber() as u32,
111111
state: ElementState::Pressed,
112112
},
113-
});
113+
}));
114114

115115
AppState::queue_events(events);
116116
}
117117
appkit::NSLeftMouseUp | appkit::NSRightMouseUp | appkit::NSOtherMouseUp => {
118118
let mut events = VecDeque::with_capacity(1);
119119

120-
events.push_back(Event::DeviceEvent {
120+
events.push_back(EventWrapper::StaticEvent(Event::DeviceEvent {
121121
device_id: DEVICE_ID,
122122
event: DeviceEvent::Button {
123123
button: event.buttonNumber() as u32,
124124
state: ElementState::Released,
125125
},
126-
});
126+
}));
127127

128128
AppState::queue_events(events);
129129
}

0 commit comments

Comments
 (0)