fui_controls 0.18.0

Standard controls for FUI UI Framework
Documentation
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
use fui_core::{ControlObject, Property, Style, ViewContext};
use fui_drawing::Color;
use fui_macros::ui;
use std::cell::RefCell;
use std::rc::{Rc, Weak};
use typed_builder::TypedBuilder;
use typemap::TypeMap;

use crate::controls::*;
use crate::{DataHolder, GestureArea};
use fui_core::*;

#[derive(TypedBuilder)]
pub struct Menu {
    #[builder(default = Orientation::Horizontal)]
    pub orientation: Orientation,

    pub items: Vec<MenuItem>,
}

impl Menu {
    pub fn to_view(
        self,
        _style: Option<Box<dyn Style<Self>>>,
        context: ViewContext,
    ) -> Rc<RefCell<dyn ControlObject>> {
        // menu is active when tapped
        let is_menu_active_prop = Property::new(false);

        let content_prop = ObservableVec::new();
        let mut close_item_popup_callbacks = Vec::new();
        let mut close_siblings_callbacks = Vec::new();

        let menu: Rc<RefCell<dyn ControlObject>> = ui!(
            Shadow {
                Style: Default { size: 12.0f32 },

                Border {
                    border_type: BorderType::None,
                    Style: Default { background_color: Color::rgba(1.0, 1.0, 1.0, 0.8), },

                    StackPanel {
                        orientation: self.orientation,

                        &content_prop,
                    }
                }
            }
        );

        menu.borrow_mut()
            .get_context_mut()
            .set_attached_values(context.attached_values);

        let uncovered_controls: Vec<_> = vec![Rc::downgrade(&menu)];

        for item in self.items.into_iter() {
            let close_siblings_callback_rc = Rc::new(RefCell::new(Callback::empty()));
            let (view, close_item_popup_callback) = Self::menu_item_to_view(
                item,
                true,
                &is_menu_active_prop,
                &uncovered_controls,
                &close_siblings_callback_rc,
            );
            content_prop.push(view);
            close_item_popup_callbacks.push(close_item_popup_callback);
            close_siblings_callbacks.push(close_siblings_callback_rc);
        }

        // setup sibling closing logic
        for i in 0..close_siblings_callbacks.len() {
            let mut close_item_popup_callbacks_for_i = Vec::new();
            for j in 0..close_item_popup_callbacks.len() {
                if j != i {
                    close_item_popup_callbacks_for_i.push(close_item_popup_callbacks[j].clone());
                }
            }

            close_siblings_callbacks[i].borrow_mut().set_sync(move |_| {
                for i in 0..close_item_popup_callbacks_for_i.len() {
                    close_item_popup_callbacks_for_i[i].emit(());
                }
            });
        }

        menu
    }

