DioxusLabs / DioxusLabs/taffy

Allow sum of percent and points in Dimension

Open
#229 14 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
3.6k
Forks
226
Avg merge
10h 41m
Merged PRs (30d)
40

Description

## What problem does this solve or what need does it fill?

This allows using percentage with an offset in one dimension.

## What solution would you like?
```rust
/// A unit of linear measurement
///
/// This is commonly combined with [`Rect`], [`Point`](crate::geometry::Point) and [`Size`].
/// The default value is [`Dimension::Undefined`].
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Dimension {
/// The dimension is not given
Undefined,
/// The dimension should be automatically computed
Auto,
/// The dimension is stored in [points](https://en.wikipedia.org/wiki/Point_(typography))
///
/// Each point is about 0.353 mm in size.
Points(f32),
/// The dimension is stored in percentage relative to the parent item.
Percent(f32),
/// The dimension is stored as the sum of percentage relative to the parent item and point.
PercentPoints(f32, f32),
}
```
```rust
impl Dimension {
/// Is this value defined?
pub(crate) fn is_defined(self) -> bool {
matches!(self, Dimension::Points(_) | Dimension::Percent(_) | Dimension::PercentPoints(_, _))
}
}
```
```rust
impl MaybeResolve> for Dimension {
/// Converts the given [`Dimension`] into a concrete value of points
///
/// Can return `None`
fn maybe_resolve(self, context: Option) -> Option {
match self {
Dimension::Points(points) => Some(points),
// parent_dim * percent
Dimension::Percent(percent) => context.map(|dim| dim * percent),
Dimension::PercentPoints(percent, points) => Some(context.map_or(points, |dim| dim * percent + points)),
_ => None,
}
}
}
```
## What alternative(s) have you considered?
I've considered something similar to #225 but thought this was a better option.

Contributor guide

Open the contributing guide

Research direction

Locate the Dimension enum and its is_defined and MaybeResolve implementations, then inspect nearby uses and tests for the existing Points and Percent variants. Add the combined percentage-and-points case consistently, and confirm resolution works with and without a parent dimension while existing behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
frontend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.