Skip to content
New issue

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

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

Already on GitHub? # to your account

Toggle Switch Widget #231

Merged
merged 5 commits into from
Nov 5, 2019
Merged

Toggle Switch Widget #231

merged 5 commits into from
Nov 5, 2019

Conversation

scholtzan
Copy link
Collaborator

I was playing around with widgets a bit after reading Building a widget for Druid and implemented a toggle switch:
Screenshot 2019-10-25 at 1 36 12 PM

Any comments are appreciated.

Copy link
Collaborator

@futurepaul futurepaul left a comment

Choose a reason for hiding this comment

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

Thank you for this! It looks good to me overall, just a few minor comments inline. I do think we could debate whether the label should be inside / outside / none at all (conveniently, switches seem to be totally different on every OS), but I think that debate can come later when we have a full palette of widgets to critique.

);

// paint different background for on and off state
let background_gradient = if *data {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have an orange "caution" color that none of the widgets have used yet. Could potentially use it here for the off state, but this also looks fine as-is. Orange might be too intense for a typical switch use case? Here's the figma if you're interested.

Copy link
Member

Choose a reason for hiding this comment

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

I think that as future work color should be configurable.

value: bool,
}

fn build_widget() -> impl Widget<DemoState> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

this might be a bit too minimal of an example. potentially we could put switch inside another example, or expand this example to show off some other complementary aspects of druid. but I defer to @cmyr who has helped keep a handle on this directory.

Copy link
Member

Choose a reason for hiding this comment

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

I find that when I'm using sample code, I benefit when examples are more complex, use API in interesting ways, or otherwise show me things I might be less likely to figure out on my own.

So this is okay, but it would also be totally okay to add this to an existing example, to change behaviour in some way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I couldn't think of how existing examples could be extended in a useful way right now. But maybe there will be some better examples in the future that could replace this one.

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

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

Awesome @scholtzan this is graet.

a bunch of little nits and observations.

The one thing this is missing (to me) is an animation during the transition. But that can be future work.

value: bool,
}

fn build_widget() -> impl Widget<DemoState> {
Copy link
Member

Choose a reason for hiding this comment

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

I find that when I'm using sample code, I benefit when examples are more complex, use API in interesting ways, or otherwise show me things I might be less likely to figure out on my own.

So this is okay, but it would also be totally okay to add this to an existing example, to change behaviour in some way.

impl SwitchRaw {
fn knob_hit_test(&self, knob_width: f64, mouse_pos: Point) -> bool {
let knob_circle = Circle::new(self.knob_pos, knob_width / 2.);
if mouse_pos.distance(knob_circle.center) < knob_circle.radius {
Copy link
Member

Choose a reason for hiding this comment

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

the less obvious but maybe canonical way to do this might be if knob_circle.winding(mouse_pos) > 0 { .. but personally I think that the Shape trait should just have a contains(pt: Point) type method that calls winding under the hood; I think it would be a much simpler API to discover and to use, in many cases.

);

// paint different background for on and off state
let background_gradient = if *data {
Copy link
Member

Choose a reason for hiding this comment

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

I think that as future work color should be configurable.

@scholtzan scholtzan force-pushed the switch branch 2 times, most recently from 0e2545f to 2a36f8b Compare November 5, 2019 00:31
Copy link
Collaborator

@futurepaul futurepaul left a comment

Choose a reason for hiding this comment

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

Looks good to me!

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

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

Okay, I withdraw my suggestion to use LocalizedString; your use case is tricky and I need to write more docs about how localization is supposed to work.

@cmyr
Copy link
Member

cmyr commented Nov 5, 2019

Feel free to merge this @scholtzan!

@scholtzan
Copy link
Collaborator Author

I don't have write access to the repo

@cmyr
Copy link
Member

cmyr commented Nov 5, 2019

oops, you do now!

@scholtzan scholtzan merged commit 5184b2c into linebender:master Nov 5, 2019
@cmyr cmyr mentioned this pull request Dec 31, 2019
7 tasks
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants