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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#![doc(html_favicon_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo-icon.png")]
#![doc(html_logo_url = "https://raw.githubusercontent.com/zng-ui/zng/main/examples/image/res/zng-logo.png")]
//!
//! Button widget.
//!
//! # Crate
//!
#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]
#![warn(unused_extern_crates)]
#![warn(missing_docs)]

zng_wgt::enable_widget_macros!();

use std::{any::TypeId, ops};

use colors::{ACCENT_COLOR_VAR, BASE_COLOR_VAR};
use zng_app::event::CommandParam;
use zng_var::ReadOnlyContextVar;
use zng_wgt::{base_color, border, corner_radius, is_disabled, prelude::*};
use zng_wgt_access::{access_role, labelled_by_child, AccessRole};
use zng_wgt_container::{child_align, padding, Container};
use zng_wgt_fill::background_color;
use zng_wgt_filter::{child_opacity, saturate};
use zng_wgt_input::{
    cursor,
    focus::FocusableMix,
    gesture::{on_click, on_disabled_click, ClickArgs},
    is_cap_hovered, is_pressed,
    pointer_capture::{capture_pointer, CaptureMode},
    CursorIcon,
};
use zng_wgt_style::{impl_style_fn, style_fn, Style, StyleMix};
use zng_wgt_text::{font_color, underline, Text, FONT_COLOR_VAR};
use zng_wgt_tooltip::{tooltip, tooltip_fn, Tip, TooltipArgs};

/// A clickable container.
///
/// # Shorthand
///
/// The `Button!` macro provides a shorthand init that sets the command, `Button!(SOME_CMD)`.
#[widget($crate::Button {
    ($cmd:expr) => {
        cmd = $cmd;
    };
})]
pub struct Button(FocusableMix<StyleMix<Container>>);
impl Button {
    fn widget_intrinsic(&mut self) {
        self.style_intrinsic(STYLE_FN_VAR, property_id!(self::style_fn));

        widget_set! {
            self;
            style_base_fn = style_fn!(|_| DefaultStyle!());
            capture_pointer = true;
            labelled_by_child = true;
        }

        self.widget_builder().push_build_action(|wgt| {
            if let Some(cmd) = wgt.capture_var::<Command>(property_id!(Self::cmd)) {
                if wgt.property(property_id!(Self::child)).is_none() {
                    wgt.set_child(presenter(cmd.clone(), CMD_CHILD_FN_VAR));
                }

                let enabled = wgt.property(property_id!(zng_wgt::enabled)).is_none();
                let visibility = wgt.property(property_id!(zng_wgt::visibility)).is_none();
                wgt.push_intrinsic(
                    NestGroup::CONTEXT,
                    "cmd-context",
                    clmv!(cmd, |mut child| {
                        if enabled {
                            child = zng_wgt::enabled(child, cmd.flat_map(|c| c.is_enabled())).boxed();
                        }
                        if visibility {
                            child = zng_wgt::visibility(child, cmd.flat_map(|c| c.has_handlers()).map_into()).boxed();
                        }

                        with_context_var(child, CMD_VAR, cmd.map(|c| Some(*c)))
                    }),
                );

                let on_click = wgt.property(property_id!(Self::on_click)).is_none();
                let on_disabled_click = wgt.property(property_id!(on_disabled_click)).is_none();
                let tooltip = wgt.property(property_id!(tooltip)).is_none() && wgt.property(property_id!(tooltip_fn)).is_none();
                if on_click || on_disabled_click || tooltip {
                    wgt.push_intrinsic(
                        NestGroup::EVENT,
                        "cmd-event",
                        clmv!(cmd, |mut child| {
                            if on_click {
                                child = self::on_click(
                                    child,
                                    hn!(cmd, |args: &ClickArgs| {
                                        let cmd = cmd.get();
                                        if cmd.is_enabled_value() {
                                            if let Some(param) = CMD_PARAM_VAR.get() {
                                                cmd.notify_param(param);
                                            } else {
                                                cmd.notify();
                                            }
                                            args.propagation().stop();
                                        }
                                    }),
                                )
                                .boxed();
                            }
                            if on_disabled_click {
                                child = self::on_disabled_click(
                                    child,
                                    hn!(cmd, |args: &ClickArgs| {
                                        let cmd = cmd.get();
                                        if !cmd.is_enabled_value() {
                                            if let Some(param) = CMD_PARAM_VAR.get() {
                                                cmd.notify_param(param);
                                            } else {
                                                cmd.notify();
                                            }
                                            args.propagation().stop();
                                        }
                                    }),
                                )
                                .boxed();
                            }
                            if tooltip {
                                child = self::tooltip_fn(
                                    child,
                                    merge_var!(cmd, CMD_TOOLTIP_FN_VAR, |cmd, tt_fn| {
                                        if tt_fn.is_nil() {
                                            WidgetFn::nil()
                                        } else {
                                            wgt_fn!(cmd, tt_fn, |tooltip| { tt_fn(CmdTooltipArgs { tooltip, cmd }) })
                                        }
                                    }),
                                )
                                .boxed();
                            }
                            child
                        }),
                    );
                }
            }
        });
    }

