use std::cell::RefCell;
use std::collections::HashSet;
use gpui_kit::base::{Root, RootPlugin};
use gpui_kit::{
App, Context, DispatchPhase, FocusHandle, IntoElement, MouseDownEvent, Render, Styled as _,
Window, WindowId, canvas,
};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Modality {
#[default]
Keyboard,
Pointer,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Input {
KeyDown,
PointerDown,
PointerMove,
}
impl Modality {
pub fn after(self, input: Input) -> Self {
match input {
Input::KeyDown => Modality::Keyboard,
Input::PointerDown => Modality::Pointer,
Input::PointerMove => self,
}
}
}
thread_local! {
static POINTER: RefCell<HashSet<WindowId>> = RefCell::new(HashSet::new());
}
pub fn modality(window: &Window) -> Modality {
let id = window.window_handle().window_id();
if POINTER.with(|p| p.borrow().contains(&id)) {
Modality::Pointer
} else {
Modality::Keyboard
}
}
pub fn focus_visible_mode(window: &Window) -> bool {
modality(window) == Modality::Keyboard
}
pub fn is_focus_visible(focus: &FocusHandle, window: &Window) -> bool {
focus.is_focused(window) && focus_visible_mode(window)
}
pub fn record(input: Input, window: &mut Window) {
let id = window.window_handle().window_id();
let before = modality(window);
let after = before.after(input);
if after == before {
return;
}
POINTER.with(|p| {
let mut p = p.borrow_mut();
match after {
Modality::Pointer => p.insert(id),
Modality::Keyboard => p.remove(&id),
}
});
window.refresh();
}
fn lone_modifier(key: &str) -> bool {
matches!(key, "shift" | "control" | "alt" | "platform" | "function")
}
pub(crate) fn init(cx: &mut App) {
Root::register_plugin::<PressProbe>(cx, PressProbe::new);
cx.intercept_keystrokes(|event, window, _| {
if !lone_modifier(&event.keystroke.key) {
record(Input::KeyDown, window);
}
})
.detach();
cx.on_window_closed(|_, id| {
POINTER.with(|p| p.borrow_mut().remove(&id));
})
.detach();
}
struct PressProbe;
impl PressProbe {
fn new(window: &mut Window, _: &mut Context<Self>) -> Self {
let id = window.window_handle().window_id();
POINTER.with(|p| p.borrow_mut().remove(&id));
PressProbe
}
}
impl Render for PressProbe {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
canvas(
|_, _, _| {},
|_, _, window, _| {
window.on_mouse_event(|_: &MouseDownEvent, phase, window, _| {
if phase == DispatchPhase::Capture {
record(Input::PointerDown, window);
}
});
},
)
.absolute()
.size_0()
}
}
impl RootPlugin for PressProbe {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn starts_in_keyboard_modality() {
assert_eq!(Modality::default(), Modality::Keyboard);
}
#[test]
fn presses_decide_and_movement_does_not() {
let m = Modality::Keyboard;
assert_eq!(m.after(Input::PointerMove), Modality::Keyboard);
let m = m.after(Input::PointerDown);
assert_eq!(m, Modality::Pointer);
assert_eq!(m.after(Input::PointerMove), Modality::Pointer);
assert_eq!(m.after(Input::KeyDown), Modality::Keyboard);
}
#[test]
fn lone_modifiers_are_not_key_presses() {
assert!(lone_modifier("shift"));
assert!(lone_modifier("platform"));
assert!(!lone_modifier("tab"));
assert!(!lone_modifier("a"));
}
}