Skip to content
Open
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
10 changes: 5 additions & 5 deletions masonry/examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use masonry::core::{
};
use masonry::imaging::Painter;
use masonry::kurbo::{Affine, Axis, BezPath, Point, Rect, Size, Stroke};
use masonry::layout::LenReq;
use masonry::layout::{AsUnit, LenReq, Length};
use masonry::parley::FontFamilyName;
use masonry::parley::style::{FontFamily, GenericFamily, StyleProperty};
use masonry::peniko::{Color, ImageBrush, ImageFormat};
Expand Down Expand Up @@ -80,16 +80,16 @@ impl Widget for CustomWidget {
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
_cross_length: Option<f64>,
) -> f64 {
_cross_length: Option<Length>,
) -> Length {
// We currently just define our preferred min/max,
// but often it takes actual work to derive these.
let min_size = Size::new(100., 50.);
let max_size = Size::new(200., 200.);

// Measurement is per axis, so we only care about a single dimension right now
let min_length = min_size.get_coord(axis);
let max_length = max_size.get_coord(axis);
let min_length = min_size.get_coord(axis).px();
let max_length = max_size.get_coord(axis).px();

// Return a result based on the parent's request
match len_req {
Expand Down
6 changes: 3 additions & 3 deletions masonry/examples/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use masonry::core::{
use masonry::imaging::Painter;
use masonry::kurbo::{Axis, Point, Size, Vec2};
use masonry::layers::Tooltip;
use masonry::layout::{AsUnit, LayoutSize, LenReq, SizeDef};
use masonry::layout::{AsUnit, LayoutSize, LenReq, Length, SizeDef};
use masonry::parley::FontWeight;
use masonry::peniko::Color;
use masonry::properties::{Background, BorderColor, BorderWidth, ContentColor};
Expand Down Expand Up @@ -117,8 +117,8 @@ impl Widget for OverlayBox {
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<f64>,
) -> f64 {
cross_length: Option<Length>,
) -> Length {
let auto_length = len_req.into();
let context_size = LayoutSize::maybe(axis.cross(), cross_length);

Expand Down
15 changes: 6 additions & 9 deletions masonry/src/doc/color_rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ use masonry::core::{Update, UpdateCtx};
// ---
use masonry::core::{LayoutCtx, MeasureCtx, PropertiesRef};
use masonry::kurbo::{Axis, Size};
use masonry::layout::LenReq;
use masonry::layout::{LenReq, Length};
// ---
use masonry::accesskit::{Node, Role};
use masonry::core::{AccessCtx, PaintCtx};
use masonry::imaging::Painter;
// ---
use masonry::core::WidgetId;
use masonry_core::layout::AsUnit;
use tracing::{Span, trace_span};
// ---
use masonry::core::{ChildrenIds, RegisterCtx};
Expand Down Expand Up @@ -130,16 +131,12 @@ impl Widget for ColorRectangle {
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
_cross_length: Option<f64>,
) -> f64 {
// TODO: Remove HACK: Until scale factor rework happens, just pretend it's always 1.0.
// https://github.com/linebender/xilem/issues/1264
let scale = 1.0;

_cross_length: Option<Length>,
) -> Length {
match len_req {
LenReq::MinContent | LenReq::MaxContent => match axis {
Axis::Horizontal => 200. * scale,
Axis::Vertical => 100. * scale,
Axis::Horizontal => 200.px(),
Axis::Vertical => 100.px(),
},
LenReq::FitContent(space) => space,
}
Expand Down
21 changes: 11 additions & 10 deletions masonry/src/doc/implementing_container_widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ A container widget needs to pay special attention to these methods:
trait Widget {
// ...

fn measure(&mut self, ctx: &mut MeasureCtx<'_>, props: &PropertiesRef<'_>, axis: Axis, len_req: LenReq, cross_length: Option<f64>) -> f64;
fn measure(&mut self, ctx: &mut MeasureCtx<'_>, props: &PropertiesRef<'_>, axis: Axis, len_req: LenReq, cross_length: Option<Length>) -> Length;
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, props: &PropertiesRef<'_>, size: Size);
fn compose(&mut self, ctx: &mut ComposeCtx<'_>);

Expand Down Expand Up @@ -87,7 +87,7 @@ For our `VerticalStack`, we'll lay out our children in a vertical line, with a g
```rust,ignore
use masonry::core::{LayoutCtx, MeasureCtx, PropertiesRef};
use masonry::kurbo::{Axis, Point, Size};
use masonry::layout::{LayoutSize, LenDef, LenReq, SizeDef};
use masonry::layout::{AsUnit, LayoutSize, Length, LenDef, LenReq, SizeDef};

impl Widget for VerticalStack {
// ...
Expand All @@ -98,28 +98,29 @@ impl Widget for VerticalStack {
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<f64>,
) -> f64 {
cross_length: Option<Length>,
) -> Length {
let (len_req, min_result) = match len_req {
LenReq::MinContent | LenReq::MaxContent => (len_req, 0.),
LenReq::MinContent | LenReq::MaxContent => (len_req, Length::ZERO),
LenReq::FitContent(space) => (LenReq::MinContent, space),
};

let auto_length = len_req.into();
let context_size = LayoutSize::maybe(axis.cross(), cross_length);

let mut length: f64 = 0.;
let mut length = Length::ZERO;
for child in &mut self.children {
let child_length = ctx.compute_length(child, auto_length, context_size, axis, cross_length);
match axis {
Axis::Horizontal => length = length.max(child_length),
Axis::Vertical => length += child_length,
Axis::Vertical => length = length.saturating_add(child_length),
}
}

if axis == Axis::Vertical {
let gap_count = (self.children.len() - 1) as f64;
length += gap_count * self.gap;
let gaps_length = gap_count * self.gap;
length = length.saturating_add(gaps_length.px());
}

min_result.max(length)
Expand All @@ -135,8 +136,8 @@ impl Widget for VerticalStack {
let total_child_vertical_space = size.height - self.gap * gap_count;
let child_vertical_space = total_child_vertical_space / self.children.len() as f64;

let width_def = LenDef::FitContent(size.width);
let height_def = LenDef::FitContent(child_vertical_space.max(0.));
let width_def = LenDef::FitContent(size.width.px());
let height_def = LenDef::FitContent(child_vertical_space.max(0.).px());
let auto_size = SizeDef::new(width_def, height_def);
let context_size = size.into();

Expand Down
15 changes: 6 additions & 9 deletions masonry/src/doc/implementing_widget.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait Widget {
fn on_anim_frame(&mut self, ctx: &mut UpdateCtx<'_>, props: &mut PropertiesMut<'_>, interval: u64);
fn update(&mut self, ctx: &mut UpdateCtx<'_>, props: &mut PropertiesMut<'_>, event: &Update);

fn measure(&mut self, ctx: &mut MeasureCtx<'_>, props: &PropertiesRef<'_>, axis: Axis, len_req: LenReq, cross_length: Option<f64>) -> f64;
fn measure(&mut self, ctx: &mut MeasureCtx<'_>, props: &PropertiesRef<'_>, axis: Axis, len_req: LenReq, cross_length: Option<Length>) -> Length;
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, props: &PropertiesRef<'_>, size: Size);

fn paint(&mut self, ctx: &mut PaintCtx<'_>, props: &PropertiesRef<'_>, painter: &mut Painter<'_>);
Expand Down Expand Up @@ -169,7 +169,7 @@ Next we implement layout:
```rust,ignore
use masonry::core::{LayoutCtx, MeasureCtx, PropertiesRef};
use masonry::kurbo::{Axis, Size};
use masonry::layout::LenReq;
use masonry::layout::{Length, LenReq};

impl Widget for ColorRectangle {
// ...
Expand All @@ -180,16 +180,13 @@ impl Widget for ColorRectangle {
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
_cross_length: Option<f64>,
) -> f64 {
// TODO: Remove HACK: Until scale factor rework happens, just pretend it's always 1.0.
// https://github.com/linebender/xilem/issues/1264
let scale = 1.0;
_cross_length: Option<Length>,
) -> Length {

match len_req {
LenReq::MinContent | LenReq::MaxContent => match axis {
Axis::Horizontal => 200. * scale,
Axis::Vertical => 100. * scale,
Axis::Horizontal => 200.px(),
Axis::Vertical => 100.px(),
}
LenReq::FitContent(space) => space,
}
Expand Down
19 changes: 10 additions & 9 deletions masonry/src/doc/vertical_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use masonry::core::{Widget, WidgetPod};
// ---
use masonry::core::{LayoutCtx, MeasureCtx, PropertiesRef};
use masonry::kurbo::{Axis, Point, Size};
use masonry::layout::{LayoutSize, LenDef, LenReq, SizeDef};
use masonry::layout::{AsUnit, LayoutSize, LenDef, LenReq, Length, SizeDef};
// ---
use masonry::core::ComposeCtx;
// ---
Expand Down Expand Up @@ -94,28 +94,29 @@ impl Widget for VerticalStack {
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<f64>,
) -> f64 {
cross_length: Option<Length>,
) -> Length {
let (len_req, min_result) = match len_req {
LenReq::MinContent | LenReq::MaxContent => (len_req, 0.),
LenReq::MinContent | LenReq::MaxContent => (len_req, Length::ZERO),
LenReq::FitContent(space) => (LenReq::MinContent, space),
};

let auto_length = len_req.into();
let context_size = LayoutSize::maybe(axis.cross(), cross_length);

let mut length: f64 = 0.;
let mut length = Length::ZERO;
for child in &mut self.children {
let child_length = ctx.compute_length(child, auto_length, context_size, axis, cross_length);
match axis {
Axis::Horizontal => length = length.max(child_length),
Axis::Vertical => length += child_length,
Axis::Vertical => length = length.saturating_add(child_length),
}
}

if axis == Axis::Vertical {
let gap_count = (self.children.len() - 1) as f64;
length += gap_count * self.gap;
let gaps_length = gap_count * self.gap;
length = length.saturating_add(gaps_length.px());
}

min_result.max(length)
Expand All @@ -131,8 +132,8 @@ impl Widget for VerticalStack {
let total_child_vertical_space = size.height - self.gap * gap_count;
let child_vertical_space = total_child_vertical_space / self.children.len() as f64;

let width_def = LenDef::FitContent(size.width);
let height_def = LenDef::FitContent(child_vertical_space.max(0.));
let width_def = LenDef::FitContent(size.width.px());
let height_def = LenDef::FitContent(child_vertical_space.max(0.).px());
let auto_size = SizeDef::new(width_def, height_def);
let context_size = size.into();

Expand Down
31 changes: 12 additions & 19 deletions masonry/src/layers/selector_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::core::{
};
use crate::imaging::Painter;
use crate::kurbo::{Axis, Point, Size};
use crate::layout::{LayoutSize, LenDef, LenReq, SizeDef};
use crate::layout::{AsUnit, LayoutSize, LenDef, LenReq, Length, SizeDef};
use crate::properties::Gap;
use crate::widgets::{SelectionChanged, Selector};

Expand Down Expand Up @@ -236,58 +236,51 @@ impl Widget for SelectorMenu {
props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<f64>,
) -> f64 {
// TODO: Remove HACK: Until scale factor rework happens, just pretend it's always 1.0.
// https://github.com/linebender/xilem/issues/1264
let scale = 1.0;

cross_length: Option<Length>,
) -> Length {
let cache = ctx.property_cache();
let gap = props.get::<Gap>(cache);

let gap_length = gap.gap.dp(scale);
let gap_length = gap.gap.get();

let (len_req, min_result) = match len_req {
LenReq::MinContent | LenReq::MaxContent => (len_req, 0.),
LenReq::MinContent | LenReq::MaxContent => (len_req, Length::ZERO),
LenReq::FitContent(space) => (LenReq::MinContent, space),
};

let auto_length = len_req.into();
let context_size = LayoutSize::maybe(axis.cross(), cross_length);

let mut length: f64 = 0.;
let mut length = Length::ZERO;
for child in &mut self.children {
let child_length =
ctx.compute_length(child, auto_length, context_size, axis, cross_length);
match axis {
Axis::Horizontal => length = length.max(child_length),
Axis::Vertical => length += child_length,
Axis::Vertical => length = length.saturating_add(child_length),
}
}

if axis == Axis::Vertical {
let gap_count = (self.children.len() - 1) as f64;
length += gap_count * gap_length;
let gaps_length = gap_count * gap_length;
length = length.saturating_add(gaps_length.px());
}

min_result.max(length)
}

fn layout(&mut self, ctx: &mut LayoutCtx<'_>, props: &PropertiesRef<'_>, size: Size) {
// TODO: Remove HACK: Until scale factor rework happens, just pretend it's always 1.0.
// https://github.com/linebender/xilem/issues/1264
let scale = 1.0;

let cache = ctx.property_cache();
let gap = props.get::<Gap>(cache);

let gap_count = (self.children.len() - 1) as f64;
let gap_length = gap.gap.dp(scale);
let gap_length = gap.gap.get();
let total_child_vertical_space = size.height - gap_length * gap_count;
let child_vertical_space = total_child_vertical_space / self.children.len() as f64;

let width_def = LenDef::FitContent(size.width);
let height_def = LenDef::FitContent(child_vertical_space.max(0.));
let width_def = LenDef::FitContent(size.width.px());
let height_def = LenDef::FitContent(child_vertical_space.max(0.).px());
let auto_size = SizeDef::new(width_def, height_def);
let context_size = size.into();

Expand Down
6 changes: 3 additions & 3 deletions masonry/src/layers/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::core::{
};
use crate::imaging::Painter;
use crate::kurbo::{Axis, Size};
use crate::layout::{LayoutSize, LenReq, SizeDef};
use crate::layout::{LayoutSize, LenReq, Length, SizeDef};

/// A [`Layer`] representing a simple tooltip showing some content until the mouse moves.
pub struct Tooltip {
Expand Down Expand Up @@ -94,8 +94,8 @@ impl Widget for Tooltip {
_props: &PropertiesRef<'_>,
axis: Axis,
len_req: LenReq,
cross_length: Option<f64>,
) -> f64 {
cross_length: Option<Length>,
) -> Length {
let auto_length = len_req.into();
let context_size = LayoutSize::maybe(axis.cross(), cross_length);

Expand Down
Loading
Loading