launcher: keyboard and contrast tests for the Servers page; readable pre-update backup names
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+145
-31
@@ -287,6 +287,65 @@ fn bytes(n: u64) -> String {
|
||||
}
|
||||
|
||||
/// A backup's date and kind from its name (`2026-10-19_11-28-57-manual.zip`, UTC), for people.
|
||||
/// What a key did in a one-line field.
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum Key {
|
||||
Enter,
|
||||
Escape,
|
||||
/// Command/Ctrl-V: the caller pastes the clipboard's first line.
|
||||
Paste,
|
||||
Edited,
|
||||
/// Not the field's: Tab (focus), arrows, shortcuts.
|
||||
Pass,
|
||||
}
|
||||
|
||||
/// Types `key` into `text`. A `selected` suggestion is replaced by what's typed or pasted, and
|
||||
/// cleared by Backspace.
|
||||
fn type_key(
|
||||
text: &mut String,
|
||||
selected: bool,
|
||||
key: &str,
|
||||
key_char: Option<&str>,
|
||||
command: bool,
|
||||
) -> Key {
|
||||
let replace = |text: &mut String| {
|
||||
if selected {
|
||||
text.clear();
|
||||
}
|
||||
};
|
||||
match key {
|
||||
"enter" => Key::Enter,
|
||||
"escape" => Key::Escape,
|
||||
"v" if command => {
|
||||
replace(text);
|
||||
Key::Paste
|
||||
}
|
||||
"backspace" => {
|
||||
if selected {
|
||||
text.clear();
|
||||
} else {
|
||||
text.pop();
|
||||
}
|
||||
Key::Edited
|
||||
}
|
||||
_ if command => Key::Pass,
|
||||
// Not every platform gives Space a character.
|
||||
"space" => {
|
||||
replace(text);
|
||||
text.push(' ');
|
||||
Key::Edited
|
||||
}
|
||||
_ => match key_char {
|
||||
Some(ch) if !ch.is_empty() && !ch.chars().any(char::is_control) => {
|
||||
replace(text);
|
||||
text.push_str(ch);
|
||||
Key::Edited
|
||||
}
|
||||
_ => Key::Pass,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn backup_label(b: &Backup) -> (String, String) {
|
||||
let stem = b.name.trim_end_matches(".zip");
|
||||
let (when, kind) = match stem.get(..19) {
|
||||
@@ -298,6 +357,13 @@ fn backup_label(b: &Backup) -> (String, String) {
|
||||
let kind = match kind {
|
||||
"auto" => "Automatic".to_string(),
|
||||
"manual" => "Made by hand".to_string(),
|
||||
"before-update" => "Before an update".to_string(),
|
||||
// horizon-server writes the tag with dashes (before-v2026-10-19-10).
|
||||
k if k.starts_with("before-v")
|
||||
&& k[8..].chars().all(|c| c.is_ascii_digit() || c == '-') =>
|
||||
{
|
||||
format!("Before updating to {}", k[7..].replace('-', "."))
|
||||
}
|
||||
k if k.starts_with("before-") => format!("Before updating to {}", &k[7..]),
|
||||
k => k.to_string(),
|
||||
};
|
||||
@@ -849,7 +915,9 @@ impl Launcher {
|
||||
} else {
|
||||
div()
|
||||
.text_color(t.ink)
|
||||
.when(selected, |d| d.rounded(px(2.)).bg(t.accent.opacity(0.22)))
|
||||
.when(selected, |d| {
|
||||
d.rounded(px(2.)).bg(t.accent.opacity(theme::SELECTION))
|
||||
})
|
||||
.child(shown)
|
||||
})
|
||||
.when(focused, |d| {
|
||||
@@ -871,46 +939,31 @@ impl Launcher {
|
||||
if k.key != "enter" && this.servers.invalid.is_some_and(|(g, _)| g == f) {
|
||||
this.servers.invalid = None;
|
||||
}
|
||||
// A suggested value is selected: typing or pasting replaces it, Backspace clears it.
|
||||
if this.servers.selected.take() == Some(f)
|
||||
&& (k.key == "backspace"
|
||||
|| (k.key == "v" && command)
|
||||
|| (!command && (k.key == "space" || k.key_char.is_some())))
|
||||
&& !matches!(k.key.as_str(), "enter" | "escape" | "tab")
|
||||
{
|
||||
this.servers.text(f).clear();
|
||||
if k.key == "backspace" {
|
||||
cx.stop_propagation();
|
||||
cx.notify();
|
||||
return;
|
||||
}
|
||||
}
|
||||
match k.key.as_str() {
|
||||
"enter" => this.field_enter(f),
|
||||
// Not every platform gives Space a character.
|
||||
"space" if !command => this.servers.text(f).push(' '),
|
||||
"escape" => {
|
||||
let selected = this.servers.selected.take() == Some(f);
|
||||
let typed = type_key(
|
||||
this.servers.text(f),
|
||||
selected,
|
||||
&k.key,
|
||||
k.key_char.as_deref(),
|
||||
command,
|
||||
);
|
||||
match typed {
|
||||
Key::Enter => this.field_enter(f),
|
||||
Key::Escape => {
|
||||
if this.servers.create.is_some() && !this.servers.creating {
|
||||
this.servers.create = None;
|
||||
}
|
||||
window.blur();
|
||||
}
|
||||
"backspace" => {
|
||||
this.servers.text(f).pop();
|
||||
}
|
||||
"v" if command => {
|
||||
Key::Paste => {
|
||||
if let Some(text) = cx.read_from_clipboard().and_then(|c| c.text()) {
|
||||
let line = text.lines().next().unwrap_or_default().trim().to_string();
|
||||
this.servers.text(f).push_str(&line);
|
||||
}
|
||||
}
|
||||
_ if !command => match &k.key_char {
|
||||
Some(ch) if !ch.chars().any(char::is_control) => {
|
||||
this.servers.text(f).push_str(ch)
|
||||
}
|
||||
_ => return,
|
||||
},
|
||||
_ => return,
|
||||
Key::Edited => {}
|
||||
// Tab and the rest go on to the window: Tab moves the focus.
|
||||
Key::Pass => return,
|
||||
}
|
||||
clip(this.servers.text(f));
|
||||
cx.stop_propagation();
|
||||
@@ -2926,6 +2979,14 @@ mod tests {
|
||||
backup_label(&b("2026-10-19_11-28-57-before-v2026.10.20.zip")).1,
|
||||
"Before updating to v2026.10.20"
|
||||
);
|
||||
assert_eq!(
|
||||
backup_label(&b("2026-10-19_13-35-36-before-v2026-10-19-10.zip")).1,
|
||||
"Before updating to v2026.10.19.10"
|
||||
);
|
||||
assert_eq!(
|
||||
backup_label(&b("2026-10-14_17-46-40-before-update.zip")).1,
|
||||
"Before an update"
|
||||
);
|
||||
assert_eq!(
|
||||
backup_label(&b("2026-10-19_11-28-57-auto.zip")).1,
|
||||
"Automatic"
|
||||
@@ -2937,4 +2998,57 @@ mod tests {
|
||||
clip(&mut long);
|
||||
assert_eq!(long.chars().count(), 256);
|
||||
}
|
||||
|
||||
/// The keyboard in a one-line field, as the Create dialog, Players, Settings and the console use it.
|
||||
#[test]
|
||||
fn fields_type_as_people_expect() {
|
||||
let mut text = String::from("My server");
|
||||
// The suggestion is selected: the first letter replaces it.
|
||||
assert_eq!(
|
||||
type_key(&mut text, true, "f", Some("F"), false),
|
||||
Key::Edited
|
||||
);
|
||||
assert_eq!(text, "F");
|
||||
// Space, which some platforms send without a character.
|
||||
assert_eq!(
|
||||
type_key(&mut text, false, "space", None, false),
|
||||
Key::Edited
|
||||
);
|
||||
assert_eq!(
|
||||
type_key(&mut text, false, "n", Some("N"), false),
|
||||
Key::Edited
|
||||
);
|
||||
assert_eq!(text, "F N");
|
||||
assert_eq!(
|
||||
type_key(&mut text, false, "backspace", None, false),
|
||||
Key::Edited
|
||||
);
|
||||
assert_eq!(text, "F ");
|
||||
// Backspace on a suggestion clears it all.
|
||||
let mut text = String::from("Server 2");
|
||||
type_key(&mut text, true, "backspace", None, false);
|
||||
assert_eq!(text, "");
|
||||
// Paste replaces a suggestion too; the caller adds the clipboard.
|
||||
let mut text = String::from("Server 2");
|
||||
assert_eq!(type_key(&mut text, true, "v", Some("v"), true), Key::Paste);
|
||||
assert_eq!(text, "");
|
||||
// Tab, arrows and shortcuts aren't the field's: Tab moves the focus.
|
||||
let mut text = String::from("Ludo");
|
||||
for (key, ch, command) in [
|
||||
("tab", Some("\t"), false),
|
||||
("left", None, false),
|
||||
("a", Some("a"), true),
|
||||
] {
|
||||
assert_eq!(type_key(&mut text, false, key, ch, command), Key::Pass);
|
||||
}
|
||||
assert_eq!(
|
||||
type_key(&mut text, true, "enter", Some("\n"), false),
|
||||
Key::Enter
|
||||
);
|
||||
assert_eq!(
|
||||
type_key(&mut text, false, "escape", None, false),
|
||||
Key::Escape
|
||||
);
|
||||
assert_eq!(text, "Ludo");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,3 +83,47 @@ impl Theme {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// How strongly a selected suggestion in a text field is tinted with the accent.
|
||||
pub const SELECTION: f32 = 0.22;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{tokens, SELECTION};
|
||||
|
||||
/// `top` at `alpha` over `under`, as the GPU blends them.
|
||||
fn blend(top: u32, under: u32, alpha: f32) -> u32 {
|
||||
let channel = |shift: u32| {
|
||||
let (a, b) = ((top >> shift & 0xff) as f32, (under >> shift & 0xff) as f32);
|
||||
((a * alpha + b * (1. - alpha)).round() as u32) << shift
|
||||
};
|
||||
channel(16) | channel(8) | channel(0)
|
||||
}
|
||||
|
||||
/// What the Servers page draws besides text (whose colours horizon-theme's own test holds to
|
||||
/// WCAG AA on every background): the keyboard focus ring, the borders of a focused or refused
|
||||
/// field (WCAG 1.4.11 asks 3:1 for those), and text over a selected suggestion.
|
||||
#[test]
|
||||
fn marks_and_selections_read_clearly() {
|
||||
for p in [tokens::LIGHT, tokens::DARK] {
|
||||
let theme = if p.dark { "dark" } else { "light" };
|
||||
for (name, bg) in [
|
||||
("page", p.page),
|
||||
("plate_soft", p.plate_soft),
|
||||
("plate", p.plate),
|
||||
("surface", p.surface),
|
||||
] {
|
||||
let ring = tokens::contrast(p.accent, bg);
|
||||
assert!(ring >= 3.0, "{theme} focus ring on {name}: {ring:.2}");
|
||||
let refused = tokens::contrast(p.danger, bg);
|
||||
assert!(
|
||||
refused >= 3.0,
|
||||
"{theme} refused field on {name}: {refused:.2}"
|
||||
);
|
||||
}
|
||||
let selection = blend(p.accent, p.surface, SELECTION);
|
||||
let ratio = tokens::contrast(p.ink, selection);
|
||||
assert!(ratio >= 4.5, "{theme} text on a selection: {ratio:.2}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user