nsh-core 0.8.0

Platform-independent core: shadow tree, styles, layout and mount instructions.
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
//! iOS navigation-bar buttons (`ios={{ headerLeftItems }}`, `ios={{
//! headerRightItems }}`).
//!
//! Rust apps build [`HeaderItem`]s with per-item [`HeaderItem::on_press`]
//! and menus of [`super::desktop::MenuItem`]s (their
//! [`super::desktop::MenuItem::on_select`]). [`register`] turns them into
//! the platform's [`crate::HeaderItem`]s with numeric ids, keeps the
//! handlers in [`super::menus::ViewMenus`], and a cleanup drops them when
//! the binding is replaced or the page goes. The platform reports a
//! chosen id as the root's [`crate::EventKind::Menu`].

use std::{fmt, rc::Rc, sync::Arc};

use super::{cleanup, desktop, menus::MenuHandler, reactive::Owner, view_menus};
use crate::{HeaderItemContent, HeaderSystemItem};

/// An iOS bar button for [`super::host::Screen::header_left_items`] and
/// [`super::host::Screen::header_right_items`]:
/// what it shows, an optional native menu of [`desktop::MenuItem`]s, and
/// handlers that run when the platform reports their ids as the root's
/// `Menu` event.
///
/// ```ignore
/// HeaderItem::system(HeaderSystemItem::Edit).on_press(edit)
/// HeaderItem::symbol_title("sparkles", "Upgrade").on_press(upgrade)
/// HeaderItem::symbol("ellipsis.circle")
///     .label("More")
///     .menu([
///         desktop::MenuItem::new("Share").on_select(share),
///         desktop::MenuItem::submenu("Danger", [
///             desktop::MenuItem::new("Delete").destructive(true).on_select(remove),
///         ])
///         .inline(true),
///     ])
/// ```
#[derive(Clone)]
pub struct HeaderItem {
    key: Option<Arc<str>>,
    content: HeaderItemContent,
    label: Option<Arc<str>>,
    disabled: bool,
    on_press: Option<MenuHandler>,
    menu: Option<Vec<desktop::MenuItem>>,
    shares_background: bool,
}

impl HeaderItem {
    fn of(content: HeaderItemContent) -> Self {
        Self {
            key: None,
            content,
            label: None,
            disabled: false,
            on_press: None,
            menu: None,
            shares_background: true,
        }
    }

    /// A system-item button.
    pub fn system(item: HeaderSystemItem) -> Self {
        Self::of(HeaderItemContent::SystemItem(item))
    }

    /// An SF Symbol button.
    pub fn symbol(name: impl Into<Arc<str>>) -> Self {
        Self::of(HeaderItemContent::Symbol(name.into()))
    }

    /// A titled button (`label` is what it shows).
    pub fn new(label: impl Into<Arc<str>>) -> Self {
        Self::of(HeaderItemContent::Label(label.into()))
    }

    /// A button showing an SF Symbol and `title` after it; VoiceOver reads
    /// the title unless [`Self::label`] is set.
    pub fn symbol_title(name: impl Into<Arc<str>>, title: impl Into<Arc<str>>) -> Self {
        Self::of(HeaderItemContent::SymbolTitle {
            symbol: name.into(),
            title: title.into(),
        })
    }

    /// Optional key for keeping a `UIBarButtonItem` across updates; also
    /// the button's accessibility identifier, which tests name it by
    /// (`id:<key>`).
    pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
        self.key = Some(key.into());
        self
    }

    /// VoiceOver's label when the content is a system item or symbol.
    pub fn label(mut self, label: impl Into<Arc<str>>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Whether the button shares its background with the bar buttons next
    /// to it (default true): on iOS 26 and later, false gives it a glass
    /// capsule of its own instead of the one its neighbors share
    /// (`UIBarButtonItem.sharesBackground`). Earlier iOS versions ignore
    /// it.
    pub fn shares_background(mut self, shares: bool) -> Self {
        self.shares_background = shares;
        self
    }

    /// Disables the button.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Runs `f` when the button is pressed and it has no menu.
    pub fn on_press(mut self, f: impl Fn() + 'static) -> Self {
        self.on_press = Some(Rc::new(f));
        self
    }

    /// Opens this native menu on press (the button's [`Self::on_press`] is
    /// unused while a menu is set).
    pub fn menu(mut self, items: impl IntoIterator<Item = desktop::MenuItem>) -> Self {
        self.menu = Some(items.into_iter().collect());
        self
    }
}

impl fmt::Debug for HeaderItem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HeaderItem")
            .field("key", &self.key)
            .field("content", &self.content)
            .field("label", &self.label)
            .field("disabled", &self.disabled)
            .field("shares_background", &self.shares_background)
            .field("menu", &self.menu.as_ref().map(|m| m.len()))
            .finish_non_exhaustive()
    }
}

