From 8f7ef7dde50258d17f933c8b744739adaa068b50 Mon Sep 17 00:00:00 2001 From: ArtyomSinyugin <74111551+ArtyomSinyugin@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:40:38 +0300 Subject: [PATCH] xilem: Button with customisable label(text) (#797) Co-authored-by: Artyom Sinyugin --- xilem/examples/calc.rs | 24 +++++++++++++----- xilem/examples/external_event_loop.rs | 5 ++-- xilem/examples/flex.rs | 5 ++-- xilem/src/view/button.rs | 35 +++++++++++++++++++-------- xilem/src/view/label.rs | 14 ++++++++--- 5 files changed, 58 insertions(+), 25 deletions(-) diff --git a/xilem/examples/calc.rs b/xilem/examples/calc.rs index 5126aaec5..e8bb20177 100644 --- a/xilem/examples/calc.rs +++ b/xilem/examples/calc.rs @@ -11,9 +11,9 @@ use winit::error::EventLoopError; use winit::window::Window; use xilem::view::{ button, flex, grid, label, sized_box, Axis, Flex, FlexSequence, FlexSpacer, GridExt, - GridSequence, + GridSequence, Label, }; -use xilem::{EventLoop, EventLoopBuilder, WidgetView, Xilem}; +use xilem::{Color, EventLoop, EventLoopBuilder, WidgetView, Xilem}; #[derive(Copy, Clone)] enum MathOperator { @@ -53,7 +53,11 @@ struct Calculator { impl Calculator { fn get_current_number(&self) -> String { - self.numbers[self.current_num_index].clone() + self.current_number().to_string() + } + + fn current_number(&self) -> &str { + &self.numbers[self.current_num_index] } fn set_current_number(&mut self, new_num: String) { @@ -215,7 +219,15 @@ fn app_logic(data: &mut Calculator) -> impl WidgetView { )) .grid_item(GridParams::new(0, 0, 4, 1)), // Top row - expanded_button("CE", Calculator::clear_entry).grid_pos(0, 1), + expanded_button( + label("CE").brush(if data.get_current_number().is_empty() { + Color::MEDIUM_VIOLET_RED + } else { + Color::WHITE + }), + Calculator::clear_entry, + ) + .grid_pos(0, 1), expanded_button("C", Calculator::clear_all).grid_pos(1, 1), expanded_button("DEL", Calculator::on_delete).grid_pos(2, 1), operator_button(MathOperator::Divide).grid_pos(3, 1), @@ -255,9 +267,9 @@ fn display_label(text: &str) -> impl WidgetView { /// Returns a button contained in an expanded box. Useful for the buttons so that /// they take up all available space in flex containers. fn expanded_button( - text: &str, + text: impl Into