Skip to content

Commit 21b3308

Browse files
authored
Make Parse work better with floats (#2148)
This PR updates Parse to skip applying the to_string() in the event that the text already parses to the same value as data. This improves the behavior for f32 and any similar types where there is text lost from a parse/to_string round trip.
1 parent dda0073 commit 21b3308

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ Marcin Zając
2020
Laura Gallo
2121
Tim Murison
2222
Manmeet Singh
23+
Simon Fell

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ You can find its changes [documented below](#070---2021-01-01).
7070
- `ClipBox` and `Tabs` handle SCROLL_TO_VIEW ([#2141] by [@xarvic])
7171
- `EventCtx::submit_notification_without_warning` ([#2141] by [@xarvic])
7272
- `WidgetPod::requested_layout` ([#2145] by [@xarvic])
73+
- Make `Parse` work better with floats and similar types ([#2148] by [@superfell])
7374

7475
### Changed
7576

@@ -546,6 +547,7 @@ Last release without a changelog :(
546547
[@zedseven]: https://github.com/zedseven
547548
[@Pavel-N]: https://github.com/Pavel-N
548549
[@maurerdietmar]: https://github.com/maurerdietmar
550+
[@superfell]: https://github.com/superfell
549551

550552
[#599]: https://github.com/linebender/druid/pull/599
551553
[#611]: https://github.com/linebender/druid/pull/611
@@ -834,6 +836,7 @@ Last release without a changelog :(
834836
[#2117]: https://github.com/linebender/druid/pull/2117
835837
[#2117]: https://github.com/linebender/druid/pull/2141
836838
[#2145]: https://github.com/linebender/druid/pull/2145
839+
[#2148]: https://github.com/linebender/druid/pull/2148
837840

838841
[Unreleased]: https://github.com/linebender/druid/compare/v0.7.0...master
839842
[0.7.0]: https://github.com/linebender/druid/compare/v0.6.0...v0.7.0

druid/src/widget/parse.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,27 @@ impl<T: FromStr + Display + Data, W: Widget<String>> Widget<Option<T>> for Parse
6464
fn update(&mut self, ctx: &mut UpdateCtx, _old_data: &Option<T>, data: &Option<T>, env: &Env) {
6565
let old = match *data {
6666
None => return, // Don't clobber the input
67-
Some(ref x) => mem::replace(&mut self.state, x.to_string()),
67+
Some(ref x) => {
68+
// Its possible that the current self.state already represents the data value
69+
// in that case we shouldn't clobber the self.state. This helps deal
70+
// with types where parse()/to_string() round trips can lose information
71+
// e.g. with floating point numbers, text of "1.0" becomes "1" in the
72+
// round trip, and this makes it impossible to type in the . otherwise
73+
match self.state.parse() {
74+
Err(_) => Some(mem::replace(&mut self.state, x.to_string())),
75+
Ok(v) => {
76+
if !Data::same(&v, x) {
77+
Some(mem::replace(&mut self.state, x.to_string()))
78+
} else {
79+
None
80+
}
81+
}
82+
}
83+
}
6884
};
69-
self.widget.update(ctx, &old, &self.state, env)
85+
// if old is None here, that means that self.state hasn't changed
86+
let old_data = old.as_ref().unwrap_or(&self.state);
87+
self.widget.update(ctx, old_data, &self.state, env)
7088
}
7189

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

0 commit comments

Comments
 (0)