/// Decodes `items` into the platform's form, keeps their handlers under a
/// generation that a cleanup drops when this binding is replaced or the
/// owner goes, and returns what [`crate::ScreenConfig::left_items`] /
/// [`crate::ScreenConfig::right_items`] holds.
pub(crate) fn register(items: Vec<HeaderItem>) -> Vec<crate::HeaderItem> {
    let menus = view_menus();
    let owner = Owner::current();
    let mut handlers = Vec::new();
    let mac = super::os() == crate::platform::Os::MacOs;
    let decoded = items
        .into_iter()
        .map(|item| decode_item(item, mac, &owner, &mut handlers))
        .collect();
    let generation = menus.borrow_mut().push_header(handlers);
    let weak = Rc::downgrade(&menus);
    cleanup(move || {
        if let Some(menus) = weak.upgrade() {
            menus.borrow_mut().take_header(generation);
        }
    });
    decoded
}

fn decode_item(
    item: HeaderItem,
    mac: bool,
    owner: &Owner,
    handlers: &mut Vec<(u32, MenuHandler)>,
) -> crate::HeaderItem {
    let menus = view_menus();
    let press_id = match item.on_press {
        Some(f) if item.menu.is_none() => {
            let id = menus.borrow_mut().next_id();
            let owner = *owner;
            handlers.push((id, Rc::new(move || owner.run(&*f))));
            id
        }
        _ => 0,
    };
    let menu = item.menu.map(|items| {
        // Reuse the desktop menu decoder so item ids and handlers match.
        desktop::decode_for_header(items, mac, owner, handlers)
    });
    crate::HeaderItem {
        key: item.key,
        content: item.content,
        label: item.label,
        disabled: item.disabled,
        press_id,
        menu,
        shares_background: item.shares_background,
    }
}

/// Whether two lists keep the same `UIBarButtonItem`s: count,
/// keys (or unkeyed contents), content kind that can update in place, and
/// whether each has a menu. A label's title or a symbol's image can change
/// without rebuilding; a system-item change or a content-kind change needs
/// a new button. Menus' contents, accessibility labels and `disabled` can
/// change either way.
#[doc(hidden)]
pub fn same_buttons(a: &[crate::HeaderItem], b: &[crate::HeaderItem]) -> bool {
    a.len() == b.len()
        && a.iter()
            .zip(b)
            .all(|(x, y)| keepable(x, y) && x.menu.is_some() == y.menu.is_some())
}

/// What a kept button ([`same_buttons`]) shows once updated to an
/// item: the platforms apply it to the button they kept, so a changed
/// label, symbol, accessibility label or `disabled` reaches the screen.
#[doc(hidden)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeptButton<'a> {
    pub content: KeptContent<'a>,
    /// VoiceOver's label; `None` clears the one set before.
    pub label: Option<&'a str>,
    pub enabled: bool,
    /// Whether it shares its background with its neighbors (iOS 26+).
    pub shares_background: bool,
}

/// A kept button's content after an update.
#[doc(hidden)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeptContent<'a> {
    /// This title, and no image.
    Title(&'a str),
    /// This SF Symbol, and no title; `fallback` is the title where the
    /// platform has no such symbol.
    Symbol { name: &'a str, fallback: &'a str },
    /// This SF Symbol with this title after it (the title alone where the
    /// platform has no such symbol).
    SymbolTitle { name: &'a str, title: &'a str },
    /// A system item: kept only while it is the same item, so its look
    /// stays as it is.
    Unchanged,
}

/// What the button kept for `item` shows ([`KeptButton`]).
#[doc(hidden)]
pub fn kept_button(item: &crate::HeaderItem) -> KeptButton<'_> {
    let content = match &item.content {
        HeaderItemContent::Label(title) => KeptContent::Title(title),
        HeaderItemContent::Symbol(name) => KeptContent::Symbol {
            name,
            fallback: item.label.as_deref().unwrap_or(name),
        },
        HeaderItemContent::SymbolTitle { symbol, title } => KeptContent::SymbolTitle {
            name: symbol,
            title,
        },
        _ => KeptContent::Unchanged,
    };
    KeptButton {
        content,
        label: item.label.as_deref(),
        enabled: !item.disabled,
        shares_background: item.shares_background,
    }
}

/// Whether UIKit can keep `a`'s `UIBarButtonItem` and apply `b` in place.
fn keepable(a: &crate::HeaderItem, b: &crate::HeaderItem) -> bool {
    match (a.key.as_deref(), b.key.as_deref()) {
        (Some(ka), Some(kb)) if ka == kb => content_keeps_button(&a.content, &b.content),
        (None, None) => a.content == b.content,
        _ => false,
    }
}

