Skip to content

Commit

Permalink
Expose line break parameter in label widget (#817)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel McNab <[email protected]>
  • Loading branch information
melix99 and DJMcNab authored Jan 16, 2025
1 parent e7ed917 commit 2172d4d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
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

0 comments on commit 2172d4d

Please sign in to comment.