Add justified and/or center- and right-aligned text

Label text will now be centered, right-aligned and/or
justified based on the layout.

Galleys are no longer always pivoted in the left top corner,
so now have a Rect rather than just a size.
This commit is contained in:
Emil Ernerfeldt
2021-09-07 20:37:50 +02:00
parent cbafd10ee4
commit acb5501fe4
23 changed files with 316 additions and 112 deletions
+3 -3
View File
@@ -270,21 +270,21 @@ impl CollapsingHeader {
let text_pos = available.min + vec2(ui.spacing().indent, 0.0);
let galley =
label.layout_width(ui, available.right() - text_pos.x, Color32::TEMPORARY_COLOR);
let text_max_x = text_pos.x + galley.size.x;
let text_max_x = text_pos.x + galley.size().x;
let mut desired_width = text_max_x + button_padding.x - available.left();
if ui.visuals().collapsing_header_frame {
desired_width = desired_width.max(available.width()); // fill full width
}
let mut desired_size = vec2(desired_width, galley.size.y + 2.0 * button_padding.y);
let mut desired_size = vec2(desired_width, galley.size().y + 2.0 * button_padding.y);
desired_size = desired_size.at_least(ui.spacing().interact_size);
let (_, rect) = ui.allocate_space(desired_size);
let mut header_response = ui.interact(rect, id, Sense::click());
let text_pos = pos2(
text_pos.x,
header_response.rect.center().y - galley.size.y / 2.0,
header_response.rect.center().y - galley.size().y / 2.0,
);
let mut state = State::from_memory_with_default_open(ui.ctx(), id, default_open);
+3 -3
View File
@@ -162,9 +162,9 @@ fn combo_box<R>(
ui.fonts()
.layout_delayed_color(selected.to_string(), TextStyle::Button, f32::INFINITY);
let width = galley.size.x + ui.spacing().item_spacing.x + icon_size.x;
let width = galley.size().x + ui.spacing().item_spacing.x + icon_size.x;
let width = width.at_least(full_minimum_width);
let height = galley.size.y.max(icon_size.y);
let height = galley.size().y.max(icon_size.y);
let (_, rect) = ui.allocate_space(Vec2::new(width, height));
let button_rect = ui.min_rect().expand2(ui.spacing().button_padding);
@@ -179,7 +179,7 @@ fn combo_box<R>(
};
paint_icon(ui.painter(), icon_rect.expand(visuals.expansion), visuals);
let text_rect = Align2::LEFT_CENTER.align_size_within_rect(galley.size, rect);
let text_rect = Align2::LEFT_CENTER.align_size_within_rect(galley.size(), rect);
ui.painter()
.galley_with_color(text_rect.min, galley, visuals.text_color());
});
+6 -4
View File
@@ -795,9 +795,9 @@ fn show_title_bar(
let minimum_width = if collapsible || show_close_button {
// If at least one button is shown we make room for both buttons (since title is centered):
2.0 * (pad + button_size.x + item_spacing.x) + title_galley.size.x
2.0 * (pad + button_size.x + item_spacing.x) + title_galley.size().x
} else {
pad + title_galley.size.x + pad
pad + title_galley.size().x + pad
};
let min_rect = Rect::from_min_size(ui.min_rect().min, vec2(minimum_width, height));
let id = ui.advance_cursor_after_rect(min_rect);
@@ -846,8 +846,10 @@ impl TitleBar {
self.title_label = self.title_label.text_color(style.fg_stroke.color);
let full_top_rect = Rect::from_x_y_ranges(self.rect.x_range(), self.min_rect.y_range());
let text_pos = emath::align::center_size_in_rect(self.title_galley.size, full_top_rect);
let text_pos = text_pos.left_top() - 1.5 * Vec2::Y; // HACK: center on x-height of text (looks better)
let text_pos =
emath::align::center_size_in_rect(self.title_galley.size(), full_top_rect).left_top();
let text_pos = text_pos - self.title_galley.rect.min.to_vec2();
let text_pos = text_pos - 1.5 * Vec2::Y; // HACK: center on x-height of text (looks better)
let text_color = ui.visuals().text_color();
self.title_label
.paint_galley(ui, text_pos, self.title_galley, false, text_color);
+2 -2
View File
@@ -221,7 +221,7 @@ impl Painter {
text: impl ToString,
) -> Rect {
let galley = self.layout_no_wrap(text.to_string(), TextStyle::Monospace, color);
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size));
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size()));
let frame_rect = rect.expand(2.0);
self.add(Shape::Rect {
rect: frame_rect,
@@ -343,7 +343,7 @@ impl Painter {
text_color: Color32,
) -> Rect {
let galley = self.layout_no_wrap(text.to_string(), text_style, text_color);
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size));
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size()));
self.galley(rect.min, galley);
rect
}
+6 -6
View File
@@ -164,7 +164,7 @@ impl Button {
.fonts()
.layout_delayed_color(text, text_style, wrap_width);
let mut desired_size = galley.size + 2.0 * button_padding;
let mut desired_size = galley.size() + 2.0 * button_padding;
if !small {
desired_size.y = desired_size.y.at_least(ui.spacing().interact_size.y);
}
@@ -177,7 +177,7 @@ impl Button {
let visuals = ui.style().interact(&response);
let text_pos = ui
.layout()
.align_size_within_rect(galley.size, rect.shrink2(button_padding))
.align_size_within_rect(galley.size(), rect.shrink2(button_padding))
.min;
if frame {
@@ -290,7 +290,7 @@ impl<'a> Widget for Checkbox<'a> {
.fonts()
.layout_delayed_color(text, text_style, wrap_width);
let mut desired_size = total_extra + galley.size;
let mut desired_size = total_extra + galley.size();
desired_size = desired_size.at_least(spacing.interact_size);
desired_size.y = desired_size.y.max(icon_width);
let (rect, mut response) = ui.allocate_exact_size(desired_size, Sense::click());
@@ -306,7 +306,7 @@ impl<'a> Widget for Checkbox<'a> {
let visuals = ui.style().interact(&response);
let text_pos = pos2(
rect.min.x + button_padding.x + icon_width + icon_spacing,
rect.center().y - 0.5 * galley.size.y,
rect.center().y - 0.5 * galley.size().y,
);
let (small_icon_rect, big_icon_rect) = ui.spacing().icon_rectangles(rect);
ui.painter().add(Shape::Rect {
@@ -414,7 +414,7 @@ impl Widget for RadioButton {
.fonts()
.layout_delayed_color(text, text_style, wrap_width);
let mut desired_size = total_extra + galley.size;
let mut desired_size = total_extra + galley.size();
desired_size = desired_size.at_least(ui.spacing().interact_size);
desired_size.y = desired_size.y.max(icon_width);
let (rect, response) = ui.allocate_exact_size(desired_size, Sense::click());
@@ -423,7 +423,7 @@ impl Widget for RadioButton {
let text_pos = pos2(
rect.min.x + button_padding.x + icon_width + icon_spacing,
rect.center().y - 0.5 * galley.size.y,
rect.center().y - 0.5 * galley.size().y,
);
// let visuals = ui.style().interact_selectable(&response, checked); // too colorful
+25 -3
View File
@@ -171,9 +171,18 @@ impl Label {
/// `line_color`: used for underline and strikethrough, if any.
pub fn layout_width(&self, ui: &Ui, max_width: f32, line_color: Color32) -> Arc<Galley> {
self.layout_impl(ui, 0.0, max_width, 0.0, line_color)
let (halign, justify) = if ui.is_grid() {
(Align::LEFT, false) // TODO: remove special Grid hacks like these
} else {
(
ui.layout().horizontal_align(),
ui.layout().horizontal_justify(),
)
};
self.layout_impl(ui, 0.0, max_width, 0.0, line_color, halign, justify)
}
#[allow(clippy::too_many_arguments)]
fn layout_impl(
&self,
ui: &Ui,
@@ -181,6 +190,8 @@ impl Label {
max_width: f32,
first_row_min_height: f32,
line_color: Color32,
halign: Align,
justify: bool,
) -> Arc<Galley> {
let text_style = self.text_style_or_default(ui.style());
let wrap_width = if self.should_wrap(ui) {
@@ -227,6 +238,8 @@ impl Label {
}],
wrap_width,
first_row_min_height,
halign,
justify,
..Default::default()
};
@@ -323,12 +336,16 @@ impl Label {
let first_row_min_height = cursor.height();
let default_color = self.get_text_color(ui, ui.visuals().text_color());
let halign = Align::Min;
let justify = false;
let galley = self.layout_impl(
ui,
first_row_indentation,
max_width,
first_row_min_height,
default_color,
halign,
justify,
);
let pos = pos2(ui.max_rect().left(), ui.cursor().top());
@@ -343,8 +360,13 @@ impl Label {
(pos, galley, response)
} else {
let galley = self.layout(ui);
let (rect, response) = ui.allocate_exact_size(galley.size, sense);
(rect.min, galley, response)
let (rect, response) = ui.allocate_exact_size(galley.size(), sense);
let pos = match galley.job.halign {
Align::LEFT => rect.left_top(),
Align::Center => rect.center_top(),
Align::RIGHT => rect.right_top(),
};
(pos, galley, response)
}
}
}
+1 -1
View File
@@ -915,7 +915,7 @@ impl PlotItem for Text {
.layout_no_wrap(self.text.clone(), self.style, color);
let rect = self
.anchor
.anchor_rect(Rect::from_min_size(pos, galley.size));
.anchor_rect(Rect::from_min_size(pos, galley.size()));
shapes.push(Shape::galley(rect.min, galley));
if self.highlight {
shapes.push(Shape::rect_stroke(
+4 -4
View File
@@ -94,11 +94,11 @@ impl LegendEntry {
ui.fonts()
.layout_delayed_color(text, ui.style().body_text_style, f32::INFINITY);
let icon_size = galley.size.y;
let icon_size = galley.size().y;
let icon_spacing = icon_size / 5.0;
let total_extra = vec2(icon_size + icon_spacing, 0.0);
let desired_size = total_extra + galley.size;
let desired_size = total_extra + galley.size();
let (rect, response) = ui.allocate_exact_size(desired_size, Sense::click());
response
@@ -139,12 +139,12 @@ impl LegendEntry {
}
let text_position_x = if label_on_the_left {
rect.right() - icon_size - icon_spacing - galley.size.x
rect.right() - icon_size - icon_spacing - galley.size().x
} else {
rect.left() + icon_size + icon_spacing
};
let text_position = pos2(text_position_x, rect.center().y - 0.5 * galley.size.y);
let text_position = pos2(text_position_x, rect.center().y - 0.5 * galley.size().y);
painter.galley_with_color(text_position, galley, visuals.text_color());
*checked ^= response.clicked_by(PointerButton::Primary);
+2 -2
View File
@@ -628,11 +628,11 @@ impl Prepared {
let galley = ui.painter().layout_no_wrap(text, text_style, color);
let mut text_pos = pos_in_gui + vec2(1.0, -galley.size.y);
let mut text_pos = pos_in_gui + vec2(1.0, -galley.size().y);
// Make sure we see the labels, even if the axis is off-screen:
text_pos[1 - axis] = text_pos[1 - axis]
.at_most(transform.frame().max[1 - axis] - galley.size[1 - axis] - 2.0)
.at_most(transform.frame().max[1 - axis] - galley.size()[1 - axis] - 2.0)
.at_least(transform.frame().min[1 - axis] + 1.0);
shapes.push(Shape::galley(text_pos, galley));
+2 -2
View File
@@ -69,7 +69,7 @@ impl Widget for SelectableLabel {
.fonts()
.layout_delayed_color(text, text_style, wrap_width);
let mut desired_size = total_extra + galley.size;
let mut desired_size = total_extra + galley.size();
desired_size.y = desired_size.y.at_least(ui.spacing().interact_size.y);
let (rect, response) = ui.allocate_at_least(desired_size, Sense::click());
response.widget_info(|| {
@@ -78,7 +78,7 @@ impl Widget for SelectableLabel {
let text_pos = ui
.layout()
.align_size_within_rect(galley.size, rect.shrink2(button_padding))
.align_size_within_rect(galley.size(), rect.shrink2(button_padding))
.min;
let visuals = ui.style().interact_selectable(&response, selected);
+2 -2
View File
@@ -521,7 +521,7 @@ impl<'t, S: TextBuffer> TextEdit<'t, S> {
let mut galley = layouter(ui, text.as_ref(), wrap_width);
let desired_height = (desired_height_rows.at_least(1) as f32) * row_height;
let desired_size = vec2(wrap_width, galley.size.y.max(desired_height));
let desired_size = vec2(wrap_width, galley.size().y.max(desired_height));
let (auto_id, rect) = ui.allocate_space(desired_size);
let id = id.unwrap_or_else(|| {
@@ -804,7 +804,7 @@ impl<'t, S: TextBuffer> TextEdit<'t, S> {
}
offset_x = offset_x
.at_most(galley.size.x - desired_size.x)
.at_most(galley.size().x - desired_size.x)
.at_least(0.0);
state.singleline_offset = offset_x;