rungui 0.2.0

Small portable native GUI library: a thin layer over GTK3, Win32 and AppKit with hand-declared FFI
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//! Unicode helpers shared by the core and the backends. Pure functions, no platform code.
//!
//! * Index conversion between UTF-8 byte offsets (what Rust uses), UTF-16 code-unit offsets
//!   (Win32, Cocoa `NSRange`, AT-SPI/UIA text ranges) and `char` offsets.
//! * Grapheme-cluster stepping (approximation of UAX #29 without tables: combining marks,
//!   variation selectors, emoji modifiers, ZWJ sequences, flags, CRLF, Hangul syllables), so
//!   cursor/backspace/truncation helpers never split what the user sees as one character.
//! * Bidi-neutral helpers: first-strong direction, stripping/isolating bidi controls.
//! * Conversion to NUL-terminated UTF-16 / UTF-8 without silent truncation at interior NULs.
//! * Mnemonics ("&File", "&&" for a literal ampersand) translated per platform.
//!
//! Mnemonics apply only to Button, CheckBox, RadioButton, GroupBox, Menu and MenuItem text. The
//! core passes text through verbatim; a backend calls [`to_gtk_mnemonic`] (GTK `_`-style),
//! [`to_win32_mnemonic`] (native `&`, safe against stray prefix handling) or [`strip_mnemonic`]
//! (Cocoa has no mnemonics) when pushing `Prop::Text` for those kinds. Accessible names always
//! use [`strip_mnemonic`].

use std::ffi::CString;

// ------------------------------------------------------------------ index conversion

/// Number of UTF-16 code units in `s`.
pub fn utf16_len(s: &str) -> usize {
    s.chars().map(char::len_utf16).sum()
}

/// UTF-8 byte offset -> UTF-16 offset. `None` if `byte` is out of range or inside a character.
pub fn utf8_to_utf16(s: &str, byte: usize) -> Option<usize> {
    if byte > s.len() || !s.is_char_boundary(byte) {
        return None;
    }
    Some(utf16_len(&s[..byte]))
}

/// UTF-16 offset -> UTF-8 byte offset. `None` if out of range or in the middle of a surrogate pair.
pub fn utf16_to_utf8(s: &str, unit: usize) -> Option<usize> {
    let mut u = 0;
    for (b, c) in s.char_indices() {
        if u == unit {
            return Some(b);
        }
        if u > unit {
            return None;
        }
        u += c.len_utf16();
    }
    if u == unit { Some(s.len()) } else { None }
}

/// Like [`utf8_to_utf16`] but never fails: out of range clamps to the end, a byte inside a
/// character snaps back to the start of that character.
pub fn utf8_to_utf16_clamped(s: &str, byte: usize) -> usize {
    utf16_len(&s[..floor_boundary(s, byte)])
}

/// Like [`utf16_to_utf8`] but never fails: out of range clamps to the end, an offset inside a
/// surrogate pair snaps back to the start of the pair.
pub fn utf16_to_utf8_clamped(s: &str, unit: usize) -> usize {
    let mut u = 0;
    for (b, c) in s.char_indices() {
        let n = c.len_utf16();
        if unit < u + n {
            return b;
        }
        u += n;
    }
    s.len()
}

/// Largest char boundary `<= byte` (clamped to `s.len()`).
pub fn floor_boundary(s: &str, byte: usize) -> usize {
    let mut b = byte.min(s.len());
    while !s.is_char_boundary(b) {
        b -= 1;
    }
    b
}

/// Smallest char boundary `>= byte` (clamped to `s.len()`).
pub fn ceil_boundary(s: &str, byte: usize) -> usize {
    let mut b = byte.min(s.len());
    while !s.is_char_boundary(b) {
        b += 1;
    }
    b
}

/// UTF-8 byte offset -> `char` (scalar value) offset.
pub fn byte_to_char(s: &str, byte: usize) -> usize {
    s[..floor_boundary(s, byte)].chars().count()
}

/// `char` offset -> UTF-8 byte offset (clamped to the end).
pub fn char_to_byte(s: &str, ch: usize) -> usize {
    s.char_indices().nth(ch).map_or(s.len(), |(b, _)| b)
}

// ------------------------------------------------------------------ graphemes

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
enum Gc {
    Cr,
    Lf,
    Control,
    Extend,
    Zwj,
    Ri,
    Pict,
    L,
    V,
    T,
    Lv,
    Lvt,
    Other,
}