    widget_impl! {
        /// Button click event.
        pub on_click(handler: impl WidgetHandler<ClickArgs>);

        /// If pointer interaction with other widgets is blocked while the button is pressed.
        ///
        /// Enabled by default in this widget.
        pub capture_pointer(mode: impl IntoVar<CaptureMode>);
    }
}
impl_style_fn!(Button);

context_var! {
    /// Optional parameter for the button to use when notifying command.
    pub static CMD_PARAM_VAR: Option<CommandParam> = None;

    /// Widget function used when `cmd` is set and `child` is not.
    pub static CMD_CHILD_FN_VAR: WidgetFn<Command> = WidgetFn::new(default_cmd_child_fn);

    /// Widget function used when `cmd` is set and `tooltip_fn`, `tooltip` are not set.
    pub static CMD_TOOLTIP_FN_VAR: WidgetFn<CmdTooltipArgs> = WidgetFn::new(default_cmd_tooltip_fn);

    static CMD_VAR: Option<Command> = None;
}

/// Arguments for [`cmd_tooltip_fn`].
///
/// [`cmd_tooltip_fn`]: fn@cmd_tooltip_fn
#[derive(Clone)]
pub struct CmdTooltipArgs {
    /// The tooltip arguments.
    pub tooltip: TooltipArgs,
    /// The command.
    pub cmd: Command,
}
impl ops::Deref for CmdTooltipArgs {
    type Target = TooltipArgs;

    fn deref(&self) -> &Self::Target {
        &self.tooltip
    }
}

/// Default [`CMD_CHILD_FN_VAR`].
pub fn default_cmd_child_fn(cmd: Command) -> impl UiNode {
    Text!(cmd.name())
}

/// Default [`CMD_TOOLTIP_FN_VAR`].
pub fn default_cmd_tooltip_fn(args: CmdTooltipArgs) -> impl UiNode {
    let info = args.cmd.info();
    let has_info = info.map(|s| !s.is_empty());
    let shortcut = args.cmd.shortcut().map(|s| match s.first() {
        Some(s) => s.to_txt(),
        None => Txt::from(""),
    });
    let has_shortcut = shortcut.map(|s| !s.is_empty());
    Tip! {
        child = Text! {
            zng_wgt::visibility = has_info.map_into();
            txt = info;
        };
        child_bottom = {
            node: Text! {
                font_weight = zng_ext_font::FontWeight::BOLD;
                zng_wgt::visibility = has_shortcut.map_into();
                txt = shortcut;
            },
            spacing: 4,
        };

        zng_wgt::visibility = expr_var!((*#{has_info} || *#{has_shortcut}).into())
    }
}

/// Sets the [`Command`] the button represents.
///
/// When this is set the button widget sets these properties if they are not set:
///
/// * [`child`]: Set to a widget produced by [`cmd_child_fn`](fn@cmd_child_fn), by default is `Text!(cmd.name())`.
/// * [`tooltip_fn`]: Set to a widget function provided by [`cmd_tooltip_fn`](fn@cmd_tooltip_fn), by default it
///    shows the command info and first shortcut.
/// * [`enabled`]: Set to `cmd.is_enabled()`.
/// * [`visibility`]: Set to `cmd.has_handlers().into()`.
/// * [`on_click`]: Set to a handler that notifies the command if `cmd.is_enabled()`.
/// * [`on_disabled_click`]: Set to a handler that notifies the command if `!cmd.is_enabled()`.
///
/// [`child`]: struct@Container#method.child
/// [`tooltip_fn`]: fn@tooltip_fn
/// [`Command`]: zng_app::event::Command
/// [`enabled`]: fn@zng_wgt::enabled
/// [`visibility`]: fn@zng_wgt::visibility
/// [`on_click`]: fn@on_click
/// [`on_disabled_click`]: fn@on_disabled_click
#[property(CHILD, capture, widget_impl(Button))]
pub fn cmd(cmd: impl IntoVar<Command>) {}

/// Optional command parameter for the button to use when notifying [`cmd`].
///
/// If `T` is `Option<CommandParam>` the param can be dynamically unset, otherwise the value is the param.
///
/// [`cmd`]: fn@cmd
#[property(CONTEXT, default(CMD_PARAM_VAR), widget_impl(Button))]
pub fn cmd_param<T: VarValue>(child: impl UiNode, cmd_param: impl IntoVar<T>) -> impl UiNode {
    if TypeId::of::<T>() == TypeId::of::<Option<CommandParam>>() {
        let cmd_param = *cmd_param
            .into_var()
            .boxed_any()
            .double_boxed_any()
            .downcast::<BoxedVar<Option<CommandParam>>>()
            .unwrap();
        with_context_var(child, CMD_PARAM_VAR, cmd_param).boxed()
    } else {
        with_context_var(
            child,
            CMD_PARAM_VAR,
            cmd_param.into_var().map(|p| Some(CommandParam::new(p.clone()))),
        )
        .boxed()
    }
}

/// Sets the widget function used to produce the button child when [`cmd`] is set and [`child`] is not.
///
/// [`cmd`]: fn@cmd
/// [`child`]: fn@zng_wgt_container::child
#[property(CONTEXT, default(CMD_CHILD_FN_VAR), widget_impl(Button))]
pub fn cmd_child_fn(child: impl UiNode, cmd_child: impl IntoVar<WidgetFn<Command>>) -> impl UiNode {
    with_context_var(child, CMD_CHILD_FN_VAR, cmd_child)
}

/// Sets the widget function used to produce the button tooltip when [`cmd`] is set and tooltip is not.
///
/// [`cmd`]: fn@cmd
#[property(CONTEXT, default(CMD_TOOLTIP_FN_VAR), widget_impl(Button))]
pub fn cmd_tooltip_fn(child: impl UiNode, cmd_tooltip: impl IntoVar<WidgetFn<CmdTooltipArgs>>) -> impl UiNode {
    with_context_var(child, CMD_TOOLTIP_FN_VAR, cmd_tooltip)
}

/// Button default style.
#[widget($crate::DefaultStyle)]
pub struct DefaultStyle(Style);
impl DefaultStyle {
    fn widget_intrinsic(&mut self) {
        widget_set! {
            self;

            replace = true;

            access_role = AccessRole::Button;

            padding = (7, 15);
            corner_radius = 4;
            child_align = Align::CENTER;

            base_color = light_dark(rgb(0.82, 0.82, 0.82), rgb(0.18, 0.18, 0.18));

            #[easing(150.ms())]
            background_color = BASE_COLOR_VAR.rgba();
            #[easing(150.ms())]
            border = {
                widths: 1,
                sides: BASE_COLOR_VAR.rgba_into()
            };

            when *#is_cap_hovered {
                #[easing(0.ms())]
                background_color = BASE_COLOR_VAR.shade(1);
                #[easing(0.ms())]
                border = {
                    widths: 1,
                    sides: BASE_COLOR_VAR.shade_into(2),
                };
            }

            when *#is_pressed {
                #[easing(0.ms())]
                background_color = BASE_COLOR_VAR.shade(2);
            }

            when *#is_disabled {
                saturate = false;
                child_opacity = 50.pct();
                cursor = CursorIcon::NotAllowed;
            }
        }
    }
}

