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

implemented multiline support for labels (alternative) #799

Closed
wants to merge 1 commit into from
Closed
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
63 changes: 47 additions & 16 deletions src/ui/widgets/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::borrow::Cow;

pub struct Label<'a> {
position: Option<Vec2>,
_multiline: Option<f32>,
multiline: Option<f32>,
size: Option<Vec2>,
label: Cow<'a, str>,
}
Expand All @@ -19,15 +19,15 @@ impl<'a> Label<'a> {
{
Label {
position: None,
_multiline: None,
multiline: None,
size: None,
label: label.into(),
}
}

pub fn multiline(self, line_height: f32) -> Self {
Label {
_multiline: Some(line_height),
multiline: Some(line_height),
..self
}
}
Expand Down Expand Up @@ -55,23 +55,54 @@ impl<'a> Label<'a> {
)
});

let pos = context
let mut pos = context
.window
.cursor
.fit(size, self.position.map_or(Layout::Vertical, Layout::Free));

context.window.painter.draw_element_content(
&context.style.label_style,
pos,
size,
&UiContent::Label(self.label),
ElementState {
focused: context.focused,
hovered: false,
clicked: false,
selected: false,
},
);
if let Some(line_height) = self.multiline {
let lines = self.label.lines();
let last_line_index = lines.clone().count() - 1;
for (n, line) in lines.enumerate() {
// need to recaclulate size for each line
let size = context.window.painter.content_with_margins_size(
&context.style.label_style,
&UiContent::Label(line.into()),
);
context.window.painter.draw_element_content(
&context.style.label_style,
pos,
size,
&UiContent::Label(line.into()),
ElementState {
focused: context.focused,
hovered: false,
clicked: false,
selected: false,
},
);

pos.y += line_height;
// only move window cursor if Layout::Vertical and this is not the last line
if n != last_line_index && self.position.is_none() {
context.window.cursor.y += line_height;
context.window.cursor.max_row_y = size.y + context.window.cursor.margin;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh idk what this line is for, I just copied it from Cursor::fit()

}
}
} else {
context.window.painter.draw_element_content(
&context.style.label_style,
pos,
size,
&UiContent::Label(self.label),
ElementState {
focused: context.focused,
hovered: false,
clicked: false,
selected: false,
},
);
}
}
}

Expand Down
Loading