fn gc(c: char) -> Gc {
    let u = c as u32;
    match u {
        0x0D => Gc::Cr,
        0x0A => Gc::Lf,
        0x200D => Gc::Zwj,
        0..=0x1F
        | 0x7F..=0x9F
        | 0x2028
        | 0x2029
        | 0xFEFF
        | 0x200B
        | 0x200E
        | 0x200F
        | 0x202A..=0x202E
        | 0x2060..=0x206F => Gc::Control,
        0x300..=0x36F
        | 0x483..=0x489
        | 0x591..=0x5BD
        | 0x5BF
        | 0x5C1..=0x5C2
        | 0x5C4..=0x5C5
        | 0x5C7
        | 0x610..=0x61A
        | 0x64B..=0x65F
        | 0x670
        | 0x6D6..=0x6DC
        | 0x6DF..=0x6E4
        | 0x6E7..=0x6E8
        | 0x6EA..=0x6ED
        | 0x711
        | 0x730..=0x74A
        | 0x7A6..=0x7B0
        | 0x900..=0x903
        | 0x93A..=0x93C
        | 0x93E..=0x94F
        | 0x951..=0x957
        | 0x962..=0x963
        | 0x981..=0x983
        | 0x9BC
        | 0x9BE..=0x9CD
        | 0xA01..=0xA03
        | 0xA3C..=0xA51
        | 0xB01..=0xB03
        | 0xB3C..=0xB57
        | 0xBBE..=0xBCD
        | 0xC00..=0xC04
        | 0xC3E..=0xC56
        | 0xCBC..=0xCD6
        | 0xD00..=0xD03
        | 0xD3B..=0xD4D
        | 0xE31
        | 0xE34..=0xE3A
        | 0xE47..=0xE4E
        | 0xEB1
        | 0xEB4..=0xEBC
        | 0xEC8..=0xECE
        | 0xF18..=0xF19
        | 0xF35
        | 0xF37
        | 0xF39
        | 0xF71..=0xF84
        | 0x102B..=0x103E
        | 0x135D..=0x135F
        | 0x1AB0..=0x1AFF
        | 0x1DC0..=0x1DFF
        | 0x200C
        | 0x20D0..=0x20FF
        | 0x302A..=0x302F
        | 0x3099..=0x309A
        | 0xFE00..=0xFE0F
        | 0xFE20..=0xFE2F
        | 0x1F3FB..=0x1F3FF
        | 0xE0020..=0xE007F
        | 0xE0100..=0xE01EF => Gc::Extend,
        0x1F1E6..=0x1F1FF => Gc::Ri,
        0xA9
        | 0xAE
        | 0x203C
        | 0x2049
        | 0x2122
        | 0x2139
        | 0x2194..=0x21AA
        | 0x231A..=0x23FF
        | 0x24C2
        | 0x25AA..=0x25FE
        | 0x2600..=0x27BF
        | 0x2934..=0x2935
        | 0x2B05..=0x2B55
        | 0x3030
        | 0x303D
        | 0x3297
        | 0x3299
        | 0x1F000..=0x1FAFF => Gc::Pict,
        0x1100..=0x115F | 0xA960..=0xA97C => Gc::L,
        0x1160..=0x11A7 | 0xD7B0..=0xD7C6 => Gc::V,
        0x11A8..=0x11FF | 0xD7CB..=0xD7FB => Gc::T,
        0xAC00..=0xD7A3 => {
            if (u - 0xAC00) % 28 == 0 {
                Gc::Lv
            } else {
                Gc::Lvt
            }
        }
        _ => Gc::Other,
    }
}

/// Is there a grapheme boundary between `a` and `b`? `prev_pict` / `ri_run` carry context.
fn breaks(a: Gc, b: Gc, pict_zwj: bool, ri_odd: bool) -> bool {
    use Gc::*;
    match (a, b) {
        (Cr, Lf) => false,
        (Cr | Lf | Control, _) | (_, Cr | Lf | Control) => true,
        (L, L | V | Lv | Lvt) | (Lv | V, V | T) | (Lvt | T, T) => false,
        (_, Extend | Zwj) => false,
        (Zwj, Pict) if pict_zwj => false,
        (Ri, Ri) if ri_odd => false,
        _ => true,
    }
}

/// Byte offset of the end of the grapheme cluster starting at `from` (`s.len()` at the end).
/// `from` is snapped down to a char boundary. Always makes progress.
pub fn next_grapheme(s: &str, from: usize) -> usize {
    let from = floor_boundary(s, from);
    let mut it = s[from..].char_indices();
    let Some((_, first)) = it.next() else {
        return s.len();
    };
    let mut prev = gc(first);
    let mut pict_seen = prev == Gc::Pict; // inside Pict Extend* ZWJ ?
    let mut pict_zwj = false;
    let mut ri = usize::from(prev == Gc::Ri);
    for (i, c) in it {
        let cur = gc(c);
        if breaks(prev, cur, pict_zwj, ri % 2 == 1) {
            return from + i;
        }
        match cur {
            Gc::Pict => {
                pict_seen = true;
                pict_zwj = false;
            }
            Gc::Extend => {}
            Gc::Zwj => pict_zwj = pict_seen,
            _ => {
                pict_seen = false;
                pict_zwj = false;
            }
        }
        ri = if cur == Gc::Ri { ri + 1 } else { 0 };
        prev = cur;
    }
    s.len()
}

