refactor(window): update to latest winit, glutin interface

This commit is contained in:
OJ Kwon 2018-06-29 13:37:58 -07:00 committed by UK992
parent 2bc1fc8b3b
commit 679af055e2
2 changed files with 28 additions and 23 deletions

View file

@ -295,7 +295,6 @@ pub fn is_printable(key_code: VirtualKeyCode) -> bool {
Back | Back |
LAlt | LAlt |
LControl | LControl |
LMenu |
LShift | LShift |
LWin | LWin |
Mail | Mail |
@ -312,7 +311,6 @@ pub fn is_printable(key_code: VirtualKeyCode) -> bool {
PrevTrack | PrevTrack |
RAlt | RAlt |
RControl | RControl |
RMenu |
RShift | RShift |
RWin | RWin |
Sleep | Sleep |

View file

@ -38,6 +38,7 @@ use user32;
use winapi; use winapi;
use winit; use winit;
use winit::{ElementState, Event, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode}; use winit::{ElementState, Event, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
use winit::dpi::{LogicalPosition, LogicalSize, PhysicalSize};
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
use winit::os::macos::{ActivationPolicy, WindowBuilderExt}; use winit::os::macos::{ActivationPolicy, WindowBuilderExt};
@ -173,8 +174,10 @@ fn window_creation_scale_factor() -> TypedScale<f32, DeviceIndependentPixel, Dev
impl Window { impl Window {
pub fn new(is_foreground: bool, pub fn new(
window_size: TypedSize2D<u32, DeviceIndependentPixel>) -> Rc<Window> { is_foreground: bool,
window_size: TypedSize2D<u32, DeviceIndependentPixel>,
) -> Rc<Window> {
let win_size: DeviceUintSize = (window_size.to_f32() * window_creation_scale_factor()).to_u32(); let win_size: DeviceUintSize = (window_size.to_f32() * window_creation_scale_factor()).to_u32();
let width = win_size.to_untyped().width; let width = win_size.to_untyped().width;
let height = win_size.to_untyped().height; let height = win_size.to_untyped().height;
@ -197,7 +200,7 @@ impl Window {
.with_title("Servo".to_string()) .with_title("Servo".to_string())
.with_decorations(!opts::get().no_native_titlebar) .with_decorations(!opts::get().no_native_titlebar)
.with_transparency(opts::get().no_native_titlebar) .with_transparency(opts::get().no_native_titlebar)
.with_dimensions(width, height) .with_dimensions(LogicalSize::new(width as f64, height as f64))
.with_visibility(visible) .with_visibility(visible)
.with_multitouch(); .with_multitouch();
@ -225,11 +228,15 @@ impl Window {
glutin_window.context().make_current().expect("Couldn't make window current"); glutin_window.context().make_current().expect("Couldn't make window current");
} }
let (screen_width, screen_height) = events_loop.get_primary_monitor().get_dimensions(); let PhysicalSize {
screen_size = TypedSize2D::new(screen_width, screen_height); width: screen_width,
height: screen_height,
} = events_loop.get_primary_monitor().get_dimensions();
screen_size = TypedSize2D::new(screen_width as u32, screen_height as u32);
// TODO(ajeffrey): can this fail? // TODO(ajeffrey): can this fail?
let (width, height) = glutin_window.get_inner_size().expect("Failed to get window inner size."); let LogicalSize { width, height } =
inner_size = TypedSize2D::new(width, height); glutin_window.get_inner_size().expect("Failed to get window inner size.");
inner_size = TypedSize2D::new(width as u32, height as u32);
glutin_window.show(); glutin_window.show();
@ -301,8 +308,8 @@ impl Window {
let dpr = self.hidpi_factor(); let dpr = self.hidpi_factor();
match self.kind { match self.kind {
WindowKind::Window(ref window, _) => { WindowKind::Window(ref window, _) => {
let (_, height) = window.get_inner_size().expect("Failed to get window inner size."); let size = window.get_inner_size().expect("Failed to get window inner size.");
height as f32 * dpr.get() size.height as f32 * dpr.get()
}, },
WindowKind::Headless(ref context) => { WindowKind::Headless(ref context) => {
context.height as f32 * dpr.get() context.height as f32 * dpr.get()
@ -319,14 +326,14 @@ impl Window {
pub fn set_inner_size(&self, size: DeviceUintSize) { pub fn set_inner_size(&self, size: DeviceUintSize) {
if let WindowKind::Window(ref window, _) = self.kind { if let WindowKind::Window(ref window, _) = self.kind {
let size = size.to_f32() / self.hidpi_factor(); let size = size.to_f32() / self.hidpi_factor();
window.set_inner_size(size.width as u32, size.height as u32) window.set_inner_size(LogicalSize::new(size.width.into(), size.height.into()))
} }
} }
pub fn set_position(&self, point: DeviceIntPoint) { pub fn set_position(&self, point: DeviceIntPoint) {
if let WindowKind::Window(ref window, _) = self.kind { if let WindowKind::Window(ref window, _) = self.kind {
let point = point.to_f32() / self.hidpi_factor(); let point = point.to_f32() / self.hidpi_factor();
window.set_position(point.x as i32, point.y as i32) window.set_position(LogicalPosition::new(point.x.into(), point.y.into()))
} }
} }
@ -486,7 +493,7 @@ impl Window {
}, },
Event::WindowEvent { Event::WindowEvent {
event: winit::WindowEvent::CursorMoved { event: winit::WindowEvent::CursorMoved {
position: (x, y), position: LogicalPosition { x, y },
.. ..
}, },
.. ..
@ -501,7 +508,7 @@ impl Window {
} => { } => {
let (mut dx, mut dy) = match delta { let (mut dx, mut dy) = match delta {
MouseScrollDelta::LineDelta(dx, dy) => (dx, dy * LINE_HEIGHT), MouseScrollDelta::LineDelta(dx, dy) => (dx, dy * LINE_HEIGHT),
MouseScrollDelta::PixelDelta(dx, dy) => (dx, dy), MouseScrollDelta::PixelDelta(LogicalPosition { x: dx, y: dy }) => (dx as f32, dy as f32),
}; };
// Scroll events snap to the major axis of movement, with vertical // Scroll events snap to the major axis of movement, with vertical
// preferred over horizontal. // preferred over horizontal.
@ -524,7 +531,7 @@ impl Window {
let phase = winit_phase_to_touch_event_type(touch.phase); let phase = winit_phase_to_touch_event_type(touch.phase);
let id = TouchId(touch.id as i32); let id = TouchId(touch.id as i32);
let point = TypedPoint2D::new(touch.location.0 as f32, touch.location.1 as f32); let point = TypedPoint2D::new(touch.location.x as f32, touch.location.y as f32);
self.event_queue.borrow_mut().push(WindowEvent::Touch(phase, id, point)); self.event_queue.borrow_mut().push(WindowEvent::Touch(phase, id, point));
} }
Event::WindowEvent { Event::WindowEvent {
@ -538,13 +545,13 @@ impl Window {
self.event_queue.borrow_mut().push(WindowEvent::Quit); self.event_queue.borrow_mut().push(WindowEvent::Quit);
} }
Event::WindowEvent { Event::WindowEvent {
event: winit::WindowEvent::Resized(width, height), event: winit::WindowEvent::Resized(LogicalSize { width, height }),
.. ..
} => { } => {
// width and height are DevicePixel. // width and height are DevicePixel.
// window.resize() takes DevicePixel. // window.resize() takes DevicePixel.
if let WindowKind::Window(ref window, _) = self.kind { if let WindowKind::Window(ref window, _) = self.kind {
window.resize(width, height); window.resize(PhysicalSize { width, height });
} }
// window.set_inner_size() takes DeviceIndependentPixel. // window.set_inner_size() takes DeviceIndependentPixel.
let new_size = TypedSize2D::new(width as f32, height as f32); let new_size = TypedSize2D::new(width as f32, height as f32);
@ -622,7 +629,7 @@ impl Window {
fn platform_hidpi_factor(&self) -> TypedScale<f32, DeviceIndependentPixel, DevicePixel> { fn platform_hidpi_factor(&self) -> TypedScale<f32, DeviceIndependentPixel, DevicePixel> {
match self.kind { match self.kind {
WindowKind::Window(ref window, ..) => { WindowKind::Window(ref window, ..) => {
TypedScale::new(window.hidpi_factor()) TypedScale::new(window.get_hidpi_factor() as f32)
} }
WindowKind::Headless(..) => { WindowKind::Headless(..) => {
TypedScale::new(1.0) TypedScale::new(1.0)
@ -645,7 +652,6 @@ impl Window {
let winit_cursor = match cursor { let winit_cursor = match cursor {
CursorKind::Auto => MouseCursor::Default, CursorKind::Auto => MouseCursor::Default,
CursorKind::None => MouseCursor::NoneCursor,
CursorKind::Default => MouseCursor::Default, CursorKind::Default => MouseCursor::Default,
CursorKind::Pointer => MouseCursor::Hand, CursorKind::Pointer => MouseCursor::Hand,
CursorKind::ContextMenu => MouseCursor::ContextMenu, CursorKind::ContextMenu => MouseCursor::ContextMenu,
@ -680,6 +686,7 @@ impl Window {
CursorKind::AllScroll => MouseCursor::AllScroll, CursorKind::AllScroll => MouseCursor::AllScroll,
CursorKind::ZoomIn => MouseCursor::ZoomIn, CursorKind::ZoomIn => MouseCursor::ZoomIn,
CursorKind::ZoomOut => MouseCursor::ZoomOut, CursorKind::ZoomOut => MouseCursor::ZoomOut,
_ => MouseCursor::Default
}; };
window.set_cursor(winit_cursor); window.set_cursor(winit_cursor);
} }
@ -698,13 +705,13 @@ impl WindowMethods for Window {
match self.kind { match self.kind {
WindowKind::Window(ref window, _) => { WindowKind::Window(ref window, _) => {
// TODO(ajeffrey): can this fail? // TODO(ajeffrey): can this fail?
let (width, height) = window.get_outer_size().expect("Failed to get window outer size."); let LogicalSize { width, height } = window.get_outer_size().expect("Failed to get window outer size.");
let (x, y) = window.get_position().unwrap_or((0, 0)); let LogicalPosition { x, y } = window.get_position().unwrap_or(LogicalPosition::new(0., 0.));
let win_size = (TypedSize2D::new(width as f32, height as f32) * dpr).to_u32(); let win_size = (TypedSize2D::new(width as f32, height as f32) * dpr).to_u32();
let win_origin = (TypedPoint2D::new(x as f32, y as f32) * dpr).to_i32(); let win_origin = (TypedPoint2D::new(x as f32, y as f32) * dpr).to_i32();
let screen = (self.screen_size.to_f32() * dpr).to_u32(); let screen = (self.screen_size.to_f32() * dpr).to_u32();
let (width, height) = window.get_inner_size().expect("Failed to get window inner size."); let LogicalSize { width, height } = window.get_inner_size().expect("Failed to get window inner size.");
let inner_size = (TypedSize2D::new(width as f32, height as f32) * dpr).to_u32(); let inner_size = (TypedSize2D::new(width as f32, height as f32) * dpr).to_u32();
let viewport = DeviceUintRect::new(TypedPoint2D::zero(), inner_size); let viewport = DeviceUintRect::new(TypedPoint2D::zero(), inner_size);