From d7a9356389a2b6dec779bdac062a513ed482f6ed Mon Sep 17 00:00:00 2001 From: Dongdong Zhou Date: Tue, 24 Oct 2023 22:28:58 +0100 Subject: [PATCH] bump winit --- Cargo.toml | 2 +- examples/widget-gallery/src/lists.rs | 6 ++-- examples/widget-gallery/src/main.rs | 6 ++-- src/app.rs | 16 ++++------ src/app_handle.rs | 48 ++++++++++++---------------- src/event.rs | 9 ++++-- src/keyboard.rs | 2 +- src/views/text_input.rs | 18 +++++------ src/window_handle.rs | 4 +-- 9 files changed, 53 insertions(+), 58 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ac7c4df2..412cd7a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ floem_renderer = { path = "renderer" } floem_vger = { path = "vger" } floem_tiny_skia = { path = "tiny_skia" } floem_reactive = { path = "reactive" } -winit = { git = "https://github.com/lapce/winit", rev = "25edc72fa4869d0fa83c61c26f0e38d7d7be9b0d" } +winit = { git = "https://github.com/lapce/winit", rev = "fa3a4f5cb8eadcfe07d9a629e57f4c3c0f6ce109", features = ["rwh_05"] } # winit = { path = "../winit" } image = { version = "0.24", features = ["jpeg", "png"] } diff --git a/examples/widget-gallery/src/lists.rs b/examples/widget-gallery/src/lists.rs index 549d1d44..3acda210 100644 --- a/examples/widget-gallery/src/lists.rs +++ b/examples/widget-gallery/src/lists.rs @@ -1,7 +1,7 @@ use floem::{ cosmic_text::Weight, event::{Event, EventListener}, - keyboard::Key, + keyboard::{Key, NamedKey}, peniko::Color, reactive::create_signal, style::{CursorStyle, JustifyContent}, @@ -108,13 +108,13 @@ fn enhanced_list() -> impl View { if let Event::KeyDown(key_event) = e { let sel = selected.get(); match key_event.key.logical_key { - Key::ArrowUp => { + Key::Named(NamedKey::ArrowUp) => { if sel > 0 { set_selected.update(|v| *v -= 1); } true } - Key::ArrowDown => { + Key::Named(NamedKey::ArrowDown) => { if sel < long_list.get().len() - 1 { set_selected.update(|v| *v += 1); } diff --git a/examples/widget-gallery/src/main.rs b/examples/widget-gallery/src/main.rs index 962b6100..2859720c 100644 --- a/examples/widget-gallery/src/main.rs +++ b/examples/widget-gallery/src/main.rs @@ -10,7 +10,7 @@ pub mod rich_text; use floem::{ event::{Event, EventListener}, - keyboard::Key, + keyboard::{Key, NamedKey}, peniko::Color, reactive::create_signal, style::CursorStyle, @@ -61,13 +61,13 @@ fn app_view() -> impl View { if let Event::KeyDown(key_event) = e { let active = active_tab.get(); match key_event.key.logical_key { - Key::ArrowUp => { + Key::Named(NamedKey::ArrowUp) => { if active > 0 { set_active_tab.update(|v| *v -= 1) } true } - Key::ArrowDown => { + Key::Named(NamedKey::ArrowDown) => { if active < tabs.get().len() - 1 { set_active_tab.update(|v| *v += 1) } diff --git a/src/app.rs b/src/app.rs index 32fc1b23..6451c000 100644 --- a/src/app.rs +++ b/src/app.rs @@ -3,7 +3,7 @@ use std::{cell::RefCell, sync::Arc}; use once_cell::sync::Lazy; use parking_lot::Mutex; use winit::{ - event_loop::{EventLoop, EventLoopBuilder, EventLoopProxy}, + event_loop::{ControlFlow, EventLoop, EventLoopBuilder, EventLoopProxy}, window::WindowId, }; @@ -112,30 +112,28 @@ impl Application { pub fn run(mut self) { let mut handle = self.handle.take().unwrap(); handle.idle(); - let _ = self.event_loop.run(move |event, event_loop, control_flow| { - control_flow.set_wait(); - handle.handle_timer(control_flow); + let _ = self.event_loop.run(move |event, event_loop| { + event_loop.set_control_flow(ControlFlow::Wait); + handle.handle_timer(event_loop); match event { winit::event::Event::NewEvents(_) => {} winit::event::Event::WindowEvent { window_id, event } => { - handle.handle_window_event(window_id, event, control_flow); + handle.handle_window_event(window_id, event, event_loop); } winit::event::Event::DeviceEvent { .. } => {} winit::event::Event::UserEvent(event) => { - handle.handle_user_event(event_loop, event, control_flow); + handle.handle_user_event(event_loop, event); } winit::event::Event::Suspended => {} winit::event::Event::Resumed => {} winit::event::Event::AboutToWait => {} - winit::event::Event::RedrawRequested(window_id) => { - handle.redraw_requested(window_id); - } winit::event::Event::LoopExiting => { if let Some(action) = self.event_listener.as_ref() { action(AppEvent::WillTerminate); } } + winit::event::Event::MemoryWarning => {} } }); } diff --git a/src/app_handle.rs b/src/app_handle.rs index 69bca02b..3355b7b0 100644 --- a/src/app_handle.rs +++ b/src/app_handle.rs @@ -34,26 +34,21 @@ impl ApplicationHandle { &mut self, event_loop: &EventLoopWindowTarget, event: UserEvent, - control_flow: &mut ControlFlow, ) { match event { UserEvent::AppUpdate => { - self.handle_update_event(event_loop, control_flow); + self.handle_update_event(event_loop); } UserEvent::Idle => { self.idle(); } UserEvent::QuitApp => { - control_flow.set_exit(); + event_loop.exit(); } } } - pub(crate) fn handle_update_event( - &mut self, - event_loop: &EventLoopWindowTarget, - control_flow: &mut ControlFlow, - ) { + pub(crate) fn handle_update_event(&mut self, event_loop: &EventLoopWindowTarget) { let events = APP_UPDATE_EVENTS.with(|events| { let mut events = events.borrow_mut(); std::mem::take(&mut *events) @@ -64,10 +59,10 @@ impl ApplicationHandle { self.new_window(event_loop, view_fn, config) } AppUpdateEvent::CloseWindow { window_id } => { - self.close_window(window_id, control_flow); + self.close_window(window_id, event_loop); } AppUpdateEvent::RequestTimer { timer } => { - self.request_timer(timer, control_flow); + self.request_timer(timer, event_loop); } #[cfg(target_os = "linux")] AppUpdateEvent::MenuAction { @@ -88,7 +83,7 @@ impl ApplicationHandle { &mut self, window_id: winit::window::WindowId, event: WindowEvent, - control_flow: &mut ControlFlow, + event_loop: &EventLoopWindowTarget, ) { let window_handle = match self.window_handles.get_mut(&window_id) { Some(window_handle) => window_handle, @@ -108,10 +103,10 @@ impl ApplicationHandle { window_handle.position(point); } WindowEvent::CloseRequested => { - self.close_window(window_id, control_flow); + self.close_window(window_id, event_loop); } WindowEvent::Destroyed => { - self.close_window(window_id, control_flow); + self.close_window(window_id, event_loop); } WindowEvent::DroppedFile(_) => {} WindowEvent::HoveredFile(_) => {} @@ -157,12 +152,9 @@ impl ApplicationHandle { WindowEvent::MenuAction(id) => { window_handle.menu_action(id); } - } - } - - pub(crate) fn redraw_requested(&mut self, window_id: winit::window::WindowId) { - if let Some(window_handle) = self.window_handles.get_mut(&window_id) { - window_handle.paint(); + WindowEvent::RedrawRequested => { + window_handle.paint(); + } } } @@ -214,8 +206,8 @@ impl ApplicationHandle { fn close_window( &mut self, window_id: WindowId, - #[cfg(target_os = "macos")] _control_flow: &mut ControlFlow, - #[cfg(not(target_os = "macos"))] control_flow: &mut ControlFlow, + #[cfg(target_os = "macos")] _event_loop: &EventLoopWindowTarget, + #[cfg(not(target_os = "macos"))] event_loop: &EventLoopWindowTarget, ) { if let Some(handle) = self.window_handles.get_mut(&window_id) { handle.window = None; @@ -224,7 +216,7 @@ impl ApplicationHandle { self.window_handles.remove(&window_id); #[cfg(not(target_os = "macos"))] if self.window_handles.is_empty() { - control_flow.set_exit(); + event_loop.exit(); } } @@ -237,23 +229,23 @@ impl ApplicationHandle { } } - fn request_timer(&mut self, timer: Timer, control_flow: &mut ControlFlow) { + fn request_timer(&mut self, timer: Timer, event_loop: &EventLoopWindowTarget) { self.timers.insert(timer.token, timer); - self.fire_timer(control_flow); + self.fire_timer(event_loop); } - fn fire_timer(&mut self, control_flow: &mut ControlFlow) { + fn fire_timer(&mut self, event_loop: &EventLoopWindowTarget) { if self.timers.is_empty() { return; } let deadline = self.timers.values().map(|timer| timer.deadline).min(); if let Some(deadline) = deadline { - control_flow.set_wait_until(deadline); + event_loop.set_control_flow(ControlFlow::WaitUntil(deadline)); } } - pub(crate) fn handle_timer(&mut self, control_flow: &mut ControlFlow) { + pub(crate) fn handle_timer(&mut self, event_loop: &EventLoopWindowTarget) { let now = Instant::now(); let tokens: Vec = self .timers @@ -276,6 +268,6 @@ impl ApplicationHandle { handle.process_update(); } } - self.fire_timer(control_flow); + self.fire_timer(event_loop); } } diff --git a/src/event.rs b/src/event.rs index c60b0a3e..db550eed 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,5 +1,8 @@ use kurbo::{Point, Size}; -use winit::{keyboard::KeyCode, window::Theme}; +use winit::{ + keyboard::{KeyCode, PhysicalKey}, + window::Theme, +}; use crate::{ keyboard::KeyEvent, @@ -120,7 +123,9 @@ impl Event { Event::KeyDown(key) | Event::KeyUp(key) => { matches!( key.key.physical_key, - KeyCode::NumpadEnter | KeyCode::Enter | KeyCode::Space, + PhysicalKey::Code(KeyCode::NumpadEnter) + | PhysicalKey::Code(KeyCode::Enter) + | PhysicalKey::Code(KeyCode::Space), ) } _ => false, diff --git a/src/keyboard.rs b/src/keyboard.rs index 4aa9e322..262702e2 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -1,4 +1,4 @@ -pub use winit::keyboard::{Key, KeyCode, ModifiersState, NativeKey}; +pub use winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, NativeKey}; #[derive(Debug, Clone, Eq, PartialEq, Hash)] pub struct KeyEvent { diff --git a/src/views/text_input.rs b/src/views/text_input.rs index 5e2294dd..e55d5ff6 100644 --- a/src/views/text_input.rs +++ b/src/views/text_input.rs @@ -11,7 +11,7 @@ use floem_renderer::{ Renderer, }; use unicode_segmentation::UnicodeSegmentation; -use winit::keyboard::{Key, ModifiersState, SmolStr}; +use winit::keyboard::{Key, ModifiersState, NamedKey, SmolStr}; use crate::{peniko::Color, style::Style, view::View}; @@ -528,7 +528,7 @@ impl TextInput { .update(|buf| buf.insert_str(self.cursor_glyph_idx, &ch.clone())); self.move_cursor(Movement::Glyph, Direction::Right) } - Key::Space => { + Key::Named(NamedKey::Space) => { if let Some(selection) = &self.selection { self.buffer .update(|buf| replace_range(buf, selection.clone(), None)); @@ -540,7 +540,7 @@ impl TextInput { } self.move_cursor(Movement::Glyph, Direction::Right) } - Key::Backspace => { + Key::Named(NamedKey::Backspace) => { let selection = self.selection.clone(); if let Some(selection) = selection { self.buffer @@ -566,7 +566,7 @@ impl TextInput { true } } - Key::Delete => { + Key::Named(NamedKey::Delete) => { let prev_cursor_idx = self.cursor_glyph_idx; if event.modifiers.contains(ModifiersState::CONTROL) { @@ -586,13 +586,13 @@ impl TextInput { self.cursor_glyph_idx = prev_cursor_idx; true } - Key::Escape => { + Key::Named(NamedKey::Escape) => { cx.app_state.clear_focus(); true } - Key::End => self.move_cursor(Movement::Line, Direction::Right), - Key::Home => self.move_cursor(Movement::Line, Direction::Left), - Key::ArrowLeft => { + Key::Named(NamedKey::End) => self.move_cursor(Movement::Line, Direction::Right), + Key::Named(NamedKey::Home) => self.move_cursor(Movement::Line, Direction::Left), + Key::Named(NamedKey::ArrowLeft) => { let old_glyph_idx = self.cursor_glyph_idx; let cursor_moved = if event.modifiers.contains(ModifiersState::CONTROL) { @@ -616,7 +616,7 @@ impl TextInput { cursor_moved } - Key::ArrowRight => { + Key::Named(NamedKey::ArrowRight) => { let old_glyph_idx = self.cursor_glyph_idx; let cursor_moved = if event.modifiers.contains(ModifiersState::CONTROL) { diff --git a/src/window_handle.rs b/src/window_handle.rs index 4ffa04ea..9ede908e 100644 --- a/src/window_handle.rs +++ b/src/window_handle.rs @@ -9,7 +9,7 @@ use winit::window::WindowId; use winit::{ dpi::{LogicalPosition, LogicalSize}, event::{ElementState, Ime, MouseButton, MouseScrollDelta}, - keyboard::{Key, ModifiersState}, + keyboard::{Key, ModifiersState, NamedKey}, window::{CursorIcon, Theme}, }; @@ -173,7 +173,7 @@ impl WindowHandle { if !processed { if let Event::KeyDown(KeyEvent { key, modifiers }) = &event { - if key.logical_key == Key::Tab { + if key.logical_key == Key::Named(NamedKey::Tab) { let _backwards = modifiers.contains(ModifiersState::SHIFT); // view_tab_navigation(&self.view, cx.app_state, backwards); // view_debug_tree(&self.view);