Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xilem: Button with customisable label(text) #797

Merged
merged 6 commits into from
Dec 20, 2024

Conversation

ArtyomSinyugin
Copy link
Contributor

No description provided.

Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a refactor I've been meaning to do myself, so thanks for taking it on. Unfortunately, as it stands it isn't directly usable, but there's a viable path towards making it be so.

xilem/src/view/button.rs Outdated Show resolved Hide resolved
xilem/src/view/button.rs Outdated Show resolved Hide resolved
xilem/src/view/button.rs Outdated Show resolved Hide resolved
@ArtyomSinyugin
Copy link
Contributor Author

@DJMcNab Thank you for comments.
I do my best to figure out the best solution for this task.

I'll be fine if you decide to do it yourself in a better way in case you need to do it a little bit faster.
For me it is just an interesting way to improve my skill in Rust and to learn UI in Rust.

Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking really good! The last thing I think this needs is actual usage in an example. For example, changing the text colour of some buttons (e.g. the clear button in calc).

xilem/src/view/label.rs Outdated Show resolved Hide resolved
xilem/src/view/label.rs Outdated Show resolved Hide resolved
xilem/src/view/button.rs Outdated Show resolved Hide resolved
xilem/src/view/button.rs Show resolved Hide resolved
@ArtyomSinyugin ArtyomSinyugin force-pushed the main branch 2 times, most recently from 1e83453 to e94e48d Compare December 19, 2024 17:21
@ArtyomSinyugin
Copy link
Contributor Author

ArtyomSinyugin commented Dec 19, 2024

This is looking really good! The last thing I think this needs is actual usage in an example. For example, changing the text colour of some buttons (e.g. the clear button in calc).

Thank you. I fixed all tasks and updated calc example: CE button is MEDIUM_VIOLET_RED when clicked now and color returns to WHITE after any number is pressed (do not blame me for this, I am really do not know how to do UI).

@ArtyomSinyugin ArtyomSinyugin force-pushed the main branch 2 times, most recently from 4678477 to 08b0e7a Compare December 19, 2024 19:39
Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest that changing the example by applying something like:

diff --git a/xilem/examples/calc.rs b/xilem/examples/calc.rs
index 33a2358ad..5d5dfad3d 100644
--- a/xilem/examples/calc.rs
+++ b/xilem/examples/calc.rs
@@ -50,12 +50,15 @@ struct Calculator {
     numbers: [String; 2],
     result: Option<String>,
     operation: Option<MathOperator>,
-    clear_button: Color,
 }
 
 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) {
@@ -73,7 +76,6 @@ impl Calculator {
 
     fn clear_entry(&mut self) {
         self.clear_current_entry_on_input = false;
-        self.clear_button = Color::MEDIUM_VIOLET_RED;
         if self.result.is_some() {
             self.clear_all();
             return;
@@ -82,9 +84,6 @@ impl Calculator {
     }
 
     fn on_entered_digit(&mut self, digit: &str) {
-        if self.clear_button != Color::WHITE {
-            self.clear_button = Color::WHITE;
-        }
         if self.result.is_some() {
             self.clear_all();
         } else if self.clear_current_entry_on_input {
@@ -204,7 +203,6 @@ fn num_row(nums: [&'static str; 3], row: i32) -> impl GridSequence<Calculator> {
 const DISPLAY_FONT_SIZE: f32 = 30.;
 const GRID_GAP: f64 = 2.;
 fn app_logic(data: &mut Calculator) -> impl WidgetView<Calculator> {
-    let label = label("CE").brush(data.clear_button);
     grid(
         (
             // Display
@@ -222,7 +220,15 @@ fn app_logic(data: &mut Calculator) -> impl WidgetView<Calculator> {
             ))
             .grid_item(GridParams::new(0, 0, 4, 1)),
             // Top row
-            expanded_button(label, 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),
@@ -290,7 +296,6 @@ fn run(event_loop: EventLoopBuilder) -> Result<(), EventLoopError> {
         numbers: ["".into(), "".into()],
         result: None,
         operation: None,
-        clear_button: Color::WHITE,
     };
 
     let app = Xilem::new(data, app_logic);

would be more idiomatic. I can't apply this myself, because you have made this PR from your fork's default branch (main).

@ArtyomSinyugin ArtyomSinyugin force-pushed the main branch 3 times, most recently from 6e888d2 to 9c53b46 Compare December 20, 2024 10:21
@ArtyomSinyugin
Copy link
Contributor Author

would be more idiomatic. I can't apply this myself, because you have made this PR from your fork's default branch (main).

I've copied your version of calc.rs to my fork.

Copy link
Member

@DJMcNab DJMcNab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks!

@DJMcNab DJMcNab added this pull request to the merge queue Dec 20, 2024
Merged via the queue into linebender:main with commit 8f7ef7d Dec 20, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants