use crate::Event;
#[cfg(any(target_os = "macos", target_os = "windows", test))]
pub(crate) fn shortcut(control: bool, alt: bool, command: bool) -> bool {
(control && !alt) || command
}
#[cfg(any(target_os = "macos", test))]
pub(crate) fn is_apple_function_key(character: char) -> bool {
('\u{F700}'..='\u{F7FF}').contains(&character)
}
pub(crate) fn push_text(
typed: impl IntoIterator<Item = char>,
shortcut: bool,
out: &mut Vec<Event>,
) {
if shortcut {
return;
}
out.extend(
typed
.into_iter()
.filter(|c| !c.is_control())
.map(Event::Text),
);
}
#[cfg(any(target_os = "windows", test))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct Utf16 {
high: Option<u16>,
}
#[cfg(any(target_os = "windows", test))]
impl Utf16 {
pub(crate) fn push(&mut self, unit: u16) -> Option<char> {
let high = self.high.take();
match unit {
0xD800..=0xDBFF => {
self.high = Some(unit);
None
}
0xDC00..=0xDFFF => {
let high = high?;
let code =
0x1_0000 + ((u32::from(high) - 0xD800) << 10) + (u32::from(unit) - 0xDC00);
char::from_u32(code)
}
_ => char::from_u32(u32::from(unit)),
}
}
pub(crate) fn reset(&mut self) {
self.high = None;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn control_or_command_is_a_shortcut_and_alt_gr_is_not() {
assert!(!shortcut(false, false, false));
assert!(shortcut(true, false, false));
assert!(shortcut(false, false, true));
assert!(!shortcut(false, true, false));
assert!(!shortcut(true, true, false));
assert!(shortcut(false, true, true));
assert!(shortcut(true, true, true));
}
#[test]
fn apple_function_key_characters_are_told_from_typed_ones() {
for key in ['\u{F700}', '\u{F704}', '\u{F747}', '\u{F7FF}'] {
assert!(is_apple_function_key(key), "{key:?}");
}
for typed in ['\u{F8FF}', '\u{F6FF}', '\u{F800}', 'a', '\u{E000}'] {
assert!(!is_apple_function_key(typed), "{typed:?}");
}
}
fn typed(text: &str, shortcut: bool) -> Vec<Event> {
let mut out = vec![Event::Focused(true)];
push_text(text.chars(), shortcut, &mut out);
out.remove(0);
out
}
#[test]
fn each_character_is_an_event_in_order_after_what_was_there() {
let mut out = vec![Event::CloseRequested];
push_text("aĆ©ā¬š".chars(), false, &mut out);
assert_eq!(
out,
[
Event::CloseRequested,
Event::Text('a'),
Event::Text('Ć©'),
Event::Text('ā¬'),
Event::Text('š'),
]
);
assert_eq!(typed("", false), []);
}
#[test]
fn control_characters_are_left_out() {
assert_eq!(typed("\r\n\t\u{8}\u{1b}\u{7f}\u{1}\u{85}", false), []);
assert_eq!(typed("a\tb", false), [Event::Text('a'), Event::Text('b')]);
assert_eq!(
typed(" \u{200d}", false),
[Event::Text(' '), Event::Text('\u{200d}')]
);
}
#[test]
fn a_shortcut_types_nothing() {
assert_eq!(typed("a1 ", true), []);
}
#[test]
fn units_of_the_basic_plane_are_characters() {
let mut utf16 = Utf16::default();
assert_eq!(utf16.push(u16::from(b'a')), Some('a'));
assert_eq!(utf16.push(0x00E9), Some('Ć©'));
assert_eq!(utf16.push(0x20AC), Some('ā¬'));
assert_eq!(utf16.push(0xFFFD), Some('\u{FFFD}'));
assert_eq!(utf16.push(0x0000), Some('\0'));
}
#[test]
fn a_surrogate_pair_is_one_character() {
let mut utf16 = Utf16::default();
assert_eq!(utf16.push(0xD83D), None);
assert_eq!(utf16.push(0xDE00), Some('š'));
assert_eq!(utf16.push(0xDBFF), None);
assert_eq!(utf16.push(0xDFFF), Some('\u{10FFFF}'));
assert_eq!(utf16.push(0xD800), None);
assert_eq!(utf16.push(0xDC00), Some('\u{10000}'));
}
#[test]
fn a_surrogate_with_no_partner_gives_nothing() {
let mut utf16 = Utf16::default();
assert_eq!(utf16.push(0xDE00), None);
assert_eq!(utf16.push(0xD83D), None);
assert_eq!(utf16.push(u16::from(b'x')), Some('x'));
assert_eq!(utf16.push(0xDE00), None);
assert_eq!(utf16.push(0xD83D), None);
assert_eq!(utf16.push(0xD83D), None);
assert_eq!(utf16.push(0xDE00), Some('š'));
}
#[test]
fn a_reset_forgets_the_waiting_surrogate() {
let mut utf16 = Utf16::default();
assert_eq!(utf16.push(0xD83D), None);
utf16.reset();
assert_eq!(utf16.push(0xDE00), None);
assert_eq!(utf16, Utf16::default());
}
}