incular-runtime 0.1.1

Application runtime and scheduling foundations for Incular
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//! Flutter 3.41 `DefaultTextEditingShortcuts`: the keys that edit and move
//! the caret in a text field, per target platform.
//!
//! Flutter chooses one table per `defaultTargetPlatform`. The Windows, Linux
//! and Android tables share `_commonShortcuts` and `_clipboardShortcuts`; the
//! macOS table (reused for iOS) uses Option for words, Command for lines and
//! documents, and adds Emacs-style Control bindings. Activators match their
//! modifiers exactly, so a chord not listed here edits nothing.

use incular_core::{Code, Modifiers};

/// The platform whose `DefaultTextEditingShortcuts` table applies to text
/// fields (Flutter's `defaultTargetPlatform`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TextShortcutPlatform {
    Android,
    Ios,
    Linux,
    MacOs,
    Windows,
}

impl TextShortcutPlatform {
    /// The platform this build targets. In a browser it is the operating
    /// system the browser runs on, so a Mac uses Command shortcuts.
    #[must_use]
    pub fn current() -> Self {
        #[cfg(target_arch = "wasm32")]
        if let Some(platform) = Self::browser() {
            return platform;
        }
        if cfg!(target_os = "macos") {
            Self::MacOs
        } else if cfg!(target_os = "ios") {
            Self::Ios
        } else if cfg!(target_os = "android") {
            Self::Android
        } else if cfg!(target_os = "linux") {
            Self::Linux
        } else {
            Self::Windows
        }
    }

    /// Reads `navigator.userAgentData.platform` where available, then the
    /// legacy `navigator.platform` and user agent strings.
    #[cfg(target_arch = "wasm32")]
    fn browser() -> Option<Self> {
        use wasm_bindgen::JsValue;

        let get = |target: &JsValue, key: &str| {
            js_sys::Reflect::get(target, &JsValue::from_str(key))
                .ok()
                .filter(|value| !value.is_undefined() && !value.is_null())
        };
        let navigator = get(&js_sys::global(), "navigator")?;
        let described = [
            get(&navigator, "userAgentData").and_then(|data| get(&data, "platform")),
            get(&navigator, "platform"),
            get(&navigator, "userAgent"),
        ]
        .into_iter()
        .flatten()
        .filter_map(|value| value.as_string())
        .collect::<Vec<_>>()
        .join(" ")
        .to_ascii_lowercase();
        // Android user agents also mention Linux. iPadOS reports itself as a
        // Mac, whose table it shares.
        if described.contains("android") {
            Some(Self::Android)
        } else if ["iphone", "ipad", "ipod", "ios"]
            .iter()
            .any(|name| described.contains(name))
        {
            Some(Self::Ios)
        } else if described.contains("mac") {
            Some(Self::MacOs)
        } else if described.contains("win") {
            Some(Self::Windows)
        } else if described.contains("linux") || described.contains("cros") {
            Some(Self::Linux)
        } else {
            None
        }
    }

    const fn is_apple(self) -> bool {
        matches!(self, Self::MacOs | Self::Ios)
    }
}

/// What a text-editing chord asks the focused field to do. Each variant is the
/// Flutter intent of the same name.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TextIntent {
    DeleteCharacter {
        forward: bool,
    },
    DeleteToNextWordBoundary {
        forward: bool,
    },
    DeleteToLineBreak {
        forward: bool,
    },
    ExtendByCharacter {
        forward: bool,
        collapse: bool,
    },
    ExtendToNextWordBoundary {
        forward: bool,
        collapse: bool,
    },
    ExtendToNextWordBoundaryOrCaretLocation {
        forward: bool,
    },
    ExtendToLineBreak {
        forward: bool,
        collapse: bool,
        continues_at_wrap: bool,
    },
    ExtendToNextParagraphBoundary {
        forward: bool,
        collapse: bool,
    },
    ExtendToNextParagraphBoundaryOrCaretLocation {
        forward: bool,
    },
    ExtendToDocumentBoundary {
        forward: bool,
        collapse: bool,
    },
    ExpandToLineBreak {
        forward: bool,
    },
    ExpandToDocumentBoundary {
        forward: bool,
    },
    ExtendVerticallyToAdjacentLine {
        forward: bool,
        collapse: bool,
    },
    ExtendVerticallyToAdjacentPage {
        forward: bool,
        collapse: bool,
    },
    ScrollToDocumentBoundary {
        forward: bool,
    },
    ScrollPage {
        forward: bool,
    },
    Copy,
    Cut,
    Paste,
    SelectAll,
    Undo,
    Redo,
    TransposeCharacters,
    /// `DoNothingAndStopPropagationTextIntent`: the key belongs to the IME.
    DoNothing,
}

