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

Make Parse work better with floats #2148

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ Marcin Zając
Laura Gallo
Tim Murison
Manmeet Singh
Simon Fell
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ You can find its changes [documented below](#070---2021-01-01).
- `ClipBox` and `Tabs` handle SCROLL_TO_VIEW ([#2141] by [@xarvic])
- `EventCtx::submit_notification_without_warning` ([#2141] by [@xarvic])
- `WidgetPod::requested_layout` ([#2145] by [@xarvic])
- Make `Parse` work better with floats and similar types ([#2148] by [@superfell])

### Changed

Expand Down Expand Up @@ -546,6 +547,7 @@ Last release without a changelog :(
[@zedseven]: https://github.com/zedseven
[@Pavel-N]: https://github.com/Pavel-N
[@maurerdietmar]: https://github.com/maurerdietmar
[@superfell]: https://github.com/superfell

[#599]: https://github.com/linebender/druid/pull/599
[#611]: https://github.com/linebender/druid/pull/611
Expand Down Expand Up @@ -834,6 +836,7 @@ Last release without a changelog :(
[#2117]: https://github.com/linebender/druid/pull/2117
[#2117]: https://github.com/linebender/druid/pull/2141
[#2145]: https://github.com/linebender/druid/pull/2145
[#2148]: https://github.com/linebender/druid/pull/2148

[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0
Expand Down
22 changes: 20 additions & 2 deletions druid/src/widget/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,27 @@ impl<T: FromStr + Display + Data, W: Widget<String>> Widget<Option<T>> for Parse
fn update(&mut self, ctx: &mut UpdateCtx, _old_data: &Option<T>, data: &Option<T>, env: &Env) {
let old = match *data {
None => return, // Don't clobber the input
Some(ref x) => mem::replace(&mut self.state, x.to_string()),
Some(ref x) => {
// Its possible that the current self.state already represents the data value
// in that case we shouldn't clobber the self.state. This helps deal
// with types where parse()/to_string() round trips can lose information
// e.g. with floating point numbers, text of "1.0" becomes "1" in the
// round trip, and this makes it impossible to type in the . otherwise
match self.state.parse() {
Err(_) => Some(mem::replace(&mut self.state, x.to_string())),
Ok(v) => {
if !Data::same(&v, x) {
Some(mem::replace(&mut self.state, x.to_string()))
} else {
None
}
}
}
}
};
self.widget.update(ctx, &old, &self.state, env)
// if old is None here, that means that self.state hasn't changed
let old_data = old.as_ref().unwrap_or(&self.state);
self.widget.update(ctx, old_data, &self.state, env)
}

#[instrument(name = "Parse", level = "trace", skip(self, ctx, bc, _data, env))]
Expand Down