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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//! 自制密码输入框

use druid::{piet::*, text::TextComponent, Rect, Widget};

/// 自制密码输入框,可以简单的输入密码和粘贴密码
pub struct PasswordBox {
    cursor: Option<usize>,
    mask_text: Option<druid::piet::PietTextLayout>,
}

impl PasswordBox {
    /// 创建一个空密码框
    pub const fn new() -> Self {
        Self {
            cursor: None,
            mask_text: None,
        }
    }
}

impl Default for PasswordBox {
    fn default() -> Self {
        Self::new()
    }
}

impl Widget<String> for PasswordBox {
    fn event(
        &mut self,
        ctx: &mut druid::EventCtx,
        event: &druid::Event,
        data: &mut String,
        env: &druid::Env,
    ) {
        match event {
            druid::Event::KeyDown(k) => {
                if ctx.is_focused() && !k.is_composing {
                    let is_ctrl_pressed = {
                        #[cfg(target_os = "macos")]
                        {
                            k.mods.meta() // 对应 Mac 上的 Command 键
                        }
                        #[cfg(not(target_os = "macos"))]
                        {
                            k.mods.ctrl()
                        }
                    };
                    if let Some(cursor) = &mut self.cursor {
                        match k.code {
                            druid::Code::Backspace => {
                                if *cursor > 0 {
                                    if is_ctrl_pressed {
                                        for _ in 0..*cursor {
                                            data.remove(0);
                                        }
                                        *cursor = 0;
                                        ctx.request_paint();
                                    } else if let Some((ci, _)) =
                                        data.char_indices().nth(*cursor - 1)
                                    {
                                        *cursor -= 1;
                                        data.remove(ci);
                                        ctx.request_paint();
                                    }
                                }
                            }
                            druid::Code::ArrowLeft => {
                                if *cursor > 0 {
                                    if let Some((ci, _)) = data.char_indices().nth(*cursor - 1) {
                                        *cursor = ci;
                                        ctx.request_paint();
                                    }
                                }
                            }
                            druid::Code::ArrowRight => {
                                if *cursor < data.chars().count() {
                                    *cursor += 1;
                                    ctx.request_paint();
                                }
                            }
                            druid::Code::ArrowUp => {
                                *cursor = 0;
                                ctx.request_paint();
                            }
                            druid::Code::ArrowDown => {
                                *cursor = data.chars().count();
                                ctx.request_paint();
                            }
                            druid::Code::Tab => {
                                if k.mods.shift() {
                                    ctx.focus_prev();
                                } else {
                                    ctx.focus_next();
                                }
                                ctx.request_paint();
                            }
                            druid::Code::Enter => {}
                            code => {
                                if is_ctrl_pressed && code == druid::Code::KeyV {
                                    // 黏贴
                                    ctx.submit_command(druid::commands::PASTE.to(ctx.window_id()));
                                } else if !is_ctrl_pressed && !k.mods.alt() {
                                    if let druid::KbKey::Character(c) = &k.key {
                                        if *cursor > 0 {
                                            if let Some((ci, ch)) =
                                                data.char_indices().nth(*cursor - 1)
                                            {
                                                data.insert_str(ci + ch.len_utf8(), c.as_str());
                                            } else {
                                                data.insert_str(0, c.as_str())
                                            }
                                        } else {
                                            data.insert_str(0, c.as_str())
                                        }
                                        *cursor += c.chars().count();
                                        ctx.request_paint();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            druid::Event::MouseDown(m) => {
                ctx.set_focus(ctx.widget_id());
                if let Some(mask_text) = &self.mask_text {
                    let textbox_insets = env.get(druid::theme::TEXTBOX_INSETS);
                    let cursor_index =
                        ((m.pos.x - textbox_insets.x0) / mask_text.size().width).max(0.);
                    self.cursor = Some((cursor_index as usize).min(data.chars().count()));
                    ctx.request_paint();
                    ctx.request_focus();
                }
            }
            druid::Event::MouseMove(_) => {
                ctx.set_cursor(&druid::Cursor::IBeam);
            }
            druid::Event::Paste(item) if ctx.is_focused() => {
                if let Some(cursor) = &mut self.cursor {
                    if let Some(c) = item.get_string() {
                        if *cursor > 0 {
                            if let Some((ci, ch)) = data.char_indices().nth(*cursor - 1) {
                                data.insert_str(ci + ch.len_utf8(), c.as_str());
                            } else {
                                data.insert_str(0, c.as_str())
                            }
                        } else {
                            data.insert_str(0, c.as_str())
                        }
                        *cursor += c.chars().count();
                        ctx.request_paint();
                    }
                }
            }
            druid::Event::Notification(cmd) => {
                if cmd.is(TextComponent::TAB) {
                    ctx.focus_next();
                    ctx.request_paint();
                    ctx.set_handled();
                }
            }
            _ => {}
        }
    }

    fn lifecycle(
        &mut self,
        ctx: &mut druid::LifeCycleCtx,
        event: &druid::LifeCycle,
        data: &String,
        _env: &druid::Env,
    ) {
        // todo!()

        if let druid::LifeCycle::FocusChanged(f) = event {
            if *f {
                self.cursor = Some(data.chars().count());
            } else {
                self.cursor = None;
            }
            ctx.request_paint();
        } else if let druid::LifeCycle::BuildFocusChain = event {
            ctx.register_for_focus();
        }
    }

    fn update(
        &mut self,
        _ctx: &mut druid::UpdateCtx,
        _old_data: &String,
        data: &String,
        _env: &druid::Env,
    ) {
        if let Some(cursor) = &mut self.cursor {
            *cursor = (*cursor).min(data.chars().count());
        }
    }

    fn layout(
        &mut self,
        _ctx: &mut druid::LayoutCtx,
        bc: &druid::BoxConstraints,
        _data: &String,
        _env: &druid::Env,
    ) -> druid::Size {
        bc.constrain((100., 32.))
    }

    fn paint(&mut self, ctx: &mut druid::PaintCtx, data: &String, env: &druid::Env) {
        if self.mask_text.is_none() {
            self.mask_text = ctx
                .text()
                .new_text_layout("•")
                .text_color(env.get(druid::theme::TEXT_COLOR))
                .font(
                    env.get(crate::theme::color::typography::CAPTION).family,
                    16.,
                )
                .build()
                .ok();
        }

        let size = ctx.size();
        let background_color = env.get(druid::theme::BACKGROUND_LIGHT);
        let border_width = env.get(druid::theme::TEXTBOX_BORDER_WIDTH);
        let pass_len = data.chars().count();

        let is_focused = ctx.is_focused();

        let border_color = if is_focused {
            env.get(druid::theme::PRIMARY_LIGHT)
        } else {
            env.get(druid::theme::BORDER_DARK)
        };

        // Paint the background
        let clip_rect = size
            .to_rect()
            .inset(-border_width / 2.0)
            .to_rounded_rect(env.get(druid::theme::TEXTBOX_BORDER_RADIUS));

        ctx.fill(clip_rect, &background_color);

        if let Some(mask_text) = &self.mask_text {
            let mask_size = mask_text.size();
            let cursor_color = env.get(druid::theme::CURSOR_COLOR);
            let textbox_insets = env.get(druid::theme::TEXTBOX_INSETS);

            ctx.with_save(|ctx| {
                ctx.clip(clip_rect);

                for c in 0..pass_len {
                    ctx.draw_text(
                        mask_text,
                        (
                            textbox_insets.x0 + (c as f64) * mask_size.width,
                            textbox_insets.y0,
                        ),
                    );
                }

                if let Some(cursor) = &self.cursor {
                    let cursor_rect = Rect::from_origin_size(
                        (
                            (*cursor as f64) * mask_size.width + textbox_insets.x0,
                            textbox_insets.y0,
                        ),
                        (1., mask_size.height),
                    );

                    ctx.fill(cursor_rect, &cursor_color);
                }
            })
        }

        // Paint the border
        ctx.stroke(clip_rect, &border_color, border_width);
    }
}