/// Resolves a key chord to its text-editing intent on `platform`.
pub(crate) fn resolve(
    platform: TextShortcutPlatform,
    code: Code,
    modifiers: Modifiers,
) -> Option<TextIntent> {
    let chord = Chord {
        ctrl: modifiers.ctrl(),
        alt: modifiers.alt(),
        shift: modifiers.shift(),
        meta: modifiers.meta(),
        num_lock: modifiers.contains(Modifiers::NUM_LOCK),
    };
    if platform.is_apple() {
        return mac(code, chord);
    }
    let table = match platform {
        TextShortcutPlatform::Linux => linux(code, chord),
        TextShortcutPlatform::Windows => windows(code, chord),
        _ => android(code, chord),
    };
    table
        .or_else(|| clipboard(code, chord))
        .or_else(|| common(code, chord))
}

#[derive(Clone, Copy)]
struct Chord {
    ctrl: bool,
    alt: bool,
    shift: bool,
    meta: bool,
    num_lock: bool,
}

impl Chord {
    /// Whether exactly the given modifiers are down (shift is matched by the
    /// callers that accept both states).
    fn only(self, ctrl: bool, alt: bool, shift: bool, meta: bool) -> bool {
        self.ctrl == ctrl && self.alt == alt && self.shift == shift && self.meta == meta
    }

    fn plain(self) -> bool {
        self.only(false, false, false, false)
    }
}

/// `_commonShortcuts`.
fn common(code: Code, c: Chord) -> Option<TextIntent> {
    use TextIntent::*;
    if c.meta {
        return None;
    }
    let collapse = !c.shift;
    let forward_of = |code| matches!(code, Code::ArrowRight | Code::ArrowDown | Code::PageDown);
    match code {
        // Delete shortcuts, with shift in either state.
        Code::Backspace => match (c.ctrl, c.alt) {
            (false, false) => Some(DeleteCharacter { forward: false }),
            (true, false) => Some(DeleteToNextWordBoundary { forward: false }),
            (false, true) => Some(DeleteToLineBreak { forward: false }),
            (true, true) => None,
        },
        Code::Delete => match (c.ctrl, c.alt) {
            (true, false) => Some(DeleteToNextWordBoundary { forward: true }),
            (false, true) => Some(DeleteToLineBreak { forward: true }),
            (false, false) if !c.shift => Some(DeleteCharacter { forward: true }),
            _ => None,
        },
        Code::ArrowLeft | Code::ArrowRight => {
            let forward = forward_of(code);
            match (c.ctrl, c.alt) {
                (false, false) => Some(ExtendByCharacter { forward, collapse }),
                (false, true) => Some(ExtendToLineBreak {
                    forward,
                    collapse,
                    continues_at_wrap: false,
                }),
                (true, false) => Some(ExtendToNextWordBoundary { forward, collapse }),
                (true, true) => None,
            }
        }
        Code::ArrowUp | Code::ArrowDown => {
            let forward = forward_of(code);
            match (c.ctrl, c.alt) {
                (false, false) => Some(ExtendVerticallyToAdjacentLine { forward, collapse }),
                (false, true) => Some(ExtendToDocumentBoundary { forward, collapse }),
                (true, false) if c.shift => Some(ExtendToNextParagraphBoundary {
                    forward,
                    collapse: false,
                }),
                _ => None,
            }
        }
        Code::PageUp | Code::PageDown if !c.ctrl && !c.alt => {
            Some(ExtendVerticallyToAdjacentPage {
                forward: forward_of(code),
                collapse,
            })
        }
        _ => None,
    }
}

