diff options
Diffstat (limited to '')
-rw-r--r-- | winit/src/application.rs | 187 | ||||
-rw-r--r-- | winit/src/conversion.rs | 92 | ||||
-rw-r--r-- | winit/src/debug/null.rs | 2 | ||||
-rw-r--r-- | winit/src/lib.rs | 1 |
4 files changed, 146 insertions, 136 deletions
diff --git a/winit/src/application.rs b/winit/src/application.rs index 3c0332ed..35a36434 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -1,8 +1,7 @@ use crate::{ - conversion, - input::{keyboard, mouse}, - window, Cache, Clipboard, Command, Debug, Element, Event, Executor, Mode, - MouseCursor, Proxy, Runtime, Settings, Size, Subscription, UserInterface, + conversion, size::Size, window, Cache, Clipboard, Command, Debug, Element, + Executor, Mode, MouseCursor, Proxy, Runtime, Settings, Subscription, + UserInterface, }; /// An interactive, native cross-platform application. @@ -14,10 +13,10 @@ use crate::{ /// An [`Application`](trait.Application.html) can execute asynchronous actions /// by returning a [`Command`](struct.Command.html) in some of its methods. pub trait Application: Sized { - /// The renderer to use to draw the [`Application`]. + /// The graphics backend to use to draw the [`Application`]. /// /// [`Application`]: trait.Application.html - type Renderer: window::Renderer; + type Backend: window::Backend; /// The [`Executor`] that will run commands and subscriptions. /// @@ -75,7 +74,9 @@ pub trait Application: Sized { /// These widgets can produce __messages__ based on user interaction. /// /// [`Application`]: trait.Application.html - fn view(&mut self) -> Element<'_, Self::Message, Self::Renderer>; + fn view( + &mut self, + ) -> Element<'_, Self::Message, <Self::Backend as window::Backend>::Renderer>; /// Returns the current [`Application`] mode. /// @@ -99,11 +100,11 @@ pub trait Application: Sized { /// [`Application`]: trait.Application.html fn run( settings: Settings, - renderer_settings: <Self::Renderer as window::Renderer>::Settings, + backend_settings: <Self::Backend as window::Backend>::Settings, ) where Self: 'static, { - use window::{Renderer as _, Target as _}; + use window::Backend as _; use winit::{ event::{self, WindowEvent}, event_loop::{ControlFlow, EventLoop}, @@ -162,17 +163,17 @@ pub trait Application: Sized { let mut resized = false; let clipboard = Clipboard::new(&window); - let mut renderer = Self::Renderer::new(renderer_settings); + let (mut backend, mut renderer) = Self::Backend::new(backend_settings); - let mut target = { + let surface = backend.create_surface(&window); + + let mut swap_chain = { let physical_size = size.physical(); - <Self::Renderer as window::Renderer>::Target::new( - &window, + backend.create_swap_chain( + &surface, physical_size.width, physical_size.height, - size.scale_factor(), - &renderer, ) }; @@ -198,8 +199,7 @@ pub trait Application: Sized { event_loop.run(move |event, _, control_flow| match event { event::Event::MainEventsCleared => { - if events.is_empty() && external_messages.is_empty() && !resized - { + if events.is_empty() && external_messages.is_empty() { return; } @@ -223,11 +223,11 @@ pub trait Application: Sized { .for_each(|event| runtime.broadcast(event)); let mut messages = user_interface.update( - &renderer, + events.drain(..), clipboard .as_ref() .map(|c| c as &dyn iced_native::Clipboard), - events.drain(..), + &renderer, ); messages.extend(external_messages.drain(..)); debug.event_processing_finished(); @@ -306,18 +306,22 @@ pub trait Application: Sized { if resized { let physical_size = size.physical(); - target.resize( + swap_chain = backend.create_swap_chain( + &surface, physical_size.width, physical_size.height, - size.scale_factor(), - &renderer, ); resized = false; } - let new_mouse_cursor = - renderer.draw(&primitive, &debug.overlay(), &mut target); + let new_mouse_cursor = backend.draw( + &mut renderer, + &mut swap_chain, + &primitive, + size.scale_factor(), + &debug.overlay(), + ); debug.render_finished(); @@ -335,106 +339,37 @@ pub trait Application: Sized { event::Event::WindowEvent { event: window_event, .. - } => match window_event { - WindowEvent::Resized(new_size) => { - size = Size::new(new_size, size.scale_factor()); - - events.push(Event::Window(window::Event::Resized { - width: size.logical().width.round() as u32, - height: size.logical().height.round() as u32, - })); - - resized = true; - } - WindowEvent::CloseRequested => { - *control_flow = ControlFlow::Exit; - } - WindowEvent::CursorMoved { position, .. } => { - let position = - position.to_logical::<f64>(size.scale_factor()); - - events.push(Event::Mouse(mouse::Event::CursorMoved { - x: position.x as f32, - y: position.y as f32, - })); - } - WindowEvent::MouseInput { button, state, .. } => { - events.push(Event::Mouse(mouse::Event::Input { - button: conversion::mouse_button(button), - state: conversion::button_state(state), - })); - } - WindowEvent::MouseWheel { delta, .. } => match delta { - winit::event::MouseScrollDelta::LineDelta( - delta_x, - delta_y, - ) => { - events.push(Event::Mouse( - mouse::Event::WheelScrolled { - delta: mouse::ScrollDelta::Lines { - x: delta_x, - y: delta_y, - }, - }, - )); + } => { + match window_event { + WindowEvent::Resized(new_size) => { + size = Size::new(new_size, window.scale_factor()); + resized = true; } - winit::event::MouseScrollDelta::PixelDelta(position) => { - events.push(Event::Mouse( - mouse::Event::WheelScrolled { - delta: mouse::ScrollDelta::Pixels { - x: position.x as f32, - y: position.y as f32, - }, - }, - )); + WindowEvent::CloseRequested => { + *control_flow = ControlFlow::Exit; } - }, - WindowEvent::ReceivedCharacter(c) - if !is_private_use_character(c) => - { - events.push(Event::Keyboard( - keyboard::Event::CharacterReceived(c), - )); + #[cfg(feature = "debug")] + WindowEvent::KeyboardInput { + input: + winit::event::KeyboardInput { + virtual_keycode: + Some(winit::event::VirtualKeyCode::F12), + state: winit::event::ElementState::Pressed, + .. + }, + .. + } => debug.toggle(), + _ => {} } - WindowEvent::KeyboardInput { - input: - winit::event::KeyboardInput { - virtual_keycode: Some(virtual_keycode), - state, - .. - }, - .. - } => { - match (virtual_keycode, state) { - ( - winit::event::VirtualKeyCode::F12, - winit::event::ElementState::Pressed, - ) => debug.toggle(), - _ => {} - } - events.push(Event::Keyboard(keyboard::Event::Input { - key_code: conversion::key_code(virtual_keycode), - state: conversion::button_state(state), - modifiers: conversion::modifiers_state(modifiers), - })); - } - WindowEvent::HoveredFile(path) => { - events - .push(Event::Window(window::Event::FileHovered(path))); + if let Some(event) = conversion::window_event( + window_event, + size.scale_factor(), + modifiers, + ) { + events.push(event); } - WindowEvent::DroppedFile(path) => { - events - .push(Event::Window(window::Event::FileDropped(path))); - } - WindowEvent::HoveredFileCancelled => { - events.push(Event::Window(window::Event::FilesHoveredLeft)); - } - WindowEvent::ScaleFactorChanged { scale_factor, .. } => { - size = Size::new(size.physical(), scale_factor); - } - _ => {} - }, + } event::Event::DeviceEvent { event: event::DeviceEvent::ModifiersChanged(new_modifiers), .. @@ -451,10 +386,10 @@ pub trait Application: Sized { fn build_user_interface<'a, A: Application>( application: &'a mut A, cache: Cache, - renderer: &mut A::Renderer, + renderer: &mut <A::Backend as window::Backend>::Renderer, size: winit::dpi::LogicalSize<f64>, debug: &mut Debug, -) -> UserInterface<'a, A::Message, A::Renderer> { +) -> UserInterface<'a, A::Message, <A::Backend as window::Backend>::Renderer> { debug.view_started(); let view = application.view(); debug.view_finished(); @@ -473,13 +408,3 @@ fn build_user_interface<'a, A: Application>( user_interface } - -// As defined in: http://www.unicode.org/faq/private_use.html -fn is_private_use_character(c: char) -> bool { - match c { - '\u{E000}'..='\u{F8FF}' - | '\u{F0000}'..='\u{FFFFD}' - | '\u{100000}'..='\u{10FFFD}' => true, - _ => false, - } -} diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index af0c4c9f..b6a0b64b 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -4,12 +4,90 @@ //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native use crate::{ input::{ - keyboard::{KeyCode, ModifiersState}, + keyboard::{self, KeyCode, ModifiersState}, mouse, ButtonState, }, - Mode, MouseCursor, + window, Event, Mode, MouseCursor, }; +/// Converts a winit window event into an iced event. +pub fn window_event( + event: winit::event::WindowEvent<'_>, + scale_factor: f64, + modifiers: winit::event::ModifiersState, +) -> Option<Event> { + use winit::event::WindowEvent; + + match event { + WindowEvent::Resized(new_size) => { + let logical_size = new_size.to_logical(scale_factor); + + Some(Event::Window(window::Event::Resized { + width: logical_size.width, + height: logical_size.height, + })) + } + WindowEvent::CursorMoved { position, .. } => { + let position = position.to_logical::<f64>(scale_factor); + + Some(Event::Mouse(mouse::Event::CursorMoved { + x: position.x as f32, + y: position.y as f32, + })) + } + WindowEvent::MouseInput { button, state, .. } => { + Some(Event::Mouse(mouse::Event::Input { + button: mouse_button(button), + state: button_state(state), + })) + } + WindowEvent::MouseWheel { delta, .. } => match delta { + winit::event::MouseScrollDelta::LineDelta(delta_x, delta_y) => { + Some(Event::Mouse(mouse::Event::WheelScrolled { + delta: mouse::ScrollDelta::Lines { + x: delta_x, + y: delta_y, + }, + })) + } + winit::event::MouseScrollDelta::PixelDelta(position) => { + Some(Event::Mouse(mouse::Event::WheelScrolled { + delta: mouse::ScrollDelta::Pixels { + x: position.x as f32, + y: position.y as f32, + }, + })) + } + }, + WindowEvent::ReceivedCharacter(c) if !is_private_use_character(c) => { + Some(Event::Keyboard(keyboard::Event::CharacterReceived(c))) + } + WindowEvent::KeyboardInput { + input: + winit::event::KeyboardInput { + virtual_keycode: Some(virtual_keycode), + state, + .. + }, + .. + } => Some(Event::Keyboard(keyboard::Event::Input { + key_code: key_code(virtual_keycode), + state: button_state(state), + modifiers: modifiers_state(modifiers), + })), + WindowEvent::HoveredFile(path) => { + Some(Event::Window(window::Event::FileHovered(path))) + } + WindowEvent::DroppedFile(path) => { + Some(Event::Window(window::Event::FileDropped(path))) + } + WindowEvent::HoveredFileCancelled => { + Some(Event::Window(window::Event::FilesHoveredLeft)) + } + _ => None, + } +} + /// Converts a [`Mode`] to a [`winit`] fullscreen mode. /// /// [`Mode`]: @@ -254,3 +332,13 @@ pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode { winit::event::VirtualKeyCode::Cut => KeyCode::Cut, } } + +// As defined in: http://www.unicode.org/faq/private_use.html +pub(crate) fn is_private_use_character(c: char) -> bool { + match c { + '\u{E000}'..='\u{F8FF}' + | '\u{F0000}'..='\u{FFFFD}' + | '\u{100000}'..='\u{10FFFD}' => true, + _ => false, + } +} diff --git a/winit/src/debug/null.rs b/winit/src/debug/null.rs index 9c809dd4..2a9430cd 100644 --- a/winit/src/debug/null.rs +++ b/winit/src/debug/null.rs @@ -6,8 +6,6 @@ impl Debug { Self } - pub fn toggle(&mut self) {} - pub fn startup_started(&mut self) {} pub fn startup_finished(&mut self) {} diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 225907a4..f99e1290 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -50,4 +50,3 @@ pub use settings::Settings; use debug::Debug; use proxy::Proxy; -use size::Size; |