/// Byte offset of the start of the grapheme cluster that ends at `from` (0 at the start).
pub fn prev_grapheme(s: &str, from: usize) -> usize {
    let from = floor_boundary(s, from);
    if from == 0 {
        return 0;
    }
    // clusters are short; scan forward from a safe restart point (a hard boundary) for exactness
    let mut start = 0;
    let head = &s[..from];
    // restart at the last control char / newline (a cluster of its own, or CR LF), bounded to keep
    // this cheap on long lines
    if let Some((i, c)) = head
        .char_indices()
        .rev()
        .find(|(_, c)| matches!(gc(*c), Gc::Control | Gc::Lf | Gc::Cr))
    {
        start = if gc(c) == Gc::Lf && head[..i].ends_with('\r') {
            i - 1
        } else {
            i
        };
    }
    let mut last = start;
    let mut p = start;
    while p < from {
        last = p;
        p = next_grapheme(s, p);
    }
    if p == from {
        last
    } else {
        floor_boundary(s, from - 1)
    }
}

/// Iterator over grapheme clusters.
pub fn graphemes(s: &str) -> impl Iterator<Item = &str> {
    let mut pos = 0;
    std::iter::from_fn(move || {
        if pos >= s.len() {
            return None;
        }
        let e = next_grapheme(s, pos);
        let g = &s[pos..e];
        pos = e;
        Some(g)
    })
}

pub fn grapheme_count(s: &str) -> usize {
    graphemes(s).count()
}

/// Snap a byte offset to the nearest grapheme boundary at or before it.
pub fn floor_grapheme(s: &str, byte: usize) -> usize {
    let b = floor_boundary(s, byte);
    let mut p = 0;
    loop {
        let n = next_grapheme(s, p);
        if n > b || n == p {
            return p;
        }
        p = n;
        if p == b {
            return p;
        }
    }
}

/// Keep at most `max` grapheme clusters.
pub fn truncate_graphemes(s: &str, max: usize) -> &str {
    let mut p = 0;
    for _ in 0..max {
        if p >= s.len() {
            break;
        }
        p = next_grapheme(s, p);
    }
    &s[..p.min(s.len())]
}

// ------------------------------------------------------------------ bidi

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Dir {
    Ltr,
    Rtl,
    /// No strongly directional character (digits, punctuation, empty).
    Neutral,
}

/// Is `c` a strong right-to-left character (Hebrew, Arabic, Syriac, Thaana, NKo, ... and the
/// presentation forms)?
pub fn is_rtl_char(c: char) -> bool {
    matches!(c as u32,
        0x590..=0x5FF | 0x600..=0x6FF | 0x700..=0x74F | 0x750..=0x77F | 0x780..=0x7BF | 0x7C0..=0x7FF
        | 0x800..=0x8FF | 0xFB1D..=0xFDFF | 0xFE70..=0xFEFF | 0x10800..=0x10FFF | 0x1E800..=0x1EFFF)
        && !c.is_numeric()
}

/// Direction of the first strong character (UBA rule P2/P3 without isolates handling).
pub fn first_strong(s: &str) -> Dir {
    for c in s.chars() {
        if is_rtl_char(c) {
            return Dir::Rtl;
        }
        if c.is_alphabetic() {
            return Dir::Ltr;
        }
    }
    Dir::Neutral
}

pub fn is_bidi_control(c: char) -> bool {
    matches!(c as u32, 0x200E | 0x200F | 0x061C | 0x202A..=0x202E | 0x2066..=0x2069)
}

/// Remove all bidi formatting characters (for comparison, accessible names, file names).
pub fn strip_bidi_controls(s: &str) -> String {
    s.chars().filter(|c| !is_bidi_control(*c)).collect()
}

/// Wrap untrusted text in a First-Strong Isolate so it cannot reorder surrounding text
/// (e.g. a file name inserted into a sentence). Idempotent for already-balanced input is not
/// attempted; call once at the point of composition.
pub fn isolate(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 6);
    out.push('\u{2068}');
    out.push_str(&strip_unbalanced_isolates(s));
    out.push('\u{2069}');
    out
}

fn strip_unbalanced_isolates(s: &str) -> String {
    // an embedded PDI could close our isolate early; drop isolate controls from the payload
    s.chars()
        .filter(|c| !matches!(*c as u32, 0x2066..=0x2069))
        .collect()
}

// ------------------------------------------------------------------ platform string conversion

