use gpui_kit::component::WindowExt as _;
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::{
AnyElement, Context, Div, Entity, EntityId, FocusHandle, Global, InteractiveElement as _,
Stateful, Subscription, Window,
};
use subspace_dashboard::Tokens;
use super::{Quit, QuitAndRestart, SubspaceApp};
use view::{Shown, gate, retries};
mod view;
use crate::account::{self as native, NativeAccount, Status};
use crate::notifications::Route;
#[derive(Default)]
pub(super) struct SignInGate {
watched: Option<(EntityId, Subscription)>,
shown: bool,
quitting: bool,
pub(in crate::app) handles: Option<Handles>,
target: Option<FocusHandle>,
kept: Vec<Subscription>,
pub(in crate::app) held: Option<Route>,
}
impl Global for SignInGate {}
#[derive(Clone)]
pub(in crate::app) struct Handles {
pub frame: FocusHandle,
pub quit: FocusHandle,
pub sign_in: FocusHandle,
pub retry: FocusHandle,
pub cancel: FocusHandle,
pub reopen: FocusHandle,
}
pub(super) fn render(
tokens: &Tokens,
quitting: bool,
window: &mut Window,
cx: &mut Context<SubspaceApp>,
) -> Option<AnyElement> {
let entity = native::shared(cx)?;
watch_account(&entity, cx);
let asks = entity.read(cx).asks_for_sign_in();
let handles = handles(window, cx);
let shown = Shown::of(entity.read(cx));
let order = order(&shown, &handles);
let holding = entity.read(cx).holds_routes();
let state = cx.default_global::<SignInGate>();
let was = std::mem::replace(&mut state.shown, asks);
state.quitting = quitting;
state.target = order.last().cloned();
let released = state.held.take_if(|_| !holding);
match (was, asks, released) {
(_, _, Some(route)) => cx.defer_in(window, move |app, window, cx| {
app.open_notification(route, window, cx)
}),
(false, true, None) => {
cx.defer_in(window, |app, window, cx| app.clear_for_sign_in(window, cx))
}
(true, false, None) => cx.defer_in(window, |app, window, cx| app.focus_active(window, cx)),
_ => {}
}
if asks && !quitting && !holds_focus(&order, &handles.frame, window) {
cx.defer_in(window, |_, window, cx| take_focus(window, cx));
}
if asks && quitting {
let quit = super::quit_dialog::quit_focus(cx);
if !(quit.cancel.is_focused(window) || quit.confirm.is_focused(window)) {
cx.defer_in(window, move |_, window, cx| window.focus(&quit.cancel, cx));
}
}
asks.then(|| gate(&entity, (&shown, &handles, order), tokens, cx))
}
pub(crate) fn hold(route: Route, cx: &mut gpui_kit::App) -> bool {
let holding = native::shared(cx).is_some_and(|account| account.read(cx).holds_routes());
if holding {
cx.default_global::<SignInGate>().held = Some(route);
}
holding
}
pub(super) fn shown(cx: &gpui_kit::App) -> bool {
cx.try_global::<SignInGate>()
.is_some_and(|state| state.shown)
}
fn handles(window: &mut Window, cx: &mut Context<SubspaceApp>) -> Handles {
if let Some(handles) = cx
.try_global::<SignInGate>()
.and_then(|state| state.handles.clone())
{
return handles;
}
let handles = Handles {
frame: cx.focus_handle(),
quit: cx.focus_handle(),
sign_in: cx.focus_handle(),
retry: cx.focus_handle(),
cancel: cx.focus_handle(),
reopen: cx.focus_handle(),
};
let kept = vec![
cx.on_focus_out(&handles.frame, window, |_, _, window, cx| {
take_focus(window, cx)
}),
cx.observe_window_activation(window, |_, window, cx| take_focus(window, cx)),
];
let state = cx.default_global::<SignInGate>();
state.handles = Some(handles.clone());
state.kept = kept;
handles
}
fn order(shown: &Shown, handles: &Handles) -> Vec<FocusHandle> {
let mut order = vec![handles.quit.clone()];
match shown.status {
Status::Starting => order.push(handles.cancel.clone()),
Status::Waiting => order.extend([handles.cancel.clone(), handles.reopen.clone()]),
status => {
if retries(status) {
order.push(handles.retry.clone());
}
if !shown.busy {
order.push(handles.sign_in.clone());
}
}
}
order
}
fn holds_focus(order: &[FocusHandle], frame: &FocusHandle, window: &Window) -> bool {
frame.is_focused(window) || order.iter().any(|handle| handle.is_focused(window))
}
fn take_focus(window: &mut Window, cx: &mut gpui_kit::App) {
let target = cx
.try_global::<SignInGate>()
.filter(|state| state.shown && !state.quitting)
.and_then(|state| state.target.clone());
if let Some(target) = target {
window.focus(&target, cx);
}
}
fn cycle(order: &[FocusHandle], back: bool, window: &mut Window, cx: &mut gpui_kit::App) {
let count = order.len();
if count == 0 {
return;
}
let at = order.iter().position(|handle| handle.is_focused(window));
let next = match (at, back) {
(Some(index), false) => (index + 1) % count,
(Some(index), true) => (index + count - 1) % count,
(None, false) => 0,
(None, true) => count - 1,
};
window.focus(&order[next], cx);
}
impl SubspaceApp {
pub(super) fn quit_actions(shell: Stateful<Div>, cx: &mut Context<Self>) -> Stateful<Div> {
shell
.on_action(cx.listener(|app, _: &Quit, _, cx| app.request_quit(cx)))
.on_action(cx.listener(|app, _: &QuitAndRestart, _, cx| app.request_restart(cx)))
}
pub(super) fn app_actions(shell: Stateful<Div>, cx: &mut Context<Self>) -> Stateful<Div> {
if shown(cx) {
return shell;
}
shell
.map(|shell| Self::shell_actions(shell, cx))
.map(|shell| Self::chat_actions(shell, cx))
.map(|shell| Self::terminal_actions(shell, cx))
.map(|shell| Self::switch_actions(shell, cx))
}
fn clear_for_sign_in(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.session = None;
self.dismiss_shortcuts(window, cx);
if self.settings.is_some() {
self.close_settings(window, cx);
}
if window.root::<gpui_kit::base::Root>().flatten().is_some() {
window.close_all_dialogs(cx);
}
take_focus(window, cx);
cx.notify();
}
}
fn watch_account(entity: &Entity<NativeAccount>, cx: &mut Context<SubspaceApp>) {
let id = entity.entity_id();
let watched = cx
.try_global::<SignInGate>()
.and_then(|state| state.watched.as_ref())
.is_some_and(|(watched, _)| *watched == id);
if !watched {
let subscription = cx.observe(entity, |_, _, cx| cx.notify());
cx.default_global::<SignInGate>().watched = Some((id, subscription));
}
}