Skip to content

Commit

Permalink
Merge branch 'virtual-scroll-list-example'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Oct 17, 2024
2 parents 5a5f6c9 + 51649a3 commit 5adb37d
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 79 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
default is `true`, which was the behavior before this flag was added.
- `Image` now supports `ImageCornerRadius`. Thanks to @danbulant for helping
with this change!
- `Scroll` now exposes its scroll amount, maximum scroll, and more information
that allows completely customizing a scroll view's behavior. Thanks to
@danbulant for helping with this change!


[139]: https://github.com/khonsulabs/cushy/issues/139
Expand Down
65 changes: 65 additions & 0 deletions examples/virtual-scroll-list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use cushy::styles::{Dimension, DimensionRange, Edges};
use cushy::value::{Destination, Dynamic, Source};
use cushy::widget::MakeWidget;
use cushy::Run;
use figures::units::{Lp, UPx};
use figures::{Point, Size};

fn list() -> impl MakeWidget {
let height = Lp::inches(10);
let content_size: Dynamic<Size<UPx>> = Dynamic::default();
let control_size = Dynamic::default();
let current_scroll: Dynamic<Point<UPx>> = Dynamic::default();
let max_scroll = Dynamic::default();

let content = content_size.map_each(|s| format!("Content size: {:?};", s));
let control = control_size.map_each(|s| format!("Control size: {:?};", s));
let scroll = current_scroll.map_each(|s| format!("Current scroll: {:?};", s));
let max = max_scroll.map_each(|s| format!("Max scroll: {:?};", s));

let content = content
.and(control)
.and(scroll)
.and(max)
.into_columns()
.and("Hello world!")
.into_rows()
.pad_by(current_scroll.map_each(|scroll| Edges {
top: Dimension::from(scroll.y),
..Default::default()
}))
.size(Size::new(
DimensionRange::default(),
DimensionRange::from(height),
));

let scroll = content.scroll();

scroll
.content_size()
.for_each_cloned(move |s| content_size.set(s))
.persist();
scroll
.control_size()
.for_each_cloned(move |s| control_size.set(s))
.persist();
scroll
.scroll
.for_each_cloned(move |s| current_scroll.set(s))
.persist();
scroll
.max_scroll()
.for_each_cloned(move |s| max_scroll.set(s))
.persist();

scroll.expand()
}

fn main() -> cushy::Result {
list().run()
}

#[test]
fn runs() {
cushy::example!(list).untested_still_frame();
}
3 changes: 2 additions & 1 deletion guide/src/about/reactive.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ change. Let's revisit the example from the [intro](../intro.md):
Both the [`Input`][input] and the [`Label`][label] widgets have been given
instances of `Dynamic<String>`s, but they are two different dynamics. The text
input field was given the dynamic we want to be edited. We react to the changes
through the `name.map_each(...)` callback.
through the `name.map_each(...)` callback. You can react to multiple `Dynamic`s
at once using `(&name, &surname).map_each(...)` callback.

## What is a `DynamicReader<T>`?

Expand Down
8 changes: 7 additions & 1 deletion src/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,12 @@ impl Default for Dimension {
}
}

impl From<UPx> for Dimension {
fn from(value: UPx) -> Self {
Self::Px(value.into_signed())
}
}

impl From<Px> for Dimension {
fn from(value: Px) -> Self {
Self::Px(value)
Expand Down Expand Up @@ -1212,7 +1218,7 @@ impl NamedComponent for Cow<'_, ComponentName> {
}

/// A type describing characteristics about the edges of a rectangle.
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Edges<T = FlexibleDimension> {
/// The left edge
pub left: T,
Expand Down
Loading

0 comments on commit 5adb37d

Please sign in to comment.