use std::fmt::Debug;
use waft_foundation::{
App, ChangeNotifier, ChangeNotifierData, DateTime, Handle, Listenable, Listener,
};
use waft_services::{RestorationData, TextEditingValue};
use crate::widgets::editable_text::{TextEditingController, TextEditingControllerLeaf};
use crate::widgets::restoration::{RestorableProperty, RestorablePropertyData};
pub struct RestorableValueData<T> {
value: Option<T>,
}
impl<T> RestorableValueData<T> {
pub fn new() -> RestorableValueData<T> {
RestorableValueData { value: None }
}
}
impl<T> Default for RestorableValueData<T> {
fn default() -> RestorableValueData<T> {
RestorableValueData::new()
}
}
#[macro_export]
macro_rules! restorable_value_accessors {
() => {
fn restorable_value_data(
self: ::waft_foundation::Handle<Self>,
app: &::waft_foundation::App,
) -> &$crate::RestorableValueData<Self::Value> {
&app.get(self).value
}
fn restorable_value_data_mut(
self: ::waft_foundation::Handle<Self>,
app: &mut ::waft_foundation::App,
) -> &mut $crate::RestorableValueData<Self::Value> {
&mut app.get_mut(self).value
}
};
}
pub trait RestorableValue: RestorableProperty<Value: Clone + PartialEq> {
fn restorable_value_data(self: Handle<Self>, app: &App) -> &RestorableValueData<Self::Value>;
fn restorable_value_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut RestorableValueData<Self::Value>;
fn value(self: Handle<Self>, app: &App) -> &Self::Value {
debug_assert!(RestorableProperty::is_registered(self, app));
self.restorable_value_data(app)
.value
.as_ref()
.expect("init_with_value runs at registration")
}
fn set_value(self: Handle<Self>, app: &mut App, new_value: Self::Value) {
debug_assert!(RestorableProperty::is_registered(self, app));
debug_assert!(self.debug_assert_valid_value(app, &new_value));
if self.restorable_value_data(app).value.as_ref() != Some(&new_value) {
let old_value = self.restorable_value_data_mut(app).value.replace(new_value);
RestorableValue::did_update_value(self, app, old_value);
}
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: Self::Value) {
self.restorable_value_data_mut(app).value = Some(value);
}
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<Self::Value>);
fn debug_assert_valid_value(self: Handle<Self>, app: &App, new_value: &Self::Value) -> bool {
let _ = (self, app, new_value);
true
}
}
pub trait RestorablePrimitive: Clone + PartialEq + 'static {
fn from_restoration_data(data: &RestorationData) -> Self;
fn to_restoration_data(&self) -> RestorationData;
}
impl RestorablePrimitive for bool {
fn from_restoration_data(data: &RestorationData) -> bool {
data.as_bool()
.expect("the restoration data of a bool property is a bool")
}
fn to_restoration_data(&self) -> RestorationData {
RestorationData::Bool(*self)
}
}
impl RestorablePrimitive for i64 {
fn from_restoration_data(data: &RestorationData) -> i64 {
data.as_int()
.expect("the restoration data of an int property is an int")
}
fn to_restoration_data(&self) -> RestorationData {
RestorationData::Int(*self)
}
}
impl RestorablePrimitive for f64 {
fn from_restoration_data(data: &RestorationData) -> f64 {
data.as_double()
.expect("the restoration data of a double property is a double")
}
fn to_restoration_data(&self) -> RestorationData {
RestorationData::Double(*self)
}
}
impl RestorablePrimitive for String {
fn from_restoration_data(data: &RestorationData) -> String {
data.as_str()
.expect("the restoration data of a String property is a String")
.to_string()
}
fn to_restoration_data(&self) -> RestorationData {
RestorationData::String(self.clone())
}
}
impl<T: RestorablePrimitive> RestorablePrimitive for Option<T> {
fn from_restoration_data(data: &RestorationData) -> Option<T> {
(!data.is_null()).then(|| T::from_restoration_data(data))
}
fn to_restoration_data(&self) -> RestorationData {
self.as_ref()
.map_or(RestorationData::Null, T::to_restoration_data)
}
}
pub trait RestorableNumValue: RestorablePrimitive {}
impl RestorableNumValue for i64 {}
impl RestorableNumValue for f64 {}
pub struct RestorablePrimitiveValueData<T> {
default_value: T,
}
impl<T> RestorablePrimitiveValueData<T> {
pub fn new(default_value: T) -> RestorablePrimitiveValueData<T> {
RestorablePrimitiveValueData { default_value }
}
}
#[macro_export]
macro_rules! restorable_primitive_value_accessors {
() => {
fn restorable_primitive_value_data(
self: ::waft_foundation::Handle<Self>,
app: &::waft_foundation::App,
) -> &$crate::RestorablePrimitiveValueData<Self::Value> {
&app.get(self).primitive
}
fn restorable_primitive_value_data_mut(
self: ::waft_foundation::Handle<Self>,
app: &mut ::waft_foundation::App,
) -> &mut $crate::RestorablePrimitiveValueData<Self::Value> {
&mut app.get_mut(self).primitive
}
};
}
pub trait RestorablePrimitiveValueN: RestorableValue<Value: RestorablePrimitive> {
fn restorable_primitive_value_data(
self: Handle<Self>,
app: &App,
) -> &RestorablePrimitiveValueData<Self::Value>;
fn restorable_primitive_value_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut RestorablePrimitiveValueData<Self::Value>;
fn create_default_value(self: Handle<Self>, app: &mut App) -> Self::Value {
self.restorable_primitive_value_data(app)
.default_value
.clone()
}
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<Self::Value>) {
let _ = old_value;
self.notify_listeners(app);
}
fn from_primitives(self: Handle<Self>, app: &mut App, data: &RestorationData) -> Self::Value {
let _ = app;
Self::Value::from_restoration_data(data)
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorableValue::value(self, app).to_restoration_data()
}
}
pub trait RestorablePrimitiveValue: RestorablePrimitiveValueN {}
pub struct RestorableNum<T: RestorableNumValue> {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
value: RestorableValueData<T>,
primitive: RestorablePrimitiveValueData<T>,
}
impl<T: RestorableNumValue> RestorableNum<T> {
pub fn new(app: &mut App, default_value: T) -> Handle<RestorableNum<T>> {
app.create(RestorableNum {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
value: RestorableValueData::new(),
primitive: RestorablePrimitiveValueData::new(default_value),
})
}
}
impl<T: RestorableNumValue> ChangeNotifier for RestorableNum<T> {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl<T: RestorableNumValue> RestorableProperty for RestorableNum<T> {
type Value = T;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> T {
RestorablePrimitiveValueN::create_default_value(self, app)
}
fn from_primitives(self: Handle<Self>, app: &mut App, data: &RestorationData) -> T {
RestorablePrimitiveValueN::from_primitives(self, app, data)
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: T) {
RestorableValue::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorablePrimitiveValueN::to_primitives(self, app)
}
}
impl<T: RestorableNumValue> RestorableValue for RestorableNum<T> {
crate::restorable_value_accessors!();
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<T>) {
RestorablePrimitiveValueN::did_update_value(self, app, old_value);
}
}
impl<T: RestorableNumValue> RestorablePrimitiveValueN for RestorableNum<T> {
crate::restorable_primitive_value_accessors!();
}
impl<T: RestorableNumValue> RestorablePrimitiveValue for RestorableNum<T> {}
pub type RestorableDouble = RestorableNum<f64>;
pub type RestorableInt = RestorableNum<i64>;
pub struct RestorableString {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
value: RestorableValueData<String>,
primitive: RestorablePrimitiveValueData<String>,
}
impl RestorableString {
pub fn new(app: &mut App, default_value: impl Into<String>) -> Handle<RestorableString> {
app.create(RestorableString {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
value: RestorableValueData::new(),
primitive: RestorablePrimitiveValueData::new(default_value.into()),
})
}
}
impl ChangeNotifier for RestorableString {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl RestorableProperty for RestorableString {
type Value = String;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> String {
RestorablePrimitiveValueN::create_default_value(self, app)
}
fn from_primitives(self: Handle<Self>, app: &mut App, data: &RestorationData) -> String {
RestorablePrimitiveValueN::from_primitives(self, app, data)
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: String) {
RestorableValue::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorablePrimitiveValueN::to_primitives(self, app)
}
}
impl RestorableValue for RestorableString {
crate::restorable_value_accessors!();
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<String>) {
RestorablePrimitiveValueN::did_update_value(self, app, old_value);
}
}
impl RestorablePrimitiveValueN for RestorableString {
crate::restorable_primitive_value_accessors!();
}
impl RestorablePrimitiveValue for RestorableString {}
pub struct RestorableBool {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
value: RestorableValueData<bool>,
primitive: RestorablePrimitiveValueData<bool>,
}
impl RestorableBool {
pub fn new(app: &mut App, default_value: bool) -> Handle<RestorableBool> {
app.create(RestorableBool {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
value: RestorableValueData::new(),
primitive: RestorablePrimitiveValueData::new(default_value),
})
}
}
impl ChangeNotifier for RestorableBool {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl RestorableProperty for RestorableBool {
type Value = bool;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> bool {
RestorablePrimitiveValueN::create_default_value(self, app)
}
fn from_primitives(self: Handle<Self>, app: &mut App, data: &RestorationData) -> bool {
RestorablePrimitiveValueN::from_primitives(self, app, data)
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: bool) {
RestorableValue::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorablePrimitiveValueN::to_primitives(self, app)
}
}
impl RestorableValue for RestorableBool {
crate::restorable_value_accessors!();
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<bool>) {
RestorablePrimitiveValueN::did_update_value(self, app, old_value);
}
}
impl RestorablePrimitiveValueN for RestorableBool {
crate::restorable_primitive_value_accessors!();
}
impl RestorablePrimitiveValue for RestorableBool {}
pub struct RestorableBoolN {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
value: RestorableValueData<Option<bool>>,
primitive: RestorablePrimitiveValueData<Option<bool>>,
}
impl RestorableBoolN {
pub fn new(app: &mut App, default_value: Option<bool>) -> Handle<RestorableBoolN> {
app.create(RestorableBoolN {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
value: RestorableValueData::new(),
primitive: RestorablePrimitiveValueData::new(default_value),
})
}
}
impl ChangeNotifier for RestorableBoolN {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl RestorableProperty for RestorableBoolN {
type Value = Option<bool>;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> Option<bool> {
RestorablePrimitiveValueN::create_default_value(self, app)
}
fn from_primitives(self: Handle<Self>, app: &mut App, data: &RestorationData) -> Option<bool> {
RestorablePrimitiveValueN::from_primitives(self, app, data)
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: Option<bool>) {
RestorableValue::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorablePrimitiveValueN::to_primitives(self, app)
}
}
impl RestorableValue for RestorableBoolN {
crate::restorable_value_accessors!();
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<Option<bool>>) {
RestorablePrimitiveValueN::did_update_value(self, app, old_value);
}
}
impl RestorablePrimitiveValueN for RestorableBoolN {
crate::restorable_primitive_value_accessors!();
}
pub struct RestorableNumN<T: RestorableNumValue> {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
value: RestorableValueData<Option<T>>,
primitive: RestorablePrimitiveValueData<Option<T>>,
}
impl<T: RestorableNumValue> RestorableNumN<T> {
pub fn new(app: &mut App, default_value: Option<T>) -> Handle<RestorableNumN<T>> {
app.create(RestorableNumN {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
value: RestorableValueData::new(),
primitive: RestorablePrimitiveValueData::new(default_value),
})
}
}
impl<T: RestorableNumValue> ChangeNotifier for RestorableNumN<T> {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl<T: RestorableNumValue> RestorableProperty for RestorableNumN<T> {
type Value = Option<T>;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> Option<T> {
RestorablePrimitiveValueN::create_default_value(self, app)
}
fn from_primitives(self: Handle<Self>, app: &mut App, data: &RestorationData) -> Option<T> {
RestorablePrimitiveValueN::from_primitives(self, app, data)
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: Option<T>) {
RestorableValue::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorablePrimitiveValueN::to_primitives(self, app)
}
}
impl<T: RestorableNumValue> RestorableValue for RestorableNumN<T> {
crate::restorable_value_accessors!();
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<Option<T>>) {
RestorablePrimitiveValueN::did_update_value(self, app, old_value);
}
}
impl<T: RestorableNumValue> RestorablePrimitiveValueN for RestorableNumN<T> {
crate::restorable_primitive_value_accessors!();
}
pub type RestorableDoubleN = RestorableNumN<f64>;
pub type RestorableIntN = RestorableNumN<i64>;
pub struct RestorableStringN {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
value: RestorableValueData<Option<String>>,
primitive: RestorablePrimitiveValueData<Option<String>>,
}
impl RestorableStringN {
pub fn new(app: &mut App, default_value: Option<String>) -> Handle<RestorableStringN> {
app.create(RestorableStringN {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
value: RestorableValueData::new(),
primitive: RestorablePrimitiveValueData::new(default_value),
})
}
}
impl ChangeNotifier for RestorableStringN {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl RestorableProperty for RestorableStringN {
type Value = Option<String>;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> Option<String> {
RestorablePrimitiveValueN::create_default_value(self, app)
}
fn from_primitives(
self: Handle<Self>,
app: &mut App,
data: &RestorationData,
) -> Option<String> {
RestorablePrimitiveValueN::from_primitives(self, app, data)
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: Option<String>) {
RestorableValue::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorablePrimitiveValueN::to_primitives(self, app)
}
}
impl RestorableValue for RestorableStringN {
crate::restorable_value_accessors!();
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<Option<String>>) {
RestorablePrimitiveValueN::did_update_value(self, app, old_value);
}
}
impl RestorablePrimitiveValueN for RestorableStringN {
crate::restorable_primitive_value_accessors!();
}
pub struct RestorableDateTime {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
value: RestorableValueData<DateTime>,
default_value: DateTime,
}
impl RestorableDateTime {
pub fn new(app: &mut App, default_value: DateTime) -> Handle<RestorableDateTime> {
app.create(RestorableDateTime {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
value: RestorableValueData::new(),
default_value,
})
}
}
impl ChangeNotifier for RestorableDateTime {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl RestorableProperty for RestorableDateTime {
type Value = DateTime;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> DateTime {
app.get(self).default_value
}
fn from_primitives(self: Handle<Self>, app: &mut App, data: &RestorationData) -> DateTime {
let _ = app;
DateTime::from_milliseconds_since_epoch(
data.as_int()
.expect("the restoration data of a DateTime property is an int"),
)
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: DateTime) {
RestorableValue::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorationData::Int(RestorableValue::value(self, app).milliseconds_since_epoch())
}
}
impl RestorableValue for RestorableDateTime {
crate::restorable_value_accessors!();
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<DateTime>) {
let _ = old_value;
self.notify_listeners(app);
}
}
pub struct RestorableDateTimeN {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
value: RestorableValueData<Option<DateTime>>,
default_value: Option<DateTime>,
}
impl RestorableDateTimeN {
pub fn new(app: &mut App, default_value: Option<DateTime>) -> Handle<RestorableDateTimeN> {
app.create(RestorableDateTimeN {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
value: RestorableValueData::new(),
default_value,
})
}
}
impl ChangeNotifier for RestorableDateTimeN {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl RestorableProperty for RestorableDateTimeN {
type Value = Option<DateTime>;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> Option<DateTime> {
app.get(self).default_value
}
fn from_primitives(
self: Handle<Self>,
app: &mut App,
data: &RestorationData,
) -> Option<DateTime> {
let _ = app;
data.as_int().map(DateTime::from_milliseconds_since_epoch)
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: Option<DateTime>) {
RestorableValue::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorableValue::value(self, app).map_or(RestorationData::Null, |value| {
RestorationData::Int(value.milliseconds_since_epoch())
})
}
}
impl RestorableValue for RestorableDateTimeN {
crate::restorable_value_accessors!();
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<Option<DateTime>>) {
let _ = old_value;
self.notify_listeners(app);
}
}
fn enum_name<T: Debug>(value: &T) -> String {
format!("{value:?}")
}
pub struct RestorableEnum<T: Copy + Debug + PartialEq + 'static> {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
value: RestorableValueData<T>,
default_value: T,
values: Vec<T>,
}
impl<T: Copy + Debug + PartialEq + 'static> RestorableEnum<T> {
pub fn new(
app: &mut App,
default_value: T,
values: impl IntoIterator<Item = T>,
) -> Handle<RestorableEnum<T>> {
let values: Vec<T> = values.into_iter().collect();
assert!(
values.contains(&default_value),
"Default value {default_value:?} not found in the values: {values:?}"
);
app.create(RestorableEnum {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
value: RestorableValueData::new(),
default_value,
values,
})
}
pub fn values(self: Handle<Self>, app: &App) -> &[T] {
&app.get(self).values
}
pub fn set_values(self: Handle<Self>, app: &mut App, values: impl IntoIterator<Item = T>) {
app.get_mut(self).values = values.into_iter().collect();
}
}
impl<T: Copy + Debug + PartialEq + 'static> ChangeNotifier for RestorableEnum<T> {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl<T: Copy + Debug + PartialEq + 'static> RestorableProperty for RestorableEnum<T> {
type Value = T;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> T {
app.get(self).default_value
}
fn from_primitives(self: Handle<Self>, app: &mut App, data: &RestorationData) -> T {
if let Some(name) = data.as_str() {
if let Some(allowed) = app
.get(self)
.values
.iter()
.find(|value| enum_name(value) == name)
{
return *allowed;
}
debug_assert!(
false,
"Attempted to restore an unknown enum value \"{name}\" that is not in the valid \
set of enum values: {:?}",
app.get(self).values
);
}
app.get(self).default_value
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: T) {
RestorableValue::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorationData::String(enum_name(RestorableValue::value(self, app)))
}
}
impl<T: Copy + Debug + PartialEq + 'static> RestorableValue for RestorableEnum<T> {
crate::restorable_value_accessors!();
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<T>) {
let _ = old_value;
self.notify_listeners(app);
}
fn debug_assert_valid_value(self: Handle<Self>, app: &App, new_value: &T) -> bool {
assert!(
app.get(self).values.contains(new_value),
"Attempted to set an unknown enum value \"{new_value:?}\" that is not in the valid \
set of enum values: {:?}",
app.get(self).values
);
true
}
}
pub struct RestorableEnumN<T: Copy + Debug + PartialEq + 'static> {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
value: RestorableValueData<Option<T>>,
default_value: Option<T>,
values: Vec<T>,
}
impl<T: Copy + Debug + PartialEq + 'static> RestorableEnumN<T> {
pub fn new(
app: &mut App,
default_value: Option<T>,
values: impl IntoIterator<Item = T>,
) -> Handle<RestorableEnumN<T>> {
let values: Vec<T> = values.into_iter().collect();
assert!(
default_value.is_none_or(|default| values.contains(&default)),
"Default value {default_value:?} not found in the values: {values:?}"
);
app.create(RestorableEnumN {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
value: RestorableValueData::new(),
default_value,
values,
})
}
pub fn values(self: Handle<Self>, app: &App) -> &[T] {
&app.get(self).values
}
pub fn set_values(self: Handle<Self>, app: &mut App, values: impl IntoIterator<Item = T>) {
app.get_mut(self).values = values.into_iter().collect();
}
}
impl<T: Copy + Debug + PartialEq + 'static> ChangeNotifier for RestorableEnumN<T> {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl<T: Copy + Debug + PartialEq + 'static> RestorableProperty for RestorableEnumN<T> {
type Value = Option<T>;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> Option<T> {
app.get(self).default_value
}
fn from_primitives(self: Handle<Self>, app: &mut App, data: &RestorationData) -> Option<T> {
if data.is_null() {
return None;
}
if let Some(name) = data.as_str() {
if let Some(allowed) = app
.get(self)
.values
.iter()
.find(|value| enum_name(value) == name)
{
return Some(*allowed);
}
debug_assert!(
false,
"Attempted to set an unknown enum value \"{name}\" that is not null, or in the \
valid set of enum values: {:?}",
app.get(self).values
);
}
app.get(self).default_value
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: Option<T>) {
RestorableValue::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorableValue::value(self, app)
.as_ref()
.map_or(RestorationData::Null, |value| {
RestorationData::String(enum_name(value))
})
}
}
impl<T: Copy + Debug + PartialEq + 'static> RestorableValue for RestorableEnumN<T> {
crate::restorable_value_accessors!();
fn did_update_value(self: Handle<Self>, app: &mut App, old_value: Option<Option<T>>) {
let _ = old_value;
self.notify_listeners(app);
}
fn debug_assert_valid_value(self: Handle<Self>, app: &App, new_value: &Option<T>) -> bool {
assert!(
new_value.is_none_or(|value| app.get(self).values.contains(&value)),
"Attempted to set an unknown enum value \"{new_value:?}\" that is not null, or in \
the valid set of enum values: {:?}",
app.get(self).values
);
true
}
}
pub struct RestorableListenableData<T> {
value: Option<T>,
}
impl<T> RestorableListenableData<T> {
pub fn new() -> RestorableListenableData<T> {
RestorableListenableData { value: None }
}
}
impl<T> Default for RestorableListenableData<T> {
fn default() -> RestorableListenableData<T> {
RestorableListenableData::new()
}
}
#[macro_export]
macro_rules! restorable_listenable_accessors {
() => {
fn restorable_listenable_data(
self: ::waft_foundation::Handle<Self>,
app: &::waft_foundation::App,
) -> &$crate::RestorableListenableData<Self::Value> {
&app.get(self).listenable
}
fn restorable_listenable_data_mut(
self: ::waft_foundation::Handle<Self>,
app: &mut ::waft_foundation::App,
) -> &mut $crate::RestorableListenableData<Self::Value> {
&mut app.get_mut(self).listenable
}
};
}
pub trait RestorableListenable: RestorableProperty<Value: Listenable + Clone + 'static> {
fn restorable_listenable_data(
self: Handle<Self>,
app: &App,
) -> &RestorableListenableData<Self::Value>;
fn restorable_listenable_data_mut(
self: Handle<Self>,
app: &mut App,
) -> &mut RestorableListenableData<Self::Value>;
fn value(self: Handle<Self>, app: &App) -> Self::Value {
debug_assert!(RestorableProperty::is_registered(self, app));
self.restorable_listenable_data(app)
.value
.clone()
.expect("init_with_value runs at registration")
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: Self::Value) {
let listener = self.notification_listener();
if let Some(old_value) = self.restorable_listenable_data(app).value.clone() {
old_value.remove_listener(app, &listener);
}
self.restorable_listenable_data_mut(app).value = Some(value.clone());
value.add_listener(app, listener);
}
fn dispose(self: Handle<Self>, app: &mut App) {
RestorableProperty::dispose_property(self, app);
let listener = self.notification_listener();
if let Some(value) = self.restorable_listenable_data(app).value.clone() {
value.remove_listener(app, &listener);
}
}
fn notification_listener(self: Handle<Self>) -> Listener {
Listener::handle_method(self, Self::forward_notification)
}
fn forward_notification(self: Handle<Self>, app: &mut App) {
self.notify_listeners(app);
}
}
pub trait RestorableChangeNotifier: RestorableListenable {
fn dispose_value(self: Handle<Self>, app: &mut App, value: Self::Value);
fn init_with_value(self: Handle<Self>, app: &mut App, value: Self::Value) {
self.dispose_old_value(app);
RestorableListenable::init_with_value(self, app, value);
}
fn dispose(self: Handle<Self>, app: &mut App) {
self.dispose_old_value(app);
RestorableListenable::dispose(self, app);
}
fn dispose_old_value(self: Handle<Self>, app: &mut App) {
if let Some(value) = self.restorable_listenable_data(app).value.clone() {
app.schedule_microtask(Listener::new(move |app| {
Self::dispose_value(self, app, value.clone());
}));
}
}
}
pub struct RestorableTextEditingController {
change_notifier: ChangeNotifierData,
property: RestorablePropertyData,
listenable: RestorableListenableData<Handle<TextEditingController>>,
initial_value: TextEditingValue,
}
impl RestorableTextEditingController {
pub fn new(app: &mut App) -> Handle<RestorableTextEditingController> {
Self::from_value(app, TextEditingValue::EMPTY)
}
pub fn text(app: &mut App, text: impl Into<String>) -> Handle<RestorableTextEditingController> {
Self::from_value(app, TextEditingValue::new().text(text))
}
pub fn from_value(
app: &mut App,
value: TextEditingValue,
) -> Handle<RestorableTextEditingController> {
app.create(RestorableTextEditingController {
change_notifier: ChangeNotifierData::new(),
property: RestorablePropertyData::new(),
listenable: RestorableListenableData::new(),
initial_value: value,
})
}
}
impl ChangeNotifier for RestorableTextEditingController {
fn change_notifier_data(&self) -> &ChangeNotifierData {
&self.change_notifier
}
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData {
&mut self.change_notifier
}
}
impl RestorableProperty for RestorableTextEditingController {
type Value = Handle<TextEditingController>;
crate::restorable_property_accessors!();
fn create_default_value(self: Handle<Self>, app: &mut App) -> Handle<TextEditingController> {
let initial_value = app.get(self).initial_value.clone();
TextEditingController::from_value(app, Some(initial_value))
}
fn from_primitives(
self: Handle<Self>,
app: &mut App,
data: &RestorationData,
) -> Handle<TextEditingController> {
TextEditingController::new(app).text(
app,
data.as_str()
.expect("RestorableTextEditingController stores a String"),
)
}
fn init_with_value(self: Handle<Self>, app: &mut App, value: Handle<TextEditingController>) {
RestorableChangeNotifier::init_with_value(self, app, value);
}
fn to_primitives(self: Handle<Self>, app: &App) -> RestorationData {
RestorationData::String(
RestorableListenable::value(self, app)
.text_value(app)
.to_string(),
)
}
fn dispose(self: Handle<Self>, app: &mut App) {
RestorableChangeNotifier::dispose(self, app);
}
}
impl RestorableListenable for RestorableTextEditingController {
crate::restorable_listenable_accessors!();
}
impl RestorableChangeNotifier for RestorableTextEditingController {
fn dispose_value(self: Handle<Self>, app: &mut App, value: Handle<TextEditingController>) {
app.get_mut(value).dispose();
}
}
#[cfg(test)]
mod tests {
use std::rc::Rc;
use waft_foundation::AppCell;
use waft_foundation::Handle;
use waft_services::{RestorationBucket, RestorationManager, RestorationMap};
use super::*;
use crate::framework::{BuildContext, GlobalKey, KeyRef, StateData, StatefulWidget, WidgetRef};
use crate::test_harness::Harness;
use crate::widgets::basic::SizedBox;
use crate::widgets::restoration::{
RestorationMixin, RestorationMixinData, RootRestorationScope,
};
use crate::{IntoWidget, State};
#[derive(Clone, Copy, Debug, PartialEq)]
enum Direction {
Up,
Down,
}
const DIRECTIONS: [Direction; 2] = [Direction::Up, Direction::Down];
fn map<const N: usize>(entries: [(&str, RestorationData); N]) -> RestorationMap {
entries
.into_iter()
.map(|(key, value)| (RestorationData::from(key), value))
.collect()
}
fn values<const N: usize>(entries: [(&str, RestorationData); N]) -> RestorationMap {
map([("v", map(entries).into())])
}
fn child(restoration_id: &str, data: RestorationMap) -> RestorationMap {
map([("c", map([(restoration_id, data.into())]).into())])
}
#[derive(Clone, Copy)]
struct Properties {
int: Handle<RestorableInt>,
double: Handle<RestorableDouble>,
string: Handle<RestorableString>,
boolean: Handle<RestorableBool>,
int_n: Handle<RestorableIntN>,
double_n: Handle<RestorableDoubleN>,
string_n: Handle<RestorableStringN>,
bool_n: Handle<RestorableBoolN>,
date_time: Handle<RestorableDateTime>,
date_time_n: Handle<RestorableDateTimeN>,
direction: Handle<RestorableEnum<Direction>>,
direction_n: Handle<RestorableEnumN<Direction>>,
}
impl Properties {
fn create(app: &mut App) -> Properties {
Properties {
int: RestorableInt::new(app, 1),
double: RestorableDouble::new(app, 1.5),
string: RestorableString::new(app, "one"),
boolean: RestorableBool::new(app, true),
int_n: RestorableIntN::new(app, Some(2)),
double_n: RestorableDoubleN::new(app, None),
string_n: RestorableStringN::new(app, None),
bool_n: RestorableBoolN::new(app, Some(false)),
date_time: RestorableDateTime::new(
app,
DateTime::from_milliseconds_since_epoch(1000),
),
date_time_n: RestorableDateTimeN::new(app, None),
direction: RestorableEnum::new(app, Direction::Up, DIRECTIONS),
direction_n: RestorableEnumN::new(app, None, DIRECTIONS),
}
}
fn register(self, state: Handle<BagState>, app: &mut App) {
state.register_for_restoration(app, self.int.as_property(), "int");
state.register_for_restoration(app, self.double.as_property(), "double");
state.register_for_restoration(app, self.string.as_property(), "string");
state.register_for_restoration(app, self.boolean.as_property(), "bool");
state.register_for_restoration(app, self.int_n.as_property(), "int-n");
state.register_for_restoration(app, self.double_n.as_property(), "double-n");
state.register_for_restoration(app, self.string_n.as_property(), "string-n");
state.register_for_restoration(app, self.bool_n.as_property(), "bool-n");
state.register_for_restoration(app, self.date_time.as_property(), "date-time");
state.register_for_restoration(app, self.date_time_n.as_property(), "date-time-n");
state.register_for_restoration(app, self.direction.as_property(), "direction");
state.register_for_restoration(app, self.direction_n.as_property(), "direction-n");
}
}
#[derive(Debug)]
struct Bag {
key: Option<KeyRef>,
}
impl StatefulWidget for Bag {
type State = BagState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> BagState {
BagState {
state: StateData::new(),
restoration: RestorationMixinData::new(),
properties: None,
}
}
}
struct BagState {
state: StateData<Bag>,
restoration: RestorationMixinData,
properties: Option<Properties>,
}
impl RestorationMixin for BagState {
crate::restoration_mixin_accessors!();
fn restoration_id(self: Handle<Self>, _app: &App) -> Option<&str> {
Some("bag")
}
fn restore_state(
self: Handle<Self>,
app: &mut App,
_old_bucket: Option<Handle<RestorationBucket>>,
_initial_restore: bool,
) {
let properties = match app.get(self).properties {
Some(properties) => properties,
None => {
let properties = Properties::create(app);
app.get_mut(self).properties = Some(properties);
properties
}
};
properties.register(self, app);
}
}
impl State for BagState {
type Widget = Bag;
crate::state_accessors!();
fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
self.did_change_dependencies_restoration(app);
}
fn dispose(self: Handle<Self>, app: &mut App) {
self.dispose_restoration(app);
}
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
let _ = app;
SizedBox::shrink().into_widget()
}
}
struct Fixture {
cell: Rc<AppCell>,
properties: Properties,
bucket: Handle<RestorationBucket>,
}
impl Fixture {
fn mount(data: Option<RestorationMap>) -> Fixture {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let manager = RestorationManager::instance(&mut app);
manager.handle_restoration_update_from_engine(&mut app, true, data);
let key = GlobalKey::new();
let harness = Harness::mount(
&mut app,
RootRestorationScope::new(
Some("app".to_string()),
Bag {
key: Some(Rc::new(key.clone())),
},
)
.into_widget(),
);
harness.pump(&mut app);
let state = harness
.owner
.global_key_element(&app, key.identity())
.expect("the bag is in the tree")
.state_handle::<BagState>(&app)
.expect("a BagState");
let properties = app.get(state).properties.expect("restore_state ran");
let bucket = state.bucket(&app).expect("the bag claimed a bucket");
drop(app);
Fixture {
cell,
properties,
bucket,
}
}
fn stored(&mut self, restoration_id: &str) -> Option<RestorationData> {
self.bucket
.read(&mut self.cell.borrow_mut(), restoration_id)
}
}
fn filled() -> Option<RestorationMap> {
Some(child(
"app",
child(
"bag",
values([
("int", 7i64.into()),
("double", 2.5f64.into()),
("string", "seven".into()),
("bool", false.into()),
("int-n", RestorationData::Null),
("double-n", 0.5f64.into()),
("string-n", "maybe".into()),
("bool-n", RestorationData::Null),
("date-time", 2000i64.into()),
("date-time-n", 3000i64.into()),
("direction", "Down".into()),
("direction-n", "Up".into()),
]),
),
))
}
#[test]
fn a_property_with_no_stored_value_takes_the_default_it_was_created_with() {
let fixture = Fixture::mount(None);
let properties = fixture.properties;
let app = &mut fixture.cell.borrow_mut();
assert_eq!(*properties.int.value(app), 1);
assert_eq!(*properties.double.value(app), 1.5);
assert_eq!(properties.string.value(app), "one");
assert!(*properties.boolean.value(app));
assert_eq!(*properties.int_n.value(app), Some(2));
assert_eq!(*properties.double_n.value(app), None);
assert_eq!(*properties.string_n.value(app), None);
assert_eq!(*properties.bool_n.value(app), Some(false));
assert_eq!(
*properties.date_time.value(app),
DateTime::from_milliseconds_since_epoch(1000)
);
assert_eq!(*properties.date_time_n.value(app), None);
assert_eq!(*properties.direction.value(app), Direction::Up);
assert_eq!(*properties.direction_n.value(app), None);
}
#[test]
fn a_default_value_is_written_into_the_bucket_at_registration() {
let mut fixture = Fixture::mount(None);
assert_eq!(fixture.stored("int"), Some(1i64.into()));
assert_eq!(fixture.stored("double"), Some(1.5f64.into()));
assert_eq!(fixture.stored("string"), Some("one".into()));
assert_eq!(fixture.stored("bool"), Some(true.into()));
assert_eq!(fixture.stored("int-n"), Some(2i64.into()));
assert_eq!(fixture.stored("double-n"), Some(RestorationData::Null));
assert_eq!(fixture.stored("string-n"), Some(RestorationData::Null));
assert_eq!(fixture.stored("bool-n"), Some(false.into()));
assert_eq!(fixture.stored("date-time"), Some(1000i64.into()));
assert_eq!(fixture.stored("date-time-n"), Some(RestorationData::Null));
assert_eq!(fixture.stored("direction"), Some("Up".into()));
assert_eq!(fixture.stored("direction-n"), Some(RestorationData::Null));
}
#[test]
fn every_property_restores_the_value_the_data_describes() {
let fixture = Fixture::mount(filled());
let properties = fixture.properties;
let app = &mut fixture.cell.borrow_mut();
assert_eq!(*properties.int.value(app), 7);
assert_eq!(*properties.double.value(app), 2.5);
assert_eq!(properties.string.value(app), "seven");
assert!(!*properties.boolean.value(app));
assert_eq!(*properties.int_n.value(app), None);
assert_eq!(*properties.double_n.value(app), Some(0.5));
assert_eq!(properties.string_n.value(app).as_deref(), Some("maybe"));
assert_eq!(*properties.bool_n.value(app), None);
assert_eq!(
*properties.date_time.value(app),
DateTime::from_milliseconds_since_epoch(2000)
);
assert_eq!(
*properties.date_time_n.value(app),
Some(DateTime::from_milliseconds_since_epoch(3000))
);
assert_eq!(*properties.direction.value(app), Direction::Down);
assert_eq!(*properties.direction_n.value(app), Some(Direction::Up));
}
#[test]
fn a_new_value_is_written_back_as_the_data_it_was_restored_from() {
let mut fixture = Fixture::mount(filled());
let properties = fixture.properties;
{
let app = &mut fixture.cell.borrow_mut();
properties.int.set_value(app, 8);
properties.double.set_value(app, 3.5);
properties.string.set_value(app, "eight".to_string());
properties.boolean.set_value(app, true);
properties.int_n.set_value(app, Some(9));
properties.double_n.set_value(app, None);
properties.string_n.set_value(app, Some("nine".to_string()));
properties.bool_n.set_value(app, Some(true));
properties
.date_time
.set_value(app, DateTime::from_milliseconds_since_epoch(4000));
properties.date_time_n.set_value(app, None);
properties.direction.set_value(app, Direction::Up);
properties.direction_n.set_value(app, Some(Direction::Down));
}
assert_eq!(fixture.stored("int"), Some(8i64.into()));
assert_eq!(fixture.stored("double"), Some(3.5f64.into()));
assert_eq!(fixture.stored("string"), Some("eight".into()));
assert_eq!(fixture.stored("bool"), Some(true.into()));
assert_eq!(fixture.stored("int-n"), Some(9i64.into()));
assert_eq!(fixture.stored("double-n"), Some(RestorationData::Null));
assert_eq!(fixture.stored("string-n"), Some("nine".into()));
assert_eq!(fixture.stored("bool-n"), Some(true.into()));
assert_eq!(fixture.stored("date-time"), Some(4000i64.into()));
assert_eq!(fixture.stored("date-time-n"), Some(RestorationData::Null));
assert_eq!(fixture.stored("direction"), Some("Up".into()));
assert_eq!(fixture.stored("direction-n"), Some("Down".into()));
}
#[test]
fn setting_the_value_it_already_has_writes_nothing() {
let fixture = Fixture::mount(filled());
let int = fixture.properties.int;
let notified = Rc::new(std::cell::Cell::new(0usize));
let seen = Rc::clone(¬ified);
int.add_listener(
&mut fixture.cell.borrow_mut(),
Listener::new(move |_app| seen.set(seen.get() + 1)),
);
int.set_value(&mut fixture.cell.borrow_mut(), 7);
assert_eq!(notified.get(), 0, "the value did not change");
int.set_value(&mut fixture.cell.borrow_mut(), 8);
assert_eq!(notified.get(), 1);
}
#[test]
fn an_unregistered_property_is_removed_from_the_data() {
let mut fixture = Fixture::mount(filled());
let int = fixture.properties.int;
let state = int.state(&fixture.cell.borrow());
let bag = state
.downcast::<BagState>(&fixture.cell.borrow())
.expect("the owner is the bag");
bag.unregister_from_restoration(&mut fixture.cell.borrow_mut(), int.as_property());
assert_eq!(fixture.stored("int"), None);
assert!(!int.as_property().is_registered(&fixture.cell.borrow()));
}
#[test]
fn restorable_text_editing_controller_create_default_value_holds_initial_text() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let property = RestorableTextEditingController::from_value(
&mut app,
TextEditingValue::new().text("hello"),
);
let controller = RestorableProperty::create_default_value(property, &mut app);
assert_eq!(controller.text_value(&app), "hello");
let restored = RestorableProperty::from_primitives(
property,
&mut app,
&RestorationData::String("hello".into()),
);
assert_eq!(restored.text_value(&app), "hello");
}
#[derive(Debug)]
struct Editor {
key: Option<KeyRef>,
}
impl StatefulWidget for Editor {
type State = EditorState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> EditorState {
EditorState {
state: StateData::new(),
restoration: RestorationMixinData::new(),
controller: None,
}
}
}
struct EditorState {
state: StateData<Editor>,
restoration: RestorationMixinData,
controller: Option<Handle<RestorableTextEditingController>>,
}
impl RestorationMixin for EditorState {
crate::restoration_mixin_accessors!();
fn restoration_id(self: Handle<Self>, _app: &App) -> Option<&str> {
Some("editor")
}
fn restore_state(
self: Handle<Self>,
app: &mut App,
_old_bucket: Option<Handle<RestorationBucket>>,
_initial_restore: bool,
) {
let controller = match app.get(self).controller {
Some(controller) => controller,
None => {
let controller = RestorableTextEditingController::text(app, "hello");
app.get_mut(self).controller = Some(controller);
controller
}
};
self.register_for_restoration(app, controller.as_property(), "controller");
}
}
impl State for EditorState {
type Widget = Editor;
crate::state_accessors!();
fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
self.did_change_dependencies_restoration(app);
}
fn dispose(self: Handle<Self>, app: &mut App) {
self.dispose_restoration(app);
}
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
let _ = app;
SizedBox::shrink().into_widget()
}
}
#[test]
fn restorable_text_editing_to_primitives_is_the_controller_text() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let manager = RestorationManager::instance(&mut app);
manager.handle_restoration_update_from_engine(&mut app, true, None);
let key = GlobalKey::new();
let harness = Harness::mount(
&mut app,
RootRestorationScope::new(
Some("app".to_string()),
Editor {
key: Some(Rc::new(key.clone())),
},
)
.into_widget(),
);
harness.pump(&mut app);
let state = harness
.owner
.global_key_element(&app, key.identity())
.expect("the editor is in the tree")
.state_handle::<EditorState>(&app)
.expect("an EditorState");
let controller = app.get(state).controller.expect("restore_state ran");
assert_eq!(
RestorableListenable::value(controller, &app).text_value(&app),
"hello"
);
assert_eq!(
RestorableProperty::to_primitives(controller, &app),
RestorationData::String("hello".into())
);
}
}