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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Implements a button UI element, as well as variants thereof.

use std::{
    fmt::Debug,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
};

use ambient_cb::{cb, Callback, Cb};
use ambient_color::Color;
use ambient_element::{
    element_component, to_owned, use_effect, use_runtime_message, use_state, use_state_with,
    Element, ElementComponent, ElementComponentExt, Hooks,
};
use ambient_guest_bridge::{
    core::{
        layout::{
            components::{
                align_vertical, fit_horizontal, height, margin, min_height, padding,
                space_between_items,
            },
            types::{Align, Fit},
        },
        messages,
        rect::components::{border_color, border_radius, border_thickness},
        rendering::components::color,
        text::{components::font_style, types::FontStyle},
    },
    ecs::World,
    run_async,
};
use ambient_shared_types::{CursorIcon, ModifiersState, VirtualKeyCode};
use futures::{future::BoxFuture, Future, FutureExt};
use glam::*;
use parking_lot::Mutex;

use crate::{
    default_theme::{cutout_color, primary_color, secondary_color},
    dropdown::Tooltip,
    layout::{FlowColumn, FlowRow},
    text::Text,
    use_keyboard_input, UIBase, UIElement, UIExt,
};

#[derive(Clone, Debug)]
/// The callback invoked when a button is clicked.
pub enum ButtonCb {
    /// A synchronous callback.
    Sync(ButtonCallback),
    /// An asynchronous callback.
    Async(Callback<(), BoxFuture<'static, ()>>),
}

/// The type of function invoked by a button.
pub type ButtonCallback<Ret = ()> = Cb<dyn Fn(&mut World) -> Ret + Sync + Send>;

impl ButtonCb {
    /// Invokes this callback.
    pub fn invoke(&self, world: &mut World, set_is_working: Cb<dyn Fn(bool) + Sync + Send>) {
        match self {
            ButtonCb::Sync(cb) => cb.0(world),
            ButtonCb::Async(cb) => {
                set_is_working(true);
                let cb = cb.clone();
                run_async(world, async move {
                    cb.0(()).await;
                    set_is_working(false);
                });
            }
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// The style of a button.
pub enum ButtonStyle {
    /// A regular button: uses variants of the cutout color for all states except hover, where it uses the secondary color.
    Regular,
    /// A primary button: uses variants of the secondary color.
    Primary,
    /// A flat button: has no background, has limited padding.
    Flat,
    /// A card button: meant to be used with "complex" child elements.
    Card,
    /// An inline button: has no additional styling aside from an underline, similar to a link on a webpage.
    Inline,
}
impl ButtonStyle {
    #[allow(clippy::too_many_arguments)]
    fn create_container(
        &self,
        is_pressed: bool,
        is_working: bool,
        disabled: bool,
        toggled: bool,
        hover: bool,
        hotkey: Option<VirtualKeyCode>,
        hotkey_modifier: ModifiersState,
        tooltip: Option<Element>,
        content: Element,
    ) -> Element {
        let background = match self {
            ButtonStyle::Regular | ButtonStyle::Card => {
                if is_pressed {
                    cutout_color().lighten(0.1)
                } else if disabled || is_working {
                    cutout_color()
                } else if toggled {
                    primary_color()
                } else if hover && *self == ButtonStyle::Card {
                    cutout_color().lighten(0.05)
                } else {
                    cutout_color()
                }
            }
            ButtonStyle::Primary => {
                if is_pressed {
                    secondary_color().lighten(0.2)
                } else if disabled || is_working {
                    secondary_color().desaturate(-1.)
                } else if hover || disabled || is_working {
                    secondary_color().lighten(0.1)
                } else {
                    secondary_color()
                }
            }
            ButtonStyle::Flat | ButtonStyle::Inline => Color::rgba(1., 1., 1., 0.0),
        };
        let content = match self {
            Self::Regular | Self::Flat | ButtonStyle::Inline => content.with(
                color(),
                if is_pressed {
                    Color::rgba(1., 1., 1., 1.)
                } else if disabled || is_working {
                    Color::rgba(0.3, 0.3, 0.3, 1.)
                } else if toggled {
                    if *self == Self::Flat || *self == Self::Inline {
                        primary_color()
                    } else {
                        Color::rgba(1., 1., 1., 1.)
                    }
                } else if hover {
                    Color::rgba(0.8, 0.8, 0.8, 1.)
                } else {
                    Color::hex("B3B3B3").unwrap()
                }
                .into(),
            ),
            Self::Primary => content.with(
                color(),
                if is_pressed {
                    Color::BLACK
                } else if disabled || is_working {
                    Color::BLACK.lighten(0.3)
                } else {
                    Color::BLACK
                }
                .into(),
            ),
            _ => content,
        };
        if *self == ButtonStyle::Inline {
            FlowColumn::el([
                content,
                UIBase
                    .el()
                    .with(fit_horizontal(), Fit::Parent)
                    .with(height(), 2.)
                    .with_background(Color::WHITE.into())
                    .with(margin(), vec4(2., 0., 0., 0.)),
            ])
            .with_background(background.into())
        } else {
            let content = content.with(font_style(), FontStyle::Bold);
            let tooltip = if let Some(hotkey) = hotkey {
                let modifier = if hotkey_modifier != ModifiersState::empty() {
                    format!("{hotkey_modifier:?} + ")
                } else {
                    String::new()
                };
                let hotkey = Text::el(format!("[{modifier}{hotkey:?}]"));
                if let Some(tooltip) = tooltip {
                    Some(FlowColumn::el([tooltip, hotkey]).with(space_between_items(), 10.))
                } else {
                    Some(hotkey)
                }
            } else {
                tooltip
            };
            let mut el = FlowRow(vec![content])
                .el()
                .with(
                    padding(),
                    vec4(
                        3.,
                        if matches!(self, Self::Card) || matches!(self, Self::Flat) {
                            3.
                        } else {
                            16.
                        },
                        3.,
                        if matches!(self, Self::Card) || matches!(self, Self::Flat) {
                            3.
                        } else {
                            16.
                        },
                    ),
                )
                .with(align_vertical(), Align::Center)
                .with_background(background.into())
                .with(
                    border_radius(),
                    match self {
                        Self::Card => Vec4::ONE * 3.,
                        Self::Flat => Vec4::ONE * 3.,
                        _ => Vec4::ONE * 26. / 2.,
                    },
                )
                .with(border_thickness(), 0.)
                .with(border_color(), Color::WHITE.into());
            if *self != Self::Flat {
                el = el.with(min_height(), 26.);
            }
            if let Some(tooltip) = tooltip {
                Tooltip { inner: el, tooltip }.el()
            } else {
                el
            }
        }
    }
}

#[element_component]
/// A button UI element.
pub fn Button(
    hooks: &mut Hooks,
    /// The content of the button; typically text, as provided by one of the constructor functions.
    content: Element,
    /// Whether or not the button is disabled.
    disabled: bool,
    /// Whether or not the button can be toggled.
    toggled: bool,
    /// The style of the button.
    style: ButtonStyle,
    /// The hotkey for the button.
    hotkey: Option<VirtualKeyCode>,
    /// The hotkey input modifiers for the button.
    hotkey_modifier: ModifiersState,
    /// The tooltip for the button.
    tooltip: Option<Element>,
    /// The callback to invoke when the button is pressed.
    on_invoked: ButtonCb,
    /// The callback to invoke when the current pressed state changes.
    on_is_pressed_changed: Option<Cb<dyn Fn(&mut World, bool) + Sync + Send>>,
) -> Element {
    let (is_pressed, set_is_pressed) = use_state(hooks, false);
    let (hover, set_hover) = use_state(hooks, false);
    let (is_working, set_is_working) = use_state(hooks, false);
    let (is_pressed_immediate, _) = use_state_with(hooks, |_| Arc::new(AtomicBool::new(false)));

    use_effect(hooks, is_pressed, move |world, _| {
        if let Some(on_is_pressed_changed) = on_is_pressed_changed {
            on_is_pressed_changed(world, is_pressed);
        }
        |_| {}
    });
    use_runtime_message::<messages::WindowMouseInput>(hooks, {
        to_owned![set_is_pressed, on_invoked, set_is_working];
        move |world, event| {
            let pressed = event.pressed;
            if pressed && hover {
                set_is_pressed(true);
                is_pressed_immediate.store(true, Ordering::SeqCst);
            }
            if !pressed {
                let is_pressed = is_pressed_immediate.load(Ordering::SeqCst);
                if hover && !disabled && is_pressed {
                    on_invoked.invoke(world, set_is_working.clone());
                }
                if is_pressed {
                    set_is_pressed(false);
                    is_pressed_immediate.store(false, Ordering::SeqCst);
                }
            }
        }
    });

    let content = style
        .create_container(
            is_pressed,
            is_working,
            disabled,
            toggled,
            hover,
            hotkey,
            hotkey_modifier,
            tooltip,
            content,
        )
        .with_clickarea()
        .on_mouse_enter({
            to_owned![set_hover];
            move |world, _| {
                set_hover(true);
                ambient_guest_bridge::window::set_cursor(world, CursorIcon::Hand);
            }
        })
        .on_mouse_leave(move |world, _| {
            set_hover(false);
            ambient_guest_bridge::window::set_cursor(world, CursorIcon::Default);
        })
        .el();

    if disabled {
        content
    } else if let Some(hotkey) = hotkey {
        Hotkey {
            hotkey,
            hotkey_modifier,
            content,
            on_is_pressed_changed: Some(set_is_pressed),
            on_invoke: cb(move |world| {
                on_invoked.invoke(world, set_is_working.clone());
            }),
        }
        .el()
    } else {
        content
    }
}
impl Button {
    /// Create a new [Button] with the given content and callback.
    pub fn new<T: Into<UIElement>>(
        content: T,
        on_invoked: impl Fn(&mut World) + Sync + Send + 'static,
    ) -> Self {
        let content: UIElement = content.into();
        Self::new_inner(content, cb(on_invoked))
    }
    /// Create a new [Button] with the given content and callback as a [Cb].
    pub fn new_inner<T: Into<UIElement>>(
        content: T,
        on_invoked: Cb<dyn Fn(&mut World) + Sync + Send + 'static>,
    ) -> Self {
        let content: UIElement = content.into();
        Self {
            content: content.0,
            disabled: false,
            toggled: false,
            style: ButtonStyle::Regular,
            hotkey: None,
            hotkey_modifier: ModifiersState::empty(),
            tooltip: None,
            on_invoked: ButtonCb::Sync(on_invoked),
            on_is_pressed_changed: None,
        }
    }
    /// Create a new one-shot [Button] with the given content and a callback that is only invoked once.
    pub fn new_once<T: Into<UIElement>>(
        content: T,
        on_invoked: impl FnOnce(&mut World) + Sync + Send + 'static,
    ) -> Self {
        let on_invoked = Arc::new(Mutex::new(Some(on_invoked)));
        Self::new(content, move |world| {
            let on_invoked = on_invoked.clone();
            let on_invoked = {
                let mut on_invoked = on_invoked.lock();
                (*on_invoked).take()
            };
            on_invoked.expect("'Once' button called more than once")(world);
        })
    }
    /// Create a new [Button] with the given content and a callback that returns a [Future] (i.e. is `async`).
    pub fn new_async<F: Future<Output = ()> + Send + 'static, T: Into<UIElement>>(
        content: T,
        on_invoked: impl Fn() -> F + Sync + Send + 'static,
    ) -> Self {
        let content: UIElement = content.into();
        Self {
            content: content.0,
            disabled: false,
            toggled: false,
            style: ButtonStyle::Regular,
            hotkey: None,
            hotkey_modifier: ModifiersState::empty(),
            tooltip: None,
            on_invoked: ButtonCb::Async(cb(move |_w| on_invoked().boxed())),
            on_is_pressed_changed: None,
        }
    }
    /// Create a new one-shot [Button] with the given content and a callback that returns a [Future] (i.e. is `async`) and is only invoked once.
    pub fn new_async_once<F: Future + Send + 'static, T: Into<UIElement>>(
        content: T,
        on_invoked: impl FnOnce() -> F + Sync + Send + 'static,
    ) -> Self {
        let on_invoked = Arc::new(Mutex::new(Some(on_invoked)));
        Self::new_async(content, move || {
            let on_invoked = on_invoked.clone();
            async move {
                let on_invoked = {
                    let mut on_invoked = on_invoked.lock();
                    (*on_invoked).take()
                };
                on_invoked.expect("'Async once' button called more than once")().await;
            }
            .boxed()
        })
    }
    /// Create a new [Button] with the given content that sets the given value to the given desired value when clicked.
    pub fn new_value<T: Into<UIElement>, V: PartialEq + Copy + Send + Sync + 'static>(
        content: T,
        value: V,
        set_value: Cb<dyn Fn(V) + Sync + Send>,
        desired_value: V,
    ) -> Button {
        Button::new(content, move |_| set_value(desired_value)).toggled(value == desired_value)
    }
    /// Set whether or not the button is disabled.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
    /// Set the style of the button.
    pub fn style(mut self, style: ButtonStyle) -> Self {
        self.style = style;
        self
    }
    /// Set the hotkey of the button.
    pub fn hotkey(mut self, hotkey: VirtualKeyCode) -> Self {
        self.hotkey = Some(hotkey);
        self
    }
    /// Set the modifiers for the hotkey of the button.
    pub fn hotkey_modifier(mut self, hotkey_modifier: ModifiersState) -> Self {
        self.hotkey_modifier = hotkey_modifier;
        self
    }
    /// Set the tooltip of the button.
    pub fn tooltip(mut self, tooltip: impl Into<UIElement>) -> Self {
        let tooltip: UIElement = tooltip.into();
        self.tooltip = Some(tooltip.0);
        self
    }
    /// Set whether or not the button can be toggled.
    pub fn toggled(mut self, toggled: bool) -> Self {
        self.toggled = toggled;
        self
    }
    /// Set the callback that is invoked when the current pressed state of the button changes.
    pub fn on_is_pressed_changed(
        mut self,
        handle: impl Fn(&mut World, bool) + Sync + Send + 'static,
    ) -> Self {
        self.on_is_pressed_changed = Some(cb(handle));
        self
    }
}

#[derive(Clone, Debug)]
/// An element that will invoke a callback when a hotkey is pressed.
pub struct Hotkey {
    /// The hotkey that will invoke the callback.
    pub hotkey: VirtualKeyCode,
    /// The keyboard modifiers for the hotkey.
    pub hotkey_modifier: ModifiersState,
    /// The callback that is invoked when the current state of the hotkey changes.
    pub on_is_pressed_changed: Option<Cb<dyn Fn(bool) + Sync + Send>>,
    /// The callback that is invoked when the hotkey is pressed.
    pub on_invoke: Cb<dyn Fn(&mut World) + Sync + Send>,
    /// What to render for this element.
    pub content: Element,
}
impl Hotkey {
    /// Create a new [Hotkey] with the given content and callback.
    pub fn new(
        hotkey: VirtualKeyCode,
        on_invoke: impl Fn(&mut World) + Sync + Send + 'static,
        content: Element,
    ) -> Self {
        Self {
            hotkey,
            hotkey_modifier: ModifiersState::empty(),
            on_invoke: cb(on_invoke),
            content,
            on_is_pressed_changed: None,
        }
    }
    /// Set the keyboard modifiers for the hotkey.
    pub fn hotkey_modifier(mut self, hotkey_modifier: ModifiersState) -> Self {
        self.hotkey_modifier = hotkey_modifier;
        self
    }
}
impl ElementComponent for Hotkey {
    fn render(self: Box<Self>, hooks: &mut Hooks) -> Element {
        let Self {
            on_is_pressed_changed,
            content,
            hotkey,
            hotkey_modifier,
            on_invoke,
        } = *self;
        let (is_pressed, _) = use_state_with(hooks, |_| Arc::new(AtomicBool::new(false)));
        use_keyboard_input(hooks, {
            let is_pressed = is_pressed.clone();
            move |world, keycode, modifiers, pressed| {
                if let Some(virtual_keycode) = keycode {
                    if virtual_keycode != hotkey {
                        return;
                    }
                    if pressed {
                        if modifiers == hotkey_modifier {
                            if let Some(on_is_pressed_changed) = on_is_pressed_changed.clone() {
                                on_is_pressed_changed.0(true);
                            }
                            is_pressed.store(true, Ordering::Relaxed);
                        }
                    } else {
                        let pressed = is_pressed.load(Ordering::Relaxed);

                        if pressed {
                            on_invoke.0(world);
                            if let Some(on_is_pressed_changed) = on_is_pressed_changed.clone() {
                                on_is_pressed_changed.0(false);
                            }
                            is_pressed.store(false, Ordering::Relaxed);
                        }
                    }
                }
            }
        });
        use_runtime_message::<messages::WindowFocusChange>(hooks, {
            move |_world, _event| {
                is_pressed.store(false, Ordering::Relaxed);
            }
        });
        content
    }
}