Skip to content

Commit

Permalink
xilem:example:calc: CE button MEDIUM_VIOLET_RED when clicked now and …
Browse files Browse the repository at this point in the history
…return to WHITE after number is pressed
  • Loading branch information
Artyom Sinyugin authored and ArtyomSinyugin committed Dec 20, 2024
1 parent 67594f3 commit 26acdd1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
24 changes: 18 additions & 6 deletions xilem/examples/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -215,7 +219,15 @@ fn app_logic(data: &mut Calculator) -> impl WidgetView<Calculator> {
))
.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),
Expand Down Expand Up @@ -255,9 +267,9 @@ fn display_label(text: &str) -> impl WidgetView<Calculator> {
/// 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<Label>,
callback: impl Fn(&mut Calculator) + Send + Sync + 'static,
) -> impl WidgetView<Calculator> + '_ {
) -> impl WidgetView<Calculator> {
sized_box(button(text, callback)).expand()
}

Expand Down
2 changes: 1 addition & 1 deletion xilem/src/view/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<T> From<T> for Label
where
T: Into<ArcStr>,
{
fn from(text: T) -> Label {
fn from(text: T) -> Self {
label(text)
}
}
Expand Down

0 comments on commit 26acdd1

Please sign in to comment.