Skip to content

Commit

Permalink
Add TextAlignment support to TextLayout and Label
Browse files Browse the repository at this point in the history
This also updates the 'text' example to demonstrate this feature.
  • Loading branch information
cmyr committed Sep 10, 2020
1 parent ce1cbd8 commit 9e350d9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ You can find its changes [documented below](#060---2020-06-01).
- `Movement::StartOfDocument`, `Movement::EndOfDocument`. ([#1092] by [@sysint64])
- `TextLayout` type simplifies drawing text ([#1182] by [@cmyr])
- `LineBreaking` enum allows configuration of label line-breaking ([#1195] by [@cmyr])
- `TextAlignment` support in `TextLayout` and `Label` ([#1208] by [@cmyr])`

### Changed

Expand Down Expand Up @@ -429,6 +430,7 @@ Last release without a changelog :(
[#1195]: https://github.com/linebender/druid/pull/1195
[#1204]: https://github.com/linebender/druid/pull/1204
[#1205]: https://github.com/linebender/druid/pull/1205
[#1208]: https://github.com/linebender/druid/pull/1208

[Unreleased]: https://github.com/linebender/druid/compare/v0.6.0...master
[0.6.0]: https://github.com/linebender/druid/compare/v0.5.0...v0.6.0
Expand Down
30 changes: 26 additions & 4 deletions druid/examples/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
use druid::widget::{Controller, Flex, Label, LineBreaking, RadioGroup, Scroll};
use druid::{
AppLauncher, Color, Data, Env, Lens, LocalizedString, UpdateCtx, Widget, WidgetExt, WindowDesc,
AppLauncher, Color, Data, Env, Lens, LocalizedString, TextAlignment, UpdateCtx, Widget,
WidgetExt, WindowDesc,
};

const WINDOW_TITLE: LocalizedString<AppState> = LocalizedString::new("Text Options");
Expand All @@ -28,8 +29,8 @@ const SPACER_SIZE: f64 = 8.0;

#[derive(Clone, Data, Lens)]
struct AppState {
/// the width at which to wrap lines.
line_break_mode: LineBreaking,
alignment: TextAlignment,
}

/// A controller that sets properties on a label.
Expand All @@ -49,6 +50,9 @@ impl Controller<AppState, Label<AppState>> for LabelController {
child.set_line_break_mode(data.line_break_mode);
ctx.request_layout();
}
if old_data.alignment != data.alignment {
child.set_text_alignment(data.alignment);
}
child.update(ctx, old_data, data, env);
}
}
Expand All @@ -62,6 +66,7 @@ pub fn main() {
// create the initial app state
let initial_state = AppState {
line_break_mode: LineBreaking::Clip,
alignment: Default::default(),
};

// start the application
Expand Down Expand Up @@ -93,9 +98,26 @@ fn build_root_widget() -> impl Widget<AppState> {
]))
.lens(AppState::line_break_mode);

Flex::column()
let alignment_picker = Flex::column()
.with_child(Label::new("Justification"))
.with_spacer(SPACER_SIZE)
.with_child(line_break_chooser)
.with_child(RadioGroup::new(vec![
("Start", TextAlignment::Start),
("End", TextAlignment::End),
("Center", TextAlignment::Center),
("Justified", TextAlignment::Justified),
]))
.lens(AppState::alignment);

let controls = Flex::row()
.cross_axis_alignment(druid::widget::CrossAxisAlignment::Start)
.with_child(alignment_picker)
.with_spacer(SPACER_SIZE)
.with_child(line_break_chooser)
.padding(SPACER_SIZE);

Flex::column()
.cross_axis_alignment(druid::widget::CrossAxisAlignment::Start)
.with_child(controls)
.with_flex_child(label, 1.0)
}
6 changes: 6 additions & 0 deletions druid/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ impl Data for piet::FontStyle {
}
}

impl Data for piet::TextAlignment {
fn same(&self, other: &Self) -> bool {
self == other
}
}

#[cfg(feature = "im")]
impl<T: Data> Data for im::Vector<T> {
fn same(&self, other: &Self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion druid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mod window;
pub use kurbo::{Affine, Insets, Point, Rect, Size, Vec2};
pub use piet::{
Color, FontFamily, FontStyle, FontWeight, LinearGradient, RadialGradient, RenderContext,
UnitPoint,
TextAlignment, UnitPoint,
};
// these are the types from shell that we expose; others we only use internally.
pub use shell::keyboard_types;
Expand Down
13 changes: 12 additions & 1 deletion druid/src/text/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::ops::Range;

use crate::kurbo::{Line, Point, Rect, Size};
use crate::piet::{
Color, PietText, PietTextLayout, Text as _, TextAttribute, TextLayout as _,
Color, PietText, PietTextLayout, Text as _, TextAlignment, TextAttribute, TextLayout as _,
TextLayoutBuilder as _,
};
use crate::{ArcStr, Data, Env, FontDescriptor, KeyOrValue, PaintCtx, RenderContext};
Expand Down Expand Up @@ -55,6 +55,7 @@ pub struct TextLayout {
// the underlying layout object. This is constructed lazily.
layout: Option<PietTextLayout>,
wrap_width: f64,
alignment: TextAlignment,
}

impl TextLayout {
Expand All @@ -75,6 +76,7 @@ impl TextLayout {
cached_text_size: None,
layout: None,
wrap_width: f64::INFINITY,
alignment: Default::default(),
}
}

Expand Down Expand Up @@ -136,6 +138,14 @@ impl TextLayout {
}
}

/// Set the [`TextAlignment`] for this layout.
///
/// [`TextAlignment`]: enum.TextAlignment.html
pub fn set_text_alignment(&mut self, alignment: TextAlignment) {
self.alignment = alignment;
self.layout = None;
}

/// The size of the laid-out text.
///
/// This is not meaningful until [`rebuild_if_needed`] has been called.
Expand Down Expand Up @@ -239,6 +249,7 @@ impl TextLayout {
factory
.new_text_layout(self.text.clone())
.max_width(self.wrap_width)
.alignment(self.alignment)
.font(descriptor.family.clone(), descriptor.size)
.default_attribute(descriptor.weight)
.default_attribute(descriptor.style)
Expand Down
19 changes: 18 additions & 1 deletion druid/src/widget/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
use crate::piet::{Color, PietText};
use crate::widget::prelude::*;
use crate::{
BoxConstraints, Data, FontDescriptor, KeyOrValue, LocalizedString, Point, Size, TextLayout,
BoxConstraints, Data, FontDescriptor, KeyOrValue, LocalizedString, Point, Size, TextAlignment,
TextLayout,
};

// added padding between the edges of the widget and the text.
Expand Down Expand Up @@ -162,6 +163,14 @@ impl<T: Data> Label<T> {
self
}

/// Builder-style method to set the [`TextAlignment`].
///
/// [`TextAlignment`]: enum.TextAlignment.html
pub fn with_text_alignment(mut self, alignment: TextAlignment) -> Self {
self.set_text_alignment(alignment);
self
}

/// Set the label's text.
///
/// If you change this property, you are responsible for calling
Expand Down Expand Up @@ -234,6 +243,14 @@ impl<T: Data> Label<T> {
self.line_break_mode = mode;
}

/// Set the [`TextAlignment`] for this layout.
///
/// [`TextAlignment`]: enum.TextAlignment.html
pub fn set_text_alignment(&mut self, alignment: TextAlignment) {
self.layout.set_text_alignment(alignment);
self.needs_rebuild = true;
}

fn rebuild_if_needed(&mut self, factory: &mut PietText, data: &T, env: &Env) {
if self.needs_rebuild {
self.text.resolve(data, env);
Expand Down

0 comments on commit 9e350d9

Please sign in to comment.