summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--examples/clock/Cargo.toml1
-rw-r--r--examples/clock/src/main.rs2
-rw-r--r--examples/the_matrix/Cargo.toml1
-rw-r--r--examples/the_matrix/src/main.rs39
4 files changed, 23 insertions, 20 deletions
diff --git a/examples/clock/Cargo.toml b/examples/clock/Cargo.toml
index 2d3d5908..dc2e5382 100644
--- a/examples/clock/Cargo.toml
+++ b/examples/clock/Cargo.toml
@@ -10,3 +10,4 @@ iced.workspace = true
iced.features = ["canvas", "tokio", "debug"]
time = { version = "0.3", features = ["local-offset"] }
+tracing-subscriber = "0.3"
diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs
index 897f8f1b..d717db36 100644
--- a/examples/clock/src/main.rs
+++ b/examples/clock/src/main.rs
@@ -8,6 +8,8 @@ use iced::{
};
pub fn main() -> iced::Result {
+ tracing_subscriber::fmt::init();
+
iced::program("Clock - Iced", Clock::update, Clock::view)
.subscription(Clock::subscription)
.theme(Clock::theme)
diff --git a/examples/the_matrix/Cargo.toml b/examples/the_matrix/Cargo.toml
index 17cf443b..775e76e0 100644
--- a/examples/the_matrix/Cargo.toml
+++ b/examples/the_matrix/Cargo.toml
@@ -10,3 +10,4 @@ iced.workspace = true
iced.features = ["canvas", "tokio", "debug"]
rand = "0.8"
+tracing-subscriber = "0.3"
diff --git a/examples/the_matrix/src/main.rs b/examples/the_matrix/src/main.rs
index 55c9da4b..f3a67ac8 100644
--- a/examples/the_matrix/src/main.rs
+++ b/examples/the_matrix/src/main.rs
@@ -1,22 +1,25 @@
use iced::mouse;
use iced::time::{self, Instant};
use iced::widget::canvas;
-use iced::widget::canvas::{Cache, Geometry};
use iced::{
Color, Element, Font, Length, Point, Rectangle, Renderer, Subscription,
Theme,
};
+use std::cell::RefCell;
+
pub fn main() -> iced::Result {
+ tracing_subscriber::fmt::init();
+
iced::program("The Matrix - Iced", TheMatrix::update, TheMatrix::view)
.subscription(TheMatrix::subscription)
.antialiasing(true)
.run()
}
+#[derive(Default)]
struct TheMatrix {
- ticks: usize,
- backgrounds: Vec<Cache>,
+ tick: usize,
}
#[derive(Debug, Clone, Copy)]
@@ -28,7 +31,7 @@ impl TheMatrix {
fn update(&mut self, message: Message) {
match message {
Message::Tick(_now) => {
- self.ticks += 1;
+ self.tick += 1;
}
}
}
@@ -45,35 +48,31 @@ impl TheMatrix {
}
}
-impl Default for TheMatrix {
- fn default() -> Self {
- let mut backgrounds = Vec::with_capacity(30);
- backgrounds.resize_with(30, Cache::default);
-
- Self {
- ticks: 0,
- backgrounds,
- }
- }
-}
-
impl<Message> canvas::Program<Message> for TheMatrix {
- type State = ();
+ type State = RefCell<Vec<canvas::Cache>>;
fn draw(
&self,
- _state: &Self::State,
+ state: &Self::State,
renderer: &Renderer,
_theme: &Theme,
bounds: Rectangle,
_cursor: mouse::Cursor,
- ) -> Vec<Geometry> {
+ ) -> Vec<canvas::Geometry> {
use rand::distributions::Distribution;
use rand::Rng;
const CELL_SIZE: f32 = 10.0;
- vec![self.backgrounds[self.ticks % self.backgrounds.len()].draw(
+ let mut caches = state.borrow_mut();
+
+ if caches.is_empty() {
+ let group = canvas::Group::unique();
+
+ caches.resize_with(30, || canvas::Cache::with_group(group));
+ }
+
+ vec![caches[self.tick % caches.len()].draw(
renderer,
bounds.size(),
|frame| {