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
use crate::{api::prelude::*, proc_macros::*};
#[derive(Default, AsAny)]
pub struct FocusBehaviorState {
request_focus: bool,
}
impl FocusBehaviorState {
fn request_focus(&mut self) {
self.request_focus = true;
}
}
impl State for FocusBehaviorState {
fn update(&mut self, _: &mut Registry, ctx: &mut Context) {
if !self.request_focus || !focus_behavior(ctx.widget()).enabled() {
return;
}
let target: Entity = (*focus_behavior(ctx.widget()).target()).into();
ctx.push_event_by_window(FocusEvent::RequestFocus(target));
self.request_focus = false;
}
}
widget!(
FocusBehavior<FocusBehaviorState>: MouseHandler {
target: u32,
focused: bool
}
);
impl Template for FocusBehavior {
fn template(self, id: Entity, _: &mut BuildContext) -> Self {
self.name("FocusBehavior")
.focused(true)
.on_mouse_down(move |states, _| {
states.get_mut::<FocusBehaviorState>(id).request_focus();
false
})
}
}