Skip to content

Commit

Permalink
Update winit to 0.29.4
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Dec 15, 2023
1 parent dd249a1 commit f16a831
Show file tree
Hide file tree
Showing 16 changed files with 650 additions and 829 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,4 @@ web-sys = "0.3"
wgpu = "0.18"
winapi = "0.3"
window_clipboard = "0.3"
winit = { git = "https://github.com/iced-rs/winit.git", rev = "c52db2045d0a2f1b8d9923870de1d4ab1994146e", default-features = false }
winit = { git = "https://github.com/iced-rs/winit.git", rev = "3bcdb9abcd7459978ec689523bc21943d38da0f9", default-features = false, features = ["rwh_05", "x11", "wayland"] }
8 changes: 4 additions & 4 deletions core/src/keyboard/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{KeyCode, Modifiers};
/// additional events, feel free to [open an issue] and share your use case!_
///
/// [open an issue]: https://github.com/iced-rs/iced/issues
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
/// A keyboard key was pressed.
KeyPressed {
Expand All @@ -15,6 +15,9 @@ pub enum Event {

/// The state of the modifier keys
modifiers: Modifiers,

/// The text produced by the key press, if any.
text: Option<String>,
},

/// A keyboard key was released.
Expand All @@ -26,9 +29,6 @@ pub enum Event {
modifiers: Modifiers,
},

/// A unicode character was received.
CharacterReceived(char),

/// The keyboard modifiers have changed.
ModifiersChanged(Modifiers),
}
6 changes: 6 additions & 0 deletions core/src/mouse/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pub enum Button {
/// The middle (wheel) button.
Middle,

/// The back mouse button.
Back,

/// The forward mouse button.
Forward,

/// Some other button.
Other(u16),
}
124 changes: 63 additions & 61 deletions examples/integration/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ use iced_winit::winit;
use iced_winit::Clipboard;

use winit::{
event::{Event, ModifiersState, WindowEvent},
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::ModifiersState,
};

#[cfg(target_arch = "wasm32")]
Expand Down Expand Up @@ -48,7 +49,7 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();

// Initialize winit
let event_loop = EventLoop::new();
let event_loop = EventLoop::new()?;

#[cfg(target_arch = "wasm32")]
let window = winit::window::WindowBuilder::new()
Expand Down Expand Up @@ -160,67 +161,15 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
);

// Run event loop
event_loop.run(move |event, _, control_flow| {
event_loop.run(move |event, window_target| {
// You should change this if you want to render continuosly
*control_flow = ControlFlow::Wait;
window_target.set_control_flow(ControlFlow::Wait);

match event {
Event::WindowEvent { event, .. } => {
match event {
WindowEvent::CursorMoved { position, .. } => {
cursor_position = Some(position);
}
WindowEvent::ModifiersChanged(new_modifiers) => {
modifiers = new_modifiers;
}
WindowEvent::Resized(_) => {
resized = true;
}
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
_ => {}
}

// Map window event to iced event
if let Some(event) = iced_winit::conversion::window_event(
window::Id::MAIN,
&event,
window.scale_factor(),
modifiers,
) {
state.queue_event(event);
}
}
Event::MainEventsCleared => {
// If there are events pending
if !state.is_queue_empty() {
// We update iced
let _ = state.update(
viewport.logical_size(),
cursor_position
.map(|p| {
conversion::cursor_position(
p,
viewport.scale_factor(),
)
})
.map(mouse::Cursor::Available)
.unwrap_or(mouse::Cursor::Unavailable),
&mut renderer,
&Theme::Dark,
&renderer::Style {
text_color: Color::WHITE,
},
&mut clipboard,
&mut debug,
);

// and request a redraw
window.request_redraw();
}
}
Event::RedrawRequested(_) => {
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
} => {
if resized {
let size = window.inner_size();

Expand Down Expand Up @@ -309,7 +258,60 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
},
}
}
Event::WindowEvent { event, .. } => {
match event {
WindowEvent::CursorMoved { position, .. } => {
cursor_position = Some(position);
}
WindowEvent::ModifiersChanged(new_modifiers) => {
modifiers = new_modifiers.state();
}
WindowEvent::Resized(_) => {
resized = true;
}
WindowEvent::CloseRequested => {
window_target.exit();
}
_ => {}
}

// Map window event to iced event
if let Some(event) = iced_winit::conversion::window_event(
window::Id::MAIN,
&event,
window.scale_factor(),
modifiers,
) {
state.queue_event(event);
}
}
_ => {}
}
})

// If there are events pending
if !state.is_queue_empty() {
// We update iced
let _ = state.update(
viewport.logical_size(),
cursor_position
.map(|p| {
conversion::cursor_position(p, viewport.scale_factor())
})
.map(mouse::Cursor::Available)
.unwrap_or(mouse::Cursor::Unavailable),
&mut renderer,
&Theme::Dark,
&renderer::Style {
text_color: Color::WHITE,
},
&mut clipboard,
&mut debug,
);

// and request a redraw
window.request_redraw();
}
})?;

