Skip to content

Commit

Permalink
Unify shader::Program API with canvas::Program
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Nov 4, 2024
1 parent 61a7b64 commit 645ae3e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 57 deletions.
54 changes: 27 additions & 27 deletions widget/src/shader.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
);
}
}

Expand Down Expand Up @@ -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(
Expand Down
23 changes: 0 additions & 23 deletions widget/src/shader/event.rs

This file was deleted.

14 changes: 7 additions & 7 deletions widget/src/shader/program.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand All @@ -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(
Expand All @@ -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`].
Expand Down

0 comments on commit 645ae3e

Please sign in to comment.