use std::fmt::{self, Debug};
use std::rc::Rc;
use std::time::Duration;
use waft_animation::AnimationRef;
use waft_foundation::{App, Handle, HandleId, Retained};
use waft_painting::Color;
use crate::framework::{BuildContext, WidgetRef};
use crate::widgets::navigator::{AnyRoute, Route, RouteData, RouteSettingsRef};
use crate::widgets::routes::{
AnyModalRoute, AnyTransitionRoute, LocalHistoryRoute, LocalHistoryRouteData, ModalRoute,
ModalRouteData, OverlayRoute, OverlayRouteData, PredictiveBackRoute, RoutePageBuilder,
RouteTransitionsBuilder, TransitionRoute, TransitionRouteData,
};
use waft_foundation::Ref;
pub struct PageRouteData {
pub fullscreen_dialog: bool,
pub allow_snapshotting: bool,
pub barrier_dismissible: bool,
}
impl PageRouteData {
pub fn new() -> PageRouteData {
PageRouteData {
fullscreen_dialog: false,
allow_snapshotting: true,
barrier_dismissible: false,
}
}
}
impl Default for PageRouteData {
fn default() -> PageRouteData {
PageRouteData::new()
}
}
#[macro_export]
macro_rules! page_route_accessors {
() => {
fn page_route_data(
self: ::waft_foundation::Handle<Self>,
app: &::waft_foundation::App,
) -> &$crate::PageRouteData {
&app.get(self).page_route
}
fn page_route_data_mut(
self: ::waft_foundation::Handle<Self>,
app: &mut ::waft_foundation::App,
) -> &mut $crate::PageRouteData {
&mut app.get_mut(self).page_route
}
};
}
pub trait PageRoute: ModalRoute {
fn page_route_data(self: Handle<Self>, app: &App) -> &PageRouteData;
fn page_route_data_mut(self: Handle<Self>, app: &mut App) -> &mut PageRouteData;
fn as_page_route(self: Handle<Self>) -> AnyPageRoute {
AnyPageRoute {
id: self.id(),
vtable: const { &PageRouteVTable::of::<Self>() },
}
}
fn fullscreen_dialog(self: Handle<Self>, app: &App) -> bool {
self.page_route_data(app).fullscreen_dialog
}
fn allow_snapshotting(self: Handle<Self>, app: &App) -> bool {
self.page_route_data(app).allow_snapshotting
}
fn opaque(self: Handle<Self>, app: &App) -> bool {
let _ = app;
true
}
fn barrier_dismissible(self: Handle<Self>, app: &App) -> bool {
self.page_route_data(app).barrier_dismissible
}
fn can_transition_to(self: Handle<Self>, app: &App, next_route: AnyTransitionRoute) -> bool {
next_route.as_route().as_page_route(app).is_some()
}
fn can_transition_from(
self: Handle<Self>,
app: &App,
previous_route: AnyTransitionRoute,
) -> bool {
previous_route.as_route().as_page_route(app).is_some()
}
fn pop_gesture_enabled(self: Handle<Self>, app: &mut App) -> bool {
!PageRoute::fullscreen_dialog(self, app) && ModalRoute::pop_gesture_enabled(self, app)
}
}
struct PageRouteVTable {
type_name: fn() -> &'static str,
route: fn(HandleId) -> AnyRoute,
modal_route: fn(HandleId) -> AnyModalRoute,
}
impl PageRouteVTable {
const fn of<R: PageRoute>() -> PageRouteVTable {
PageRouteVTable {
type_name: std::any::type_name::<R>,
route: |id| Route::as_route(resolve::<R>(id)),
modal_route: |id| ModalRoute::as_modal_route(resolve::<R>(id)),
}
}
}
#[derive(Clone, Copy)]
pub struct AnyPageRoute {
id: HandleId,
vtable: &'static PageRouteVTable,
}
impl From<AnyPageRoute> for AnyRoute {
fn from(route: AnyPageRoute) -> AnyRoute {
route.as_route()
}
}
impl From<AnyPageRoute> for AnyModalRoute {
fn from(route: AnyPageRoute) -> AnyModalRoute {
route.as_modal_route()
}
}
pub type PageRouteRef = Retained<AnyPageRoute>;
waft_foundation::erased_handle!(AnyPageRoute: PageRoute => PageRoute::as_page_route);
impl Debug for AnyPageRoute {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}({:?})", (self.vtable.type_name)(), self.id)
}
}
impl AnyPageRoute {
pub fn as_route(self) -> AnyRoute {
(self.vtable.route)(self.id)
}
pub fn as_modal_route(self) -> AnyModalRoute {
(self.vtable.modal_route)(self.id)
}
}
fn resolve<L: 'static>(id: HandleId) -> Handle<L> {
Handle::from_id(id)
}
#[macro_export]
macro_rules! page_predictive_back_route_overrides {
() => {
$crate::modal_predictive_back_route_overrides!($crate::PageRoute::pop_gesture_enabled);
};
}
#[macro_export]
macro_rules! page_transition_route_overrides {
() => {
$crate::modal_transition_route_overrides!();
fn allow_snapshotting(
self: ::waft_foundation::Handle<Self>,
app: &::waft_foundation::App,
) -> bool {
$crate::PageRoute::allow_snapshotting(self, app)
}
fn can_transition_to(
self: ::waft_foundation::Handle<Self>,
app: &::waft_foundation::App,
next_route: $crate::AnyTransitionRoute,
) -> bool {
$crate::PageRoute::can_transition_to(self, app, next_route)
}
fn can_transition_from(
self: ::waft_foundation::Handle<Self>,
app: &::waft_foundation::App,
previous_route: $crate::AnyTransitionRoute,
) -> bool {
$crate::PageRoute::can_transition_from(self, app, previous_route)
}
};
}
#[macro_export]
macro_rules! page_route_overrides {
() => {
$crate::modal_route_overrides!();
fn as_page_route(
self: ::waft_foundation::Handle<Self>,
) -> ::std::option::Option<$crate::AnyPageRoute> {
::std::option::Option::Some($crate::PageRoute::as_page_route(self))
}
};
}
fn default_transitions_builder(
_app: &mut App,
_context: BuildContext,
_animation: AnimationRef<f64>,
_secondary_animation: AnimationRef<f64>,
child: WidgetRef,
) -> WidgetRef {
child
}
pub struct PageRouteBuilder {
route: RouteData,
overlay_route: OverlayRouteData,
transition_route: TransitionRouteData,
local_history: LocalHistoryRouteData,
modal_route: ModalRouteData,
page_route: PageRouteData,
pub page_builder: RoutePageBuilder,
pub transitions_builder: RouteTransitionsBuilder,
transition_duration: Duration,
reverse_transition_duration: Duration,
opaque: bool,
barrier_dismissible: bool,
barrier_color: Option<Color>,
barrier_label: Option<String>,
maintain_state: bool,
}
impl PageRouteBuilder {
pub fn new(app: &mut App, page_builder: RoutePageBuilder) -> Ref<PageRouteBuilder> {
let route = RouteData::new(app, None, None);
let transition_route = TransitionRouteData::new(app);
let modal_route = ModalRouteData::new(app);
app.create(PageRouteBuilder {
route,
overlay_route: OverlayRouteData::new(),
transition_route,
local_history: LocalHistoryRouteData::new(),
modal_route,
page_route: PageRouteData::new(),
page_builder,
transitions_builder: Rc::new(default_transitions_builder),
transition_duration: Duration::from_millis(300),
reverse_transition_duration: Duration::from_millis(300),
opaque: true,
barrier_dismissible: false,
barrier_color: None,
barrier_label: None,
maintain_state: true,
})
}
pub fn settings(self: Ref<Self>, app: &mut App, settings: RouteSettingsRef) -> Ref<Self> {
self.route_data_mut(app).set_settings(settings);
self
}
pub fn request_focus(self: Ref<Self>, app: &mut App, request_focus: bool) -> Ref<Self> {
self.route_data_mut(app)
.set_request_focus(Some(request_focus));
self
}
pub fn transitions_builder(
self: Ref<Self>,
app: &mut App,
transitions_builder: RouteTransitionsBuilder,
) -> Ref<Self> {
app.get_mut(&self).transitions_builder = transitions_builder;
self
}
pub fn transition_duration(
self: Ref<Self>,
app: &mut App,
transition_duration: Duration,
) -> Ref<Self> {
app.get_mut(&self).transition_duration = transition_duration;
self
}
pub fn reverse_transition_duration(
self: Ref<Self>,
app: &mut App,
reverse_transition_duration: Duration,
) -> Ref<Self> {
app.get_mut(&self).reverse_transition_duration = reverse_transition_duration;
self
}
pub fn opaque(self: Ref<Self>, app: &mut App, opaque: bool) -> Ref<Self> {
app.get_mut(&self).opaque = opaque;
self
}
pub fn barrier_dismissible(
self: Ref<Self>,
app: &mut App,
barrier_dismissible: bool,
) -> Ref<Self> {
app.get_mut(&self).barrier_dismissible = barrier_dismissible;
self
}
pub fn barrier_color(self: Ref<Self>, app: &mut App, barrier_color: Color) -> Ref<Self> {
app.get_mut(&self).barrier_color = Some(barrier_color);
self
}
pub fn barrier_label(self: Ref<Self>, app: &mut App, barrier_label: String) -> Ref<Self> {
app.get_mut(&self).barrier_label = Some(barrier_label);
self
}
pub fn maintain_state(self: Ref<Self>, app: &mut App, maintain_state: bool) -> Ref<Self> {
app.get_mut(&self).maintain_state = maintain_state;
self
}
pub fn fullscreen_dialog(self: Ref<Self>, app: &mut App, fullscreen_dialog: bool) -> Ref<Self> {
self.page_route_data_mut(app).fullscreen_dialog = fullscreen_dialog;
self
}
pub fn allow_snapshotting(
self: Ref<Self>,
app: &mut App,
allow_snapshotting: bool,
) -> Ref<Self> {
self.page_route_data_mut(app).allow_snapshotting = allow_snapshotting;
self
}
}
impl Route for PageRouteBuilder {
crate::page_route_overrides!();
}
impl OverlayRoute for PageRouteBuilder {
crate::modal_overlay_route_overrides!();
}
impl PredictiveBackRoute for PageRouteBuilder {
crate::page_predictive_back_route_overrides!();
}
impl TransitionRoute for PageRouteBuilder {
crate::page_transition_route_overrides!();
fn transition_duration(self: Handle<Self>, app: &mut App) -> Duration {
app.get(self).transition_duration
}
fn reverse_transition_duration(self: Handle<Self>, app: &mut App) -> Duration {
app.get(self).reverse_transition_duration
}
fn opaque(self: Handle<Self>, app: &App) -> bool {
app.get(self).opaque
}
}
impl LocalHistoryRoute for PageRouteBuilder {
crate::local_history_route_accessors!();
}
impl ModalRoute for PageRouteBuilder {
crate::modal_route_accessors!();
fn barrier_dismissible(self: Handle<Self>, app: &App) -> bool {
app.get(self).barrier_dismissible
}
fn barrier_color(self: Handle<Self>, app: &App) -> Option<Color> {
app.get(self).barrier_color
}
fn barrier_label(self: Handle<Self>, app: &App) -> Option<String> {
app.get(self).barrier_label.clone()
}
fn maintain_state(self: Handle<Self>, app: &App) -> bool {
app.get(self).maintain_state
}
fn fullscreen_dialog(self: Handle<Self>, app: &App) -> bool {
PageRoute::fullscreen_dialog(self, app)
}
fn build_page(
self: Handle<Self>,
app: &mut App,
context: BuildContext,
animation: AnimationRef<f64>,
secondary_animation: AnimationRef<f64>,
) -> WidgetRef {
let page_builder = Rc::clone(&app.get(self).page_builder);
page_builder(app, context, animation, secondary_animation)
}
fn build_transitions(
self: Handle<Self>,
app: &mut App,
context: BuildContext,
animation: AnimationRef<f64>,
secondary_animation: AnimationRef<f64>,
child: WidgetRef,
) -> WidgetRef {
let transitions_builder = Rc::clone(&app.get(self).transitions_builder);
transitions_builder(app, context, animation, secondary_animation, child)
}
}
impl PageRoute for PageRouteBuilder {
crate::page_route_accessors!();
}