/// Primary button style.
#[widget($crate::PrimaryStyle)]
pub struct PrimaryStyle(DefaultStyle);
impl PrimaryStyle {
    fn widget_intrinsic(&mut self) {
        widget_set! {
            self;

            base_color = ACCENT_COLOR_VAR.map(|c| c.shade(-2));
            zng_wgt_text::font_weight = zng_ext_font::FontWeight::BOLD;
        }
    }
}

/// Button light style.
#[widget($crate::LightStyle)]
pub struct LightStyle(DefaultStyle);
impl LightStyle {
    fn widget_intrinsic(&mut self) {
        widget_set! {
            self;
            border = unset!;
            padding = 7;

            #[easing(150.ms())]
            background_color = FONT_COLOR_VAR.map(|c| c.with_alpha(0.pct()));

            when *#is_cap_hovered {
                #[easing(0.ms())]
                background_color = FONT_COLOR_VAR.map(|c| c.with_alpha(10.pct()));
            }

            when *#is_pressed {
                #[easing(0.ms())]
                background_color = FONT_COLOR_VAR.map(|c| c.with_alpha(20.pct()));
            }

            when *#is_disabled {
                saturate = false;
                child_opacity = 50.pct();
                cursor = CursorIcon::NotAllowed;
            }
        }
    }
}

/// Button link style.
///
/// Looks like a web hyperlink.
#[widget($crate::LinkStyle)]
pub struct LinkStyle(Style);
impl LinkStyle {
    fn widget_intrinsic(&mut self) {
        widget_set! {
            self;
            replace = true;

            font_color = light_dark(colors::BLUE, web_colors::LIGHT_BLUE);
            cursor = CursorIcon::Pointer;
            access_role = AccessRole::Link;

            when *#is_cap_hovered {
                underline = 1, LineStyle::Solid;
            }

            when *#is_pressed {
                font_color = light_dark(web_colors::BROWN, colors::YELLOW);
            }

            when *#is_disabled {
                saturate = false;
                child_opacity = 50.pct();
                cursor = CursorIcon::NotAllowed;
            }
        }
    }
}

/// Button context.
pub struct BUTTON;
impl BUTTON {
    /// The [`cmd`] value, if set.
    ///
    /// [`cmd`]: fn@cmd
    pub fn cmd(&self) -> ReadOnlyContextVar<Option<Command>> {
        CMD_VAR.read_only()
    }

    /// The [`cmd_param`] value.
    ///
    /// [`cmd_param`]: fn@cmd_param
    pub fn cmd_param(&self) -> ReadOnlyContextVar<Option<CommandParam>> {
        CMD_PARAM_VAR.read_only()
    }
}