1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
use geom::Polygon;
use crate::{
style::DEFAULT_OUTLINE_THICKNESS, text::Font, ButtonStyle, Color, ContentMode, ControlState,
CornerRounding, Drawable, EdgeInsets, EventCtx, GeomBatch, GfxCtx, Image, Line, MultiKey,
Outcome, OutlineStyle, RewriteColor, ScreenDims, ScreenPt, Text, Widget, WidgetImpl,
WidgetOutput,
};
use crate::geom::geom_batch_stack::{Axis, GeomBatchStack};
pub struct Button {
/// When a button is clicked, `Outcome::Clicked` with this string is produced.
pub action: String,
// These must have the same dimensions and are oriented with their top-left corner at
// 0, 0. Transformation happens later.
draw_normal: Drawable,
draw_hovered: Drawable,
draw_disabled: Drawable,
pub(crate) hotkey: Option<MultiKey>,
tooltip: Option<Text>,
disabled_tooltip: Option<Text>,
// Screenspace, top-left always at the origin. Also, probably not a box. :P
hitbox: Polygon,
pub(crate) hovering: bool,
is_disabled: bool,
pub(crate) top_left: ScreenPt,
pub(crate) dims: ScreenDims,
}
impl Button {
fn new(
ctx: &EventCtx,
normal: GeomBatch,
hovered: GeomBatch,
disabled: GeomBatch,
hotkey: Option<MultiKey>,
action: &str,
maybe_tooltip: Option<Text>,
hitbox: Polygon,
is_disabled: bool,
disabled_tooltip: Option<Text>,
) -> Button {
// dims are based on the hitbox, not the two drawables!
let bounds = hitbox.get_bounds();
let dims = ScreenDims::new(bounds.width(), bounds.height());
assert!(!action.is_empty());
Button {
action: action.to_string(),
draw_normal: ctx.upload(normal),
draw_hovered: ctx.upload(hovered),
draw_disabled: ctx.upload(disabled),
tooltip: if let Some(t) = maybe_tooltip {
if t.is_empty() {
None
} else {
Some(t)
}
} else {
Some(Text::tooltip(ctx, hotkey.clone(), action))
},
disabled_tooltip,
hotkey,
hitbox,
is_disabled,
hovering: false,
top_left: ScreenPt::new(0.0, 0.0),
dims,
}
}
pub fn is_enabled(&self) -> bool {
!self.is_disabled
}
}
impl WidgetImpl for Button {
fn get_dims(&self) -> ScreenDims {
self.dims
}
fn set_pos(&mut self, top_left: ScreenPt) {
self.top_left = top_left;
}
fn event(&mut self, ctx: &mut EventCtx, output: &mut WidgetOutput) {
if ctx.redo_mouseover() {
if let Some(pt) = ctx.canvas.get_cursor_in_screen_space() {
self.hovering = self
.hitbox
.translate(self.top_left.x, self.top_left.y)
.contains_pt(pt.to_pt());
} else {
self.hovering = false;
}
}
if self.is_disabled {
return;
}
if self.hovering && ctx.normal_left_click() {
self.hovering = false;
output.outcome = Outcome::Clicked(self.action.clone());
return;
}
if ctx.input.pressed(self.hotkey.clone()) {
self.hovering = false;
output.outcome = Outcome::Clicked(self.action.clone());
return;
}
if self.hovering {
ctx.cursor_clickable();
}
}
fn draw(&self, g: &mut GfxCtx) {
if self.is_disabled {
g.redraw_at(self.top_left, &self.draw_disabled);
if self.hovering {
if let Some(ref txt) = self.disabled_tooltip {
g.draw_mouse_tooltip(txt.clone());
}
}
} else if self.hovering {
g.redraw_at(self.top_left, &self.draw_hovered);
if let Some(ref txt) = self.tooltip {
g.draw_mouse_tooltip(txt.clone());
}
} else {
g.redraw_at(self.top_left, &self.draw_normal);
}
}
}
#[derive(Clone, Debug, Default)]
pub struct ButtonBuilder<'a, 'c> {
padding: EdgeInsets,
stack_spacing: f64,
hotkey: Option<MultiKey>,
tooltip: Option<Text>,
stack_axis: Option<Axis>,
is_label_before_image: bool,
corner_rounding: Option<CornerRounding>,
is_disabled: bool,
default_style: ButtonStateStyle<'a, 'c>,
hover_style: ButtonStateStyle<'a, 'c>,
disable_style: ButtonStateStyle<'a, 'c>,
disabled_tooltip: Option<Text>,
}
#[derive(Clone, Debug, Default)]
struct ButtonStateStyle<'a, 'c> {
image: Option<Image<'a, 'c>>,
label: Option<Label>,
outline: Option<OutlineStyle>,
bg_color: Option<Color>,
custom_batch: Option<GeomBatch>,
}
// can we take 'b out? and make the func that uses it generic?
impl<'b, 'a: 'b, 'c> ButtonBuilder<'a, 'c> {
pub fn new() -> Self {
ButtonBuilder {
padding: EdgeInsets {
top: 8.0,
bottom: 8.0,
left: 16.0,
right: 16.0,
},
stack_spacing: 10.0,
..Default::default()
}
}
/// Extra spacing around a button's items (label and/or image).
///
/// If not specified, a default will be applied.
/// ```
/// # use widgetry::{ButtonBuilder, EdgeInsets};
/// // Custom padding for each inset
/// let b = ButtonBuilder::new().padding(EdgeInsets{ top: 1.0, bottom: 2.0, left: 12.0, right: 14.0 });
/// // uniform padding
/// let b = ButtonBuilder::new().padding(6);
/// ```
pub fn padding<EI: Into<EdgeInsets>>(mut self, padding: EI) -> Self {
self.padding = padding.into();
self
}
/// Extra spacing around a button's items (label and/or image).
pub fn padding_top(mut self, padding: f64) -> Self {
self.padding.top = padding;
self
}
/// Extra spacing around a button's items (label and/or image).
pub fn padding_left(mut self, padding: f64) -> Self {
self.padding.left = padding;
self
}
/// Extra spacing around a button's items (label and/or image).
pub fn padding_bottom(mut self, padding: f64) -> Self {
self.padding.bottom = padding;
self
}
/// Extra spacing around a button's items (label and/or image).
pub fn padding_right(mut self, padding: f64) -> Self {
self.padding.right = padding;
self
}
/// Set the text of the button's label.
///
/// If `label_text` is not set, the button will not have a label.
pub fn label_text<I: Into<String>>(mut self, text: I) -> Self {
let mut label = self.default_style.label.take().unwrap_or_default();
label.text = Some(text.into());
self.default_style.label = Some(label);
self
}
/// Set the text of the button's label. The text will be decorated with an underline.
///
/// See `label_styled_text` if you need something more customizable text styling.
pub fn label_underlined_text<I: Into<String>>(mut self, text: I) -> Self {
let text = text.into();
let mut label = self.default_style.label.take().unwrap_or_default();
label.text = Some(text.clone());
label.styled_text = Some(Text::from(Line(text).underlined()));
self.default_style.label = Some(label);
self
}
/// Assign a pre-styled `Text` instance if your button need something more than uniformly
/// colored text.
pub fn label_styled_text(mut self, styled_text: Text, for_state: ControlState) -> Self {
let state_style = self.style_mut(for_state);
let mut label = state_style.label.take().unwrap_or_default();
label.styled_text = Some(styled_text);
// Unset plain `text` to avoid confusion. Alternatively we could assign the inner text -
// something like:
// label.text = styled_text.rows.map(|r|r.text).join(" ")
label.text = None;
state_style.label = Some(label);
self
}
/// Set the color of the button's label.
///
/// If not specified, a default font color will be used.
pub fn label_color(mut self, color: Color, for_state: ControlState) -> Self {
let state_style = self.style_mut(for_state);
let mut label = state_style.label.take().unwrap_or_default();
label.color = Some(color);
state_style.label = Some(label);
self
}
/// Set the font used by the button's label.
///
/// If not specified, a default font will be used.
pub fn font(mut self, font: Font) -> Self {
let mut label = self.default_style.label.take().unwrap_or_default();
label.font = Some(font);
self.default_style.label = Some(label);
self
}
/// Set the size of the font of the button's label.
///
/// If not specified, a default font size will be used.
pub fn font_size(mut self, font_size: usize) -> Self {
let mut label = self.default_style.label.take().unwrap_or_default();
label.font_size = Some(font_size);
self.default_style.label = Some(label);
self
}
/// Set the image for the button. If not set, the button will have no image.
///
/// This will replace any image previously set.
pub fn image_path(mut self, path: &'a str) -> Self {
// Currently we don't support setting image for other states like "hover", we easily
// could, but the API gets more verbose for a thing we don't currently need.
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.source_path(path);
self.default_style.image = Some(image);
self
}
pub fn image(mut self, image: Image<'a, 'c>) -> Self {
// Currently we don't support setting image for other states like "hover", we easily
// could, but the API gets more verbose for a thing we don't currently need.
self.default_style.image = Some(image);
self
}
/// Set the image for the button. If not set, the button will have no image.
///
/// This will replace any image previously set.
///
/// * `labeled_bytes`: is a (`label`, `bytes`) tuple you can generate with
/// [`include_labeled_bytes!`]
/// * `label`: a label to describe the bytes for debugging purposes
/// * `bytes`: UTF-8 encoded bytes of the SVG
pub fn image_bytes(mut self, labeled_bytes: (&'a str, &'a [u8])) -> Self {
// Currently we don't support setting image for other states like "hover", we easily
// could, but the API gets more verbose for a thing we don't currently need.
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.source_bytes(labeled_bytes);
self.default_style.image = Some(image);
self
}
/// Set the image for the button. If not set, the button will have no image.
///
/// This will replace any image previously set.
///
/// This method is useful when doing more complex transforms. For example, to re-write more than
/// one color for your button image, do so externally and pass in the resultant GeomBatch here.
pub fn image_batch(mut self, batch: GeomBatch, bounds: geom::Bounds) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.source_batch(batch, bounds);
self.default_style.image = Some(image);
self
}
/// Rewrite the color of the button's image.
///
/// This has no effect if the button does not have an image.
///
/// If the style hasn't been set for the current ControlState, the style for
/// `ControlState::Default` will be used.
pub fn image_color<C: Into<RewriteColor>>(mut self, color: C, for_state: ControlState) -> Self {
let state_style = self.style_mut(for_state);
let mut image = state_style.image.take().unwrap_or_default();
image = image.color(color);
state_style.image = Some(image);
self
}
/// Set a background color for the image, other than the buttons background.
///
/// This has no effect if the button does not have an image.
///
/// If the style hasn't been set for the current ControlState, the style for
/// `ControlState::Default` will be used.
pub fn image_bg_color(mut self, color: Color, for_state: ControlState) -> Self {
let state_style = self.style_mut(for_state);
let mut image = state_style.image.take().unwrap_or_default();
image = image.bg_color(color);
state_style.image = Some(image);
self
}
/// Scale the bounds containing the image. If `image_dims` are not specified, the images
/// intrinsic size will be used.
///
/// See [`ButtonBuilder::image_content_mode`] to control how the image scales to fit
/// its custom bounds.
pub fn image_dims<D: Into<ScreenDims>>(mut self, dims: D) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.dims(dims);
self.default_style.image = Some(image);
self
}
pub fn override_style(self, style: &ButtonStyle) -> Self {
style.apply(self)
}
/// If a custom `image_dims` was set, control how the image should be scaled to its new bounds
///
/// If `image_dims` were not specified, the image will not be scaled, so content_mode has no
/// affect.
///
/// The default, [`ContentMode::ScaleAspectFit`] will only grow as much as it can while
/// maintaining its aspect ratio and not exceeding its bounds.
pub fn image_content_mode(mut self, content_mode: ContentMode) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.content_mode(content_mode);
self.default_style.image = Some(image);
self
}
/// Set independent rounding for each of the button's image's corners
pub fn image_corner_rounding<R: Into<CornerRounding>>(mut self, value: R) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.corner_rounding(value);
self.default_style.image = Some(image);
self
}
/// Set padding for the image
pub fn image_padding<EI: Into<EdgeInsets>>(mut self, value: EI) -> Self {
let mut image = self.default_style.image.take().unwrap_or_default();
image = image.padding(value);
self.default_style.image = Some(image);
self
}
/// Set a background color for the button based on the button's [`ControlState`].
///
/// If the style hasn't been set for the current ControlState, the style for
/// `ControlState::Default` will be used.
pub fn bg_color(mut self, color: Color, for_state: ControlState) -> Self {
self.style_mut(for_state).bg_color = Some(color);
self
}
/// Set an outline for the button based on the button's [`ControlState`].
///
/// If the style hasn't been set for the current ControlState, the style for
/// `ControlState::Default` will be used.
pub fn outline(mut self, outline: OutlineStyle, for_state: ControlState) -> Self {
self.style_mut(for_state).outline = Some(outline);
self
}
pub fn outline_color(mut self, color: Color, for_state: ControlState) -> Self {
let thickness: f64 = self
.style(for_state)
.outline
.map(|outline| outline.0)
.unwrap_or(DEFAULT_OUTLINE_THICKNESS);
self.style_mut(for_state).outline = Some((thickness, color));
self
}
/// Set a pre-rendered [GeomBatch] to use for the button instead of individual fields.
///
/// This is useful for applying one-off button designs that can't be accommodated by the
/// the existing ButtonBuilder methods.
pub fn custom_batch(mut self, batch: GeomBatch, for_state: ControlState) -> Self {
self.style_mut(for_state).custom_batch = Some(batch);
self
}
/// Set a hotkey for the button
pub fn hotkey<MK: Into<Option<MultiKey>>>(mut self, key: MK) -> Self {
self.hotkey = key.into();
self
}
/// Set a non-default tooltip [`Text`] to appear when hovering over the button.
///
/// If a `tooltip` is not specified, a default tooltip will be applied.
pub fn tooltip(mut self, tooltip: impl Into<Text>) -> Self {
self.tooltip = Some(tooltip.into());
self
}
/// If a `tooltip` is not specified, a default tooltip will be applied. Use `no_tooltip` when
/// you do not want even the default tooltip to appear.
pub fn no_tooltip(mut self) -> Self {
// otherwise the widgets `name` is used
self.tooltip = Some(Text::new());
self
}
/// Set a tooltip [`Text`] to appear when hovering over the button, when the button is
/// disabled.
///
/// This tooltip is only displayed when `disabled(true)` is also called.
pub fn disabled_tooltip(mut self, tooltip: impl Into<Text>) -> Self {
self.disabled_tooltip = Some(tooltip.into());
self
}
/// Like `disabled_tooltip`, but the tooltip may not exist.
pub fn maybe_disabled_tooltip(mut self, tooltip: Option<impl Into<Text>>) -> Self {
self.disabled_tooltip = tooltip.map(|x| x.into());
self
}
/// Sets a tooltip to appear whether the button is disabled or not.
pub fn tooltip_and_disabled(mut self, tooltip: impl Into<Text>) -> Self {
let tooltip = tooltip.into();
self.tooltip = Some(tooltip.clone());
self.disabled_tooltip = Some(tooltip.clone());
self
}
/// The button's items will be rendered in a vertical column
///
/// If the button doesn't have both an image and label, this has no effect.
pub fn vertical(mut self) -> Self {
self.stack_axis = Some(Axis::Vertical);
self
}
/// The button's items will be rendered in a horizontal row
///
/// If the button doesn't have both an image and label, this has no effect.
pub fn horizontal(mut self) -> Self {
self.stack_axis = Some(Axis::Horizontal);
self
}
/// The button cannot be clicked and will be styled as [`ControlState::Disabled`]
pub fn disabled(mut self, is_disabled: bool) -> Self {
self.is_disabled = is_disabled;
self
}
/// Display the button's label before the button's image.
///
/// If the button doesn't have both an image and label, this has no effect.
pub fn label_first(mut self) -> Self {
self.is_label_before_image = true;
self
}
/// Display the button's image before the button's label.
///
/// If the button doesn't have both an image and label, this has no effect.
pub fn image_first(mut self) -> Self {
self.is_label_before_image = false;
self
}
/// Spacing between the image and text of a button.
/// Has no effect if the button is text-only or image-only.
pub fn stack_spacing(mut self, value: f64) -> Self {
self.stack_spacing = value;
self
}
/// Set independent rounding for each of the button's corners
pub fn corner_rounding<R: Into<CornerRounding>>(mut self, value: R) -> Self {
self.corner_rounding = Some(value.into());
self
}
// Building
/// Build a button.
///
/// `action`: The event that will be fired when clicked
/// ```
/// # use widgetry::{Color, ButtonBuilder, ControlState, EventCtx};
///
/// fn build_some_buttons(ctx: &EventCtx) {
/// let one_off_builder = ButtonBuilder::new().label_text("foo").build(ctx, "foo");
///
/// // If you'd like to build a series of similar buttons, `clone` the builder first.
/// let red_builder = ButtonBuilder::new()
/// .bg_color(Color::RED, ControlState::Default)
/// .bg_color(Color::RED.alpha(0.3), ControlState::Disabled)
/// .outline((2.0, Color::WHITE), ControlState::Default);
///
/// let red_button_1 = red_builder.clone().label_text("First red button").build(ctx, "first");
/// let red_button_2 = red_builder.clone().label_text("Second red button").build(ctx, "second");
/// let red_button_3 = red_builder.label_text("Last red button").build(ctx, "third");
/// }
/// ```
pub fn build(&self, ctx: &EventCtx, action: &str) -> Button {
let normal = self.batch(ctx, ControlState::Default);
let hovered = self.batch(ctx, ControlState::Hovered);
let disabled = self.batch(ctx, ControlState::Disabled);
assert!(
normal.get_bounds() != geom::Bounds::zero(),
"button was empty"
);
let hitbox = normal.get_bounds().get_rectangle();
Button::new(
ctx,
normal,
hovered,
disabled,
self.hotkey.clone(),
action,
self.tooltip.clone(),
hitbox,
self.is_disabled,
self.disabled_tooltip.clone(),
)
}
/// Shorthand method to build a Button wrapped in a Widget
///
/// `action`: The event that will be fired when clicked
pub fn build_widget<I: AsRef<str>>(&self, ctx: &EventCtx, action: I) -> Widget {
let action = action.as_ref();
Widget::new(Box::new(self.build(ctx, action))).named(action)
}
/// Get the button's text label, if defined
pub fn get_action(&self) -> Option<&String> {
self.default_style
.label
.as_ref()
.and_then(|label| label.text.as_ref())
}
/// Shorthand method to build a default widget whose `action` is derived from the label's text.
pub fn build_def(&self, ctx: &EventCtx) -> Widget {
let action = self
.get_action()
.expect("Must set `label_text` before calling build_def");
self.build_widget(ctx, action)
}
// private methods
fn style_mut(&'b mut self, state: ControlState) -> &'b mut ButtonStateStyle<'a, 'c> {
match state {
ControlState::Default => &mut self.default_style,
ControlState::Hovered => &mut self.hover_style,
ControlState::Disabled => &mut self.disable_style,
}
}
fn style(&'b self, state: ControlState) -> &'b ButtonStateStyle<'a, 'c> {
match state {
ControlState::Default => &self.default_style,
ControlState::Hovered => &self.hover_style,
ControlState::Disabled => &self.disable_style,
}
}
pub fn batch(&self, ctx: &EventCtx, for_state: ControlState) -> GeomBatch {
let state_style = self.style(for_state);
if let Some(custom_batch) = state_style.custom_batch.as_ref() {
return custom_batch.clone();
}
let default_style = &self.default_style;
if let Some(custom_batch) = default_style.custom_batch.as_ref() {
return custom_batch.clone();
}
let image_batch: Option<GeomBatch> = match (&state_style.image, &default_style.image) {
(Some(state_image), Some(default_image)) => default_image
.merged_image_style(state_image)
.build_batch(ctx),
(None, Some(default_image)) => default_image.build_batch(ctx),
(None, None) => None,
(Some(_), None) => {
debug_assert!(
false,
"unexpectedly found a per-state image with no default image"
);
None
}
}
.map(|b| b.0);
let label_batch = state_style
.label
.as_ref()
.or_else(|| default_style.label.as_ref())
.and_then(|label| {
let default = default_style.label.as_ref();
if let Some(styled_text) = label
.styled_text
.as_ref()
.or_else(|| default.and_then(|d| d.styled_text.as_ref()))
{
return Some(styled_text.clone().bg(Color::CLEAR).render(ctx));
}
let text = label
.text
.clone()
.or_else(|| default.and_then(|d| d.text.clone()))?;
let color = label
.color
.or_else(|| default.and_then(|d| d.color))
.unwrap_or_else(|| ctx.style().text_primary_color);
let mut line = Line(text).fg(color);
if let Some(font_size) = label
.font_size
.or_else(|| default.and_then(|d| d.font_size))
{
line = line.size(font_size);
}
if let Some(font) = label.font.or_else(|| default.and_then(|d| d.font)) {
line = line.font(font);
}
Some(
Text::from(line)
// Add a clear background to maintain a consistent amount of space for the
// label based on the font, rather than the particular text.
// Otherwise a button with text "YYY" will not line up with a button
// with text "aaa".
.bg(Color::CLEAR)
.render(ctx),
)
});
let mut items = vec![];
if let Some(image_batch) = image_batch {
items.push(image_batch);
}
if let Some(label_batch) = label_batch {
items.push(label_batch);
}
if self.is_label_before_image {
items.reverse()
}
let mut stack = GeomBatchStack::horizontal(items);
if let Some(stack_axis) = self.stack_axis {
stack.set_axis(stack_axis);
}
stack.set_spacing(self.stack_spacing);
let mut button_widget = stack
.batch()
.batch() // TODO: rename -> `widget` or `build_widget`
.container()
.padding(self.padding)
.bg(state_style
.bg_color
.or(default_style.bg_color)
// If we have *no* background, buttons will be cropped differently depending on
// their specific content, and it becomes impossible to have
// uniformly sized buttons.
.unwrap_or(Color::CLEAR));
if let Some(outline) = state_style.outline.or(default_style.outline) {
button_widget = button_widget.outline(outline);
}
if let Some(corner_rounding) = self.corner_rounding {
button_widget = button_widget.corner_rounding(corner_rounding);
}
let (geom_batch, _hitbox) = button_widget.into_geom(ctx, None);
geom_batch
}
}
#[derive(Clone, Debug, Default)]
struct Label {
text: Option<String>,
color: Option<Color>,
styled_text: Option<Text>,
font_size: Option<usize>,
font: Option<Font>,
}