/// Replace NULs, which cannot be represented in C strings, by U+FFFD (instead of truncating).
fn no_nul(s: &str) -> std::borrow::Cow<'_, str> {
    if s.contains('\0') {
        s.replace('\0', "\u{FFFD}").into()
    } else {
        s.into()
    }
}

/// UTF-16 with a trailing NUL, for Win32 `W` functions. Interior NULs become U+FFFD.
pub fn to_wide_nul(s: &str) -> Vec<u16> {
    no_nul(s).encode_utf16().chain(std::iter::once(0)).collect()
}

/// Strict decode (lone surrogates -> `None`).
pub fn from_utf16(w: &[u16]) -> Option<String> {
    String::from_utf16(w).ok()
}

/// Decode UTF-16 up to the first NUL; lone surrogates become U+FFFD (the only lossy case, and
/// unavoidable: Rust strings cannot hold them).
pub fn from_wide_nul(w: &[u16]) -> String {
    let end = w.iter().position(|c| *c == 0).unwrap_or(w.len());
    String::from_utf16_lossy(&w[..end])
}

/// Decode a NUL-terminated UTF-16 buffer from a raw pointer.
///
/// # Safety
/// `p` must be null or point to a NUL-terminated `u16` sequence.
pub unsafe fn from_wide_ptr(p: *const u16) -> String {
    if p.is_null() {
        return String::new();
    }
    let mut n = 0;
    while unsafe { *p.add(n) } != 0 {
        n += 1;
    }
    String::from_utf16_lossy(unsafe { std::slice::from_raw_parts(p, n) })
}

/// C string for GTK/Cocoa APIs. Interior NULs become U+FFFD.
pub fn to_cstring(s: &str) -> CString {
    CString::new(no_nul(s).as_bytes()).unwrap_or_default()
}

/// Decode a NUL-terminated UTF-8 C string; invalid sequences become U+FFFD.
///
/// # Safety
/// `p` must be null or a valid NUL-terminated C string.
pub unsafe fn from_cptr(p: *const std::ffi::c_char) -> String {
    if p.is_null() {
        return String::new();
    }
    unsafe { std::ffi::CStr::from_ptr(p) }
        .to_string_lossy()
        .into_owned()
}

// ------------------------------------------------------------------ mnemonics

/// A parsed "&File"-style label.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Mnemonic {
    /// Label with markers removed and `&&` collapsed to `&`.
    pub text: String,
    /// Byte offset in `text` of the character to underline.
    pub index: Option<usize>,
}

impl Mnemonic {
    /// The mnemonic character (the full grapheme cluster that gets underlined).
    pub fn key(&self) -> Option<&str> {
        let i = self.index?;
        Some(&self.text[i..next_grapheme(&self.text, i)])
    }
}

/// Parse `&X` (first single `&` wins; later single `&` are dropped; `&&` is a literal `&`; a
/// trailing lone `&` is dropped).
pub fn parse_mnemonic(s: &str) -> Mnemonic {
    let mut text = String::with_capacity(s.len());
    let mut index = None;
    let mut it = s.chars().peekable();
    while let Some(c) = it.next() {
        if c != '&' {
            text.push(c);
            continue;
        }
        match it.peek() {
            Some('&') => {
                it.next();
                text.push('&');
            }
            Some(_) => {
                if index.is_none() {
                    index = Some(text.len());
                }
            }
            None => {}
        }
    }
    Mnemonic { text, index }
}

/// Label without mnemonic markers (Cocoa, accessible names).
pub fn strip_mnemonic(s: &str) -> String {
    parse_mnemonic(s).text
}

/// GTK form: `_` marks the mnemonic, literal underscores are doubled.
pub fn to_gtk_mnemonic(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 1);
    let mut it = s.chars().peekable();
    let mut used = false;
    while let Some(c) = it.next() {
        match c {
            '_' => out.push_str("__"),
            '&' => match it.peek() {
                Some('&') => {
                    it.next();
                    out.push('&');
                }
                Some(_) if !used => {
                    used = true;
                    out.push('_');
                }
                _ => {}
            },
            c => out.push(c),
        }
    }
    out
}

/// Win32 form: only the first single `&` keeps its meaning; surplus markers are removed so
/// stray ampersands cannot hide characters; `&&` stays `&&` (renders as `&`).
pub fn to_win32_mnemonic(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 1);
    let mut it = s.chars().peekable();
    let mut used = false;
    while let Some(c) = it.next() {
        if c != '&' {
            out.push(c);
            continue;
        }
        match it.peek() {
            Some('&') => {
                it.next();
                out.push_str("&&");
            }
            Some(_) if !used => {
                used = true;
                out.push('&');
            }
            _ => {}
        }
    }
    out
}

/// Escape plain text so it displays literally when fed to Win32 controls with prefix processing.
pub fn escape_win32_literal(s: &str) -> String {
    s.replace('&', "&&")
}