1use std::cell::RefCell;
6use std::panic::{self, AssertUnwindSafe};
7use std::rc::Rc;
8
9use crabshell_reactive::{ReadSignal, Signal, untrack};
10
11use crate::cx::{Anchor, Cx};
12use crate::input::{Handler, panic_message};
13use crate::keyboard::{KeyEvent, KeyHandler};
14use crate::prop::{IntoProp, Prop, PropKind};
15use crate::style::Style;
16use crate::tree::{Tree, borrow};
17use crate::widgets::Widget;
18use crate::window::{Output, Window};
19
20type Build = Rc<dyn Fn(&Cx, &Output) -> Box<dyn Widget>>;
22
23type OnKey = Rc<dyn Fn(&Cx, &KeyEvent) -> bool>;
25
26type Parts = (Option<Rc<dyn Fn() -> Style>>, Option<Build>, Option<OnKey>);
28
29#[derive(Default)]
43pub struct SessionLock {
44 locked: Option<Prop<bool>>,
45 style: Option<Prop<Style>>,
46 surface: Option<Build>,
47 on_key: Option<OnKey>,
48 refused: Option<Signal<bool>>,
49}
50
51impl SessionLock {
52 pub fn new() -> SessionLock {
54 SessionLock::default()
55 }
56
57 pub fn locked(mut self, locked: impl IntoProp<bool>) -> SessionLock {
60 self.locked = Some(locked.into_prop());
61 self
62 }
63
64 pub fn refused(mut self, refused: Signal<bool>) -> SessionLock {
71 self.refused = Some(refused);
72 self
73 }
74
75 pub fn style(mut self, style: impl IntoProp<Style>) -> SessionLock {
79 self.style = Some(style.into_prop());
80 self
81 }
82
83 pub fn surface<W: Widget>(mut self, build: impl Fn(&Cx, &Output) -> W + 'static) -> SessionLock {
87 self.surface = Some(Rc::new(move |cx: &Cx, output: &Output| {
88 Box::new(build(cx, output)) as Box<dyn Widget>
89 }));
90 self
91 }
92
93 pub fn on_key(mut self, handler: impl Fn(&Cx, &KeyEvent) -> bool + 'static) -> SessionLock {
100 self.on_key = Some(Rc::new(handler));
101 self
102 }
103}
104
105pub struct Lock {
110 cx: Cx,
111 wanted: Signal<bool>,
113 locked: Signal<bool>,
115 refused: Option<Signal<bool>>,
117 parts: Signal<Parts>,
120}
121
122impl Lock {
123 pub fn new(cx: &Cx, lock: SessionLock) -> Lock {
130 let cx = cx.child().named("SessionLock");
131 let wanted = cx.signal(false);
132 match lock.locked.map(|locked| locked.0) {
133 None => {}
134 Some(PropKind::Value(value)) => wanted.set(value),
135 Some(PropKind::Binding(mut compute)) => {
136 cx.effect(move || wanted.set(compute()))
137 .named("SessionLock.locked");
138 }
139 }
140 Lock {
141 wanted,
142 locked: cx.signal(false),
143 refused: lock.refused,
144 parts: cx.signal((lock.style.map(Prop::shared), lock.surface, lock.on_key)),
145 cx,
146 }
147 }
148
149 pub fn wanted(&self) -> Option<bool> {
151 untrack(|| self.wanted.try_get())
152 }
153
154 pub fn locked(&self) -> ReadSignal<bool> {
156 self.locked.read_only()
157 }
158
159 pub fn set_locked(&self, locked: bool) {
162 if untrack(|| self.locked.try_get()) == Some(!locked) {
163 self.locked.set(locked);
164 }
165 }
166
167 pub fn set_refused(&self, refused: bool) {
171 if let Some(signal) = self.refused
172 && untrack(|| signal.try_get()) == Some(!refused)
173 {
174 signal.set(refused);
175 }
176 }
177
178 pub fn surface(&self, output: &Output) -> Option<Window> {
183 let (style, build, on_key) = untrack(|| self.parts.try_with(Clone::clone))?;
184 let path: Rc<str> = format!("SessionLock[{}]", output.name()).into();
185 let cx = self
186 .cx
187 .child()
188 .named(path.to_string())
189 .at(Rc::new(Anchor::default()));
190 let built = panic::catch_unwind(AssertUnwindSafe(|| {
192 untrack(|| build.map(|build| build(&cx, output).into_element()))
193 }));
194 let child = built.unwrap_or_else(|payload| {
195 tracing::error!(
196 "{path} panicked while it was built, so it shows only the lock's style: {}",
197 panic_message(payload.as_ref())
198 );
199 None
200 });
201 let owner = cx.owner();
202 let tree = Rc::new_cyclic(|this| {
203 RefCell::new(Tree::new(
204 this.clone(),
205 "SessionLock",
206 output.clone(),
207 cx,
208 path,
209 ))
210 });
211 let style = style.map(|style| (move || style()).into_prop());
212 let mut mounted = borrow(&tree);
213 mounted.keyboard.handler = on_key.map(|on_key| {
215 Rc::new(Handler::new(move |cx: &Cx, event: &KeyEvent| {
216 on_key(cx, event)
217 })) as KeyHandler
218 });
219 mounted.mount_root(style, child);
220 drop(mounted);
221 Some(Window { tree, owner })
222 }
223}
224
225impl Drop for Lock {
226 fn drop(&mut self) {
227 self.cx.owner().dispose();
228 }
229}