waft_widgets/widgets/
pop_scope.rs1use crate::widgets::routes::ModalRouteRef;
4use std::rc::Rc;
5
6use waft_foundation::{App, Handle, OptionRetained, ValueListenable, ValueNotifier};
7
8use crate::framework::{
9 BuildContext, IntoWidget, KeyRef, State, StateData, StatefulWidget, WidgetRef,
10};
11use crate::widgets::navigator::RouteResult;
12use crate::widgets::routes::{
13 AnyModalRoute, PopEntry, PopEntryObject, PopInvokedWithResultCallback,
14};
15use waft_foundation::Ref;
16
17pub struct PopScope {
47 pub key: Option<KeyRef>,
48 pub child: WidgetRef,
50 pub on_pop_invoked_with_result: Option<PopInvokedWithResultCallback>,
64 pub can_pop: bool,
71}
72
73impl PopScope {
74 #[cfg_attr(debug_assertions, track_caller)]
79 pub fn new<K>(child: impl IntoWidget<K>) -> PopScope {
80 PopScope {
81 key: None,
82 child: child.into_widget(),
83 on_pop_invoked_with_result: None,
84 can_pop: true,
85 }
86 }
87
88 pub fn key(mut self, key: KeyRef) -> PopScope {
90 self.key = Some(key);
91 self
92 }
93
94 pub fn on_pop_invoked_with_result(
96 mut self,
97 on_pop_invoked_with_result: PopInvokedWithResultCallback,
98 ) -> PopScope {
99 self.on_pop_invoked_with_result = Some(on_pop_invoked_with_result);
100 self
101 }
102
103 pub fn can_pop(mut self, can_pop: bool) -> PopScope {
105 self.can_pop = can_pop;
106 self
107 }
108
109 fn pop_invocation(&self, did_pop: bool, result: RouteResult) -> impl FnOnce(&mut App) + use<> {
111 let callback = self.on_pop_invoked_with_result.clone();
112 move |app: &mut App| {
113 if let Some(callback) = callback {
114 callback(app, did_pop, result);
115 }
116 }
117 }
118}
119
120impl std::fmt::Debug for PopScope {
121 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122 f.debug_struct("PopScope")
123 .field("can_pop", &self.can_pop)
124 .finish_non_exhaustive()
125 }
126}
127
128impl StatefulWidget for PopScope {
129 type State = PopScopeState;
130
131 fn key(&self) -> Option<&KeyRef> {
132 self.key.as_ref()
133 }
134
135 fn create_state(&self) -> PopScopeState {
136 PopScopeState {
137 state: StateData::new(),
138 route: None,
139 can_pop_notifier: None,
140 pop_entry: None,
141 }
142 }
143}
144
145pub struct PopScopeState {
147 state: StateData<PopScope>,
148 route: Option<ModalRouteRef>,
149 can_pop_notifier: Option<Ref<ValueNotifier<bool>>>,
150 pop_entry: Option<Rc<dyn PopEntry>>,
153}
154
155impl PopScopeState {
156 fn pop_entry(self: Handle<Self>, app: &App) -> Rc<dyn PopEntry> {
157 Rc::clone(
158 app.get(self)
159 .pop_entry
160 .as_ref()
161 .expect("init_state has run"),
162 )
163 }
164}
165
166impl PopEntryObject for PopScopeState {
167 fn on_pop_invoked_with_result(
168 self: Handle<Self>,
169 app: &mut App,
170 did_pop: bool,
171 result: RouteResult,
172 ) {
173 let call = self.widget(app).pop_invocation(did_pop, result);
175 call(app);
176 }
177
178 fn can_pop_notifier(self: Handle<Self>, app: &App) -> Rc<dyn ValueListenable<bool>> {
179 Rc::new(
180 app.get(self)
181 .can_pop_notifier
182 .clone()
183 .expect("init_state has run"),
184 )
185 }
186}
187
188impl State for PopScopeState {
189 type Widget = PopScope;
190 crate::state_accessors!();
191
192 fn init_state(self: Handle<Self>, app: &mut App) {
193 let can_pop = self.widget(app).can_pop;
194 app.get_mut(self).can_pop_notifier = Some(app.create(ValueNotifier::new(can_pop)));
195 app.get_mut(self).pop_entry = Some(Rc::new(self));
196 }
197
198 fn did_change_dependencies(self: Handle<Self>, app: &mut App) {
199 let context = self.context(app);
200 let next_route = AnyModalRoute::of(app, context);
201 if next_route != app.get(self).route.handle() {
202 if let Some(route) = app.get(self).route.handle() {
203 route.unregister_pop_entry(app, self.pop_entry(app));
204 }
205 app.get_mut(self).route = next_route.and_then(|route| route.upgrade(app));
206 if let Some(route) = next_route {
207 route.register_pop_entry(app, self.pop_entry(app));
208 }
209 }
210 }
211
212 fn did_update_widget(self: Handle<Self>, app: &mut App, _old_widget: &PopScope) {
213 let can_pop = self.widget(app).can_pop;
214 let notifier = app
215 .get(self)
216 .can_pop_notifier
217 .handle()
218 .expect("init_state has run");
219 notifier.set_value(app, can_pop);
220 }
221
222 fn dispose(self: Handle<Self>, app: &mut App) {
223 if let Some(route) = app.get(self).route.handle() {
224 route.unregister_pop_entry(app, self.pop_entry(app));
225 }
226 let notifier = app
227 .get(self)
228 .can_pop_notifier
229 .handle()
230 .expect("init_state has run");
231 notifier.dispose(app);
232 }
233
234 fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
235 self.widget(app).child.clone()
236 }
237}