summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2024-01-17 08:12:44 +0100
committerLibravatar GitHub <noreply@github.com>2024-01-17 08:12:44 +0100
commit0001a6d63642b299531ff089f961732a1bfa2339 (patch)
tree7b100257baf72dbceccf5c3343abf3a6b9f1db03 /examples
parentc4ba657de86d7606587dad5124f435141258f570 (diff)
parent985acb2b1532b3e56161bd35201c4a2e21a86b85 (diff)
downloadiced-0001a6d63642b299531ff089f961732a1bfa2339.tar.gz
iced-0001a6d63642b299531ff089f961732a1bfa2339.tar.bz2
iced-0001a6d63642b299531ff089f961732a1bfa2339.zip
Merge pull request #2169 from iced-rs/update-winit
Update `winit` to `0.29`
Diffstat (limited to 'examples')
-rw-r--r--examples/editor/src/main.rs4
-rw-r--r--examples/integration/src/main.rs124
-rw-r--r--examples/layout/src/main.rs10
-rw-r--r--examples/loading_spinners/src/circular.rs6
-rw-r--r--examples/loading_spinners/src/linear.rs6
-rw-r--r--examples/modal/src/main.rs6
-rw-r--r--examples/pane_grid/src/main.rs31
-rw-r--r--examples/screenshot/src/main.rs33
-rw-r--r--examples/stopwatch/src/main.rs12
-rw-r--r--examples/toast/src/main.rs6
-rw-r--r--examples/todos/src/main.rs16
11 files changed, 133 insertions, 121 deletions
diff --git a/examples/editor/src/main.rs b/examples/editor/src/main.rs
index 03d1e283..bf2aaaa3 100644
--- a/examples/editor/src/main.rs
+++ b/examples/editor/src/main.rs
@@ -134,8 +134,8 @@ impl Application for Editor {
}
fn subscription(&self) -> Subscription<Message> {
- keyboard::on_key_press(|key_code, modifiers| match key_code {
- keyboard::KeyCode::S if modifiers.command() => {
+ keyboard::on_key_press(|key, modifiers| match key.as_ref() {
+ keyboard::Key::Character("s") if modifiers.command() => {
Some(Message::SaveFile)
}
_ => None,
diff --git a/examples/integration/src/main.rs b/examples/integration/src/main.rs
index 276794c8..b0939d68 100644
--- a/examples/integration/src/main.rs
+++ b/examples/integration/src/main.rs
@@ -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")]
@@ -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()
@@ -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();
@@ -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(())
}
diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs
index 60dabe54..6cf0e570 100644
--- a/examples/layout/src/main.rs
+++ b/examples/layout/src/main.rs
@@ -71,9 +71,13 @@ impl Application for Layout {
}
fn subscription(&self) -> Subscription<Message> {
- keyboard::on_key_release(|key_code, _modifiers| match key_code {
- keyboard::KeyCode::Left => Some(Message::Previous),
- keyboard::KeyCode::Right => Some(Message::Next),
+ use keyboard::key;
+
+ keyboard::on_key_release(|key, _modifiers| match key {
+ keyboard::Key::Named(key::Named::ArrowLeft) => {
+ Some(Message::Previous)
+ }
+ keyboard::Key::Named(key::Named::ArrowRight) => Some(Message::Next),
_ => None,
})
}
diff --git a/examples/loading_spinners/src/circular.rs b/examples/loading_spinners/src/circular.rs
index 1b163585..2e119979 100644
--- a/examples/loading_spinners/src/circular.rs
+++ b/examples/loading_spinners/src/circular.rs
@@ -271,8 +271,6 @@ where
shell: &mut Shell<'_, Message>,
_viewport: &Rectangle,
) -> event::Status {
- const FRAME_RATE: u64 = 60;
-
let state = tree.state.downcast_mut::<State>();
if let Event::Window(_, window::Event::RedrawRequested(now)) = event {
@@ -283,9 +281,7 @@ where
);
state.cache.clear();
- shell.request_redraw(RedrawRequest::At(
- now + Duration::from_millis(1000 / FRAME_RATE),
- ));
+ shell.request_redraw(RedrawRequest::NextFrame);
}
event::Status::Ignored
diff --git a/examples/loading_spinners/src/linear.rs b/examples/loading_spinners/src/linear.rs
index d245575c..497e0834 100644
--- a/examples/loading_spinners/src/linear.rs
+++ b/examples/loading_spinners/src/linear.rs
@@ -192,16 +192,12 @@ where
shell: &mut Shell<'_, Message>,
_viewport: &Rectangle,
) -> event::Status {
- const FRAME_RATE: u64 = 60;
-
let state = tree.state.downcast_mut::<State>();
if let Event::Window(_, window::Event::RedrawRequested(now)) = event {
*state = state.timed_transition(self.cycle_duration, now);
- shell.request_redraw(RedrawRequest::At(
- now + Duration::from_millis(1000 / FRAME_RATE),
- ));
+ shell.request_redraw(RedrawRequest::NextFrame);
}
event::Status::Ignored
diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs
index 631efe6e..963c839e 100644
--- a/examples/modal/src/main.rs
+++ b/examples/modal/src/main.rs
@@ -1,6 +1,7 @@
use iced::event::{self, Event};
use iced::executor;
use iced::keyboard;
+use iced::keyboard::key;
use iced::theme;
use iced::widget::{
self, button, column, container, horizontal_space, pick_list, row, text,
@@ -85,8 +86,9 @@ impl Application for App {
}
Message::Event(event) => match event {
Event::Keyboard(keyboard::Event::KeyPressed {
- key_code: keyboard::KeyCode::Tab,
+ key: keyboard::Key::Named(key::Named::Tab),
modifiers,
+ ..
}) => {
if modifiers.shift() {
widget::focus_previous()
@@ -95,7 +97,7 @@ impl Application for App {
}
}
Event::Keyboard(keyboard::Event::KeyPressed {
- key_code: keyboard::KeyCode::Escape,
+ key: keyboard::Key::Named(key::Named::Escape),
..
}) => {
self.hide_modal();
diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs
index 96bb8e4e..d5e5bcbe 100644
--- a/examples/pane_grid/src/main.rs
+++ b/examples/pane_grid/src/main.rs
@@ -220,23 +220,26 @@ const PANE_ID_COLOR_FOCUSED: Color = Color::from_rgb(
0x47 as f32 / 255.0,
);
-fn handle_hotkey(key_code: keyboard::KeyCode) -> Option<Message> {
- use keyboard::KeyCode;
+fn handle_hotkey(key: keyboard::Key) -> Option<Message> {
+ use keyboard::key::{self, Key};
use pane_grid::{Axis, Direction};
- let direction = match key_code {
- KeyCode::Up => Some(Direction::Up),
- KeyCode::Down => Some(Direction::Down),
- KeyCode::Left => Some(Direction::Left),
- KeyCode::Right => Some(Direction::Right),
- _ => None,
- };
+ match key.as_ref() {
+ Key::Character("v") => Some(Message::SplitFocused(Axis::Vertical)),
+ Key::Character("h") => Some(Message::SplitFocused(Axis::Horizontal)),
+ Key::Character("w") => Some(Message::CloseFocused),
+ Key::Named(key) => {
+ let direction = match key {
+ key::Named::ArrowUp => Some(Direction::Up),
+ key::Named::ArrowDown => Some(Direction::Down),
+ key::Named::ArrowLeft => Some(Direction::Left),
+ key::Named::ArrowRight => Some(Direction::Right),
+ _ => None,
+ };
- match key_code {
- KeyCode::V => Some(Message::SplitFocused(Axis::Vertical)),
- KeyCode::H => Some(Message::SplitFocused(Axis::Horizontal)),
- KeyCode::W => Some(Message::CloseFocused),
- _ => direction.map(Message::FocusAdjacent),
+ direction.map(Message::FocusAdjacent)
+ }
+ _ => None,
}
}
diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs
index 20d34be6..6955551e 100644
--- a/examples/screenshot/src/main.rs
+++ b/examples/screenshot/src/main.rs
@@ -1,11 +1,13 @@
-use iced::keyboard::KeyCode;
-use iced::theme::{Button, Container};
+use iced::alignment;
+use iced::executor;
+use iced::keyboard;
+use iced::theme;
use iced::widget::{button, column, container, image, row, text, text_input};
+use iced::window;
use iced::window::screenshot::{self, Screenshot};
-use iced::{alignment, window};
use iced::{
- event, executor, keyboard, Alignment, Application, Command, ContentFit,
- Element, Event, Length, Rectangle, Renderer, Subscription, Theme,
+ Alignment, Application, Command, ContentFit, Element, Length, Rectangle,
+ Renderer, Subscription, Theme,
};
use ::image as img;
@@ -147,7 +149,7 @@ impl Application for Example {
let image = container(image)
.padding(10)
- .style(Container::Box)
+ .style(theme::Container::Box)
.width(Length::FillPortion(2))
.height(Length::Fill)
.center_x()
@@ -202,9 +204,10 @@ impl Application for Example {
self.screenshot.is_some().then(|| Message::Png),
)
} else {
- button(centered_text("Saving...")).style(Button::Secondary)
+ button(centered_text("Saving..."))
+ .style(theme::Button::Secondary)
}
- .style(Button::Secondary)
+ .style(theme::Button::Secondary)
.padding([10, 20, 10, 20])
.width(Length::Fill)
]
@@ -213,7 +216,7 @@ impl Application for Example {
crop_controls,
button(centered_text("Crop"))
.on_press(Message::Crop)
- .style(Button::Destructive)
+ .style(theme::Button::Destructive)
.padding([10, 20, 10, 20])
.width(Length::Fill),
]
@@ -256,16 +259,10 @@ impl Application for Example {
}
fn subscription(&self) -> Subscription<Self::Message> {
- event::listen_with(|event, status| {
- if let event::Status::Captured = status {
- return None;
- }
+ use keyboard::key;
- if let Event::Keyboard(keyboard::Event::KeyPressed {
- key_code: KeyCode::F5,
- ..
- }) = event
- {
+ keyboard::on_key_press(|key, _modifiers| {
+ if let keyboard::Key::Named(key::Named::F5) = key {
Some(Message::Screenshot)
} else {
None
diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs
index 0b0f0607..8a0674c1 100644
--- a/examples/stopwatch/src/main.rs
+++ b/examples/stopwatch/src/main.rs
@@ -86,12 +86,16 @@ impl Application for Stopwatch {
};
fn handle_hotkey(
- key_code: keyboard::KeyCode,
+ key: keyboard::Key,
_modifiers: keyboard::Modifiers,
) -> Option<Message> {
- match key_code {
- keyboard::KeyCode::Space => Some(Message::Toggle),
- keyboard::KeyCode::R => Some(Message::Reset),
+ use keyboard::key;
+
+ match key.as_ref() {
+ keyboard::Key::Named(key::Named::Space) => {
+ Some(Message::Toggle)
+ }
+ keyboard::Key::Character("r") => Some(Message::Reset),
_ => None,
}
}
diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs
index 300343b9..2e837fa3 100644
--- a/examples/toast/src/main.rs
+++ b/examples/toast/src/main.rs
@@ -1,6 +1,7 @@
use iced::event::{self, Event};
use iced::executor;
use iced::keyboard;
+use iced::keyboard::key;
use iced::widget::{
self, button, column, container, pick_list, row, slider, text, text_input,
};
@@ -93,11 +94,12 @@ impl Application for App {
Command::none()
}
Message::Event(Event::Keyboard(keyboard::Event::KeyPressed {
- key_code: keyboard::KeyCode::Tab,
+ key: keyboard::Key::Named(key::Named::Tab),
modifiers,
+ ..
})) if modifiers.shift() => widget::focus_previous(),
Message::Event(Event::Keyboard(keyboard::Event::KeyPressed {
- key_code: keyboard::KeyCode::Tab,
+ key: keyboard::Key::Named(key::Named::Tab),
..
})) => widget::focus_next(),
Message::Event(_) => Command::none(),
diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs
index aad47c20..3d79f087 100644
--- a/examples/todos/src/main.rs
+++ b/examples/todos/src/main.rs
@@ -260,15 +260,21 @@ impl Application for Todos {
}
fn subscription(&self) -> Subscription<Message> {
- keyboard::on_key_press(|key_code, modifiers| {
- match (key_code, modifiers) {
- (keyboard::KeyCode::Tab, _) => Some(Message::TabPressed {
+ use keyboard::key;
+
+ keyboard::on_key_press(|key, modifiers| {
+ let keyboard::Key::Named(key) = key else {
+ return None;
+ };
+
+ match (key, modifiers) {
+ (key::Named::Tab, _) => Some(Message::TabPressed {
shift: modifiers.shift(),
}),
- (keyboard::KeyCode::Up, keyboard::Modifiers::SHIFT) => {
+ (key::Named::ArrowUp, keyboard::Modifiers::SHIFT) => {
Some(Message::ToggleFullscreen(window::Mode::Fullscreen))
}
- (keyboard::KeyCode::Down, keyboard::Modifiers::SHIFT) => {
+ (key::Named::ArrowDown, keyboard::Modifiers::SHIFT) => {
Some(Message::ToggleFullscreen(window::Mode::Windowed))
}
_ => None,