/// Label, symbol and symbol-with-title content update in place; a system
/// item only when it is the same item (UIKit cannot change a system item on
/// an existing button).
fn content_keeps_button(a: &HeaderItemContent, b: &HeaderItemContent) -> bool {
    match (a, b) {
        (HeaderItemContent::Label(_), HeaderItemContent::Label(_)) => true,
        (HeaderItemContent::Symbol(_), HeaderItemContent::Symbol(_)) => true,
        (HeaderItemContent::SymbolTitle { .. }, HeaderItemContent::SymbolTitle { .. }) => true,
        (HeaderItemContent::SystemItem(x), HeaderItemContent::SystemItem(y)) => x == y,
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::*;
    use crate::{HeaderItem as PlatformHeaderItem, HeaderSystemItem};

    fn keyed(key: &str, content: HeaderItemContent) -> PlatformHeaderItem {
        PlatformHeaderItem {
            key: Some(Arc::from(key)),
            content,
            label: None,
            disabled: false,
            press_id: 0,
            menu: None,
            shares_background: true,
        }
    }

    #[test]
    fn keyed_label_change_keeps_button() {
        let a = keyed("edit", HeaderItemContent::Label(Arc::from("Edit")));
        let b = keyed("edit", HeaderItemContent::Label(Arc::from("Done")));
        assert!(
            same_buttons(&[a], &[b]),
            "same key + Label→Label: update title in place"
        );
    }

    #[test]
    fn keyed_symbol_change_keeps_button() {
        let a = keyed("star", HeaderItemContent::Symbol(Arc::from("star")));
        let b = keyed("star", HeaderItemContent::Symbol(Arc::from("star.fill")));
        assert!(
            same_buttons(&[a], &[b]),
            "same key + Symbol→Symbol: update image in place"
        );
    }

    #[test]
    fn a_kept_button_shows_the_new_title_symbol_label_and_state() {
        // Edit → Done: the same key, a new title.
        let done = keyed("edit", HeaderItemContent::Label(Arc::from("Done")));
        assert_eq!(kept_button(&done).content, KeptContent::Title("Done"));
        // star → star.fill: a new image; the label is the fallback title.
        let mut filled = keyed("fav", HeaderItemContent::Symbol(Arc::from("star.fill")));
        filled.label = Some(Arc::from("Favorite"));
        assert_eq!(
            kept_button(&filled).content,
            KeptContent::Symbol {
                name: "star.fill",
                fallback: "Favorite"
            }
        );
        // An accessibility label taken away is cleared, not kept.
        filled.label = None;
        filled.disabled = true;
        let kept = kept_button(&filled);
        assert_eq!(kept.label, None);
        assert!(!kept.enabled);
        assert_eq!(
            kept.content,
            KeptContent::Symbol {
                name: "star.fill",
                fallback: "star.fill"
            }
        );
        // A system item keeps its look.
        let system = keyed("x", HeaderItemContent::SystemItem(HeaderSystemItem::Edit));
        assert_eq!(kept_button(&system).content, KeptContent::Unchanged);
    }

    #[test]
    fn keyed_symbol_title_change_keeps_button() {
        let a = keyed(
            "plan",
            HeaderItemContent::SymbolTitle {
                symbol: Arc::from("sparkles"),
                title: Arc::from("Upgrade"),
            },
        );
        let b = keyed(
            "plan",
            HeaderItemContent::SymbolTitle {
                symbol: Arc::from("star"),
                title: Arc::from("Plans"),
            },
        );
        assert!(same_buttons(&[a], std::slice::from_ref(&b)));
        assert_eq!(
            kept_button(&b).content,
            KeptContent::SymbolTitle {
                name: "star",
                title: "Plans"
            }
        );
        // A symbol alone is a different button (an image, no title).
        let c = keyed("plan", HeaderItemContent::Symbol(Arc::from("star")));
        assert!(!same_buttons(&[b], &[c]));
    }

    #[test]
    fn keyed_system_item_change_rebuilds() {
        let a = keyed("x", HeaderItemContent::SystemItem(HeaderSystemItem::Edit));
        let b = keyed("x", HeaderItemContent::SystemItem(HeaderSystemItem::Done));
        assert!(
            !same_buttons(&[a], &[b]),
            "UIKit cannot change a system item in place"
        );
    }

    #[test]
    fn keyed_content_kind_change_rebuilds() {
        let a = keyed("x", HeaderItemContent::Label(Arc::from("Edit")));
        let b = keyed("x", HeaderItemContent::Symbol(Arc::from("pencil")));
        assert!(
            !same_buttons(&[a], &[b]),
            "Label→Symbol needs a new UIBarButtonItem"
        );
    }
}