Skip to content

Commit

Permalink
Refactored Scroll to be UPx
Browse files Browse the repository at this point in the history
Also addressed comments in #185
  • Loading branch information
ecton committed Oct 17, 2024
1 parent e3898bd commit 51649a3
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 99 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,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
37 changes: 20 additions & 17 deletions examples/virtual-scroll-list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@ use cushy::styles::{Dimension, DimensionRange, Edges};
use cushy::value::{Destination, Dynamic, Source};
use cushy::widget::MakeWidget;
use cushy::Run;
use figures::units::{Lp, Px};
use figures::units::{Lp, UPx};
use figures::{Point, Size};

fn list() -> impl MakeWidget {
let height = Lp::inches(10);
let content_size: Dynamic<Size<Px>> = Dynamic::default();
let content_size: Dynamic<Size<UPx>> = Dynamic::default();
let control_size = Dynamic::default();
let current_scroll: Dynamic<Point<Px>> = 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_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)
Expand All @@ -29,23 +25,30 @@ fn list() -> impl MakeWidget {
.and("Hello world!")
.into_rows()
.pad_by(current_scroll.map_each(|scroll| Edges {
top: Dimension::from(-scroll.y),
top: Dimension::from(scroll.y),
..Default::default()
}))
.size(Size::new(DimensionRange::default(), DimensionRange::from(height)));
.size(Size::new(
DimensionRange::default(),
DimensionRange::from(height),
));

let scroll = content.scroll();

scroll.get_content_size()
scroll
.content_size()
.for_each_cloned(move |s| content_size.set(s))
.persist();
scroll.get_control_size()
scroll
.control_size()
.for_each_cloned(move |s| control_size.set(s))
.persist();
scroll.get_scroll()
scroll
.scroll
.for_each_cloned(move |s| current_scroll.set(s))
.persist();
scroll.get_max_scroll()
scroll
.max_scroll()
.for_each_cloned(move |s| max_scroll.set(s))
.persist();

Expand Down
6 changes: 6 additions & 0 deletions 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
Loading

0 comments on commit 51649a3

Please sign in to comment.