Ok(())
}
1 change: 1 addition & 0 deletions examples/modal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl Application for App {
Event::Keyboard(keyboard::Event::KeyPressed {
key_code: keyboard::KeyCode::Tab,
modifiers,
..
}) => {
if modifiers.shift() {
widget::focus_previous()
Expand Down
1 change: 1 addition & 0 deletions examples/toast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl Application for App {
Message::Event(Event::Keyboard(keyboard::Event::KeyPressed {
key_code: keyboard::KeyCode::Tab,
modifiers,
..
})) if modifiers.shift() => widget::focus_previous(),
Message::Event(Event::Keyboard(keyboard::Event::KeyPressed {
key_code: keyboard::KeyCode::Tab,
Expand Down
1 change: 1 addition & 0 deletions futures/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ where
core::Event::Keyboard(Event::KeyPressed {
key_code,
modifiers,
..
}),
core::event::Status::Ignored,
) => f(key_code, modifiers),
Expand Down
2 changes: 1 addition & 1 deletion widget/src/canvas/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use crate::core::event::Status;
/// A [`Canvas`] event.
///
/// [`Canvas`]: crate::Canvas
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub enum Event {
/// A mouse event.
Mouse(mouse::Event),
Expand Down
2 changes: 1 addition & 1 deletion widget/src/shader/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use crate::core::event::Status;
/// A [`Shader`] event.
///
/// [`Shader`]: crate::Shader
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub enum Event {
/// A mouse event.
Mouse(mouse::Event),
Expand Down
12 changes: 8 additions & 4 deletions widget/src/text_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ impl Update {
keyboard::Event::KeyPressed {
key_code,
modifiers,
text,
} if state.is_focused => {
if let Some(motion) = motion(key_code) {
let motion =
Expand Down Expand Up @@ -678,12 +679,15 @@ impl Update {
{
Some(Self::Paste)
}
_ => None,
_ => {
let text = text?;

edit(Edit::Insert(
text.chars().next().unwrap_or_default(),
))
}
}
}
keyboard::Event::CharacterReceived(c) if state.is_focused => {
edit(Edit::Insert(c))
}
_ => None,
},
_ => None,
Expand Down
56 changes: 27 additions & 29 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,34 +752,9 @@ where
return event::Status::Captured;
}
}
Event::Keyboard(keyboard::Event::CharacterReceived(c)) => {
let state = state();

if let Some(focus) = &mut state.is_focused {
let Some(on_input) = on_input else {
return event::Status::Ignored;
};

if state.is_pasting.is_none()
&& !state.keyboard_modifiers.command()
&& !c.is_control()
{
let mut editor = Editor::new(value, &mut state.cursor);

editor.insert(c);

let message = (on_input)(editor.contents());
shell.publish(message);

focus.updated_at = Instant::now();

update_cache(state, value);

return event::Status::Captured;
}
}
}
Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) => {
Event::Keyboard(keyboard::Event::KeyPressed {
key_code, text, ..
}) => {
let state = state();

if let Some(focus) = &mut state.is_focused {
Expand Down Expand Up @@ -971,7 +946,30 @@ where
| keyboard::KeyCode::Down => {
return event::Status::Ignored;
}
_ => {}
_ => {
if let Some(text) = text {
let c = text.chars().next().unwrap_or_default();

if state.is_pasting.is_none()
&& !state.keyboard_modifiers.command()
&& !c.is_control()
{
let mut editor =
Editor::new(value, &mut state.cursor);

editor.insert(c);

let message = (on_input)(editor.contents());
shell.publish(message);

focus.updated_at = Instant::now();

update_cache(state, value);

return event::Status::Captured;
}
}
}
}

return event::Status::Captured;
Expand Down
Loading

0 comments on commit f16a831

Please sign in to comment.