zng_wgt_text_input/
label.rs1use zng_app::access::ACCESS_CLICK_EVENT;
4use zng_ext_input::{
5 focus::{FOCUS, FocusInfoBuilder},
6 mouse::MOUSE_INPUT_EVENT,
7 touch::TOUCH_INPUT_EVENT,
8};
9use zng_wgt::prelude::*;
10use zng_wgt_input::focus::FocusableMix;
11use zng_wgt_style::{Style, StyleMix, impl_style_fn};
12
13#[widget($crate::label::Label {
33 ($txt:expr, $target:expr $(,)?) => {
34 txt = $txt;
35 target = $target;
36 };
37})]
38pub struct Label(FocusableMix<StyleMix<zng_wgt_text::Text>>);
39impl Label {
40 fn widget_intrinsic(&mut self) {
41 self.style_intrinsic(STYLE_FN_VAR, property_id!(self::style_fn));
42 }
43}
44impl_style_fn!(Label, DefaultStyle);
45
46#[widget($crate::label::DefaultStyle)]
48pub struct DefaultStyle(Style);
49impl DefaultStyle {
50 fn widget_intrinsic(&mut self) {
51 widget_set! {
52 self;
53 replace = true;
54 }
55 }
56}
57
58#[property(CONTEXT, widget_impl(Label))]
65pub fn target(child: impl IntoUiNode, target: impl IntoVar<WidgetId>) -> UiNode {
66 let target = target.into_var();
67 let mut prev_target = None::<WidgetId>;
68
69 match_node(child, move |c, op| match op {
70 UiNodeOp::Init => {
71 WIDGET
72 .sub_var(&target)
73 .sub_event_when(&MOUSE_INPUT_EVENT, |a| a.is_mouse_down())
74 .sub_event_when(&TOUCH_INPUT_EVENT, |a| a.is_touch_start())
75 .sub_event_when(&ACCESS_CLICK_EVENT, |a| a.is_primary);
76 }
77 UiNodeOp::Info { info } => {
78 c.info(info);
79
80 FocusInfoBuilder::new(info).focusable(false);
81
82 if let Some(mut a) = info.access() {
83 let target = target.get();
84 prev_target = Some(target);
85 a.set_labels(target);
86 }
87 }
88 UiNodeOp::Update { updates } => {
89 if let Some(id) = target.get_new() {
90 if let Some(id) = prev_target.take() {
91 UPDATES.update_info(id);
92 }
93 UPDATES.update_info(id);
94 WIDGET.update_info();
95 }
96
97 c.update(updates);
98
99 if MOUSE_INPUT_EVENT.any_update(true, |a| a.is_mouse_down())
100 || TOUCH_INPUT_EVENT.any_update(true, |a| a.is_touch_start())
101 || ACCESS_CLICK_EVENT.any_update(true, |a| a.is_primary)
102 {
103 FOCUS.focus_widget_or_enter(target.get(), true, false);
104 }
105 }
106 _ => {}
107 })
108}