Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
- name: Delete `web-sys` dependency from `integration` example
run: sed -i '$d' examples/integration/Cargo.toml
- name: Find outdated dependencies
run: cargo outdated --workspace --exit-code 1
run: cargo outdated --workspace --exit-code 1 --ignore raw-window-handle
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ glyphon = { git = "https://github.com/grovesNL/glyphon.git", rev = "2caa9fc5e592
guillotiere = "0.6"
half = "2.2"
image = "0.24"
instant = "0.1"
kamadak-exif = "0.5"
kurbo = "0.9"
log = "0.4"
Expand All @@ -157,7 +156,8 @@ unicode-segmentation = "1.0"
wasm-bindgen-futures = "0.4"
wasm-timer = "0.2"
web-sys = "0.3"
web-time = "0.2"
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", features = ["rwh_05"] }
7 changes: 3 additions & 4 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ log.workspace = true
thiserror.workspace = true
xxhash-rust.workspace = true
num-traits.workspace = true
web-time.workspace = true

palette.workspace = true
palette.optional = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
instant.workspace = true

[target.'cfg(windows)'.dependencies]
raw-window-handle.workspace = true
# TODO: Use `workspace` dependency once `wgpu` upgrades `raw-window-handle`
raw-window-handle = "0.6"

[dev-dependencies]
approx = "0.5"
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),
}
13 changes: 2 additions & 11 deletions core/src/time.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
//! Keep track of time, both in native and web platforms!

#[cfg(target_arch = "wasm32")]
pub use instant::Instant;

#[cfg(target_arch = "wasm32")]
pub use instant::Duration;

#[cfg(not(target_arch = "wasm32"))]
pub use std::time::Instant;

#[cfg(not(target_arch = "wasm32"))]
pub use std::time::Duration;
pub use web_time::Duration;
pub use web_time::Instant;
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