diff options
Diffstat (limited to 'winit/src/application.rs')
-rw-r--r-- | winit/src/application.rs | 187 |
1 files changed, 56 insertions, 131 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, - } -} |