/// `_clipboardShortcuts`.
fn clipboard(code: Code, c: Chord) -> Option<TextIntent> {
    use TextIntent::*;
    let ctrl_only = c.only(true, false, false, false);
    match code {
        Code::KeyX if ctrl_only => Some(Cut),
        Code::KeyC if ctrl_only => Some(Copy),
        Code::KeyV if ctrl_only => Some(Paste),
        Code::Delete if c.only(false, false, true, false) => Some(Cut),
        Code::Insert if ctrl_only => Some(Copy),
        Code::Insert if c.only(false, false, true, false) => Some(Paste),
        Code::KeyA if ctrl_only => Some(SelectAll),
        Code::KeyZ if ctrl_only => Some(Undo),
        Code::KeyZ if c.only(true, false, true, false) => Some(Redo),
        // These keys go to the IME when a field is focused.
        Code::Space | Code::Enter if c.plain() => Some(DoNothing),
        _ => None,
    }
}

/// Home/End on Windows and Android continue across soft wraps.
fn home_end(code: Code, c: Chord, continues_at_wrap: bool) -> Option<TextIntent> {
    use TextIntent::*;
    if c.alt || c.meta {
        return None;
    }
    let forward = match code {
        Code::Home => false,
        Code::End => true,
        _ => return None,
    };
    let collapse = !c.shift;
    Some(if c.ctrl {
        ExtendToDocumentBoundary { forward, collapse }
    } else {
        ExtendToLineBreak {
            forward,
            collapse,
            continues_at_wrap,
        }
    })
}

fn windows(code: Code, c: Chord) -> Option<TextIntent> {
    home_end(code, c, true)
}

fn android(code: Code, c: Chord) -> Option<TextIntent> {
    home_end(code, c, true)
}

/// `_linuxShortcuts` plus `_linuxNumpadShortcuts`.
fn linux(code: Code, c: Chord) -> Option<TextIntent> {
    use TextIntent::*;
    if let Some(intent) = home_end(code, c, false) {
        return Some(intent);
    }
    if c.alt || c.meta {
        return None;
    }
    // With NumLock on the numpad needs Shift to navigate, with it off Shift
    // must be released; Control selects the next word or paragraph.
    if c.shift != c.num_lock {
        return None;
    }
    let collapse = !c.num_lock;
    let word_or_paragraph = c.ctrl;
    match code {
        Code::Numpad6 | Code::Numpad4 => {
            let forward = code == Code::Numpad6;
            Some(if word_or_paragraph {
                ExtendToNextWordBoundary { forward, collapse }
            } else {
                ExtendByCharacter { forward, collapse }
            })
        }
        Code::Numpad8 | Code::Numpad2 => {
            let forward = code == Code::Numpad2;
            Some(if word_or_paragraph {
                ExtendToNextParagraphBoundary { forward, collapse }
            } else {
                ExtendVerticallyToAdjacentLine { forward, collapse }
            })
        }
        Code::Numpad9 | Code::Numpad3 if !word_or_paragraph => {
            Some(ExtendVerticallyToAdjacentPage {
                forward: code == Code::Numpad3,
                collapse,
            })
        }
        Code::Numpad7 | Code::Numpad1 if !word_or_paragraph => {
            Some(ExtendVerticallyToAdjacentLine {
                forward: code == Code::Numpad1,
                collapse,
            })
        }
        Code::NumpadDecimal => Some(if word_or_paragraph {
            DeleteToNextWordBoundary { forward: true }
        } else {
            DeleteCharacter { forward: true }
        }),
        _ => None,
    }
}

