forked from linebender/druid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.rs.old
88 lines (78 loc) · 2.54 KB
/
parse.rs.old
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2020 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
////use std::fmt::Display;
////use std::mem;
////use std::str::FromStr;
use crate::Size; ////
////use crate::kurbo::Size;
use crate::{
BoxConstraints, Data, Env, Event, EventCtx, LayoutCtx, LifeCycle, LifeCycleCtx, PaintCtx,
UpdateCtx, Widget, WidgetId,
};
use crate::String; ////
/// Converts a `Widget<String>` to a `Widget<Option<T>>`, mapping parse errors to None
pub struct Parse<T> {
widget: T,
state: String,
}
impl<T> Parse<T> {
pub fn new(widget: T) -> Self {
Self {
widget,
state: String::new(),
}
}
}
impl<T: FromStr + Display + Data, W: Widget<String>> Widget<Option<T>> for Parse<W> {
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut Option<T>, env: &Env) {
self.widget.event(ctx, event, &mut self.state, env);
*data = self.state.parse().ok();
}
fn lifecycle(
&mut self,
ctx: &mut LifeCycleCtx,
event: &LifeCycle,
data: &Option<T>,
env: &Env,
) {
if let LifeCycle::WidgetAdded = event {
if let Some(data) = data {
self.state = data.to_string();
}
}
self.widget.lifecycle(ctx, event, &self.state, env)
}
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()),
};
self.widget.update(ctx, &old, &self.state, env)
}
fn layout(
&mut self,
ctx: &mut LayoutCtx,
bc: &BoxConstraints,
_data: &Option<T>,
env: &Env,
) -> Size {
self.widget.layout(ctx, bc, &self.state, env)
}
fn paint(&mut self, paint: &mut PaintCtx, _data: &Option<T>, env: &Env) {
self.widget.paint(paint, &self.state, env)
}
fn id(&self) -> Option<WidgetId> {
self.widget.id()
}
}