-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathapp.rs
More file actions
1341 lines (1163 loc) · 53.6 KB
/
app.rs
File metadata and controls
1341 lines (1163 loc) · 53.6 KB
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! MoFA Studio App - Main application shell
//!
//! This file contains the main App struct and all UI definitions.
//! Organized into sections:
//! - UI Definitions (live_design! macro)
//! - Widget Structs (Dashboard, App)
//! - Event Handling (AppMain impl)
//! - Helper Methods (organized by responsibility)
use makepad_widgets::*;
use mofa_studio_shell::widgets::sidebar::SidebarWidgetRefExt;
use mofa_ui::{MofaTheme, MofaAppData};
use mofa_dora_bridge::SharedDoraState;
use std::sync::OnceLock;
use crate::cli::Args;
// ============================================================================
// CLI ARGS STORAGE
// ============================================================================
/// Global storage for CLI arguments (set once at startup)
static CLI_ARGS: OnceLock<Args> = OnceLock::new();
/// Set CLI arguments (called from main.rs before app starts)
pub fn set_cli_args(args: Args) {
CLI_ARGS.set(args).ok();
}
/// Get CLI arguments (returns default if not set)
pub fn get_cli_args() -> &'static Args {
CLI_ARGS.get_or_init(Args::default)
}
// App plugin system imports
use mofa_widgets::{MofaApp, AppRegistry, TimerControl, PageRouter, PageId, tab_clicked};
use mofa_fm::{MoFaFMApp, MoFaFMScreenWidgetRefExt};
use mofa_debate::{MoFaDebateApp, MoFaDebateScreenWidgetRefExt};
feature/ai-ui-generator
use mofa_asr::{MoFaASRApp, MoFaASRScreenWidgetRefExt};
use mofa_ui_generator::{MoFaUIGeneratorApp, screen::MoFaUIGeneratorScreenWidgetRefExt};
=======
// use mofa_asr::{MoFaASRApp, MoFaASRScreenWidgetRefExt};
main
use mofa_settings::MoFaSettingsApp;
use mofa_settings::data::Preferences;
use mofa_settings::screen::SettingsScreenWidgetRefExt;
// ============================================================================
// TAB IDENTIFIER
// ============================================================================
/// Type-safe tab identifiers (replaces magic strings)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TabId {
Profile,
Settings,
}
// ============================================================================
// UI DEFINITIONS
// ============================================================================
live_design! {
use link::theme::*;
use link::shaders::*;
use link::widgets::*;
// Import from shared theme
use mofa_widgets::theme::DARK_BG;
use mofa_widgets::theme::DARK_BG_DARK;
use mofa_widgets::theme::DIVIDER;
use mofa_widgets::theme::BORDER;
use mofa_widgets::theme::SLATE_50;
use mofa_widgets::theme::SLATE_200;
use mofa_widgets::theme::SLATE_300;
use mofa_widgets::theme::SLATE_500;
use mofa_widgets::theme::SLATE_600;
use mofa_widgets::theme::SLATE_700;
use mofa_widgets::theme::SLATE_800;
use mofa_widgets::theme::GRAY_700;
use mofa_widgets::theme::DIVIDER_DARK;
use mofa_widgets::theme::BORDER_DARK;
// Import extracted widgets
use mofa_studio_shell::widgets::sidebar::Sidebar;
use mofa_studio_shell::widgets::dashboard::Dashboard;
// ------------------------------------------------------------------------
// App Window
// ------------------------------------------------------------------------
App = {{App}} {
ui: <Window> {
window: { title: "MoFA Studio", inner_size: vec2(1400, 900) }
pass: { clear_color: (DARK_BG) }
flow: Overlay
body = <View> {
width: Fill, height: Fill
// Dashboard fills the body
dashboard_wrapper = <Dashboard> {}
}
// Pinned sidebar - positioned below header, pushes content_area only
pinned_sidebar = <View> {
width: 0, height: Fill
abs_pos: vec2(0.0, 72.0)
visible: false
show_bg: true
draw_bg: {
instance dark_mode: 0.0
fn pixel(self) -> vec4 {
let sdf = Sdf2d::viewport(self.pos * self.rect_size);
sdf.box(0., 0., self.rect_size.x, self.rect_size.y, 0.0);
let bg = mix((SLATE_50), (SLATE_800), self.dark_mode);
sdf.fill(bg);
// Right border
sdf.rect(self.rect_size.x - 1.0, 0., 1.0, self.rect_size.y);
let border = mix((DIVIDER), (DIVIDER_DARK), self.dark_mode);
sdf.fill(border);
return sdf.result;
}
}
pinned_sidebar_content = <Sidebar> {
expand_to_fill: true // Fill available space when "Show More" is clicked
}
}
sidebar_trigger_overlay = <View> {
width: 34, height: 34
abs_pos: vec2(15.0, 13.0)
cursor: Hand
}
sidebar_menu_overlay = <View> {
width: 250, height: Fit
abs_pos: vec2(0.0, 52.0)
visible: false
show_bg: true
draw_bg: {
instance dark_mode: 0.0
fn pixel(self) -> vec4 {
let sdf = Sdf2d::viewport(self.pos * self.rect_size);
sdf.box(0., 0., self.rect_size.x, self.rect_size.y, 4.0);
let bg = mix((SLATE_50), (SLATE_800), self.dark_mode);
sdf.fill(bg);
sdf.box(0., 0., self.rect_size.x, self.rect_size.y, 4.0);
let border = mix((DIVIDER), (DIVIDER_DARK), self.dark_mode);
sdf.stroke(border, 1.0);
return sdf.result;
}
}
sidebar_content = <Sidebar> {
height: Fit // Override to Fit for hover overlay
}
}
user_btn_overlay = <View> {
width: 60, height: 44
abs_pos: vec2(1320.0, 10.0)
cursor: Hand
}
user_menu = <View> {
width: 140, height: Fit
abs_pos: vec2(1250.0, 55.0)
visible: false
padding: 6
show_bg: true
draw_bg: {
instance dark_mode: 0.0
fn pixel(self) -> vec4 {
let sdf = Sdf2d::viewport(self.pos * self.rect_size);
sdf.box(0., 0., self.rect_size.x, self.rect_size.y, 4.0);
let bg = mix((SLATE_50), (SLATE_800), self.dark_mode);
sdf.fill(bg);
sdf.box(0., 0., self.rect_size.x, self.rect_size.y, 4.0);
let border = mix((DIVIDER), (DIVIDER_DARK), self.dark_mode);
sdf.stroke(border, 1.0);
return sdf.result;
}
}
flow: Down
spacing: 2
menu_profile_btn = <Button> {
width: Fill, height: Fit
padding: {top: 10, bottom: 10, left: 10, right: 10}
align: {x: 0.0, y: 0.5}
text: "Profile"
icon_walk: {width: 14, height: 14, margin: {right: 8}}
animator: {
hover = {
default: off,
off = {
from: {all: Forward {duration: 0.15}}
apply: { draw_bg: {hover: 0.0} }
}
on = {
from: {all: Forward {duration: 0.15}}
apply: { draw_bg: {hover: 1.0} }
}
}
pressed = {
default: off,
off = {
from: {all: Forward {duration: 0.1}}
apply: { draw_bg: {pressed: 0.0} }
}
on = {
from: {all: Forward {duration: 0.1}}
apply: { draw_bg: {pressed: 1.0} }
}
}
}
draw_icon: {
svg_file: dep("crate://self/resources/icons/user.svg")
fn get_color(self) -> vec4 { return (SLATE_500); }
}
draw_text: {
instance dark_mode: 0.0
text_style: { font_size: 11.0 }
fn get_color(self) -> vec4 {
return mix((GRAY_700), (SLATE_200), self.dark_mode);
}
}
draw_bg: {
instance hover: 0.0
instance pressed: 0.0
instance dark_mode: 0.0
fn pixel(self) -> vec4 {
let sdf = Sdf2d::viewport(self.pos * self.rect_size);
let light_normal = (SLATE_50);
let light_hover = (SLATE_200);
let light_pressed = (SLATE_300);
let dark_normal = (SLATE_800);
let dark_hover = (SLATE_700);
let dark_pressed = (SLATE_600);
let normal = mix(light_normal, dark_normal, self.dark_mode);
let hover_color = mix(light_hover, dark_hover, self.dark_mode);
let pressed_color = mix(light_pressed, dark_pressed, self.dark_mode);
let color = mix(mix(normal, hover_color, self.hover), pressed_color, self.pressed);
sdf.box(0., 0., self.rect_size.x, self.rect_size.y, 4.0);
sdf.fill(color);
return sdf.result;
}
}
}
menu_divider = <View> {
width: Fill, height: 1
show_bg: true
draw_bg: {
instance dark_mode: 0.0
fn pixel(self) -> vec4 {
return mix((BORDER), (BORDER_DARK), self.dark_mode);
}
}
}
menu_settings_btn = <Button> {
width: Fill, height: Fit
padding: {top: 10, bottom: 10, left: 10, right: 10}
align: {x: 0.0, y: 0.5}
text: "Settings"
icon_walk: {width: 14, height: 14, margin: {right: 8}}
animator: {
hover = {
default: off,
off = {
from: {all: Forward {duration: 0.15}}
apply: { draw_bg: {hover: 0.0} }
}
on = {
from: {all: Forward {duration: 0.15}}
apply: { draw_bg: {hover: 1.0} }
}
}
pressed = {
default: off,
off = {
from: {all: Forward {duration: 0.1}}
apply: { draw_bg: {pressed: 0.0} }
}
on = {
from: {all: Forward {duration: 0.1}}
apply: { draw_bg: {pressed: 1.0} }
}
}
}
draw_icon: {
svg_file: dep("crate://self/resources/icons/settings.svg")
fn get_color(self) -> vec4 { return (SLATE_500); }
}
draw_text: {
instance dark_mode: 0.0
text_style: { font_size: 11.0 }
fn get_color(self) -> vec4 {
return mix((GRAY_700), (SLATE_200), self.dark_mode);
}
}
draw_bg: {
instance hover: 0.0
instance pressed: 0.0
instance dark_mode: 0.0
fn pixel(self) -> vec4 {
let sdf = Sdf2d::viewport(self.pos * self.rect_size);
let light_normal = (SLATE_50);
let light_hover = (SLATE_200);
let light_pressed = (SLATE_300);
let dark_normal = (SLATE_800);
let dark_hover = (SLATE_700);
let dark_pressed = (SLATE_600);
let normal = mix(light_normal, dark_normal, self.dark_mode);
let hover_color = mix(light_hover, dark_hover, self.dark_mode);
let pressed_color = mix(light_pressed, dark_pressed, self.dark_mode);
let color = mix(mix(normal, hover_color, self.hover), pressed_color, self.pressed);
sdf.box(0., 0., self.rect_size.x, self.rect_size.y, 4.0);
sdf.fill(color);
return sdf.result;
}
}
}
}
}
}
}
// ============================================================================
// WIDGET STRUCTS
// ============================================================================
// Dashboard widget is now in mofa_studio_shell::widgets::dashboard
#[derive(Live)]
pub struct App {
#[live]
ui: WidgetRef,
#[rust]
user_menu_open: bool,
#[rust]
sidebar_menu_open: bool,
#[rust]
open_tabs: Vec<TabId>,
#[rust]
active_tab: Option<TabId>,
#[rust]
last_window_size: DVec2,
#[rust]
sidebar_animating: bool,
#[rust]
sidebar_animation_start: f64,
#[rust]
sidebar_slide_in: bool,
/// Sidebar pinned state (click to toggle squeeze effect)
#[rust]
sidebar_pinned: bool,
#[rust]
sidebar_pin_animating: bool,
#[rust]
sidebar_pin_anim_start: f64,
#[rust]
sidebar_pin_expanding: bool,
/// Registry of installed apps (populated on init)
#[rust]
app_registry: AppRegistry,
/// Page router for managing page visibility
#[rust]
page_router: PageRouter,
/// Theme manager from mofa-ui (handles dark mode with animation)
#[rust]
theme: MofaTheme,
/// App data for scope-based state injection
#[rust]
app_data: MofaAppData,
/// Whether dark mode animation is in progress
#[rust]
theme_animating: bool,
/// Animation start time
#[rust]
theme_anim_start: f64,
/// Whether initial theme has been applied (on first draw)
#[rust]
theme_initialized: bool,
}
impl LiveHook for App {
fn after_new_from_doc(&mut self, _cx: &mut Cx) {
// Initialize the app registry with all installed apps
self.app_registry.register(MoFaFMApp::info());
self.app_registry.register(MoFaDebateApp::info());
feature/ai-ui-generator
self.app_registry.register(MoFaASRApp::info());
self.app_registry.register(MoFaUIGeneratorApp::info());
=======
// self.app_registry.register(MoFaASRApp::info());
main
self.app_registry.register(MoFaSettingsApp::info());
// Initialize page router (defaults to MoFA FM)
self.page_router = PageRouter::new();
// Initialize app_data with shared Dora state
let dora_state = SharedDoraState::new();
self.app_data = MofaAppData::new(dora_state);
// Load user preferences and restore dark mode
let prefs = Preferences::load();
// CLI --dark-mode flag overrides saved preference
let cli_args = get_cli_args();
let use_dark_mode = cli_args.dark_mode || prefs.dark_mode;
// Initialize theme using MofaTheme from mofa-ui
self.theme = MofaTheme::default();
self.theme.set_dark_mode(use_dark_mode);
self.app_data.set_dark_mode(use_dark_mode);
::log::debug!(
"Theme initialized: dark_mode={} (cli={}, prefs={})",
use_dark_mode,
cli_args.dark_mode,
prefs.dark_mode
);
}
}
// ============================================================================
// APP REGISTRY METHODS
// ============================================================================
impl App {
/// Get the number of installed apps
#[allow(dead_code)]
pub fn app_count(&self) -> usize {
self.app_registry.len()
}
/// Get app info by ID
#[allow(dead_code)]
pub fn get_app_info(&self, id: &str) -> Option<&mofa_widgets::AppInfo> {
self.app_registry.find_by_id(id)
}
/// Get all registered apps
#[allow(dead_code)]
pub fn apps(&self) -> &[mofa_widgets::AppInfo] {
self.app_registry.apps()
}
}
// ============================================================================
// WIDGET REGISTRATION
// ============================================================================
impl LiveRegister for App {
fn live_register(cx: &mut Cx) {
// Core widget libraries
makepad_widgets::live_design(cx);
mofa_widgets::live_design(cx);
mofa_ui::live_design(cx); // Register mofa-ui shared components (MofaLogPanel renamed to avoid conflict)
// Register apps via MofaApp trait BEFORE dashboard (dashboard uses app widgets)
// Note: Widget types in live_design! macro still require compile-time imports
// (Makepad constraint), but registration uses the standardized trait interface
<MoFaFMApp as MofaApp>::live_design(cx);
<MoFaDebateApp as MofaApp>::live_design(cx);
feature/ai-ui-generator
<MoFaASRApp as MofaApp>::live_design(cx);
<MoFaUIGeneratorApp as MofaApp>::live_design(cx);
=======
// <MoFaASRApp as MofaApp>::live_design(cx);
main
<MoFaSettingsApp as MofaApp>::live_design(cx);
// Shell widgets (order matters - tabs before dashboard, apps before dashboard)
mofa_studio_shell::widgets::sidebar::live_design(cx);
mofa_studio_shell::widgets::tabs::live_design(cx);
mofa_studio_shell::widgets::dashboard::live_design(cx);
}
}
// ============================================================================
// EVENT HANDLING
// ============================================================================
impl AppMain for App {
fn handle_event(&mut self, cx: &mut Cx, event: &Event) {
// Use empty scope - mofa-fm widgets don't expect MofaAppData yet
self.ui.handle_event(cx, event, &mut Scope::empty());
// Initialize theme on first draw (widgets are ready)
if !self.theme_initialized {
if let Event::Draw(_) = event {
self.theme_initialized = true;
// Apply initial dark mode from preferences (full update)
self.apply_dark_mode_panels(cx);
self.apply_dark_mode_screens(cx);
// Update header theme toggle icon
self.update_theme_toggle_icon(cx);
}
}
// Window resize handling
self.handle_window_resize(cx, event);
// Sidebar overlay animation (hover effect)
if self.sidebar_animating {
self.update_sidebar_animation(cx);
}
// Pinned sidebar animation (squeeze effect)
if self.sidebar_pin_animating {
self.update_sidebar_pin_animation(cx);
}
// Dark mode animation (using MofaTheme)
if self.theme_animating {
self.update_dark_mode_animation(cx);
}
// Extract actions
let actions = match event {
Event::Actions(actions) => actions.as_slice(),
_ => &[],
};
// Handle hover events
self.handle_user_menu_hover(cx, event);
self.handle_sidebar_hover(cx, event);
self.handle_theme_toggle(cx, event);
// Handle click events
self.handle_sidebar_clicks(cx, &actions);
self.handle_user_menu_clicks(cx, &actions);
self.handle_mofa_hero_buttons(cx, event);
self.handle_tab_clicks(cx, &actions);
self.handle_tab_close_clicks(cx, event);
}
}
// ============================================================================
// WINDOW & LAYOUT METHODS
// ============================================================================
impl App {
/// Handle window resize events
fn handle_window_resize(&mut self, cx: &mut Cx, event: &Event) {
if let Event::WindowGeomChange(wg) = event {
let new_size = wg.new_geom.inner_size;
if new_size != self.last_window_size {
self.last_window_size = new_size;
self.update_overlay_positions(cx);
}
}
if let Event::Draw(_) = event {
let window_rect = self.ui.area().rect(cx);
if window_rect.size.x > 0.0 && window_rect.size != self.last_window_size {
self.last_window_size = window_rect.size;
self.update_overlay_positions(cx);
}
}
}
/// Update overlay positions based on window size
fn update_overlay_positions(&mut self, cx: &mut Cx) {
let window_width = self.last_window_size.x;
let window_height = self.last_window_size.y;
if window_width <= 0.0 {
return;
}
let user_btn_x = window_width - 80.0;
self.ui.view(ids!(user_btn_overlay)).apply_over(cx, live!{
abs_pos: (dvec2(user_btn_x, 10.0))
});
let user_menu_x = window_width - 150.0;
self.ui.view(ids!(user_menu)).apply_over(cx, live!{
abs_pos: (dvec2(user_menu_x, 55.0))
});
let max_scroll_height = (window_height - 230.0).max(200.0);
self.ui.sidebar(ids!(sidebar_menu_overlay.sidebar_content)).set_max_scroll_height(max_scroll_height);
// Pinned sidebar: starts at header bottom (~72px), so less available height
// Reserved space: header(72) + sidebar padding(30) + logo(5) + mofa_fm(44) + spacing(12)
// + divider(17) + settings(44) + more spacing(8) = ~232px
let pinned_max_scroll = (window_height - 232.0).max(200.0);
self.ui.sidebar(ids!(pinned_sidebar.pinned_sidebar_content)).set_max_scroll_height(pinned_max_scroll);
self.ui.redraw(cx);
}
}
// ============================================================================
// USER MENU METHODS
// ============================================================================
impl App {
/// Handle user menu hover
fn handle_user_menu_hover(&mut self, cx: &mut Cx, event: &Event) {
let user_btn = self.ui.view(ids!(user_btn_overlay));
let user_menu = self.ui.view(ids!(user_menu));
match event.hits(cx, user_btn.area()) {
Hit::FingerHoverIn(_) => {
if !self.user_menu_open {
self.user_menu_open = true;
user_menu.set_visible(cx, true);
self.ui.redraw(cx);
}
}
_ => {}
}
if self.user_menu_open {
if let Event::MouseMove(mm) = event {
let btn_rect = user_btn.area().rect(cx);
let menu_rect = user_menu.area().rect(cx);
let in_btn = mm.abs.x >= btn_rect.pos.x - 5.0
&& mm.abs.x <= btn_rect.pos.x + btn_rect.size.x + 5.0
&& mm.abs.y >= btn_rect.pos.y - 5.0
&& mm.abs.y <= btn_rect.pos.y + btn_rect.size.y + 10.0;
let in_menu = mm.abs.x >= menu_rect.pos.x - 5.0
&& mm.abs.x <= menu_rect.pos.x + menu_rect.size.x + 5.0
&& mm.abs.y >= menu_rect.pos.y - 5.0
&& mm.abs.y <= menu_rect.pos.y + menu_rect.size.y + 5.0;
if !in_btn && !in_menu {
self.user_menu_open = false;
user_menu.set_visible(cx, false);
self.ui.redraw(cx);
}
}
}
}
/// Handle user menu button clicks
fn handle_user_menu_clicks(&mut self, cx: &mut Cx, actions: &[Action]) {
if self.ui.button(ids!(user_menu.menu_profile_btn)).clicked(actions) {
self.user_menu_open = false;
self.ui.view(ids!(user_menu)).set_visible(cx, false);
self.open_or_switch_tab(cx, TabId::Profile);
}
if self.ui.button(ids!(user_menu.menu_settings_btn)).clicked(actions) {
self.user_menu_open = false;
self.ui.view(ids!(user_menu)).set_visible(cx, false);
self.open_or_switch_tab(cx, TabId::Settings);
}
}
/// Handle header theme toggle button
fn handle_theme_toggle(&mut self, cx: &mut Cx, event: &Event) {
let theme_btn = self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.header.theme_toggle));
match event.hits(cx, theme_btn.area()) {
Hit::FingerHoverIn(_) => {
self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.header.theme_toggle)).apply_over(cx, live!{
draw_bg: { hover: 1.0 }
});
self.ui.redraw(cx);
}
Hit::FingerHoverOut(_) => {
self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.header.theme_toggle)).apply_over(cx, live!{
draw_bg: { hover: 0.0 }
});
self.ui.redraw(cx);
}
Hit::FingerUp(_) => {
self.toggle_dark_mode(cx);
self.update_theme_toggle_icon(cx);
// Save preference to disk
let mut prefs = Preferences::load();
prefs.dark_mode = self.theme.is_dark();
if let Err(e) = prefs.save() {
eprintln!("Failed to save dark mode preference: {}", e);
}
}
_ => {}
}
}
/// Update the theme toggle icon based on current mode
fn update_theme_toggle_icon(&mut self, cx: &mut Cx) {
let is_dark = self.theme.is_dark();
self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.header.theme_toggle.sun_icon)).set_visible(cx, !is_dark);
self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.header.theme_toggle.moon_icon)).set_visible(cx, is_dark);
self.ui.redraw(cx);
}
}
// ============================================================================
// SIDEBAR METHODS
// ============================================================================
impl App {
/// Handle sidebar hover and click
fn handle_sidebar_hover(&mut self, cx: &mut Cx, event: &Event) {
let sidebar_trigger = self.ui.view(ids!(sidebar_trigger_overlay));
let sidebar_menu = self.ui.view(ids!(sidebar_menu_overlay));
match event.hits(cx, sidebar_trigger.area()) {
Hit::FingerHoverIn(_) => {
// Hover: show overlay sidebar (only if not pinned)
if !self.sidebar_pinned && !self.sidebar_menu_open && !self.sidebar_animating {
self.sidebar_menu_open = true;
self.start_sidebar_slide_in(cx);
}
}
Hit::FingerUp(_) => {
// Click: toggle pinned sidebar (squeeze effect)
if !self.sidebar_pin_animating {
self.toggle_sidebar_pinned(cx);
}
}
_ => {}
}
if self.sidebar_menu_open && !self.sidebar_animating {
if let Event::MouseMove(mm) = event {
let trigger_rect = sidebar_trigger.area().rect(cx);
let sidebar_rect = sidebar_menu.area().rect(cx);
let in_trigger = mm.abs.x >= trigger_rect.pos.x - 5.0
&& mm.abs.x <= trigger_rect.pos.x + trigger_rect.size.x + 5.0
&& mm.abs.y >= trigger_rect.pos.y - 5.0
&& mm.abs.y <= trigger_rect.pos.y + trigger_rect.size.y + 5.0;
let in_sidebar = mm.abs.x >= sidebar_rect.pos.x - 5.0
&& mm.abs.x <= sidebar_rect.pos.x + sidebar_rect.size.x + 10.0
&& mm.abs.y >= sidebar_rect.pos.y - 5.0
&& mm.abs.y <= sidebar_rect.pos.y + sidebar_rect.size.y + 5.0;
if !in_trigger && !in_sidebar {
self.sidebar_menu_open = false;
self.start_sidebar_slide_out(cx);
}
}
}
}
/// Handle sidebar menu item clicks (both overlay and pinned sidebars)
/// Uses path-based click detection to avoid WidgetUid mismatch issues
fn handle_sidebar_clicks(&mut self, cx: &mut Cx, actions: &[Action]) {
// Use PageRouter to detect tab clicks via path-based search
if let Some(page) = self.page_router.check_tab_click(actions) {
::log::info!("Tab clicked: {:?}", page);
self.navigate_to_page(cx, page);
return;
}
// App buttons (1-20) - use path-based detection
let app_btn_ids = [
live_id!(app1_btn), live_id!(app2_btn), live_id!(app3_btn), live_id!(app4_btn),
live_id!(app5_btn), live_id!(app6_btn), live_id!(app7_btn), live_id!(app8_btn),
live_id!(app9_btn), live_id!(app10_btn), live_id!(app11_btn), live_id!(app12_btn),
live_id!(app13_btn), live_id!(app14_btn), live_id!(app15_btn), live_id!(app16_btn),
live_id!(app17_btn), live_id!(app18_btn), live_id!(app19_btn), live_id!(app20_btn),
];
let app_clicked = app_btn_ids.iter().any(|btn_id| tab_clicked(actions, *btn_id));
if app_clicked {
self.navigate_to_page(cx, PageId::App);
}
}
/// Navigate to a page using the PageRouter
fn navigate_to_page(&mut self, cx: &mut Cx, page: PageId) {
// Close overlay if open
if self.sidebar_menu_open {
self.sidebar_menu_open = false;
self.start_sidebar_slide_out(cx);
}
// Clear tabs
self.open_tabs.clear();
self.active_tab = None;
self.ui.view(ids!(body.tab_overlay)).set_visible(cx, false);
// Navigate router
let old_page = self.page_router.current();
if !self.page_router.navigate_to(page) {
return; // Already on this page
}
// Stop timers on old page if it was FM
if old_page == Some(PageId::MofaFM) {
self.ui.mo_fa_fmscreen(ids!(body.dashboard_wrapper.dashboard_base.content_area.main_content.content.fm_page)).stop_timers(cx);
}
// Update page visibility
self.update_page_visibility(cx);
// Update hero title panel
self.update_hero_title(cx, page);
// Start timers on new page if it's FM
if page == PageId::MofaFM {
self.ui.mo_fa_fmscreen(ids!(body.dashboard_wrapper.dashboard_base.content_area.main_content.content.fm_page)).start_timers(cx);
}
self.ui.redraw(cx);
}
/// Update page visibility based on router state
fn update_page_visibility(&mut self, cx: &mut Cx) {
let current = self.page_router.current();
// Set visibility for each page
self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.content_area.main_content.content.fm_page))
.apply_over(cx, live!{ visible: (current == Some(PageId::MofaFM)) });
self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.content_area.main_content.content.debate_page))
.apply_over(cx, live!{ visible: (current == Some(PageId::Debate)) });
self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.content_area.main_content.content.asr_page))
.apply_over(cx, live!{ visible: (current == Some(PageId::Asr)) });
self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.content_area.main_content.content.app_page))
.apply_over(cx, live!{ visible: (current == Some(PageId::App)) });
self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.content_area.main_content.content.settings_page))
.apply_over(cx, live!{ visible: (current == Some(PageId::Settings)) });
}
/// Update hero title panel with current app info
fn update_hero_title(&mut self, cx: &mut Cx, page: PageId) {
let (title, description) = match page {
PageId::MofaFM => ("MoFA FM", "AI-powered audio streaming and voice interface"),
PageId::Debate => ("MoFA Debate", "Multi-agent debate and discussion platform"),
PageId::Asr => ("MoFA ASR", "Speech recognition with MLX-based ASR engines"),
PageId::Settings => ("Settings", "Configure providers and preferences"),
PageId::App => ("Demo App", "Select an app from the sidebar"),
};
self.ui.label(ids!(body.dashboard_wrapper.dashboard_base.content_area.main_content.hero_title_panel.title_container.app_title))
.set_text(cx, title);
self.ui.label(ids!(body.dashboard_wrapper.dashboard_base.content_area.main_content.hero_title_panel.title_container.app_description))
.set_text(cx, description);
}
}
// ============================================================================
// ANIMATION METHODS
// ============================================================================
impl App {
/// Update sidebar slide animation (hover overlay effect)
fn update_sidebar_animation(&mut self, cx: &mut Cx) {
const ANIMATION_DURATION: f64 = 0.2;
const SIDEBAR_WIDTH: f64 = 250.0;
let elapsed = Cx::time_now() - self.sidebar_animation_start;
let progress = (elapsed / ANIMATION_DURATION).min(1.0);
let eased = 1.0 - (1.0 - progress).powi(3);
let x = if self.sidebar_slide_in {
-SIDEBAR_WIDTH * (1.0 - eased)
} else {
-SIDEBAR_WIDTH * eased
};
self.ui.view(ids!(sidebar_menu_overlay)).apply_over(cx, live!{
abs_pos: (dvec2(x, 52.0))
});
if progress >= 1.0 {
self.sidebar_animating = false;
if !self.sidebar_slide_in {
self.ui.view(ids!(sidebar_menu_overlay)).set_visible(cx, false);
self.ui.sidebar(ids!(sidebar_menu_overlay.sidebar_content)).collapse_show_more(cx);
}
}
self.ui.redraw(cx);
}
/// Start sidebar slide-in animation
fn start_sidebar_slide_in(&mut self, cx: &mut Cx) {
self.sidebar_animating = true;
self.sidebar_animation_start = Cx::time_now();
self.sidebar_slide_in = true;
self.ui.view(ids!(sidebar_menu_overlay)).apply_over(cx, live!{
abs_pos: (dvec2(-250.0, 52.0))
});
self.ui.view(ids!(sidebar_menu_overlay)).set_visible(cx, true);
self.ui.sidebar(ids!(sidebar_menu_overlay.sidebar_content)).restore_selection_state(cx);
self.ui.redraw(cx);
}
/// Start sidebar slide-out animation
fn start_sidebar_slide_out(&mut self, cx: &mut Cx) {
self.sidebar_animating = true;
self.sidebar_animation_start = Cx::time_now();
self.sidebar_slide_in = false;
self.ui.redraw(cx);
}
/// Toggle pinned sidebar (squeeze effect)
fn toggle_sidebar_pinned(&mut self, cx: &mut Cx) {
// Close hover overlay if open
if self.sidebar_menu_open {
self.sidebar_menu_open = false;
self.ui.view(ids!(sidebar_menu_overlay)).set_visible(cx, false);
}
self.sidebar_pinned = !self.sidebar_pinned;
self.sidebar_pin_animating = true;
self.sidebar_pin_anim_start = Cx::time_now();
self.sidebar_pin_expanding = self.sidebar_pinned;
// Show/hide pinned sidebar
self.ui.view(ids!(pinned_sidebar)).set_visible(cx, true);
self.ui.redraw(cx);
}
/// Update pinned sidebar animation (squeeze effect)
fn update_sidebar_pin_animation(&mut self, cx: &mut Cx) {
const ANIMATION_DURATION: f64 = 0.25;
const SIDEBAR_WIDTH: f64 = 250.0;
let elapsed = Cx::time_now() - self.sidebar_pin_anim_start;
let progress = (elapsed / ANIMATION_DURATION).min(1.0);
let eased = 1.0 - (1.0 - progress).powi(3); // Cubic ease-out
// Calculate sidebar width based on animation
let sidebar_width = if self.sidebar_pin_expanding {
SIDEBAR_WIDTH * eased
} else {
SIDEBAR_WIDTH * (1.0 - eased)
};
// Get header's actual bottom position
let header_rect = self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.header)).area().rect(cx);
let header_bottom = header_rect.pos.y + header_rect.size.y;
// Apply width and position to pinned sidebar
self.ui.view(ids!(pinned_sidebar)).apply_over(cx, live!{
width: (sidebar_width)
abs_pos: (dvec2(0.0, header_bottom))
});
// Apply left margin to content_area to push it (not the header)
self.ui.view(ids!(body.dashboard_wrapper.dashboard_base.content_area)).apply_over(cx, live!{
margin: { left: (sidebar_width) }
});
// Trigger overlay stays at original position - hamburger is in header which doesn't move
if progress >= 1.0 {
self.sidebar_pin_animating = false;
if !self.sidebar_pin_expanding {
self.ui.view(ids!(pinned_sidebar)).set_visible(cx, false);
}
}
self.ui.redraw(cx);
}
/// Toggle dark mode with animation
pub fn toggle_dark_mode(&mut self, cx: &mut Cx) {
self.theme.toggle();
self.app_data.set_dark_mode(self.theme.is_dark());
self.theme_animating = true;
self.theme_anim_start = Cx::time_now();
// Apply screens immediately at target value (snap, not animated)
// This avoids calling update_dark_mode on every frame
let target = self.theme.target_value();
self.apply_dark_mode_screens_with_value(cx, target);
self.ui.redraw(cx);
}
/// Update dark mode animation using MofaTheme
fn update_dark_mode_animation(&mut self, cx: &mut Cx) {
let elapsed = Cx::time_now() - self.theme_anim_start;
let duration = mofa_ui::THEME_TRANSITION_DURATION;
// Use MofaTheme's animation update
let still_animating = self.theme.update_animation(elapsed, duration);
// During animation: only update main panels (no errors)
// Full update with screens happens only at the end
self.apply_dark_mode_panels(cx);
if !still_animating {
self.theme_animating = false;
// Apply to ALL widgets including screens at animation end
self.apply_dark_mode_screens(cx);