/// `_macShortcuts` (also `_iOSShortcuts`).
fn mac(code: Code, c: Chord) -> Option<TextIntent> {
    use TextIntent::*;
    let collapse = !c.shift;
    let forward_of = |code| matches!(code, Code::ArrowRight | Code::ArrowDown | Code::PageDown);
    let mods = (c.ctrl, c.alt, c.meta);
    match code {
        // Delete shortcuts, with shift in either state.
        Code::Backspace | Code::Delete => {
            let forward = code == Code::Delete;
            match mods {
                (false, false, false) => Some(DeleteCharacter { forward }),
                (false, true, false) => Some(DeleteToNextWordBoundary { forward }),
                (false, false, true) => Some(DeleteToLineBreak { forward }),
                _ => None,
            }
        }
        Code::ArrowLeft | Code::ArrowRight => {
            let forward = forward_of(code);
            match (mods, c.shift) {
                ((false, false, false), _) => Some(ExtendByCharacter { forward, collapse }),
                ((false, true, false), false) => Some(ExtendToNextWordBoundary {
                    forward,
                    collapse: true,
                }),
                ((false, true, false), true) => {
                    Some(ExtendToNextWordBoundaryOrCaretLocation { forward })
                }
                ((false, false, true), false) => Some(ExtendToLineBreak {
                    forward,
                    collapse: true,
                    continues_at_wrap: false,
                }),
                ((false, false, true), true) => Some(ExpandToLineBreak { forward }),
                _ => None,
            }
        }
        Code::ArrowUp | Code::ArrowDown => {
            let forward = forward_of(code);
            match (mods, c.shift) {
                ((false, false, false), _) => {
                    Some(ExtendVerticallyToAdjacentLine { forward, collapse })
                }
                ((false, true, false), false) => Some(ExtendToLineBreak {
                    forward,
                    collapse: true,
                    continues_at_wrap: false,
                }),
                ((false, true, false), true) => {
                    Some(ExtendToNextParagraphBoundaryOrCaretLocation { forward })
                }
                ((false, false, true), false) => Some(ExtendToDocumentBoundary {
                    forward,
                    collapse: true,
                }),
                ((false, false, true), true) => Some(ExpandToDocumentBoundary { forward }),
                _ => None,
            }
        }
        Code::Home | Code::End if mods == (false, false, false) => {
            let forward = code == Code::End;
            Some(if c.shift {
                ExpandToDocumentBoundary { forward }
            } else {
                ScrollToDocumentBoundary { forward }
            })
        }
        Code::PageUp | Code::PageDown if mods == (false, false, false) => {
            let forward = code == Code::PageDown;
            Some(if c.shift {
                ExtendVerticallyToAdjacentPage {
                    forward,
                    collapse: false,
                }
            } else {
                ScrollPage { forward }
            })
        }
        Code::KeyX if c.only(false, false, false, true) => Some(Cut),
        Code::KeyC if c.only(false, false, false, true) => Some(Copy),
        Code::KeyV if c.only(false, false, false, true) => Some(Paste),
        Code::KeyA if c.only(false, false, false, true) => Some(SelectAll),
        Code::KeyZ if c.only(false, false, false, true) => Some(Undo),
        Code::KeyZ if c.only(false, false, true, true) => Some(Redo),
        // Emacs-style bindings.
        Code::KeyE if c.only(true, false, false, false) => Some(ExtendToLineBreak {
            forward: true,
            collapse: true,
            continues_at_wrap: false,
        }),
        Code::KeyA if c.only(true, false, false, false) => Some(ExtendToLineBreak {
            forward: false,
            collapse: true,
            continues_at_wrap: false,
        }),
        Code::KeyF if c.only(true, false, false, false) => Some(ExtendByCharacter {
            forward: true,
            collapse: true,
        }),
        Code::KeyB if c.only(true, false, false, false) => Some(ExtendByCharacter {
            forward: false,
            collapse: true,
        }),
        Code::KeyN if c.only(true, false, false, false) => Some(ExtendVerticallyToAdjacentLine {
            forward: true,
            collapse: true,
        }),
        Code::KeyP if c.only(true, false, false, false) => Some(ExtendVerticallyToAdjacentLine {
            forward: false,
            collapse: true,
        }),
        Code::KeyT if c.only(true, false, false, false) => Some(TransposeCharacters),
        Code::Space | Code::Enter if c.plain() => Some(DoNothing),
        _ => None,
    }
}