use std::any::TypeId;
use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::rc::Rc;
use waft_embedder::{Color, Locale};
use waft_foundation::App;
use waft_painting::TextStyle;
use crate::{
ActionRef, BuildContext, GlobalKey, InitialRouteListFactory, IntoWidget, KeyRef,
LocaleListResolutionCallback, LocalizationsDelegateRef, NavigatorObserverRef, PageRouteBuilder,
PageRouteFactory, PageRouteRef, RouteFactory, RoutePageBuilder, RouteRef, RouteSettings,
RouteSettingsRef, ShortcutMap, StatelessWidget, TransitionBuilder, WidgetBuilder, WidgetRef,
WidgetsApp,
};
pub struct TestWidgetsApp {
pub key: Option<KeyRef>,
pub navigator_key: Option<GlobalKey>,
pub home: Option<WidgetRef>,
pub initial_route: Option<String>,
pub on_generate_route: Option<RouteFactory>,
pub on_generate_initial_routes: Option<InitialRouteListFactory>,
pub on_unknown_route: Option<RouteFactory>,
pub navigator_observers: Vec<NavigatorObserverRef>,
pub routes: HashMap<String, WidgetBuilder>,
pub color: Color,
pub text_style: Option<TextStyle>,
pub page_route_builder: PageRouteFactory,
pub builder: Option<TransitionBuilder>,
pub shortcuts: Option<ShortcutMap>,
pub actions: Option<HashMap<TypeId, ActionRef>>,
pub restoration_scope_id: Option<String>,
pub localizations_delegates: Option<Vec<LocalizationsDelegateRef>>,
pub locale_list_resolution_callback: Option<LocaleListResolutionCallback>,
}
impl TestWidgetsApp {
pub fn new() -> TestWidgetsApp {
TestWidgetsApp {
key: None,
navigator_key: None,
home: None,
initial_route: None,
on_generate_route: None,
on_generate_initial_routes: None,
on_unknown_route: None,
navigator_observers: Vec::new(),
routes: HashMap::new(),
color: Color::new(0xFFFFFFFF),
text_style: None,
page_route_builder: Rc::new(TestWidgetsApp::default_page_route_builder),
builder: None,
shortcuts: None,
actions: None,
restoration_scope_id: None,
localizations_delegates: None,
locale_list_resolution_callback: None,
}
}
pub fn key(mut self, key: KeyRef) -> TestWidgetsApp {
self.key = Some(key);
self
}
pub fn navigator_key(mut self, navigator_key: GlobalKey) -> TestWidgetsApp {
self.navigator_key = Some(navigator_key);
self
}
#[cfg_attr(debug_assertions, track_caller)]
pub fn home<K>(mut self, home: impl IntoWidget<K>) -> TestWidgetsApp {
self.home = Some(home.into_widget());
self
}
pub fn initial_route(mut self, initial_route: impl Into<String>) -> TestWidgetsApp {
self.initial_route = Some(initial_route.into());
self
}
pub fn on_generate_route(
mut self,
on_generate_route: impl Fn(&mut App, &RouteSettings) -> Option<RouteRef> + 'static,
) -> TestWidgetsApp {
self.on_generate_route = Some(Rc::new(on_generate_route));
self
}
pub fn on_generate_initial_routes(
mut self,
on_generate_initial_routes: impl Fn(&mut App, &str) -> Vec<RouteRef> + 'static,
) -> TestWidgetsApp {
self.on_generate_initial_routes = Some(Rc::new(on_generate_initial_routes));
self
}
pub fn on_unknown_route(
mut self,
on_unknown_route: impl Fn(&mut App, &RouteSettings) -> Option<RouteRef> + 'static,
) -> TestWidgetsApp {
self.on_unknown_route = Some(Rc::new(on_unknown_route));
self
}
pub fn navigator_observers(
mut self,
navigator_observers: impl IntoIterator<Item = NavigatorObserverRef>,
) -> TestWidgetsApp {
self.navigator_observers = navigator_observers.into_iter().collect();
self
}
pub fn routes(
mut self,
routes: impl IntoIterator<Item = (String, WidgetBuilder)>,
) -> TestWidgetsApp {
self.routes = routes.into_iter().collect();
self
}
pub fn color(mut self, color: Color) -> TestWidgetsApp {
self.color = color;
self
}
pub fn text_style(mut self, text_style: TextStyle) -> TestWidgetsApp {
self.text_style = Some(text_style);
self
}
pub fn page_route_builder(
mut self,
page_route_builder: impl Fn(&mut App, &RouteSettings, WidgetBuilder) -> PageRouteRef + 'static,
) -> TestWidgetsApp {
self.page_route_builder = Rc::new(page_route_builder);
self
}
pub fn builder(
mut self,
builder: impl Fn(&mut App, BuildContext, Option<&WidgetRef>) -> WidgetRef + 'static,
) -> TestWidgetsApp {
self.builder = Some(Rc::new(builder));
self
}
pub fn shortcuts(mut self, shortcuts: ShortcutMap) -> TestWidgetsApp {
self.shortcuts = Some(shortcuts);
self
}
pub fn actions(mut self, actions: HashMap<TypeId, ActionRef>) -> TestWidgetsApp {
self.actions = Some(actions);
self
}
pub fn restoration_scope_id(
mut self,
restoration_scope_id: impl Into<String>,
) -> TestWidgetsApp {
self.restoration_scope_id = Some(restoration_scope_id.into());
self
}
pub fn localizations_delegates(
mut self,
localizations_delegates: impl IntoIterator<Item = LocalizationsDelegateRef>,
) -> TestWidgetsApp {
self.localizations_delegates = Some(localizations_delegates.into_iter().collect());
self
}
pub fn locale_list_resolution_callback(
mut self,
locale_list_resolution_callback: impl Fn(Option<&[Locale]>, &[Locale]) -> Option<Locale>
+ 'static,
) -> TestWidgetsApp {
self.locale_list_resolution_callback = Some(Rc::new(locale_list_resolution_callback));
self
}
}
impl TestWidgetsApp {
pub fn default_page_route_builder(
app: &mut App,
settings: &RouteSettings,
builder: WidgetBuilder,
) -> PageRouteRef {
let page_builder: RoutePageBuilder =
Rc::new(move |app, context, _animation, _secondary| builder(app, context));
PageRouteBuilder::new(app, page_builder)
.settings(app, RouteSettingsRef::Settings(settings.clone()))
.into_erased()
}
}
impl Default for TestWidgetsApp {
fn default() -> TestWidgetsApp {
TestWidgetsApp::new()
}
}
impl Debug for TestWidgetsApp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TestWidgetsApp")
.field("color", &self.color)
.finish_non_exhaustive()
}
}
impl StatelessWidget for TestWidgetsApp {
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
let mut widgets_app = WidgetsApp::new(self.color);
widgets_app.navigator_key = self.navigator_key.clone();
widgets_app.on_generate_route = self.on_generate_route.clone();
widgets_app.on_generate_initial_routes = self.on_generate_initial_routes.clone();
widgets_app.page_route_builder = Some(Rc::clone(&self.page_route_builder));
widgets_app.home = self.home.clone();
widgets_app.routes = self.routes.clone();
widgets_app.on_unknown_route = self.on_unknown_route.clone();
widgets_app.initial_route = self.initial_route.clone();
widgets_app.navigator_observers = self.navigator_observers.clone();
widgets_app.builder = self.builder.clone();
widgets_app.text_style = self.text_style.clone();
widgets_app.localizations_delegates = self.localizations_delegates.clone();
widgets_app.locale_list_resolution_callback = self.locale_list_resolution_callback.clone();
widgets_app.shortcuts = self.shortcuts.clone();
widgets_app.actions = self.actions.clone();
widgets_app.restoration_scope_id = self.restoration_scope_id.clone();
widgets_app.into_widget()
}
}