Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
application to create deadlocks with relatively simple data flows like the new
`7guis-timer` example.
- The type `cushy::Graphics` is now available at `cushy::graphics::Graphics`.
- `Source::for_each_cloned_*` now invoke the callback with the current contents
of of the source before attaching the callback. New functions beginning with
`for_each_subsequent_cloned` have been added with the original behavior. This
change was done to make `for_each_cloned` and `for_each` have the same
semantics.

### Changed

Expand Down
58 changes: 58 additions & 0 deletions examples/channels.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::time::{Duration, Instant};

use cushy::value::{Destination, Dynamic, Source};
use cushy::widget::MakeWidget;
use cushy::widgets::label::Displayable;
use cushy::Run;

fn main() -> cushy::Result {
let channel_counter = Dynamic::new(0_usize);
let channel_counter_label = channel_counter.to_label();
let sender = cushy::channel::build()
.on_receive({
move |_| {
std::thread::sleep(Duration::from_secs(1));
*channel_counter.lock() += 1;
}
})
.finish();

let dynamic_counter = Dynamic::new(0_usize);
let dynamic_counter_label = dynamic_counter.to_label();
let dynamic_value = Dynamic::new(Instant::now());
// We use a `for_each_subsequent_cloned` to only execute the callback after
// the current value changes, and the `cloned` version ensures the dynamic
// isn't locked while the callback is being executed.
dynamic_value
.for_each_subsequent_cloned(move |_| {
std::thread::sleep(Duration::from_secs(1));
*dynamic_counter.lock() += 1;
})
.persist();

"Channels ensure every value sent is received. Try \
clicking the button quickly and seeing how the \
channel version increments for every click while \
the dynamic version increments at most once every \
second."
.and(
"Click Me"
.into_button()
.on_click(move |_| {
let now = Instant::now();
sender.send(now).expect("value to be received");
dynamic_value.set(now);
})
.centered(),
)
.and(
"Channel Counter"
.and(channel_counter_label)
.into_rows()
.and("Dynamic Counter".and(dynamic_counter_label).into_rows())
.into_columns()
.centered(),
)
.into_rows()
.run()
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ mod names;
#[macro_use]
pub mod styles;
mod app;
mod reactive;
pub use reactive::{channel, value};
pub mod debug;
pub mod fonts;
mod tick;
mod tree;
pub mod value;
pub mod widget;
pub mod widgets;
pub mod window;
Expand Down
Loading
Loading