1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 05:40:03 -04:00

Better handling of full-width widgets inside of Ui:s with inf max size

This commit is contained in:
Emil Ernerfeldt
2020-05-10 18:59:18 +02:00
parent be6ada6923
commit 274acff47e
3 changed files with 46 additions and 4 deletions

View File

@@ -23,6 +23,7 @@ pub struct Label {
// TODO: not pub
pub(crate) text: String,
pub(crate) multiline: bool,
auto_shrink: bool,
pub(crate) text_style: TextStyle, // TODO: Option<TextStyle>, where None means "use the default for the ui"
pub(crate) text_color: Option<Color>,
}
@@ -32,6 +33,7 @@ impl Label {
Self {
text: text.into(),
multiline: true,
auto_shrink: false,
text_style: TextStyle::Body,
text_color: None,
}
@@ -46,6 +48,13 @@ impl Label {
self
}
/// If true, will word wrap to the width of the current child_bounds.
/// If false (defailt), will word wrap to the available width
pub fn auto_shrink(mut self) -> Self {
self.auto_shrink = true;
self
}
pub fn text_style(mut self, text_style: TextStyle) -> Self {
self.text_style = text_style;
self
@@ -58,7 +67,13 @@ impl Label {
pub fn layout(&self, pos: Pos2, ui: &Ui) -> (Vec<font::TextFragment>, Vec2) {
let font = &ui.fonts()[self.text_style];
let max_width = ui.rect().right() - pos.x;
let max_width = if self.auto_shrink {
ui.child_bounds().right() - pos.x
} else {
ui.rect().right() - pos.x
};
if self.multiline {
font.layout_multiline(&self.text, max_width)
} else {
@@ -389,7 +404,8 @@ impl Separator {
impl Widget for Separator {
fn ui(self, ui: &mut Ui) -> GuiResponse {
let available_space = ui.available_space();
let available_space = ui.available_space_min();
let extra = self.extra;
let (points, interact) = match ui.direction() {
Direction::Horizontal => {