use crate::reactive::Signal;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CursorIcon {
#[default]
Default,
Pointer,
Text,
Move,
NotAllowed,
Grab,
Grabbing,
Crosshair,
ColResize,
RowResize,
}
type ShortcutEntry = (u32, KeyChord, Rc<dyn Fn()>);
thread_local! {
static MANUAL_CURSOR: RefCell<Signal<Option<CursorIcon>>> = RefCell::new(Signal::new(None));
static CURSOR_REQUESTS: RefCell<Vec<(CursorIcon, Signal<bool>)>> = const { RefCell::new(Vec::new()) };
static REGISTERED_SHORTCUTS: RefCell<Vec<ShortcutEntry>> = RefCell::new(Vec::new());
static NEXT_SHORTCUT_ID: RefCell<u32> = const { RefCell::new(1) };
static CLIPBOARD_CONTENT: RefCell<Signal<String>> = RefCell::new(Signal::new(String::new()));
}
pub fn current_cursor() -> CursorIcon {
if let Some(manual) = MANUAL_CURSOR.with(|c| c.borrow().get()) {
return manual;
}
CURSOR_REQUESTS.with(|reqs| {
for (icon, active) in reqs.borrow().iter().rev() {
if active.get() {
return *icon;
}
}
CursorIcon::Default
})
}
pub fn set_current_cursor(icon: CursorIcon) {
MANUAL_CURSOR.with(|c| c.borrow().set(Some(icon)));
}
pub fn clear_cursor_override() {
MANUAL_CURSOR.with(|c| c.borrow().set(None));
}
pub fn use_cursor(icon: CursorIcon, active: Signal<bool>) {
CURSOR_REQUESTS.with(|reqs| {
reqs.borrow_mut().push((icon, active));
});
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyChord {
pub key: String,
pub ctrl: bool,
pub shift: bool,
pub alt: bool,
pub meta: bool,
}
impl KeyChord {
fn normalize_key_token(raw: &str) -> String {
if raw == " " {
return "space".to_string();
}
match raw.trim().to_ascii_lowercase().as_str() {
"" | "space" | "spacebar" if raw == " " || raw.eq_ignore_ascii_case("space") || raw.eq_ignore_ascii_case("spacebar") => {
"space".to_string()
}
"esc" | "escape" => "escape".to_string(),
"return" | "enter" => "enter".to_string(),
other => other.to_string(),
}
}
pub fn parse(s: &str) -> Self {
let mut ctrl = false;
let mut shift = false;
let mut alt = false;
let mut meta = false;
let mut key = String::new();
for part in s.split('+') {
if part == " " {
key = "space".to_string();
continue;
}
let p = part.trim();
match p.to_ascii_lowercase().as_str() {
"ctrl" | "control" => ctrl = true,
"shift" => shift = true,
"alt" | "option" => alt = true,
"cmd" | "command" | "meta" | "super" => meta = true,
other => key = Self::normalize_key_token(other),
}
}
Self {
key,
ctrl,
shift,
alt,
meta,
}
}
pub fn matches(&self, key: &str, ctrl: bool, shift: bool, alt: bool, meta: bool) -> bool {
let incoming = Self::normalize_key_token(key);
self.key.eq_ignore_ascii_case(&incoming)
&& self.ctrl == ctrl
&& self.shift == shift
&& self.alt == alt
&& self.meta == meta
}
}
pub struct ShortcutHandle {
id: u32,
}
impl Drop for ShortcutHandle {
fn drop(&mut self) {
REGISTERED_SHORTCUTS.with(|s| {
s.borrow_mut().retain(|(id, _, _)| *id != self.id);
});
}
}
pub fn use_keyboard_shortcut<F: Fn() + 'static>(shortcut: &str, handler: F) -> ShortcutHandle {
let chord = KeyChord::parse(shortcut);
let id = NEXT_SHORTCUT_ID.with(|n| {
let mut val = n.borrow_mut();
let cur = *val;
*val += 1;
cur
});
let callback = Rc::new(handler);
REGISTERED_SHORTCUTS.with(|s| {
s.borrow_mut().push((id, chord, callback));
});
ShortcutHandle { id }
}
pub fn dispatch_global_keydown(key: &str, ctrl: bool, shift: bool, alt: bool, meta: bool) -> bool {
let mut handled = false;
REGISTERED_SHORTCUTS.with(|s| {
let list = s.borrow().clone();
for (_, chord, callback) in list {
if chord.matches(key, ctrl, shift, alt, meta) {
callback();
handled = true;
break;
}
}
});
handled
}
#[derive(Clone)]
pub struct ClipboardHandle {
pub text: Signal<String>,
pub copy: Rc<dyn Fn(&str)>,
pub read_text: Rc<dyn Fn() -> String>,
}
pub fn use_clipboard() -> ClipboardHandle {
let text_sig = CLIPBOARD_CONTENT.with(|c| c.borrow().clone());
let copy = {
let sig = text_sig.clone();
Rc::new(move |val: &str| {
sig.set(val.to_string());
})
};
let read_text = {
let sig = text_sig.clone();
Rc::new(move || sig.get())
};
ClipboardHandle {
text: text_sig,
copy,
read_text,
}
}