    fn menu_item_to_view(
        menu_item: MenuItem,
        is_top: bool,
        is_menu_active_prop: &Property<bool>,
        uncovered_controls: &Vec<Weak<RefCell<dyn ControlObject>>>,
        close_siblings_callback_rc: &Rc<RefCell<Callback<()>>>,
    ) -> (Rc<RefCell<dyn ControlObject>>, Callback<()>) {
        match menu_item {
            MenuItem::Separator => {
                let separator: Rc<RefCell<dyn ControlObject>> = ui! {
                    Text {
                        Style: Default { color: [0.0f32, 0.0f32, 0.0f32, 1.0f32] },
                        text: "---------"
                    }
                };
                (separator, Callback::empty())
            }

            MenuItem::Text {
                text,
                shortcut: _shortcut,
                icon: _icon,
                callback,
                sub_items,
            } => {
                let has_sub_items = sub_items.len() > 0;

                let is_open_prop = Property::new(false);
                let background_property = Property::new(Color::rgba(0.0, 0.0, 0.0, 0.0));
                let foreground_property = Property::new(Color::rgba(0.0, 0.0, 0.0, 1.0));

                let mut on_hover_callback = Callback::empty();
                let mut on_tap_up_callback = Callback::empty();

                if is_top {
                    // top bar menu case

                    // open sub menu on tap down
                    let is_menu_active_prop_clone = is_menu_active_prop.clone();
                    let is_open_prop_clone = is_open_prop.clone();
                    on_tap_up_callback.set_sync(move |_| {
                        if has_sub_items {
                            is_menu_active_prop_clone.set(true);
                            is_open_prop_clone.set(true);
                        } else {
                            // execute menu command
                            callback.emit(());
                        }
                    });

                    // hover highlights items even when menu is not active
                    let background_property_clone = background_property.clone();
                    let foreground_property_clone = foreground_property.clone();
                    let is_menu_active_prop_clone = is_menu_active_prop.clone();
                    let is_open_prop_clone = is_open_prop.clone();
                    let close_siblings_callback_clone = close_siblings_callback_rc.clone();
                    on_hover_callback.set_sync(move |value| {
                        background_property_clone.set(
                            if value || is_menu_active_prop_clone.get() {
                                Color::rgba(0.0, 0.0, 0.0, 0.8)
                            } else {
                                Color::rgba(0.0, 0.0, 0.0, 0.0)
                            },
                        );
                        foreground_property_clone.set(
                            if value || is_menu_active_prop_clone.get() {
                                Color::rgba(1.0, 1.0, 0.0, 1.0)
                            } else {
                                Color::rgba(0.0, 0.0, 0.0, 1.0)
                            },
                        );

                        if value && is_menu_active_prop_clone.get() {
                            // close all the other popups on the same level (siblings)
                            close_siblings_callback_clone.borrow().emit(());

                            // open popup if there are sub items
                            if has_sub_items {
                                is_open_prop_clone.set(true);
                            }
                        }
                    });
                } else {
                    let background_property_clone = background_property.clone();
                    let foreground_property_clone = foreground_property.clone();
                    let is_open_prop_clone = is_open_prop.clone();
                    let close_siblings_callback_clone = close_siblings_callback_rc.clone();
                    on_hover_callback.set_sync(move |value| {
                        background_property_clone.set(if value || has_sub_items {
                            Color::rgba(0.0, 0.0, 0.0, 0.8)
                        } else {
                            Color::rgba(0.0, 0.0, 0.0, 0.0)
                        });
                        foreground_property_clone.set(if value || has_sub_items {
                            Color::rgba(1.0, 1.0, 0.0, 1.0)
                        } else {
                            Color::rgba(0.0, 0.0, 0.0, 1.0)
                        });

                        if value {
                            // close all the other popups on the same level (siblings)
                            close_siblings_callback_clone.borrow().emit(());

                            // open popup if there are sub items
                            if has_sub_items {
                                is_open_prop_clone.set(true);
                            }
                        }
                    });

                    let is_menu_active_prop_clone = is_menu_active_prop.clone();
                    on_tap_up_callback.set_sync(move |_| {
                        if !has_sub_items {
                            // close menu
                            is_menu_active_prop_clone.set(false);
                            // execute menu command
                            callback.emit(());
                        }
                    });
                }

                let title_content: Rc<RefCell<dyn ControlObject>> = if is_top {
                    ui!(Text {
                        Row: 0,
                        Column: 1,
                        Margin: Thickness::new(5.0f32, 0.0f32, 5.0f32, 0.0f32),
                        Style: Dynamic {
                            color: foreground_property.clone()
                        },
                        text: text
                    })
                } else {
                    ui!(
                        Grid {
                            columns: 4,
                            widths: vec![
                                (0, Length::Exact(25.0f32)),
                                (1, Length::Fill(1.0f32)),
                                (2, Length::Auto),
                                (3, Length::Exact(25.0f32)),
                            ],

                            Text {
                                Row: 0, Column: 1,
                                HorizontalAlignment: Alignment::Start,
                                Style: Dynamic { color: foreground_property.clone() },

                                text: text
                            },

                            Text {
                                Row: 0, Column: 3,
                                Style: Dynamic { color: foreground_property.clone() },
                                text: if sub_items.len() > 0 { ">" } else { "" },
                            }
                        }
                    )
                };

                // return callback that closes the popup
                let mut close_popup_callback = Callback::empty();

                let popup = if sub_items.len() == 0 {
                    Children::None
                } else {
                    let sub_content_prop = ObservableVec::new();

                    let popup_placement = if is_top {
                        PopupPlacement::BelowOrAboveParent
                    } else {
                        PopupPlacement::LeftOrRightParent
                    };

                    let background_property_clone = background_property.clone();
                    let foreground_property_clone = foreground_property.clone();
                    let popup_close_subscription = is_open_prop.on_changed(move |value| {
                        if value == false {
                            background_property_clone.set(Color::rgba(0.0, 0.0, 0.0, 0.0));
                            foreground_property_clone.set(Color::rgba(0.0, 0.0, 0.0, 1.0));
                        }

                        if is_top {
                            //is_menu_active_prop_clone.set(false);
                        }
                    });

                    let popup_content: Rc<RefCell<dyn ControlObject>> = ui!(
                        Shadow {
                            Style: Default { size: 12.0f32 },

                            Border {
                                border_type: BorderType::Raisen,
                                Style: Default { background_color: Color::rgba(1.0, 1.0, 1.0, 0.8), },

                                Grid {
                                    columns: 1,
                                    default_width: Length::Fill(1.0f32),
                                    default_height: Length::Auto,

                                    &sub_content_prop,
                                }
                            }
                        }
                    );

                    let mut close_item_popup_callbacks = Vec::new();
                    let mut close_siblings_callbacks = Vec::new();

                    let mut uncovered_controls = uncovered_controls.to_vec();
                    uncovered_controls.push(Rc::downgrade(&popup_content));
                    for item in sub_items.into_iter() {
                        let close_siblings_callback_rc = Rc::new(RefCell::new(Callback::empty()));
                        let (view, close_item_popup_callback) = Self::menu_item_to_view(
                            item,
                            false,
                            &is_menu_active_prop,
                            &uncovered_controls,
                            &close_siblings_callback_rc,
                        );
                        sub_content_prop.push(view);
                        close_item_popup_callbacks.push(close_item_popup_callback);
                        close_siblings_callbacks.push(close_siblings_callback_rc);
                    }

                    // setup sibling closing logic
                    for i in 0..close_siblings_callbacks.len() {
                        let mut close_item_popup_callbacks_for_i = Vec::new();
                        for j in 0..close_item_popup_callbacks.len() {
                            if j != i {
                                close_item_popup_callbacks_for_i
                                    .push(close_item_popup_callbacks[j].clone());
                            }
                        }

                        close_siblings_callbacks[i].borrow_mut().set_sync(move |_| {
                            for i in 0..close_item_popup_callbacks_for_i.len() {
                                close_item_popup_callbacks_for_i[i].emit(());
                            }
                        });
                    }

                    // when clicked outside last open submenu
                    // make whole menu inactive (and close all submenu windows)
                    let mut auto_hide_occured_callback = Callback::empty();
                    let is_menu_active_prop_clone = is_menu_active_prop.clone();
                    auto_hide_occured_callback.set_sync(move |_| {
                        is_menu_active_prop_clone.set(false);
                    });

                    let is_open_prop_clone = is_open_prop.clone();
                    let is_menu_active_prop_changed =
                        is_menu_active_prop.on_changed(move |value| {
                            if !value {
                                is_open_prop_clone.set(false);
                            }
                        });

                    // return callback that closes the popup
                    let is_open_prop_clone = is_open_prop.clone();
                    close_popup_callback.set_sync(move |_| {
                        // close this popup
                        is_open_prop_clone.set(false);
                        // close all sub-popups
                        for subsibling in &close_siblings_callbacks {
                            subsibling.borrow().emit(());
                        }
                    });

                    let data_holder = DataHolder {
                        data: (popup_close_subscription, is_menu_active_prop_changed),
                    }
                    .to_view(
                        None,
                        ViewContext {
                            attached_values: TypeMap::new(),
                            children: Children::None,
                        },
                    );

                    let popup = ui!(Popup {
                        is_open: is_open_prop,
                        placement: popup_placement,
                        auto_hide: PopupAutoHide::ClickedOutside,
                        auto_hide_occured: auto_hide_occured_callback,
                        uncovered_controls: uncovered_controls,

                        popup_content,

                        data_holder,
                    });

                    Children::SingleStatic(popup)
                };

                let content = ui!(
                    GestureArea {
                        hover_change: on_hover_callback,
                        tap_up: on_tap_up_callback,

                        Border {
                            border_type: BorderType::None,
                            Style: Default { background_color: background_property },

                            title_content,
                        },

                        popup,
                    }
                );

                (content, close_popup_callback)
            }

            MenuItem::Custom {
                content,
                callback: _,
                sub_items: _,
            } => (content, Callback::empty()),
        }
    }
}