1use std::any::TypeId;
11use std::collections::HashMap;
12use std::fmt::{self, Debug};
13
14use waft_embedder::{Brightness, Color, Locale, SystemUiOverlayStyle};
15use waft_foundation::{App, Handle};
16use waft_painting::TextStyle;
17use waft_services::SystemChrome;
18use waft_widgets::{
19 ActionRef, BuildContext, DefaultSelectionStyle, GenerateAppTitle, GlobalKey,
20 InitialRouteListFactory, IntoWidget, KeyRef, LocaleListResolutionCallback,
21 LocaleResolutionCallback, LocalizationsDelegateRef, MediaQuery, NavigationNotification,
22 NavigatorObserverRef, NotificationListenerCallback, RouteFactory, RouteSettingsRef,
23 ScrollBehaviorRef, ScrollConfiguration, ShortcutMap, State, StateData, StatefulWidget,
24 TransitionBuilder, View, WidgetBuilder, WidgetRef, WidgetsApp,
25};
26
27use crate::primitives::install_icon_font;
28use crate::route::FluentPageRoute;
29use crate::scroll_behavior::FluentScrollBehavior;
30use crate::theme::{AccentPalette, TextBlockStyle, Theme, ThemeResources, ThemeScope};
31
32#[derive(Clone, Copy, Debug, PartialEq)]
35pub struct FluentTheme {
36 pub theme: Theme,
38 pub accent: AccentPalette,
40}
41
42impl FluentTheme {
43 pub const fn light() -> FluentTheme {
45 FluentTheme {
46 theme: Theme::Light,
47 accent: AccentPalette::WINDOWS_BLUE,
48 }
49 }
50
51 pub const fn dark() -> FluentTheme {
53 FluentTheme {
54 theme: Theme::Dark,
55 accent: AccentPalette::WINDOWS_BLUE,
56 }
57 }
58
59 pub const fn new(theme: Theme, accent: AccentPalette) -> FluentTheme {
61 FluentTheme { theme, accent }
62 }
63
64 pub fn accent(mut self, accent: AccentPalette) -> FluentTheme {
66 self.accent = accent;
67 self
68 }
69}
70
71impl Default for FluentTheme {
72 fn default() -> FluentTheme {
73 FluentTheme::light()
74 }
75}
76
77#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
82pub enum FluentThemeMode {
83 #[default]
85 System,
86 Light,
88 Dark,
90}
91
92pub struct FluentApp {
111 pub key: Option<KeyRef>,
113
114 pub navigator_key: Option<GlobalKey>,
116
117 pub home: Option<WidgetRef>,
119
120 pub theme: Option<FluentTheme>,
123
124 pub dark_theme: Option<FluentTheme>,
128
129 pub theme_mode: Option<FluentThemeMode>,
131
132 pub routes: HashMap<String, WidgetBuilder>,
134
135 pub initial_route: Option<String>,
137
138 pub on_generate_route: Option<RouteFactory>,
140
141 pub on_generate_initial_routes: Option<InitialRouteListFactory>,
143
144 pub on_unknown_route: Option<RouteFactory>,
146
147 pub on_navigation_notification: Option<NotificationListenerCallback<NavigationNotification>>,
149
150 pub navigator_observers: Vec<NavigatorObserverRef>,
152
153 pub builder: Option<TransitionBuilder>,
155
156 pub title: Option<String>,
158
159 pub on_generate_title: Option<GenerateAppTitle>,
161
162 pub color: Option<Color>,
165
166 pub locale: Option<Locale>,
168
169 pub localizations_delegates: Option<Vec<LocalizationsDelegateRef>>,
171
172 pub locale_list_resolution_callback: Option<LocaleListResolutionCallback>,
174
175 pub locale_resolution_callback: Option<LocaleResolutionCallback>,
177
178 pub supported_locales: Vec<Locale>,
180
181 pub show_performance_overlay: bool,
183
184 pub show_semantics_debugger: bool,
186
187 pub debug_show_checked_mode_banner: bool,
189
190 pub shortcuts: Option<ShortcutMap>,
192
193 pub actions: Option<HashMap<TypeId, ActionRef>>,
195
196 pub restoration_scope_id: Option<String>,
198
199 pub use_inherited_media_query: bool,
201}
202
203impl Default for FluentApp {
204 fn default() -> FluentApp {
205 FluentApp {
206 key: None,
207 navigator_key: None,
208 home: None,
209 theme: None,
210 dark_theme: None,
211 theme_mode: None,
212 routes: HashMap::new(),
213 initial_route: None,
214 on_generate_route: None,
215 on_generate_initial_routes: None,
216 on_unknown_route: None,
217 on_navigation_notification: None,
218 navigator_observers: Vec::new(),
219 builder: None,
220 title: None,
221 on_generate_title: None,
222 color: None,
223 locale: None,
224 localizations_delegates: None,
225 locale_list_resolution_callback: None,
226 locale_resolution_callback: None,
227 supported_locales: vec![Locale::new("en").country_code("US")],
228 show_performance_overlay: false,
229 show_semantics_debugger: false,
230 debug_show_checked_mode_banner: true,
231 shortcuts: None,
232 actions: None,
233 restoration_scope_id: None,
234 use_inherited_media_query: false,
235 }
236 }
237}
238
239impl FluentApp {
240 pub fn new() -> FluentApp {
242 FluentApp::default()
243 }
244
245 pub fn key(mut self, key: KeyRef) -> FluentApp {
247 self.key = Some(key);
248 self
249 }
250
251 pub fn navigator_key(mut self, navigator_key: GlobalKey) -> FluentApp {
253 self.navigator_key = Some(navigator_key);
254 self
255 }
256
257 #[cfg_attr(debug_assertions, track_caller)]
259 pub fn home<K>(mut self, home: impl waft_widgets::IntoWidget<K>) -> FluentApp {
260 self.home = Some(home.into_widget());
261 self
262 }
263
264 pub fn theme(mut self, theme: FluentTheme) -> FluentApp {
267 self.theme = Some(theme);
268 self
269 }
270
271 pub fn dark_theme(mut self, theme: FluentTheme) -> FluentApp {
273 self.dark_theme = Some(theme);
274 self
275 }
276
277 pub fn theme_mode(mut self, mode: FluentThemeMode) -> FluentApp {
279 self.theme_mode = Some(mode);
280 self
281 }
282
283 pub fn routes(
285 mut self,
286 routes: impl IntoIterator<Item = (String, WidgetBuilder)>,
287 ) -> FluentApp {
288 self.routes = routes.into_iter().collect();
289 self
290 }
291
292 pub fn initial_route(mut self, initial_route: impl Into<String>) -> FluentApp {
294 self.initial_route = Some(initial_route.into());
295 self
296 }
297
298 pub fn on_generate_route(
300 mut self,
301 on_generate_route: impl Fn(
302 &mut App,
303 &waft_widgets::RouteSettings,
304 ) -> Option<waft_widgets::RouteRef>
305 + 'static,
306 ) -> FluentApp {
307 self.on_generate_route = Some(std::rc::Rc::new(on_generate_route));
308 self
309 }
310
311 pub fn on_generate_initial_routes(
313 mut self,
314 on_generate_initial_routes: impl Fn(&mut App, &str) -> Vec<waft_widgets::RouteRef> + 'static,
315 ) -> FluentApp {
316 self.on_generate_initial_routes = Some(std::rc::Rc::new(on_generate_initial_routes));
317 self
318 }
319
320 pub fn on_unknown_route(
322 mut self,
323 on_unknown_route: impl Fn(
324 &mut App,
325 &waft_widgets::RouteSettings,
326 ) -> Option<waft_widgets::RouteRef>
327 + 'static,
328 ) -> FluentApp {
329 self.on_unknown_route = Some(std::rc::Rc::new(on_unknown_route));
330 self
331 }
332
333 pub fn on_navigation_notification(
335 mut self,
336 on_navigation_notification: impl Fn(&mut App, &NavigationNotification) -> bool + 'static,
337 ) -> FluentApp {
338 self.on_navigation_notification = Some(std::rc::Rc::new(on_navigation_notification));
339 self
340 }
341
342 pub fn navigator_observers(
344 mut self,
345 navigator_observers: impl IntoIterator<Item = NavigatorObserverRef>,
346 ) -> FluentApp {
347 self.navigator_observers = navigator_observers.into_iter().collect();
348 self
349 }
350
351 pub fn builder(
353 mut self,
354 builder: impl Fn(&mut App, BuildContext, Option<&WidgetRef>) -> WidgetRef + 'static,
355 ) -> FluentApp {
356 self.builder = Some(std::rc::Rc::new(builder));
357 self
358 }
359
360 pub fn title(mut self, title: impl Into<String>) -> FluentApp {
362 self.title = Some(title.into());
363 self
364 }
365
366 pub fn on_generate_title(
368 mut self,
369 on_generate_title: impl Fn(&mut App, BuildContext) -> String + 'static,
370 ) -> FluentApp {
371 self.on_generate_title = Some(std::rc::Rc::new(on_generate_title));
372 self
373 }
374
375 pub fn color(mut self, color: Color) -> FluentApp {
377 self.color = Some(color);
378 self
379 }
380
381 pub fn locale(mut self, locale: Locale) -> FluentApp {
383 self.locale = Some(locale);
384 self
385 }
386
387 pub fn localizations_delegates(
389 mut self,
390 delegates: impl IntoIterator<Item = LocalizationsDelegateRef>,
391 ) -> FluentApp {
392 self.localizations_delegates = Some(delegates.into_iter().collect());
393 self
394 }
395
396 pub fn locale_list_resolution_callback(
398 mut self,
399 callback: impl Fn(Option<&[Locale]>, &[Locale]) -> Option<Locale> + 'static,
400 ) -> FluentApp {
401 self.locale_list_resolution_callback = Some(std::rc::Rc::new(callback));
402 self
403 }
404
405 pub fn locale_resolution_callback(
407 mut self,
408 callback: impl Fn(Option<&Locale>, &[Locale]) -> Option<Locale> + 'static,
409 ) -> FluentApp {
410 self.locale_resolution_callback = Some(std::rc::Rc::new(callback));
411 self
412 }
413
414 pub fn supported_locales(mut self, locales: impl IntoIterator<Item = Locale>) -> FluentApp {
416 self.supported_locales = locales.into_iter().collect();
417 self
418 }
419
420 pub fn show_performance_overlay(mut self, show: bool) -> FluentApp {
422 self.show_performance_overlay = show;
423 self
424 }
425
426 pub fn show_semantics_debugger(mut self, show: bool) -> FluentApp {
428 self.show_semantics_debugger = show;
429 self
430 }
431
432 pub fn debug_show_checked_mode_banner(mut self, show: bool) -> FluentApp {
434 self.debug_show_checked_mode_banner = show;
435 self
436 }
437
438 pub fn shortcuts(mut self, shortcuts: ShortcutMap) -> FluentApp {
440 self.shortcuts = Some(shortcuts);
441 self
442 }
443
444 pub fn actions(mut self, actions: HashMap<TypeId, ActionRef>) -> FluentApp {
446 self.actions = Some(actions);
447 self
448 }
449
450 pub fn restoration_scope_id(mut self, id: impl Into<String>) -> FluentApp {
452 self.restoration_scope_id = Some(id.into());
453 self
454 }
455}
456
457impl FluentAppState {
458 fn effective_theme(
460 self: Handle<FluentAppState>,
461 app: &mut App,
462 context: BuildContext,
463 ) -> FluentTheme {
464 let (theme, dark_theme, mode) = {
465 let widget = self.widget(app);
466 (
467 widget.theme,
468 widget.dark_theme,
469 widget.theme_mode.unwrap_or_default(),
470 )
471 };
472 let dark = mode == FluentThemeMode::Dark
473 || (mode == FluentThemeMode::System
474 && MediaQuery::platform_brightness_of(app, context) == Brightness::Dark);
475 if dark {
476 dark_theme.or(theme).unwrap_or_else(FluentTheme::dark)
477 } else {
478 theme.unwrap_or_else(FluentTheme::light)
479 }
480 }
481
482 fn build_widget_app(
484 self: Handle<FluentAppState>,
485 app: &mut App,
486 context: BuildContext,
487 ) -> WidgetsApp {
488 let theme = self.effective_theme(app, context);
489 let widget = self.widget(app);
490 let color = widget.color.unwrap_or(theme.accent.base);
491
492 let mut widgets_app = WidgetsApp::new(color)
494 .key(GlobalKey::object(self.id()).into())
495 .navigator_observers(widget.navigator_observers.clone())
496 .page_route_builder(|app, settings, builder| {
497 let route = FluentPageRoute::new(app, builder)
498 .settings(app, RouteSettingsRef::Settings(settings.clone()));
499 route.into_erased()
500 })
501 .routes(widget.routes.clone())
502 .supported_locales(widget.supported_locales.clone())
503 .show_performance_overlay(widget.show_performance_overlay)
504 .show_semantics_debugger(widget.show_semantics_debugger)
505 .debug_show_checked_mode_banner(widget.debug_show_checked_mode_banner)
506 .text_style(default_text_style(&theme));
507
508 if let Some(navigator_key) = widget.navigator_key.clone() {
509 widgets_app = widgets_app.navigator_key(navigator_key);
510 }
511 if let Some(home) = widget.home.clone() {
512 widgets_app = widgets_app.home(home);
513 }
514 if let Some(initial_route) = widget.initial_route.clone() {
515 widgets_app = widgets_app.initial_route(initial_route);
516 }
517 if let Some(on_generate_route) = widget.on_generate_route.clone() {
518 widgets_app = widgets_app
519 .on_generate_route(move |app, settings| on_generate_route(app, settings));
520 }
521 if let Some(on_generate_initial_routes) = widget.on_generate_initial_routes.clone() {
522 widgets_app = widgets_app
523 .on_generate_initial_routes(move |app, name| on_generate_initial_routes(app, name));
524 }
525 if let Some(on_unknown_route) = widget.on_unknown_route.clone() {
526 widgets_app =
527 widgets_app.on_unknown_route(move |app, settings| on_unknown_route(app, settings));
528 }
529 if let Some(on_navigation_notification) = widget.on_navigation_notification.clone() {
530 widgets_app = widgets_app.on_navigation_notification(move |app, notification| {
531 on_navigation_notification(app, notification)
532 });
533 }
534 if let Some(builder) = widget.builder.clone() {
535 widgets_app =
536 widgets_app.builder(move |app, context, child| builder(app, context, child));
537 }
538 if let Some(title) = widget.title.clone() {
539 widgets_app = widgets_app.title(title);
540 }
541 if let Some(on_generate_title) = widget.on_generate_title.clone() {
542 widgets_app =
543 widgets_app.on_generate_title(move |app, context| on_generate_title(app, context));
544 }
545 if let Some(locale) = widget.locale.clone() {
546 widgets_app = widgets_app.locale(locale);
547 }
548 if let Some(delegates) = widget.localizations_delegates.clone() {
549 widgets_app = widgets_app.localizations_delegates(delegates);
550 }
551 if let Some(callback) = widget.locale_resolution_callback.clone() {
552 widgets_app = widgets_app
553 .locale_resolution_callback(move |locale, supported| callback(locale, supported));
554 }
555 if let Some(callback) = widget.locale_list_resolution_callback.clone() {
556 widgets_app = widgets_app.locale_list_resolution_callback(move |locales, supported| {
557 callback(locales, supported)
558 });
559 }
560 if let Some(shortcuts) = widget.shortcuts.clone() {
561 widgets_app = widgets_app.shortcuts(shortcuts);
562 }
563 if let Some(actions) = widget.actions.clone() {
564 widgets_app = widgets_app.actions(actions);
565 }
566 if let Some(restoration_scope_id) = widget.restoration_scope_id.clone() {
567 widgets_app = widgets_app.restoration_scope_id(restoration_scope_id);
568 }
569 widgets_app
570 }
571}
572
573impl StatefulWidget for FluentApp {
574 type State = FluentAppState;
575
576 fn key(&self) -> Option<&KeyRef> {
577 self.key.as_ref()
578 }
579
580 fn create_state(&self) -> FluentAppState {
581 FluentAppState {
582 state: StateData::new(),
583 }
584 }
585}
586
587pub struct FluentAppState {
589 state: StateData<FluentApp>,
590}
591
592impl State for FluentAppState {
593 type Widget = FluentApp;
594 waft_widgets::state_accessors!();
595
596 fn init_state(self: Handle<Self>, app: &mut App) {
597 install_icon_font(app);
598 }
599
600 fn build(self: Handle<Self>, app: &mut App, context: BuildContext) -> WidgetRef {
601 let theme = self.effective_theme(app, context);
602 if let Some(view) = View::maybe_of(app, context) {
603 SystemChrome::set_system_ui_overlay_style(
604 app,
605 view.id(),
606 match theme.theme {
607 Theme::Dark => &SystemUiOverlayStyle::LIGHT,
608 Theme::Light => &SystemUiOverlayStyle::DARK,
609 },
610 );
611 }
612 let app = waft_widgets::Builder::new(move |app, context| {
613 self.build_widget_app(app, context).into_widget()
614 })
615 .into_widget();
616 let accent = theme.accent.base;
617 #[allow(deprecated)]
620 let selected = accent.with_opacity(0.4);
621 let selection = DefaultSelectionStyle::new(app)
622 .selection_color(selected)
623 .cursor_color(accent)
624 .into_widget();
625 let scrolling = ScrollConfiguration::new(
626 ScrollBehaviorRef::new(FluentScrollBehavior::new()),
627 selection,
628 )
629 .into_widget();
630 ThemeScope::new(theme.theme, scrolling)
631 .accent(theme.accent)
632 .into_widget()
633 }
634}
635
636fn default_text_style(theme: &FluentTheme) -> TextStyle {
639 let resources = ThemeResources::new(theme.theme, theme.accent);
640 TextBlockStyle::Body.text_style(&resources, resources.text_fill_color_primary)
641}
642
643impl Debug for FluentApp {
644 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
645 formatter
646 .debug_struct("FluentApp")
647 .field("key", &self.key)
648 .field("home", &self.home)
649 .field("theme", &self.theme)
650 .field("darkTheme", &self.dark_theme)
651 .field("themeMode", &self.theme_mode)
652 .field("routes", &self.routes.keys().collect::<Vec<_>>())
653 .field("locale", &self.locale)
654 .field("restorationScopeId", &self.restoration_scope_id)
655 .finish_non_exhaustive()
656 }
657}