Skip to content

Commit

Permalink
fix baseline offset for sized widget
Browse files Browse the repository at this point in the history
Baseline offset is relative to the bottom of a widget. Current code
assume that the widget has exacltly the required height. For sized
widgets, where there is more vertical space, the baseline is totally
wrong. This patch series considers the additional space to adjust the
baseline offset.

Signed-off-by: Dietmar Maurer <[email protected]>
  • Loading branch information
maurerdietmar committed Jan 6, 2022
1 parent d5485ee commit 8fa6b5a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
7 changes: 5 additions & 2 deletions druid/src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ impl<T: Data> Widget<T> for Button<T> {
// HACK: to make sure we look okay at default sizes when beside a textbox,
// we make sure we will have at least the same height as the default textbox.
let min_height = env.get(theme::BORDERED_WIDGET_HEIGHT);
let baseline = self.label.baseline_offset();
ctx.set_baseline_offset(baseline + LABEL_INSETS.y1);

let button_size = bc.constrain(Size::new(
self.label_size.width + padding.width,
(self.label_size.height + padding.height).max(min_height),
));

let extra_height = (button_size.height - self.label_size.height).max(0.0);
let baseline = self.label.baseline_offset() + extra_height / 2.0;
ctx.set_baseline_offset(baseline);

trace!("Computed button size: {}", button_size);
button_size
}
Expand Down
2 changes: 1 addition & 1 deletion druid/src/widget/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,11 @@ impl<T: TextStorage> Widget<T> for RawLabel<T> {
self.layout.rebuild_if_needed(ctx.text(), env);

let text_metrics = self.layout.layout_metrics();
ctx.set_baseline_offset(text_metrics.size.height - text_metrics.first_baseline);
let size = bc.constrain(Size::new(
text_metrics.size.width + 2. * LABEL_X_PADDING,
text_metrics.size.height,
));
ctx.set_baseline_offset(size.height - text_metrics.first_baseline);
trace!("Computed size: {}", size);
size
}
Expand Down

0 comments on commit 8fa6b5a

Please sign in to comment.