Skip to content

Commit b9512e5

Browse files
committed
Updating changelog + API cleanup
While updating the changelog I noticed that size now returned a layout, and it seemed more appropriate to rename the function even though it's a larger breaking change
1 parent 0ab6f62 commit b9512e5

File tree

6 files changed

+38
-20
lines changed

6 files changed

+38
-20
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
127127
value containers.
128128
- `cushy::value::CallbackDisconnected` and `cushy::value::CallbackHandle` are
129129
now exported from `cushy::reactive`.
130+
- Cushy now generically supports `HorizontalAlignment` and `VerticalAlignment`
131+
of widgets contained within other widgets. Additionally, baseline vertical
132+
alignment support has been added to make it simple to align widgets vertically
133+
based on the content of the widget. To support this, many APIs have required
134+
changes:
135+
136+
- `Widget::layout()` and `LayoutContext::layout()` now returns `WidgetLayout`.
137+
- `IndicatorBehavior::size()` has been renamed to
138+
`IndicatorBehavior::layout()` and now returns `WidgetLayout`.
139+
- `WrappedLayout` now has the field `baseline: Baseline`.
140+
- `WrappedLayout::aligned` now accepts `Into<WidgetLayout>` for its first
141+
parameter. This should be mostly compatible with existing code.
142+
- `WrapperWidget::position_child` now accepts a `WidgetLayout` instead of a
143+
`Size<UPx>` as its first parameter.
144+
- `Button`'s label is automatically centered horizontally using a new style
145+
component: `ButtonLabelAlignment`.
130146

131147
### Changed
132148

src/widgets/checkbox.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ fn indicator_layout(context: &mut GraphicsContext<'_, '_, '_, '_>) -> WidgetLayo
214214
impl IndicatorBehavior for CheckboxIndicator {
215215
type Colors = CheckboxColors;
216216

217-
fn size(&self, context: &mut GraphicsContext<'_, '_, '_, '_>) -> WidgetLayout {
217+
fn layout(&self, context: &mut GraphicsContext<'_, '_, '_, '_>) -> WidgetLayout {
218218
indicator_layout(context)
219219
}
220220

src/widgets/grid.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,6 @@ impl GridLayout {
684684
row_index: usize,
685685
vertical_alignment: VerticalAlign,
686686
) -> Rect<Px> {
687-
// TODO We need to honor expanding the child if we are supposed to expand it.
688687
let mut position = Rect::new(
689688
self.orientation
690689
.make_point(layout.offset, other_offset)

src/widgets/indicator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ pub trait IndicatorBehavior: Send + Debug + 'static {
5959
region: Rect<Px>,
6060
context: &mut GraphicsContext<'_, '_, '_, '_>,
6161
);
62-
/// Returns the size of this indicator.
63-
fn size(&self, context: &mut GraphicsContext<'_, '_, '_, '_>) -> WidgetLayout;
62+
/// Returns the size and basline of this indicator.
63+
fn layout(&self, context: &mut GraphicsContext<'_, '_, '_, '_>) -> WidgetLayout;
6464
}
6565

6666
/// The current state of an [`Indicator`] widget.
@@ -252,7 +252,7 @@ where
252252
context: &mut LayoutContext<'_, '_, '_, '_>,
253253
) -> WidgetLayout {
254254
let window_local = self.per_window.entry(context).or_default();
255-
let indicator_layout = self.behavior.size(context);
255+
let indicator_layout = self.behavior.layout(context);
256256
window_local.size = indicator_layout.size.ceil();
257257
window_local.checkbox_region.size = window_local.size.into_signed();
258258

src/widgets/menu.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use crate::styles::components::{
2424
};
2525
use crate::styles::Styles;
2626
use crate::widget::{
27-
Callback, EventHandling, MakeWidget, MakeWidgetWithTag, SharedNotify, Widget, WidgetId,
28-
WidgetInstance, WidgetLayout, WidgetRef, WidgetTag, HANDLED,
27+
Baseline, Callback, EventHandling, MakeWidget, MakeWidgetWithTag, SharedNotify, Widget,
28+
WidgetId, WidgetInstance, WidgetLayout, WidgetRef, WidgetTag, HANDLED,
2929
};
3030
use crate::ConstraintLimit;
3131

@@ -664,20 +664,21 @@ where
664664
let available_width = available_space.width.max() - double_padding;
665665

666666
let mut y = self.padding;
667-
for rendered in &mut self.items {
667+
let mut first_baseline = Baseline::NONE;
668+
for (index, rendered) in self.items.iter_mut().enumerate() {
668669
let (height, full_height) = match &mut rendered.item {
669670
ItemKind::Item(item) => {
670671
let mounted = item.contents.mounted(context);
671672
let available_width = available_width - submenu_space;
672-
let size = context
673-
.for_other(&mounted)
674-
.layout(Size::new(
675-
ConstraintLimit::SizeToFit(available_width),
676-
ConstraintLimit::SizeToFit(remaining_height),
677-
))
678-
.size;
679-
maximum_item_width = maximum_item_width.max(size.width);
680-
(size.height, size.height + double_padding)
673+
let layout = context.for_other(&mounted).layout(Size::new(
674+
ConstraintLimit::SizeToFit(available_width),
675+
ConstraintLimit::SizeToFit(remaining_height),
676+
));
677+
maximum_item_width = maximum_item_width.max(layout.size.width);
678+
if index == 0 {
679+
first_baseline = layout.baseline;
680+
}
681+
(layout.size.height, layout.size.height + double_padding)
681682
}
682683
ItemKind::Separator => (UPx::ZERO, self.padding),
683684
};
@@ -704,8 +705,10 @@ where
704705
);
705706
}
706707

707-
// TODO should a Menu report its baseline?
708-
Size::new(maximum_item_width + double_padding * 2 + submenu_space, y).into()
708+
WidgetLayout {
709+
size: Size::new(maximum_item_width + double_padding * 2 + submenu_space, y),
710+
baseline: first_baseline,
711+
}
709712
}
710713

711714
fn hit_test(

src/widgets/radio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ where
165165
{
166166
type Colors = RadioColors;
167167

168-
fn size(&self, context: &mut GraphicsContext<'_, '_, '_, '_>) -> WidgetLayout {
168+
fn layout(&self, context: &mut GraphicsContext<'_, '_, '_, '_>) -> WidgetLayout {
169169
let size = Size::squared(context.get(&RadioSize).into_upx(context.gfx.scale()).ceil());
170170
let outline_width = context
171171
.get(&OutlineWidth)

0 commit comments

Comments
 (0)