Skip to content

Commit

Permalink
ability to specify font size when building a button
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Jun 21, 2024
1 parent 6f46213 commit f9976b2
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion fyrox-ui/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ pub enum ButtonContent {
text: String,
/// Optional font of the button. If [`None`], the default font will be used.
font: Option<FontResource>,
/// Font size of the text. Default is 14.0
size: f32,
},
/// Arbitrary widget handle. It could be any widget handle, for example a handle of [`crate::image::Image`]
/// widget.
Expand All @@ -235,6 +237,7 @@ impl ButtonContent {
Self::Text {
text: s.as_ref().to_owned(),
font: None,
size: 14.0,
}
}

Expand All @@ -243,6 +246,16 @@ impl ButtonContent {
Self::Text {
text: s.as_ref().to_owned(),
font: Some(font),
size: 14.0,
}
}

/// Creates [`ButtonContent::Text`] with custom font and size.
pub fn text_with_font_size<S: AsRef<str>>(s: S, font: FontResource, size: f32) -> Self {
Self::Text {
text: s.as_ref().to_owned(),
font: Some(font),
size,
}
}

Expand All @@ -253,11 +266,12 @@ impl ButtonContent {

fn build(&self, ctx: &mut BuildContext) -> Handle<UiNode> {
match self {
Self::Text { text, font } => TextBuilder::new(WidgetBuilder::new())
Self::Text { text, font, size } => TextBuilder::new(WidgetBuilder::new())
.with_text(text)
.with_horizontal_text_alignment(HorizontalAlignment::Center)
.with_vertical_text_alignment(VerticalAlignment::Center)
.with_font(font.clone().unwrap_or_else(|| ctx.default_font()))
.with_font_size(*size)
.build(ctx),
Self::Node(node) => *node,
}
Expand Down Expand Up @@ -297,6 +311,12 @@ impl ButtonBuilder {
self
}

/// Sets the content of the button to be [`ButtonContent::Text`] (text with a custom font and size).
pub fn with_text_and_font_size(mut self, text: &str, font: FontResource, size: f32) -> Self {
self.content = Some(ButtonContent::text_with_font_size(text, font, size));
self
}

/// Sets the content of the button to be [`ButtonContent::Node`] (arbitrary widget handle).
pub fn with_content(mut self, node: Handle<UiNode>) -> Self {
self.content = Some(ButtonContent::Node(node));
Expand Down

0 comments on commit f9976b2

Please sign in to comment.