Skip to main content

lingxia_platform/
control_session.rs

1//! The shell's "an AI assistant is in control" indicator, platform side.
2//!
3//! The control runtime decides when a session is running and what Stop does;
4//! it sits above this crate, so it hands the Stop action down as a handler
5//! and each desktop shell calls [`request_control_session_stop`] when the user
6//! presses Stop.
7
8use std::sync::{Arc, Mutex};
9
10pub type ControlSessionStopHandler = Arc<dyn Fn() + Send + Sync>;
11
12static STOP_HANDLER: Mutex<Option<ControlSessionStopHandler>> = Mutex::new(None);
13
14/// Install what the indicator's Stop button does.
15pub fn set_control_session_stop_handler(handler: ControlSessionStopHandler) {
16    *STOP_HANDLER
17        .lock()
18        .unwrap_or_else(|error| error.into_inner()) = Some(handler);
19}
20
21/// The user pressed Stop. Returns whether anything handled it.
22pub fn request_control_session_stop() -> bool {
23    let handler = STOP_HANDLER
24        .lock()
25        .unwrap_or_else(|error| error.into_inner())
26        .clone();
27    match handler {
28        Some(handler) => {
29            handler();
30            true
31        }
32        None => false,
33    }
34}