forked from linebender/druid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_widgets_md.rs
More file actions
77 lines (69 loc) · 2.23 KB
/
custom_widgets_md.rs
File metadata and controls
77 lines (69 loc) · 2.23 KB
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
use druid::widget::prelude::*;
use druid::widget::{Controller, Label, Painter, SizedBox, TextBox};
use druid::{
Color, Env, Event, EventCtx, KeyCode, PaintCtx, RenderContext, Selector, TimerToken, Widget,
WidgetExt,
};
use std::time::Duration;
const CORNER_RADIUS: f64 = 4.0;
const STROKE_WIDTH: f64 = 2.0;
// ANCHOR: color_swatch
fn make_color_swatch() -> Painter<Color> {
Painter::new(|ctx: &mut PaintCtx, data: &Color, env: &Env| {
let bounds = ctx.size().to_rect();
let rounded = bounds.to_rounded_rect(CORNER_RADIUS);
ctx.fill(rounded, data);
ctx.stroke(rounded, &env.get(druid::theme::PRIMARY_DARK), STROKE_WIDTH);
})
}
// ANCHOR_END: color_swatch
// ANCHOR: sized_swatch
fn sized_swatch() -> impl Widget<Color> {
SizedBox::new(make_color_swatch()).width(20.0).height(20.0)
}
// ANCHOR_END: sized_swatch
// ANCHOR: background_label
fn background_label() -> impl Widget<Color> {
Label::dynamic(|color: &Color, _| {
let (r, g, b, _) = color.as_rgba8();
format!("#{:X}{:X}{:X}", r, g, b)
})
.background(make_color_swatch())
}
// ANCHOR_END: background_label
// ANCHOR: annoying_textbox
const ACTION: Selector = Selector::new("hello.textbox-action");
const DELAY: Duration = Duration::from_millis(300);
struct TextBoxActionController {
timer: Option<TimerToken>,
}
impl TextBoxActionController {
pub fn new() -> Self {
TextBoxActionController { timer: None }
}
}
impl Controller<String, TextBox> for TextBoxActionController {
fn event(
&mut self,
child: &mut TextBox,
ctx: &mut EventCtx,
event: &Event,
data: &mut String,
env: &Env,
) {
match event {
Event::KeyDown(k) if k.key_code == KeyCode::Return => {
ctx.submit_command(ACTION, None);
}
Event::KeyUp(k) if k.key_code != KeyCode::Return => {
self.timer = Some(ctx.request_timer(DELAY));
child.event(ctx, event, data, env);
}
Event::Timer(token) if Some(*token) == self.timer => {
ctx.submit_command(ACTION, None);
}
_ => child.event(ctx, event, data, env),
}
}
}
// ANCHOR_END: annoying_textbox