-
Notifications
You must be signed in to change notification settings - Fork 259
[Fixes #763] Introduced labelled checkboxes #765
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
04c5ab0
[Fixes #763] Introduced labelled checkboxes
michalfita cfdc5dd
[Fixes #763] First attempt at `MultiChoiceGroup<T>`
michalfita b3fd11c
Merge branch 'main' into issue/763/improve-checkbox-ux
gyscos 202a732
Fix clippy warnings
gyscos e832382
Merge branch 'main' into issue/763/improve-checkbox-ux
michalfita 5a41fa2
Some clean up of forgotten code
michalfita 8fe896b
Address review comments
michalfita File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,3 +96,4 @@ rand = "0.9" | |
| pretty-bytes = "0.2" | ||
| serde_json = "1.0.85" | ||
| serde_yaml = "0.9.13" | ||
| parking_lot = "0.12" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| //! This example demonstrates how to use a checkboxes manually to | ||
| //! allow users to select multiple values in a set. | ||
| use ahash::HashSet; | ||
| use cursive::views::{Checkbox, Dialog, DummyView, LinearLayout}; | ||
| use parking_lot::Mutex; | ||
| use std::fmt::Display; | ||
| use std::sync::Arc; | ||
|
|
||
| // This example uses checkboxes. | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| enum Toppings { | ||
| ChocolateSprinkles, | ||
| CrushedAlmonds, | ||
| StrawberrySauce, | ||
| } | ||
|
|
||
| // #[derive(Debug, PartialEq, Eq, Hash)] | ||
| // enum Extras { | ||
| // Tissues, | ||
| // DarkCone, | ||
| // ChocolateFlake, | ||
| // } | ||
|
|
||
| #[derive(Debug, Default)] | ||
| struct Extras { | ||
| tissues: bool, | ||
| dark_cone: bool, | ||
| chocolate_flake: bool, | ||
| } | ||
|
|
||
| impl Display for Toppings { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| match *self { | ||
| Toppings::ChocolateSprinkles => write!(f, "Chocolate Sprinkles"), | ||
| Toppings::CrushedAlmonds => write!(f, "Crushed Almonds"), | ||
| Toppings::StrawberrySauce => write!(f, "Strawberry Sauce"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Display for Extras { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| let extras = [ | ||
| if self.tissues { "Tissues" } else { "" }, | ||
| if self.dark_cone { "Dark Cone" } else { "" }, | ||
| if self.chocolate_flake { | ||
| "Chocolate Flake" | ||
| } else { | ||
| "" | ||
| }, | ||
| ]; | ||
| write!( | ||
| f, | ||
| "{}", | ||
| extras | ||
| .into_iter() | ||
| .filter(|s| !s.is_empty()) | ||
| .collect::<Vec<&str>>() | ||
| .join(", ") | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| let mut siv = cursive::default(); | ||
|
|
||
| // Application wide container w/toppings choices. | ||
| let toppings: Arc<Mutex<HashSet<Toppings>>> = Arc::new(Mutex::new(HashSet::default())); | ||
|
|
||
| // Application wide container w/extras choices. | ||
| let extras: Arc<Mutex<Extras>> = Arc::new(Mutex::new(Extras::default())); | ||
|
|
||
| siv.add_layer( | ||
| Dialog::new() | ||
| .title("Make your selections") | ||
| .content( | ||
| LinearLayout::horizontal() | ||
| .child( | ||
| LinearLayout::vertical() | ||
| .child(Checkbox::labelled("Chocolate Sprinkles").on_change({ | ||
| let toppings = Arc::clone(&toppings); | ||
| move |_, checked| { | ||
| if checked { | ||
| toppings.lock().insert(Toppings::ChocolateSprinkles); | ||
| } else { | ||
| toppings.lock().remove(&Toppings::ChocolateSprinkles); | ||
| } | ||
| } | ||
| })) | ||
| .child(Checkbox::labelled("Crushed Almonds").on_change({ | ||
| let toppings = Arc::clone(&toppings); | ||
| move |_, checked| { | ||
| if checked { | ||
| toppings.lock().insert(Toppings::CrushedAlmonds); | ||
| } else { | ||
| toppings.lock().remove(&Toppings::CrushedAlmonds); | ||
| } | ||
| } | ||
| })) | ||
| .child(Checkbox::labelled("Strawberry Sauce").on_change({ | ||
| let toppings = Arc::clone(&toppings); | ||
| move |_, checked| { | ||
| if checked { | ||
| toppings.lock().insert(Toppings::StrawberrySauce); | ||
| } else { | ||
| toppings.lock().remove(&Toppings::StrawberrySauce); | ||
| } | ||
| } | ||
| })), | ||
| ) | ||
| .child(DummyView) | ||
| .child( | ||
| LinearLayout::vertical() | ||
| .child(Checkbox::labelled("Chocolate Flake").on_change({ | ||
| let extras = Arc::clone(&extras); | ||
| move |_, checked| { | ||
| extras.lock().chocolate_flake = checked; | ||
| } | ||
| })) | ||
| .child(Checkbox::labelled("Dark Cone").on_change({ | ||
| let extras = Arc::clone(&extras); | ||
| move |_, checked| { | ||
| extras.lock().dark_cone = checked; | ||
| } | ||
| })) | ||
| .child(Checkbox::labelled("Tissues").on_change({ | ||
| let extras = Arc::clone(&extras); | ||
| move |_, checked| { | ||
| extras.lock().tissues = checked; | ||
| } | ||
| })), | ||
| ), | ||
| ) | ||
| .button("Ok", move |s| { | ||
| s.pop_layer(); | ||
| let toppings = toppings | ||
| .lock() | ||
| .iter() | ||
| .map(|t| t.to_string()) | ||
| .collect::<Vec<String>>() | ||
| .join(", "); | ||
| let extras = extras.lock().to_string(); | ||
| let text = format!("Toppings: {toppings}\nExtras: {extras}"); | ||
| s.add_layer(Dialog::text(text).button("Ok", |s| s.quit())); | ||
| }), | ||
| ); | ||
|
|
||
| siv.run(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You might be able to switch the two around, so it only clones the
Arc<T>, which means you don't need to implementCloneforItem<T>anymore.