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

Expose line break parameter in label widget #817

Merged
merged 2 commits into from
Jan 16, 2025
Merged
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
1 change: 1 addition & 0 deletions xilem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use crate::core::{
ViewPathTracker, ViewSequence,
};
pub use masonry::event_loop_runner::{EventLoop, EventLoopBuilder};
pub use masonry::widget::LineBreaking;
pub use masonry::{dpi, palette, Affine, Color, FontWeight, TextAlignment, Vec2};
pub use xilem_core as core;

Expand Down
18 changes: 15 additions & 3 deletions xilem/src/view/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use masonry::parley::style::{FontStack, FontWeight};
use masonry::text::{ArcStr, StyleProperty};
use masonry::widget;
use masonry::widget::{self, LineBreaking};
use vello::peniko::Brush;

use crate::core::{DynMessage, Mut, ViewMarker};
Expand All @@ -19,6 +19,7 @@ pub fn label(label: impl Into<ArcStr>) -> Label {
text_size: masonry::theme::TEXT_SIZE_NORMAL,
weight: FontWeight::NORMAL,
font: FontStack::List(std::borrow::Cow::Borrowed(&[])),
line_break_mode: LineBreaking::Overflow,
transform: Affine::IDENTITY,
}
}
Expand All @@ -30,7 +31,8 @@ pub struct Label {
alignment: TextAlignment,
text_size: f32,
weight: FontWeight,
font: FontStack<'static>, // TODO: add more attributes of `masonry::widget::Label`
font: FontStack<'static>,
line_break_mode: LineBreaking, // TODO: add more attributes of `masonry::widget::Label`
transform: Affine,
}

Expand Down Expand Up @@ -65,6 +67,12 @@ impl Label {
self.font = font.into();
self
}

/// Set how line breaks will be handled by this label (i.e. if there is insufficient horizontal space).
pub fn line_break_mode(mut self, line_break_mode: LineBreaking) -> Self {
self.line_break_mode = line_break_mode;
self
}
}

impl Transformable for Label {
Expand Down Expand Up @@ -94,7 +102,8 @@ impl<State, Action> View<State, Action, ViewCtx> for Label {
.with_alignment(self.alignment)
.with_style(StyleProperty::FontSize(self.text_size))
.with_style(StyleProperty::FontWeight(self.weight))
.with_style(StyleProperty::FontStack(self.font.clone())),
.with_style(StyleProperty::FontStack(self.font.clone()))
.with_line_break_mode(self.line_break_mode),
self.transform,
);
(widget_pod, ())
Expand Down Expand Up @@ -128,6 +137,9 @@ impl<State, Action> View<State, Action, ViewCtx> for Label {
if prev.font != self.font {
widget::Label::insert_style(&mut element, StyleProperty::FontStack(self.font.clone()));
}
if prev.line_break_mode != self.line_break_mode {
widget::Label::set_line_break_mode(&mut element, self.line_break_mode);
}
}

fn teardown(&self, (): &mut Self::ViewState, _: &mut ViewCtx, _: Mut<Self::Element>) {}
Expand Down
Loading