Unify shader::Program API with canvas::Program

This commit is contained in:
Héctor Ramón Jiménez 2024-11-04 18:26:46 +01:00
parent 14ec330730
commit 6fc16769c4
No known key found for this signature in database
GPG key ID: 4C07CEC81AFA161F
3 changed files with 34 additions and 57 deletions

View file

@ -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))
}
core::Event::Touch(touch_event) => Some(Event::Touch(touch_event)),
core::Event::Window(window::Event::RedrawRequested(instant)) => {
Some(Event::RedrawRequested(instant))
}
core::Event::Window(_) => None,
};
let state = tree.state.downcast_mut::<P::State>();
if let Some(custom_shader_event) = custom_shader_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();
self.program.update(
state,
custom_shader_event,
bounds,
cursor,
shell,
);
if let Some(message) = message {
shell.publish(message);
}
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();
}
}
}
@ -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(

View file

@ -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),
}

View file

@ -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`].