Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix baseline offset for sized widget #2110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
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
8 changes: 5 additions & 3 deletions druid/src/widget/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub struct RawLabel<T> {

disabled: bool,
default_text_color: KeyOrValue<Color>,
baseline_offset: f64,
}

/// Options for handling lines that are too wide for the label.
Expand Down Expand Up @@ -160,6 +161,7 @@ impl<T: TextStorage> RawLabel<T> {
line_break_mode: LineBreaking::Overflow,
disabled: false,
default_text_color: crate::theme::TEXT_COLOR.into(),
baseline_offset: 0f64,
}
}

Expand Down Expand Up @@ -287,8 +289,7 @@ impl<T: TextStorage> RawLabel<T> {

/// Return the offset of the first baseline relative to the bottom of the widget.
pub fn baseline_offset(&self) -> f64 {
let text_metrics = self.layout.layout_metrics();
text_metrics.size.height - text_metrics.first_baseline
self.baseline_offset
}
}

Expand Down Expand Up @@ -615,11 +616,12 @@ 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,
));
self.baseline_offset = size.height - text_metrics.first_baseline;
ctx.set_baseline_offset(self.baseline_offset);
trace!("Computed size: {}", size);
size
}
Expand Down
9 changes: 3 additions & 6 deletions druid/src/widget/textbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,11 +580,8 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
self.text().borrow().layout.layout_metrics()
};

let layout_baseline = text_metrics.size.height - text_metrics.first_baseline;
let baseline_off = layout_baseline
- (self.inner.child_size().height - self.inner.viewport_rect().height())
+ textbox_insets.y1;
ctx.set_baseline_offset(baseline_off);
let baseline_offset = size.height - text_metrics.first_baseline - textbox_insets.y1;
ctx.set_baseline_offset(baseline_offset);
if self.scroll_to_selection_after_layout {
self.scroll_to_selection_end();
self.scroll_to_selection_after_layout = false;
Expand All @@ -593,7 +590,7 @@ impl<T: TextStorage + EditableText> Widget<T> for TextBox<T> {
trace!(
"Computed layout: size={}, baseline_offset={:?}",
size,
baseline_off
baseline_offset
);
size
}
Expand Down