Skip to content

Commit

Permalink
bump winit
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Oct 24, 2023
1 parent cf4c09e commit d7a9356
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
6 changes: 3 additions & 3 deletions examples/widget-gallery/src/lists.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/widget-gallery/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
16 changes: 7 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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 => {}
}
});
}
Expand Down
48 changes: 20 additions & 28 deletions src/app_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,21 @@ impl ApplicationHandle {
&mut self,
event_loop: &EventLoopWindowTarget<UserEvent>,
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<UserEvent>,
control_flow: &mut ControlFlow,
) {
pub(crate) fn handle_update_event(&mut self, event_loop: &EventLoopWindowTarget<UserEvent>) {
let events = APP_UPDATE_EVENTS.with(|events| {
let mut events = events.borrow_mut();
std::mem::take(&mut *events)
Expand All @@ -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 {
Expand All @@ -88,7 +83,7 @@ impl ApplicationHandle {
&mut self,
window_id: winit::window::WindowId,
event: WindowEvent,
control_flow: &mut ControlFlow,
event_loop: &EventLoopWindowTarget<UserEvent>,
) {
let window_handle = match self.window_handles.get_mut(&window_id) {
Some(window_handle) => window_handle,
Expand All @@ -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(_) => {}
Expand Down Expand Up @@ -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();
}
}
}

Expand Down Expand Up @@ -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<UserEvent>,
#[cfg(not(target_os = "macos"))] event_loop: &EventLoopWindowTarget<UserEvent>,
) {
if let Some(handle) = self.window_handles.get_mut(&window_id) {
handle.window = None;
Expand All @@ -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();
}
}

Expand All @@ -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<UserEvent>) {
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<UserEvent>) {
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<UserEvent>) {
let now = Instant::now();
let tokens: Vec<TimerToken> = self
.timers
Expand All @@ -276,6 +268,6 @@ impl ApplicationHandle {
handle.process_update();
}
}
self.fire_timer(control_flow);
self.fire_timer(event_loop);
}
}
9 changes: 7 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/keyboard.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions src/views/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d7a9356

Please sign in to comment.