-
Notifications
You must be signed in to change notification settings - Fork 119
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
Conversation
There was a problem hiding this 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.
@DJMcNab Thank you for comments. 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. |
There was a problem hiding this 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
).
1e83453
to
e94e48d
Compare
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). |
4678477
to
08b0e7a
Compare
…use 'this check is not needed - the rebuild internally will re-do the same work'
There was a problem hiding this 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
).
6e888d2
to
9c53b46
Compare
…return to WHITE after number is pressed
I've copied your version of calc.rs to my fork. |
There was a problem hiding this 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!
No description provided.