summaryrefslogtreecommitdiffstats
path: root/widget
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--widget/src/shader.rs54
-rw-r--r--widget/src/shader/event.rs23
-rw-r--r--widget/src/shader/program.rs14
3 files changed, 34 insertions, 57 deletions
diff --git a/widget/src/shader.rs b/widget/src/shader.rs
index 115a5ed9..8ec57482 100644
--- a/widget/src/shader.rs
+++ b/widget/src/shader.rs
@@ -1,23 +1,22 @@
//! A custom shader widget for wgpu applications.
-mod event;
mod program;
-pub use event::Event;
pub use program::Program;
-use crate::core;
+use crate::core::event;
use crate::core::layout::{self, Layout};
use crate::core::mouse;
use crate::core::renderer;
use crate::core::widget::tree::{self, Tree};
use crate::core::widget::{self, Widget};
use crate::core::window;
-use crate::core::{Clipboard, Element, Length, Rectangle, Shell, Size};
+use crate::core::{Clipboard, Element, Event, Length, Rectangle, Shell, Size};
use crate::renderer::wgpu::primitive;
use std::marker::PhantomData;
pub use crate::graphics::Viewport;
+pub use crate::Action;
pub use primitive::{Primitive, Storage};
/// A widget which can render custom shaders with Iced's `wgpu` backend.
@@ -100,28 +99,30 @@ where
) {
let bounds = layout.bounds();
- let custom_shader_event = match event {
- core::Event::Mouse(mouse_event) => Some(Event::Mouse(mouse_event)),
- core::Event::Keyboard(keyboard_event) => {
- Some(Event::Keyboard(keyboard_event))
+ let state = tree.state.downcast_mut::<P::State>();
+
+ if let Some(action) = self.program.update(state, event, bounds, cursor)
+ {
+ let (message, redraw_request, event_status) = action.into_inner();
+
+ if let Some(message) = message {
+ shell.publish(message);
}
- core::Event::Touch(touch_event) => Some(Event::Touch(touch_event)),
- core::Event::Window(window::Event::RedrawRequested(instant)) => {
- Some(Event::RedrawRequested(instant))
+
+ if let Some(redraw_request) = redraw_request {
+ match redraw_request {
+ window::RedrawRequest::NextFrame => {
+ shell.request_redraw();
+ }
+ window::RedrawRequest::At(at) => {
+ shell.request_redraw_at(at);
+ }
+ }
+ }
+
+ if event_status == event::Status::Captured {
+ shell.capture_event();
}
- core::Event::Window(_) => None,
- };
-
- if let Some(custom_shader_event) = custom_shader_event {
- let state = tree.state.downcast_mut::<P::State>();
-
- self.program.update(
- state,
- custom_shader_event,
- bounds,
- cursor,
- shell,
- );
}
}
@@ -186,9 +187,8 @@ where
event: Event,
bounds: Rectangle,
cursor: mouse::Cursor,
- shell: &mut Shell<'_, Message>,
- ) {
- T::update(self, state, event, bounds, cursor, shell);
+ ) -> Option<Action<Message>> {
+ T::update(self, state, event, bounds, cursor)
}
fn draw(
diff --git a/widget/src/shader/event.rs b/widget/src/shader/event.rs
deleted file mode 100644
index 2d7c79bb..00000000
--- a/widget/src/shader/event.rs
+++ /dev/null
@@ -1,23 +0,0 @@
-//! Handle events of a custom shader widget.
-use crate::core::keyboard;
-use crate::core::mouse;
-use crate::core::time::Instant;
-use crate::core::touch;
-
-/// A [`Shader`] event.
-///
-/// [`Shader`]: crate::Shader
-#[derive(Debug, Clone, PartialEq)]
-pub enum Event {
- /// A mouse event.
- Mouse(mouse::Event),
-
- /// A touch event.
- Touch(touch::Event),
-
- /// A keyboard event.
- Keyboard(keyboard::Event),
-
- /// A window requested a redraw.
- RedrawRequested(Instant),
-}
diff --git a/widget/src/shader/program.rs b/widget/src/shader/program.rs
index 5124a1cc..0fc110af 100644
--- a/widget/src/shader/program.rs
+++ b/widget/src/shader/program.rs
@@ -1,7 +1,7 @@
use crate::core::mouse;
-use crate::core::{Rectangle, Shell};
+use crate::core::Rectangle;
use crate::renderer::wgpu::Primitive;
-use crate::shader;
+use crate::shader::{self, Action};
/// The state and logic of a [`Shader`] widget.
///
@@ -17,10 +17,10 @@ pub trait Program<Message> {
type Primitive: Primitive + 'static;
/// Update the internal [`State`] of the [`Program`]. This can be used to reflect state changes
- /// based on mouse & other events. You can use the [`Shell`] to publish messages, request a
- /// redraw for the window, etc.
+ /// based on mouse & other events. You can return an [`Action`] to publish a message, request a
+ /// redraw, or capture the event.
///
- /// By default, this method does and returns nothing.
+ /// By default, this method returns `None`.
///
/// [`State`]: Self::State
fn update(
@@ -29,8 +29,8 @@ pub trait Program<Message> {
_event: shader::Event,
_bounds: Rectangle,
_cursor: mouse::Cursor,
- _shell: &mut Shell<'_, Message>,
- ) {
+ ) -> Option<Action<Message>> {
+ None
}
/// Draws the [`Primitive`].