Skip to content

Commit

Permalink
Expose shell to canvas::Program to match shader::Program.
Browse files Browse the repository at this point in the history
Allows a canvas program to schedule redraws as needed without managing it externally.
  • Loading branch information
dtzxporter committed Sep 26, 2024
1 parent 7554837 commit 3a9748c
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/bezier_tool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ publish = false

[dependencies]
iced.workspace = true
iced.features = ["canvas", "debug"]
iced.features = ["canvas", "debug", "advanced"]
2 changes: 2 additions & 0 deletions examples/bezier_tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Example {
}

mod bezier {
use iced::advanced::Shell;
use iced::mouse;
use iced::widget::canvas::event::{self, Event};
use iced::widget::canvas::{self, Canvas, Frame, Geometry, Path, Stroke};
Expand Down Expand Up @@ -96,6 +97,7 @@ mod bezier {
event: Event,
bounds: Rectangle,
cursor: mouse::Cursor,
_shell: &mut Shell<'_, Curve>,
) -> (event::Status, Option<Curve>) {
let Some(cursor_position) = cursor.position_in(bounds) else {
return (event::Status::Ignored, None);
Expand Down
2 changes: 1 addition & 1 deletion examples/game_of_life/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dependencies]
iced.workspace = true
iced.features = ["debug", "canvas", "tokio"]
iced.features = ["debug", "canvas", "advanced", "tokio"]

itertools = "0.12"
rustc-hash.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ fn view_controls<'a>(

mod grid {
use crate::Preset;
use iced::advanced::Shell;
use iced::alignment;
use iced::mouse;
use iced::touch;
Expand Down Expand Up @@ -383,6 +384,7 @@ mod grid {
event: Event,
bounds: Rectangle,
cursor: mouse::Cursor,
_shell: &mut Shell<'_, Message>,
) -> (event::Status, Option<Message>) {
if let Event::Mouse(mouse::Event::ButtonReleased(_)) = event {
*interaction = Interaction::None;
Expand Down
2 changes: 1 addition & 1 deletion examples/multitouch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false

[dependencies]
iced.workspace = true
iced.features = ["debug", "canvas", "tokio"]
iced.features = ["debug", "canvas", "tokio", "advanced"]

tracing-subscriber = "0.3"
voronator = "0.2"
2 changes: 2 additions & 0 deletions examples/multitouch/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This example shows how to use touch events in `Canvas` to draw
//! a circle around each fingertip. This only works on touch-enabled
//! computers like Microsoft Surface.
use iced::advanced::Shell;
use iced::mouse;
use iced::touch;
use iced::widget::canvas::event;
Expand Down Expand Up @@ -59,6 +60,7 @@ impl canvas::Program<Message> for Multitouch {
event: event::Event,
_bounds: Rectangle,
_cursor: mouse::Cursor,
_shell: &mut Shell<'_, Message>,
) -> (event::Status, Option<Message>) {
match event {
event::Event::Touch(touch_event) => match touch_event {
Expand Down
2 changes: 1 addition & 1 deletion examples/sierpinski_triangle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ publish = false

[dependencies]
iced.workspace = true
iced.features = ["debug", "canvas"]
iced.features = ["debug", "canvas", "advanced"]

rand = "0.8"
2 changes: 2 additions & 0 deletions examples/sierpinski_triangle/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use iced::advanced::Shell;
use iced::mouse;
use iced::widget::canvas::event::{self, Event};
use iced::widget::canvas::{self, Canvas, Geometry};
Expand Down Expand Up @@ -80,6 +81,7 @@ impl canvas::Program<Message> for SierpinskiGraph {
event: Event,
bounds: Rectangle,
cursor: mouse::Cursor,
_shell: &mut Shell<'_, Message>,
) -> (event::Status, Option<Message>) {
let Some(cursor_position) = cursor.position_in(bounds) else {
return (event::Status::Ignored, None);
Expand Down
3 changes: 2 additions & 1 deletion widget/src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ where
let state = tree.state.downcast_mut::<P::State>();

let (event_status, message) =
self.program.update(state, canvas_event, bounds, cursor);
self.program
.update(state, canvas_event, bounds, cursor, shell);

if let Some(message) = message {
shell.publish(message);
Expand Down
6 changes: 4 additions & 2 deletions widget/src/canvas/program.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::canvas::event::{self, Event};
use crate::canvas::mouse;
use crate::canvas::Geometry;
use crate::core::Rectangle;
use crate::core::{Rectangle, Shell};
use crate::graphics::geometry;

/// The state and logic of a [`Canvas`].
Expand Down Expand Up @@ -34,6 +34,7 @@ where
_event: Event,
_bounds: Rectangle,
_cursor: mouse::Cursor,
_shell: &mut Shell<'_, Message>,
) -> (event::Status, Option<Message>) {
(event::Status::Ignored, None)
}
Expand Down Expand Up @@ -84,8 +85,9 @@ where
event: Event,
bounds: Rectangle,
cursor: mouse::Cursor,
shell: &mut Shell<'_, Message>,
) -> (event::Status, Option<Message>) {
T::update(self, state, event, bounds, cursor)
T::update(self, state, event, bounds, cursor, shell)
}

fn draw(
Expand Down

0 comments on commit 3a9748c

Please sign in to comment.