Skip to content

Commit

Permalink
macOS: Dpi overhaul (#997) (and rebase changes)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
vbogaevsky authored and Osspial committed Oct 23, 2019
1 parent a904386 commit 98bf37a
Show file tree
Hide file tree
Showing 19 changed files with 299 additions and 192 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ cocoa = "0.19.1"
core-foundation = "0.6"
core-graphics = "0.17.3"
dispatch = "0.1.4"
objc = "0.2.3"
objc = "0.2.6"

[target.'cfg(target_os = "macos")'.dependencies.core-video-sys]
version = "0.1.3"
Expand Down
30 changes: 19 additions & 11 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() {
use std::{collections::HashMap, sync::mpsc, thread, time::Duration};

use winit::{
dpi::{PhysicalPosition, PhysicalSize},
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{CursorIcon, Fullscreen, WindowBuilder},
Expand Down Expand Up @@ -116,18 +116,26 @@ fn main() {
Q => window.request_redraw(),
R => window.set_resizable(state),
S => window.set_inner_size(match state {
true => PhysicalSize::new(
WINDOW_SIZE.width + 100,
WINDOW_SIZE.height + 100,
),
true => {
PhysicalSize::new(
WINDOW_SIZE.width + 100,
WINDOW_SIZE.height + 100,
)
}
false => WINDOW_SIZE,
}),
W => window
.set_cursor_position(PhysicalPosition::new(
WINDOW_SIZE.width as f64 / 2.0,
WINDOW_SIZE.height as f64 / 2.0,
))
.unwrap(),
W => {
if let Size::Physical(size) = WINDOW_SIZE.into() {
window
.set_cursor_position(Position::Physical(
PhysicalPosition::new(
size.width as f64 / 2.0,
size.height as f64 / 2.0,
),
))
.unwrap()
}
}
Z => {
window.set_visible(false);
thread::sleep(Duration::from_secs(1));
Expand Down
2 changes: 1 addition & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl<'a> WindowEvent<'a> {
ReceivedCharacter(c) => Some(ReceivedCharacter(c)),
Focused(focused) => Some(Focused(focused)),
KeyboardInput { device_id, input } => Some(KeyboardInput { device_id, input }),
ModifiersChanged { modifiers } => Some(ModifiersChanged{ modifiers }),
ModifiersChanged { modifiers } => Some(ModifiersChanged { modifiers }),
CursorMoved {
device_id,
position,
Expand Down
4 changes: 3 additions & 1 deletion src/platform_impl/ios/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,9 @@ pub unsafe fn create_window(
let () = msg_send![uiscreen, setCurrentMode: video_mode.video_mode.screen_mode];
msg_send![window, setScreen:video_mode.monitor().ui_screen()]
}
Some(Fullscreen::Borderless(ref monitor)) => msg_send![window, setScreen:monitor.ui_screen()],
Some(Fullscreen::Borderless(ref monitor)) => {
msg_send![window, setScreen:monitor.ui_screen()]
}
None => (),
}

Expand Down
8 changes: 2 additions & 6 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use raw_window_handle::RawWindowHandle;
use smithay_client_toolkit::reexports::client::ConnectError;

pub use self::x11::XNotSupported;
use self::x11::{
ffi::XVisualInfo, util::WindowType as XWindowType, XConnection, XError,
};
use self::x11::{ffi::XVisualInfo, util::WindowType as XWindowType, XConnection, XError};
use crate::{
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
error::{ExternalError, NotSupportedError, OsError as RootOsError},
Expand Down Expand Up @@ -608,9 +606,7 @@ impl<T: 'static> EventLoop<T> {
pub fn primary_monitor(&self) -> MonitorHandle {
match *self {
EventLoop::Wayland(ref evlp) => MonitorHandle::Wayland(evlp.primary_monitor()),
EventLoop::X(ref evlp) => {
MonitorHandle::X(evlp.x_connection().primary_monitor())
}
EventLoop::X(ref evlp) => MonitorHandle::X(evlp.x_connection().primary_monitor()),
}
}

Expand Down
18 changes: 6 additions & 12 deletions src/platform_impl/linux/wayland/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ use smithay_client_toolkit::reexports::client::protocol::{

use crate::{
dpi::{LogicalSize, PhysicalPosition, PhysicalSize},
event::{DeviceEvent, DeviceId as RootDeviceId, Event, ModifiersState, StartCause, WindowEvent},
event::{
DeviceEvent, DeviceId as RootDeviceId, Event, ModifiersState, StartCause, WindowEvent,
},
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootELW},
monitor::{MonitorHandle as RootMonitorHandle, VideoMode as RootVideoMode},
platform_impl::platform::{
sticky_exit_callback,
DeviceId as PlatformDeviceId,
MonitorHandle as PlatformMonitorHandle,
VideoMode as PlatformVideoMode,
WindowId as PlatformWindowId,
sticky_exit_callback, DeviceId as PlatformDeviceId, MonitorHandle as PlatformMonitorHandle,
VideoMode as PlatformVideoMode, WindowId as PlatformWindowId,
},
window::{CursorIcon, WindowId as RootWindowId},
};
Expand Down Expand Up @@ -481,12 +480,7 @@ impl<T: 'static> EventLoop<T> {

while let Ok(event) = self.kbd_channel.try_recv() {
let event = event.map_nonuser_event().unwrap();
sticky_exit_callback(
event,
&self.window_target,
&mut control_flow,
&mut callback,
);
sticky_exit_callback(event, &self.window_target, &mut control_flow, &mut callback);
}

while let Ok(event) = self.user_channel.try_recv() {
Expand Down
3 changes: 1 addition & 2 deletions src/platform_impl/linux/wayland/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ pub fn init_keyboard(
*modifiers_tracker.lock().unwrap() = modifiers;

if let Some(wid) = *target.lock().unwrap() {
my_sink
.send_window_event(WindowEvent::ModifiersChanged { modifiers }, wid);
my_sink.send_window_event(WindowEvent::ModifiersChanged { modifiers }, wid);
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/platform_impl/linux/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
target_os = "netbsd", target_os = "openbsd"))]

pub use self::{
event_loop::{
EventLoop, EventLoopProxy, EventLoopWindowTarget, MonitorHandle, VideoMode,
},
event_loop::{EventLoop, EventLoopProxy, EventLoopWindowTarget, MonitorHandle, VideoMode},
window::Window,
};

Expand Down
9 changes: 4 additions & 5 deletions src/platform_impl/linux/wayland/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,11 @@ pub fn implement_relative_pointer(
) -> Result<ZwpRelativePointerV1, ()> {
manager.get_relative_pointer(pointer, |rel_pointer| {
rel_pointer.implement_closure(
move |evt, _rel_pointer| {
match evt {
Event::RelativeMotion { dx, dy, .. } => sink
.send_device_event(DeviceEvent::MouseMotion { delta: (dx, dy) }, DeviceId),
_ => unreachable!(),
move |evt, _rel_pointer| match evt {
Event::RelativeMotion { dx, dy, .. } => {
sink.send_device_event(DeviceEvent::MouseMotion { delta: (dx, dy) }, DeviceId)
}
_ => unreachable!(),
},
(),
)
Expand Down
22 changes: 11 additions & 11 deletions src/platform_impl/macos/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use objc::{

use crate::{
event::{DeviceEvent, ElementState, Event},
platform_impl::platform::{app_state::AppState, util, DEVICE_ID},
platform_impl::platform::{app_state::AppState, event::EventWrapper, util, DEVICE_ID},
};

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

if delta_x != 0.0 {
events.push_back(Event::DeviceEvent {
events.push_back(EventWrapper::StaticEvent(Event::DeviceEvent {
device_id: DEVICE_ID,
event: DeviceEvent::Motion {
axis: 0,
value: delta_x,
},
});
}));
}

if delta_y != 0.0 {
events.push_back(Event::DeviceEvent {
events.push_back(EventWrapper::StaticEvent(Event::DeviceEvent {
device_id: DEVICE_ID,
event: DeviceEvent::Motion {
axis: 1,
value: delta_y,
},
});
}));
}

if delta_x != 0.0 || delta_y != 0.0 {
events.push_back(Event::DeviceEvent {
events.push_back(EventWrapper::StaticEvent(Event::DeviceEvent {
device_id: DEVICE_ID,
event: DeviceEvent::MouseMotion {
delta: (delta_x, delta_y),
},
});
}));
}

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

events.push_back(Event::DeviceEvent {
events.push_back(EventWrapper::StaticEvent(Event::DeviceEvent {
device_id: DEVICE_ID,
event: DeviceEvent::Button {
button: event.buttonNumber() as u32,
state: ElementState::Pressed,
},
});
}));

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

events.push_back(Event::DeviceEvent {
events.push_back(EventWrapper::StaticEvent(Event::DeviceEvent {
device_id: DEVICE_ID,
event: DeviceEvent::Button {
button: event.buttonNumber() as u32,
state: ElementState::Released,
},
});
}));

AppState::queue_events(events);
}
Expand Down
Loading

0 comments on commit 98bf37a

Please sign in to comment.