use std::rc::Rc;
use crabshell_reactive::{Signal, untrack};
use crate::cx::Cx;
use crate::edit::Keyed;
use crate::input::{Handler, run_isolated};
use crate::select::copy_key;
use crate::text_input::AcceptHandler;
use crate::tree::{Change, NodeId, NodeKind, Tree, borrow};
use crate::window::Window;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct KeyEvent {
pub key: Key,
pub text: Option<String>,
pub modifiers: Modifiers,
pub repeat: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Key {
Enter,
Escape,
Tab,
Backspace,
Delete,
Left,
Right,
Up,
Down,
Home,
End,
PageUp,
PageDown,
Function(u8),
Character(char),
Other(u32),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Clipboard {
Copy(String),
Paste,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Modifiers {
pub ctrl: bool,
pub alt: bool,
pub shift: bool,
pub logo: bool,
}
const PAGE_LINES: f32 = 10.0;
pub(crate) type KeyHandler = Rc<Handler<dyn Fn(&Cx, &KeyEvent) -> bool>>;
#[derive(Default)]
pub(crate) struct Keyboard {
pub(crate) entered: bool,
pub(crate) focused: Option<NodeId>,
pub(crate) link: Option<usize>,
pub(crate) handler: Option<KeyHandler>,
pub(crate) modifiers: Modifiers,
}
impl Keyboard {
pub(crate) fn forget(&mut self, removed: &[NodeId]) {
if self.focused.is_some_and(|id| removed.contains(&id)) {
self.focused = None;
self.link = None;
}
}
}
impl Window {
pub fn keyboard_enter(&self) {
let mut tree = borrow(&self.tree);
tree.keyboard.entered = true;
let focused = tree.keyboard.focused;
tree.show_focus(focused, true);
}
pub fn keyboard_leave(&self) {
let mut tree = borrow(&self.tree);
tree.keyboard.entered = false;
let focused = tree.keyboard.focused;
tree.show_focus(focused, false);
}
pub fn set_modifiers(&self, modifiers: Modifiers) {
borrow(&self.tree).keyboard.modifiers = modifiers;
}
pub fn key(&self, event: &KeyEvent) -> Option<Clipboard> {
let typed = borrow(&self.tree).input_key(event);
match typed {
Some(Typed::Edited(signal, text, clipboard)) => {
signal.try_set(text);
return clipboard;
}
Some(Typed::Clipboard(clipboard)) => return Some(clipboard),
Some(Typed::Moved) => return None,
Some(Typed::Accept(call)) => {
call.run();
return None;
}
None => {}
}
let (button, window) = {
let tree = self.tree.borrow();
(tree.focused_key_call(), tree.window_key_call())
};
for call in [button, window].into_iter().flatten() {
if call.run(event) {
return None;
}
}
let click = borrow(&self.tree).default_key(event);
if let Some(click) = click {
click.run();
}
None
}
pub fn focused(&self) -> Option<String> {
let tree = self.tree.borrow();
let id = tree.keyboard.focused?;
tree.node(id).map(|node| node.path.to_string())
}
}
impl Tree {
fn show_focus(&mut self, id: Option<NodeId>, shown: bool) {
let Some(id) = id else {
return;
};
if let Some(NodeKind::Text(_)) = self.node(id).map(|node| &node.kind) {
let link = self.keyboard.link.filter(|_| shown);
self.light_link(id, true, link);
return;
}
if matches!(
self.node(id).map(|node| &node.kind),
Some(NodeKind::Input(_))
) {
self.update(id, |node| match &mut node.kind {
NodeKind::Input(input) if input.focused != shown => {
input.focused = shown;
Change::Paint
}
_ => Change::None,
});
} else {
self.update_button_style(id, |button| button.focused = shown);
}
}
pub(crate) fn focus(&mut self, id: Option<NodeId>) {
self.focus_link(id, None);
}
pub(crate) fn drop_stale_link_focus(&mut self, id: NodeId) {
let (Some(focused), Some(link)) = (self.keyboard.focused, self.keyboard.link) else {
return;
};
let still = match self.node(id).map(|node| &node.kind) {
Some(NodeKind::Text(text)) => text.spans.get(link).is_some_and(|span| span.link.is_some()),
_ => false,
};
if focused == id && !still {
self.focus(None);
}
}
fn focus_link(&mut self, id: Option<NodeId>, link: Option<usize>) {
if id.is_some_and(|id| !self.shown(id)) {
return;
}
if self.keyboard.focused == id {
if self.keyboard.link != link {
self.keyboard.link = link;
let entered = self.keyboard.entered;
self.show_focus(id, entered);
}
return;
}
let old = std::mem::replace(&mut self.keyboard.focused, id);
if let Some(old) = old
&& matches!(self.node(old).map(|node| &node.kind), Some(NodeKind::Input(input)) if input.preedit.is_some())
{
self.update_input(old, |input| {
input.preedit = None;
input.refresh();
});
}
if let Some(old) = old {
self.drop_selection(old);
}
self.show_focus(old, false);
self.keyboard.link = link;
let entered = self.keyboard.entered;
self.show_focus(id, entered);
if let Some(id) = id {
self.update_input(id, |_| {});
}
}
fn input_key(&mut self, event: &KeyEvent) -> Option<Typed> {
let id = self.keyboard.focused?;
let view = self.page_height(id);
let input = match &mut self.node_mut(id)?.kind {
NodeKind::Input(input) => input,
NodeKind::Text(text) => return copy_key(text, event).map(Typed::Clipboard),
_ => return None,
};
let page = view.unwrap_or(PAGE_LINES * input.text.buffer.metrics().line_height);
let keyed = match input.edit.multiline {
true => input.line_key(event, page),
false => None,
};
let was = input.edit.selection();
let keyed = keyed.unwrap_or_else(|| {
input.upstream = None;
input.goal = None;
input.edit.key(event, !input.password)
});
let selected = input
.edit
.selection()
.filter(|now| keyed == Keyed::Moved && Some(now) != was.as_ref() && !input.password);
let primary = selected.and_then(|range| input.edit.text.get(range).map(str::to_owned));
let accept = keyed == Keyed::Accept;
let typed = match keyed {
Keyed::Unused => return None,
Keyed::Moved => Typed::Moved,
Keyed::Edited => Typed::Edited(input.signal, input.edit.text.clone(), None),
Keyed::Copy(text) => Typed::Clipboard(Clipboard::Copy(text)),
Keyed::Cut(text) => Typed::Edited(
input.signal,
input.edit.text.clone(),
Some(Clipboard::Copy(text)),
),
Keyed::Paste => Typed::Clipboard(Clipboard::Paste),
Keyed::Accept => {
let handler = untrack(|| input.on_accept.try_with(Option::clone)).flatten();
let text = input.edit.text.clone();
let node = self.node(id)?;
Typed::Accept(AcceptCall {
cx: node.cx.clone(),
path: Rc::clone(&node.path),
handler: handler?,
text,
})
}
};
if !accept {
self.update_input(id, |input| {
input.refresh();
});
}
if let Some(text) = primary {
self.primary = Some(Clipboard::Copy(text));
}
Some(typed)
}
fn page_height(&self, id: NodeId) -> Option<f32> {
self
.path_to(id)
.into_iter()
.rev()
.find_map(|at| match &self.node(at)?.kind {
NodeKind::Scroll(scroll) => scroll
.active()
.iter()
.find(|axis| !axis.horizontal)
.map(|axis| axis.length),
_ => None,
})
}
fn focusable(&self) -> Vec<(NodeId, Option<usize>)> {
let mut found = Vec::new();
let mut pending = vec![self.root];
while let Some(id) = pending.pop() {
if self.node(id).is_some_and(|node| !node.visible) {
continue;
}
match self.node(id).map(|node| &node.kind) {
Some(NodeKind::Button(button)) if button.enabled => found.push((id, None)),
Some(NodeKind::Input(_)) => found.push((id, None)),
Some(NodeKind::Text(text)) => {
found.extend(text.link_stops().into_iter().map(|link| (id, Some(link))));
}
_ => {}
}
let mut children = self.taffy.children(id).unwrap_or_default();
children.reverse();
pending.extend(children);
}
found
}
fn focused_key_call(&self) -> Option<KeyCall> {
let id = self.keyboard.focused?;
let node = self.node(id)?;
let NodeKind::Button(button) = &node.kind else {
return None;
};
let handler = untrack(|| button.handlers.try_with(|handlers| handlers.key.clone()))??;
Some(KeyCall {
cx: node.cx.clone(),
path: Rc::clone(&node.path),
handler,
})
}
fn window_key_call(&self) -> Option<KeyCall> {
let node = self.node(self.root)?;
Some(KeyCall {
cx: node.cx.clone(),
path: Rc::clone(&node.path),
handler: self.keyboard.handler.clone()?,
})
}
fn default_key(&mut self, event: &KeyEvent) -> Option<crate::input::Call> {
match event.key {
Key::Tab => {
let buttons = self.focusable();
let at = self.keyboard.focused.and_then(|id| {
let link = self.keyboard.link;
buttons.iter().position(|&stop| stop == (id, link))
});
let count = buttons.len();
let next = match (at, event.modifiers.shift) {
_ if count == 0 => None,
(None, false) => Some(0),
(None, true) => Some(count - 1),
(Some(at), false) => Some((at + 1) % count),
(Some(at), true) => Some((at + count - 1) % count),
};
let (id, link) = next.map_or((None, None), |next| {
(Some(buttons[next].0), buttons[next].1)
});
self.focus_link(id, link);
None
}
Key::Enter | Key::Character(' ') => {
let id = self.keyboard.focused?;
match self.keyboard.link {
Some(link) => self.link_call(id, link),
None => self.click(id),
}
}
_ => None,
}
}
}
enum Typed {
Edited(Signal<String>, String, Option<Clipboard>),
Clipboard(Clipboard),
Moved,
Accept(AcceptCall),
}
struct AcceptCall {
cx: Cx,
path: Rc<str>,
handler: AcceptHandler,
text: String,
}
impl AcceptCall {
fn run(self) {
run_isolated(&self.path, "on_accept", self.handler, |f| {
f(&self.cx, &self.text)
});
}
}
struct KeyCall {
cx: Cx,
path: Rc<str>,
handler: KeyHandler,
}
impl KeyCall {
fn run(self, event: &KeyEvent) -> bool {
run_isolated(&self.path, "on_key", self.handler, |f| f(&self.cx, event)).unwrap_or(false)
}
}