#![cfg_attr(not(feature = "full"), allow(dead_code, unused_imports))]
use std::{cell::Cell, rc::Rc};
use gpui::{
canvas, div, point, prelude::*, px, AnyElement, App, Bounds, Corner, DispatchPhase, Div,
ElementId, FocusHandle, MouseMoveEvent, MouseUpEvent, Pixels, Point, Stateful, Window,
};
use super::app_root::{FocusNextElement, FocusPreviousElement};
use super::layer::layer_at;
use super::overlay::child_id;
use crate::sx::SxStyled;
use crate::{
motion::{use_presence, Presence, Transition},
styles,
theme::ActiveTheme,
};
#[doc(hidden)]
pub type Callback = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[doc(hidden)]
pub enum ModalFocus {
Opening,
PanelFocused,
Settled,
}
#[doc(hidden)]
pub fn focus_modal_on_open(
panel: &FocusHandle,
phase: &Cell<ModalFocus>,
window: &mut Window,
cx: &mut App,
) {
match phase.get() {
ModalFocus::Opening => {
phase.set(ModalFocus::PanelFocused);
let panel = panel.clone();
window.defer(cx, move |window, _| window.focus(&panel));
}
ModalFocus::PanelFocused => {
phase.set(ModalFocus::Settled);
let panel = panel.clone();
window.defer(cx, move |window, cx| {
if panel.is_focused(window) {
focus_first_inside(&panel, window, cx);
}
});
}
ModalFocus::Settled => {}
}
}
#[doc(hidden)]
pub fn trap_focus<E: InteractiveElement>(element: E, panel: &FocusHandle) -> E {
let forward_panel = panel.clone();
let backward_panel = panel.clone();
element
.on_action(move |_: &FocusNextElement, window, cx| {
window.focus_next();
if !is_inside(&forward_panel, window, cx) {
focus_first_inside(&forward_panel, window, cx);
}
})
.on_action(move |_: &FocusPreviousElement, window, cx| {
window.focus_prev();
if !is_inside(&backward_panel, window, cx) {
focus_last_inside(&backward_panel, window, cx);
}
})
}
fn is_inside(panel: &FocusHandle, window: &Window, cx: &App) -> bool {
panel.contains_focused(window, cx) && !panel.is_focused(window)
}
fn focus_first_inside(panel: &FocusHandle, window: &mut Window, cx: &App) {
window.focus(panel);
window.focus_next();
if !is_inside(panel, window, cx) {
window.focus(panel);
}
}
fn focus_last_inside(panel: &FocusHandle, window: &mut Window, cx: &App) {
focus_first_inside(panel, window, cx);
let mut last = window.focused(cx);
for _ in 0..1024 {
window.focus_next();
let current = window.focused(cx);
if !is_inside(panel, window, cx) || current == last {
break;
}
last = current;
}
match last {
Some(handle) => window.focus(&handle),
None => window.focus(panel),
}
}
#[doc(hidden)]
pub fn measure_bounds(bounds: Rc<Cell<Bounds<Pixels>>>) -> impl IntoElement {
canvas(move |measured, _, _| bounds.set(measured), |_, (), _, _| {})
.absolute()
.top_0()
.left_0()
.size_full()
}
#[doc(hidden)]
pub type PointerHandler = Rc<dyn Fn(Point<Pixels>, &mut Window, &mut App)>;
#[doc(hidden)]
pub fn track_drag(active: bool, on_move: PointerHandler, on_end: Callback) -> impl IntoElement {
canvas(
|_, _, _| {},
move |_, (), window, _| {
if !active {
return;
}
window.on_mouse_event(move |event: &MouseMoveEvent, phase, window, cx| {
if phase == DispatchPhase::Bubble && event.dragging() {
on_move(event.position, window, cx);
}
});
window.on_mouse_event(move |_: &MouseUpEvent, phase, window, cx| {
if phase == DispatchPhase::Bubble {
on_end(window, cx);
}
});
},
)
.absolute()
.size_0()
}
#[doc(hidden)]
pub fn on_activate(element: Stateful<Div>, handler: Callback) -> Stateful<Div> {
let key_handler = handler.clone();
element
.on_click(move |event, window, cx| {
if !event.is_keyboard() {
handler(window, cx);
}
})
.on_key_down(move |event, window, cx| {
if matches!(event.keystroke.key.as_str(), "enter" | "space") {
cx.stop_propagation();
key_handler(window, cx);
}
})
}
styles! {
MODAL = {
scrim: {
display: flex,
direction: row_ltr,
color: foreground,
font_family: sans,
text: theme,
text_align: start,
},
placement(ModalPlacement): {
Center: { align: center, justify: center, padding: 4 },
Top: { direction: column, justify: start },
Bottom: { direction: column, justify: end },
Left: { justify: start },
Right: { justify: end },
},
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[doc(hidden)]
pub enum ModalPlacement {
Center,
Top,
Bottom,
Left,
Right,
}
#[doc(hidden)]
pub fn modal_presence(id: &ElementId, open: bool, window: &mut Window, cx: &mut App) -> Presence {
use_presence(
child_id(id, "presence"),
window,
cx,
open,
Transition::ease_out(180),
)
}
#[doc(hidden)]
#[must_use]
pub fn modal_offset(placement: ModalPlacement, progress: f32) -> (Pixels, Pixels) {
let remaining = 1. - progress;
match placement {
ModalPlacement::Center => (px(0.), px(8. * remaining)),
ModalPlacement::Top => (px(0.), px(-320. * remaining)),
ModalPlacement::Bottom => (px(0.), px(320. * remaining)),
ModalPlacement::Left => (px(-384. * remaining), px(0.)),
ModalPlacement::Right => (px(384. * remaining), px(0.)),
}
}
#[allow(clippy::too_many_arguments)]
#[doc(hidden)]
pub fn render_modal(
id: ElementId,
placement: ModalPlacement,
progress: f32,
panel: Div,
on_escape: Option<Callback>,
on_backdrop: Option<Callback>,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let (focus_handle, focus_pending) = window
.use_keyed_state(child_id(&id, "modal-focus"), cx, |_, cx| {
(cx.focus_handle(), Rc::new(Cell::new(ModalFocus::Opening)))
})
.read(cx)
.clone();
focus_modal_on_open(&focus_handle, &focus_pending, window, cx);
let theme = cx.theme();
let viewport_size = window.viewport_size();
let (offset_x, offset_y) = modal_offset(placement, progress);
let panel = trap_focus(panel, &focus_handle);
let panel = panel
.relative()
.left(offset_x)
.top(offset_y)
.track_focus(&focus_handle)
.on_mouse_down(gpui::MouseButton::Left, |_, _, cx| cx.stop_propagation())
.on_key_down(move |event, window, cx| {
if event.keystroke.key == "escape" {
cx.stop_propagation();
if let Some(on_escape) = on_escape.as_ref() {
on_escape(window, cx);
}
}
});
let scrim = div()
.id(id)
.occlude()
.w(viewport_size.width)
.h(viewport_size.height)
.sx((&MODAL.scrim, MODAL.placement(placement)))
.bg(theme.colors.overlay.opacity(progress))
.when_some(on_backdrop, |scrim, on_backdrop| {
scrim.on_mouse_down(gpui::MouseButton::Left, move |_, window, cx| {
on_backdrop(window, cx);
})
})
.child(panel.opacity(progress));
layer_at(point(px(0.), px(0.)), Corner::TopLeft, px(0.), scrim, 1, cx)
}