Expose font weight parameter in label widget (#669)

This commit is contained in:
Marco Melorio 2024-10-14 19:01:53 +02:00 committed by GitHub
parent d74a7deea1
commit 634afd7010
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 30 additions and 5 deletions

View File

@ -11,6 +11,7 @@ use masonry::app_driver::{AppDriver, DriverCtx};
use masonry::dpi::LogicalSize;
use masonry::widget::{Button, Flex, Label, RootWidget};
use masonry::{Action, WidgetId};
use parley::fontique::Weight;
use winit::window::Window;
const VERTICAL_WIDGET_SPACING: f64 = 20.0;
@ -31,7 +32,9 @@ impl AppDriver for Driver {
}
pub fn main() {
let label = Label::new("Hello").with_text_size(32.0);
let label = Label::new("Hello")
.with_text_size(32.0)
.with_weight(Weight::BOLD);
let button = Button::new("Say hello");
// Arrange the two widgets vertically, with some padding

View File

@ -143,6 +143,7 @@ pub use event::{
WindowEvent, WindowTheme,
};
pub use kurbo::{Affine, Insets, Point, Rect, Size, Vec2};
pub use parley::fontique::Weight as TextWeight;
pub use parley::layout::Alignment as TextAlignment;
pub use util::{AsAny, Handled};
pub use vello::peniko::{Color, Gradient};

View File

@ -4,6 +4,7 @@
//! A label widget.
use accesskit::{NodeBuilder, Role};
use parley::fontique::Weight;
use parley::layout::Alignment;
use parley::style::{FontFamily, FontStack};
use smallvec::SmallVec;
@ -83,6 +84,11 @@ impl Label {
self
}
pub fn with_weight(mut self, weight: Weight) -> Self {
self.text_layout.set_weight(weight);
self
}
pub fn with_text_alignment(mut self, alignment: Alignment) -> Self {
self.text_layout.set_text_alignment(alignment);
self
@ -138,6 +144,9 @@ impl WidgetMut<'_, Label> {
pub fn set_text_size(&mut self, size: f32) {
self.set_text_properties(|layout| layout.set_text_size(size));
}
pub fn set_weight(&mut self, weight: Weight) {
self.set_text_properties(|layout| layout.set_weight(weight));
}
pub fn set_alignment(&mut self, alignment: Alignment) {
self.set_text_properties(|layout| layout.set_text_alignment(alignment));
}

View File

@ -12,7 +12,7 @@ use xilem::{
button, button_any_pointer, checkbox, flex, label, prose, task, textbox, Axis,
FlexExt as _, FlexSpacer,
},
Color, EventLoop, EventLoopBuilder, TextAlignment, WidgetView, Xilem,
Color, EventLoop, EventLoopBuilder, TextAlignment, TextWeight, WidgetView, Xilem,
};
const LOREM: &str = r"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi cursus mi sed euismod euismod. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam placerat efficitur tellus at semper. Morbi ac risus magna. Donec ut cursus ex. Etiam quis posuere tellus. Mauris posuere dui et turpis mollis, vitae luctus tellus consectetur. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur eu facilisis nisl.
@ -65,6 +65,7 @@ fn app_logic(data: &mut AppData) -> impl WidgetView<AppData> {
label("Label")
.brush(Color::REBECCA_PURPLE)
.alignment(TextAlignment::Start),
label("Bold Label").weight(TextWeight::BOLD),
// TODO masonry doesn't allow setting disabled manually anymore?
// label("Disabled label").disabled(),
))

View File

@ -25,7 +25,7 @@ use xilem_core::{
pub use masonry::{
dpi,
event_loop_runner::{EventLoop, EventLoopBuilder},
Color, TextAlignment,
Color, TextAlignment, TextWeight,
};
pub use xilem_core as core;

View File

@ -4,7 +4,7 @@
use masonry::{text::TextBrush, widget, ArcStr};
use xilem_core::{Mut, ViewMarker};
use crate::{Color, MessageResult, Pod, TextAlignment, View, ViewCtx, ViewId};
use crate::{Color, MessageResult, Pod, TextAlignment, TextWeight, View, ViewCtx, ViewId};
pub fn label(label: impl Into<ArcStr>) -> Label {
Label {
@ -12,6 +12,7 @@ pub fn label(label: impl Into<ArcStr>) -> Label {
text_brush: Color::WHITE.into(),
alignment: TextAlignment::default(),
text_size: masonry::theme::TEXT_SIZE_NORMAL as f32,
weight: TextWeight::NORMAL,
}
}
@ -21,6 +22,7 @@ pub struct Label {
text_brush: TextBrush,
alignment: TextAlignment,
text_size: f32,
weight: TextWeight,
// TODO: add more attributes of `masonry::widget::Label`
}
@ -41,6 +43,11 @@ impl Label {
self.text_size = text_size;
self
}
pub fn weight(mut self, weight: TextWeight) -> Self {
self.weight = weight;
self
}
}
impl ViewMarker for Label {}
@ -53,7 +60,8 @@ impl<State, Action> View<State, Action, ViewCtx> for Label {
widget::Label::new(self.label.clone())
.with_text_brush(self.text_brush.clone())
.with_text_alignment(self.alignment)
.with_text_size(self.text_size),
.with_text_size(self.text_size)
.with_weight(self.weight),
);
(widget_pod, ())
}
@ -77,6 +85,9 @@ impl<State, Action> View<State, Action, ViewCtx> for Label {
if prev.text_size != self.text_size {
element.set_text_size(self.text_size);
}
if prev.weight != self.weight {
element.set_weight(self.weight);
}
element
}