Skip to content

Commit

Permalink
Add option to set label fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodewarrior committed Mar 30, 2020
1 parent 47be1da commit 3da113d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
53 changes: 36 additions & 17 deletions druid/examples/styled_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

//! Example of dynamic text styling

use druid::widget::{Flex, Label, MainAxisAlignment, Painter, Parse, Stepper, TextBox};
use druid::widget::{Checkbox, Flex, Label, MainAxisAlignment, Painter, Parse, Stepper, TextBox};
use druid::{
theme, AppLauncher, Color, Data, Key, Lens, LensExt, LensWrap, LocalizedString, PlatformError,
RenderContext, Widget, WidgetExt, WindowDesc,
Expand All @@ -27,6 +27,7 @@ const MY_CUSTOM_TEXT_SIZE: Key<f64> = Key::new("styled_text.custom_text_size");
struct AppData {
text: String,
size: f64,
mono: bool,
}
fn main() -> Result<(), PlatformError> {
let main_window = WindowDesc::new(ui_builder).title(
Expand All @@ -35,6 +36,7 @@ fn main() -> Result<(), PlatformError> {
let data = AppData {
text: "Here's some sample text".to_string(),
size: 24.0,
mono: false,
};

AppLauncher::with_window(main_window)
Expand All @@ -57,26 +59,39 @@ fn ui_builder() -> impl Widget<AppData> {
});

// This is druid's default text style.
// It's set by theme::LABEL_COLOR and theme::TEXT_SIZE_NORMAL
// It's set by theme::LABEL_COLOR, theme::TEXT_SIZE_NORMAL, and theme::FONT_NAME
let label =
Label::new(|data: &String, _env: &_| format!("Default: {}", data)).lens(AppData::text);

// The text_color and text_size builder methods can override the defaults
// provided by the theme by passing in a Key or a concrete value.
// The text_color, text_size, and font builder methods can override the
// defaults provided by the theme by passing in a Key or a concrete value.
//
// In this example, text_color receives a Key from the theme, while
// text_size gets a custom key which we set with the env_scope wrapper.
let styled_label =
Label::new(|data: &AppData, _env: &_| format!("Size {:.1}: {}", data.size, data.text))
.with_text_color(theme::PRIMARY_LIGHT)
.with_text_size(MY_CUSTOM_TEXT_SIZE)
.background(my_painter)
.on_click(|_, data, _| {
data.size *= 1.1;
})
.env_scope(|env: &mut druid::Env, data: &AppData| {
env.set(MY_CUSTOM_TEXT_SIZE, data.size);
});
// In this example, text_color receives a Key from the theme, text_size
// gets a custom key which we set with the env_scope wrapper, and the
// default font key (theme::FONT_NAME) is overridden in the env_scope
// wrapper. (Like text_color and text_size, the font can be set using the
// with_font builder method, but overriding here makes it easy to fall back
// to the default font)
let styled_label = Label::new(|data: &AppData, _env: &_| {
format!(
"Size {:.1}{}: {}",
data.size,
if data.mono { " mono" } else { "" },
data.text
)
})
.with_text_color(theme::PRIMARY_LIGHT)
.with_text_size(MY_CUSTOM_TEXT_SIZE)
.background(my_painter)
.on_click(|_, data, _| {
data.size *= 1.1;
})
.env_scope(|env: &mut druid::Env, data: &AppData| {
env.set(MY_CUSTOM_TEXT_SIZE, data.size);
if data.mono {
env.set(theme::FONT_NAME, "monospace");
}
});

let stepper = Stepper::new()
.with_range(0.0, 100.0)
Expand All @@ -91,6 +106,8 @@ fn ui_builder() -> impl Widget<AppData> {

let stepper_row = Flex::row().with_child(stepper_textbox).with_child(stepper);

let mono_checkbox = Checkbox::new("Monospace").lens(AppData::mono);

let input = TextBox::new().fix_width(200.0).lens(AppData::text);

Flex::column()
Expand All @@ -101,5 +118,7 @@ fn ui_builder() -> impl Widget<AppData> {
.with_spacer(32.0)
.with_child(stepper_row)
.with_spacer(8.0)
.with_child(mono_checkbox)
.with_spacer(8.0)
.with_child(input.padding(5.0))
}
6 changes: 6 additions & 0 deletions druid/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,12 @@ macro_rules! impl_value_type_borrowed {
Value::$var(self)
}
}

impl Into<Value> for &$ty {
fn into(self) -> Value {
Value::$var(self.to_owned())
}
}
};
}

Expand Down
23 changes: 22 additions & 1 deletion druid/src/widget/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub struct Label<T> {
text: LabelText<T>,
color: KeyOrValue<Color>,
size: KeyOrValue<f64>,
font: KeyOrValue<&'static str>,
}

impl<T: Data> Label<T> {
Expand All @@ -78,6 +79,7 @@ impl<T: Data> Label<T> {
text,
color: theme::LABEL_COLOR.into(),
size: theme::TEXT_SIZE_NORMAL.into(),
font: theme::FONT_NAME.into(),
}
}

Expand Down Expand Up @@ -132,6 +134,16 @@ impl<T: Data> Label<T> {
self
}

/// Builder-style method for setting the font.
///
/// The argument can be either a `&'static str` or a [`Key<&'static str>`].
///
/// [`Key<&str>`]: struct.Key.html
pub fn with_font(mut self, font: impl Into<KeyOrValue<&'static str>>) -> Self {
self.font = font.into();
self
}

/// Set a new text.
///
/// Takes an already resolved string as input.
Expand Down Expand Up @@ -163,8 +175,17 @@ impl<T: Data> Label<T> {
self.size = size.into();
}

/// Set the text size.
///
/// The argument can be either a `&'static str` or a [`Key<&'static str>`].
///
/// [`Key<f64>`]: struct.Key.html
pub fn set_font(&mut self, font: impl Into<KeyOrValue<&'static str>>) {
self.font = font.into();
}

fn get_layout(&mut self, t: &mut PietText, env: &Env) -> PietTextLayout {
let font_name = env.get(theme::FONT_NAME);
let font_name = self.font.resolve(env);
let font_size = self.size.resolve(env);

// TODO: caching of both the format and the layout
Expand Down

0 comments on commit 3da113d

Please sign in to comment.