SmartFox: launcher, Siri Remote driver, Debian image and installer
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
217
crates/sf-launcher/src/keyboard.rs
Normal file
217
crates/sf-launcher/src/keyboard.rs
Normal file
@@ -0,0 +1,217 @@
|
||||
//! On-screen keyboard (tvOS style, one horizontal row of keys).
|
||||
//!
|
||||
//! ui/keyboard.slint draws it and moves the focus; this module owns the text,
|
||||
//! checks its length and hands it to whoever opened the keyboard.
|
||||
|
||||
use std::cell::RefCell;
|
||||
|
||||
use slint::ComponentHandle;
|
||||
|
||||
use crate::App;
|
||||
use crate::glass::Panel;
|
||||
|
||||
pub struct Request {
|
||||
pub title: String,
|
||||
pub subtitle: String,
|
||||
pub placeholder: String,
|
||||
/// Shows dots instead of the text (with a "Megjelenítés" chip).
|
||||
pub secret: bool,
|
||||
pub submit_label: String,
|
||||
/// Characters needed before submitting (0 = anything, even empty).
|
||||
pub min_len: usize,
|
||||
pub max_len: usize,
|
||||
/// Shown in red under the field until the text is edited (e.g. "wrong password").
|
||||
pub error: Option<String>,
|
||||
pub on_submit: Box<dyn FnOnce(String)>,
|
||||
}
|
||||
|
||||
struct Open {
|
||||
req: Request,
|
||||
text: String,
|
||||
error: Option<String>,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static KB: RefCell<Option<Open>> = const { RefCell::new(None) };
|
||||
}
|
||||
|
||||
/// A character that can be typed: no controls, no Slint special keys
|
||||
/// (arrows etc. arrive as private-use code points).
|
||||
fn typable(c: char) -> bool {
|
||||
!c.is_control() && !('\u{E000}'..='\u{F8FF}').contains(&c)
|
||||
}
|
||||
|
||||
pub fn install(ui: &App) {
|
||||
let w = ui.as_weak();
|
||||
ui.on_kb_type(move |s| {
|
||||
edit(&w, |t, max| {
|
||||
for c in s.chars().filter(|c| typable(*c)) {
|
||||
if t.chars().count() < max {
|
||||
t.push(c);
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
let w = ui.as_weak();
|
||||
ui.on_kb_delete(move || {
|
||||
edit(&w, |t, _| {
|
||||
t.pop();
|
||||
})
|
||||
});
|
||||
let w = ui.as_weak();
|
||||
ui.on_kb_clear(move || edit(&w, |t, _| t.clear()));
|
||||
let w = ui.as_weak();
|
||||
ui.on_kb_submit(move || submit(&w));
|
||||
// Every way out (Back, submit, settings closed) lands here: forget the text.
|
||||
let w = ui.as_weak();
|
||||
ui.on_kb_closed(move || {
|
||||
KB.with(|k| k.borrow_mut().take());
|
||||
if let Some(ui) = w.upgrade() {
|
||||
ui.set_kb_text("".into());
|
||||
ui.set_kb_masked("".into());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn open(ui: &App, req: Request) {
|
||||
ui.set_kb_title(req.title.clone().into());
|
||||
ui.set_kb_subtitle(req.subtitle.clone().into());
|
||||
ui.set_kb_placeholder(req.placeholder.clone().into());
|
||||
ui.set_kb_secret(req.secret);
|
||||
ui.set_kb_submit_label(req.submit_label.clone().into());
|
||||
ui.set_kb_reveal(false);
|
||||
ui.set_kb_mode(0);
|
||||
ui.set_kb_index(1);
|
||||
ui.set_kb_area(0);
|
||||
ui.set_kb_chip(0);
|
||||
let error = req.error.clone();
|
||||
KB.with(|k| *k.borrow_mut() = Some(Open { req, text: String::new(), error }));
|
||||
update(ui);
|
||||
ui.set_kb_open(true);
|
||||
}
|
||||
|
||||
fn edit(w: &slint::Weak<App>, f: impl FnOnce(&mut String, usize)) {
|
||||
let Some(ui) = w.upgrade() else { return };
|
||||
let changed = KB.with(|k| {
|
||||
let mut k = k.borrow_mut();
|
||||
let Some(open) = k.as_mut() else { return false };
|
||||
let before = open.text.clone();
|
||||
f(&mut open.text, open.req.max_len.max(1));
|
||||
if open.text != before {
|
||||
open.error = None;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
});
|
||||
if changed {
|
||||
update(&ui);
|
||||
}
|
||||
}
|
||||
|
||||
fn update(ui: &App) {
|
||||
KB.with(|k| {
|
||||
let k = k.borrow();
|
||||
let Some(open) = k.as_ref() else { return };
|
||||
let len = open.text.chars().count();
|
||||
let ok = len >= open.req.min_len;
|
||||
let (hint, error) = match &open.error {
|
||||
Some(e) => (e.clone(), true),
|
||||
None if !ok && len == 0 => (format!("Legalább {} karakter", open.req.min_len), false),
|
||||
None if !ok => (format!("Még {} karakter kell", open.req.min_len - len), false),
|
||||
None if len >= open.req.max_len => (format!("Legfeljebb {} karakter", open.req.max_len), false),
|
||||
None if open.req.secret => (format!("{len} karakter"), false),
|
||||
None => (String::new(), false),
|
||||
};
|
||||
ui.set_kb_text(open.text.clone().into());
|
||||
ui.set_kb_masked("•".repeat(len).into());
|
||||
ui.set_kb_hint(hint.into());
|
||||
ui.set_kb_hint_error(error);
|
||||
ui.set_kb_can_submit(ok);
|
||||
});
|
||||
}
|
||||
|
||||
fn submit(w: &slint::Weak<App>) {
|
||||
let Some(ui) = w.upgrade() else { return };
|
||||
let taken = KB.with(|k| {
|
||||
let mut k = k.borrow_mut();
|
||||
let ok = k.as_ref().is_some_and(|o| o.text.chars().count() >= o.req.min_len);
|
||||
if ok { k.take() } else { None }
|
||||
});
|
||||
let Some(open) = taken else { return };
|
||||
ui.set_kb_open(false);
|
||||
(open.req.on_submit)(open.text);
|
||||
}
|
||||
|
||||
/// Glass for the keyboard, drawn on the settings pane. Mirrors the kb-*
|
||||
/// geometry in ui/app.slint and the layout in ui/keyboard.slint.
|
||||
pub fn glass(ui: &App, scale: f32) -> Vec<Panel> {
|
||||
let t = ui.get_kb_t() * ui.get_set_t();
|
||||
if t <= 0.001 {
|
||||
return Vec::new();
|
||||
}
|
||||
let u = ui.get_u();
|
||||
let px = |v: f32| v * scale;
|
||||
let panel = |x: f32, y: f32, w: f32, h: f32, r: f32, focus: f32, alpha: f32, glow: [f32; 3], tint: [f32; 4]| Panel {
|
||||
x: px(x),
|
||||
y: px(y),
|
||||
w: px(w),
|
||||
h: px(h),
|
||||
radius: px(r),
|
||||
focus,
|
||||
alpha,
|
||||
blur: -1.0,
|
||||
glow,
|
||||
group: 0.0,
|
||||
tint,
|
||||
};
|
||||
let white = [1.0, 1.0, 1.0];
|
||||
let orange = [1.0, 0.48, 0.21];
|
||||
let mut out = Vec::with_capacity(9);
|
||||
|
||||
// Text field.
|
||||
let (fx, fy, fw, fh) = (ui.get_kb_field_x(), ui.get_kb_field_y(), ui.get_kb_field_w(), ui.get_kb_field_h());
|
||||
out.push(panel(fx, fy, fw, fh, 30.0 * u, 0.25, t, orange, [1.0, 1.0, 1.0, 0.07]));
|
||||
|
||||
// Key track and the focus lens that glides along it.
|
||||
let (rx, ry, rh, kw) = (ui.get_kb_row_x(), ui.get_kb_row_y(), ui.get_kb_row_h(), ui.get_kb_key_w());
|
||||
let n = ui.get_kb_n() as f32;
|
||||
let track_h = rh - 28.0 * u;
|
||||
out.push(panel(rx - 26.0 * u, ry + 14.0 * u, n * kw + 52.0 * u, track_h, track_h / 2.0, 0.0, t, white, [1.0, 1.0, 1.0, 0.035]));
|
||||
let active = ui.get_kb_keys_active();
|
||||
let (lw, lh) = (86.0 * u, 112.0 * u);
|
||||
let cx = rx + (ui.get_kb_pos() + 0.5) * kw;
|
||||
out.push(panel(cx - lw / 2.0, ry + (rh - lh) / 2.0, lw, lh, 30.0 * u, active, t * active, white, [1.0, 1.0, 1.0, 0.88 * active]));
|
||||
|
||||
// Chips: modes, show/clear, submit.
|
||||
let (chx, chy, chw, chh, step) =
|
||||
(ui.get_kb_chip_x(), ui.get_kb_chip_y(), ui.get_kb_chip_w(), ui.get_kb_chip_h(), ui.get_kb_chip_step());
|
||||
let (cpos, cactive, mode, can) = (ui.get_kb_cpos(), ui.get_kb_chips_active(), ui.get_kb_mode(), ui.get_kb_can_submit());
|
||||
for i in 0..6 {
|
||||
let f = (1.0 - (cpos - i as f32).abs()).max(0.0) * cactive;
|
||||
let s = 1.0 + 0.05 * f;
|
||||
let (w, h) = (chw * s, chh * s);
|
||||
let x = chx + i as f32 * step - (w - chw) / 2.0;
|
||||
let y = chy - (h - chh) / 2.0;
|
||||
let (glow, tint) = if i == 5 {
|
||||
(orange, [1.0, 0.48, 0.21, if can { 0.34 + 0.56 * f } else { 0.08 + 0.2 * f }])
|
||||
} else {
|
||||
let sel = if i < 4 && i == mode { 0.16 } else { 0.0 };
|
||||
(white, [1.0, 1.0, 1.0, 0.04 + sel + 0.80 * f])
|
||||
};
|
||||
out.push(panel(x, y, w, h, h / 2.0, f, t, glow, tint));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::typable;
|
||||
|
||||
#[test]
|
||||
fn filters_special_keys() {
|
||||
assert!(typable('a') && typable('Ő') && typable(' ') && typable('"'));
|
||||
// Slint's arrow keys, Return, Escape, Backspace.
|
||||
assert!(!typable('\u{F700}') && !typable('\n') && !typable('\u{1b}') && !typable('\u{8}'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user