Line data Source code
1 : part of 'foss_menu.dart';
2 :
3 : /// What a [FossMenuItem] renders as. Kept private: the kind is chosen by the
4 : /// constructor a caller picks, never passed in.
5 : enum _MenuItemKind { item, separator, label }
6 :
7 : /// One entry in a [FossMenu]: an action row, a divider, or a group heading.
8 : ///
9 : /// This is data passed to [FossMenu.items], not a widget, so the menu owns the
10 : /// row metrics, the colors, and the highlight. One list carries all three
11 : /// kinds, so a caller never passes a parallel structure to place a divider.
12 : ///
13 : /// An action row is the default constructor. [onSelected] null leaves the row
14 : /// visible, at full strength, and doing nothing, which is how a "coming soon"
15 : /// entry is expressed; `enabled: false` dims it and takes it out of the
16 : /// keyboard walk. Use [FossMenuItem.separator] for a divider and
17 : /// [FossMenuItem.label] for a heading above a group.
18 : ///
19 : /// ```dart
20 : /// const items = [
21 : /// FossMenuItem.label('Actions'),
22 : /// FossMenuItem(label: 'New file', shortcut: 'Ctrl N'),
23 : /// FossMenuItem.separator(),
24 : /// FossMenuItem(label: 'Delete', destructive: true),
25 : /// ];
26 : /// ```
27 : @immutable
28 : @FossSince('0.1.3')
29 : class FossMenuItem {
30 : /// Creates an action row showing [label].
31 5 : const FossMenuItem({
32 : required this.label,
33 : this.icon,
34 : this.shortcut,
35 : this.onSelected,
36 : this.enabled = true,
37 : this.destructive = false,
38 : this.inset = false,
39 : }) : _kind = _MenuItemKind.item;
40 :
41 : /// Creates a divider between two groups of rows. It takes no input and is
42 : /// skipped by the keyboard walk.
43 0 : const FossMenuItem.separator()
44 : : label = '',
45 : icon = null,
46 : shortcut = null,
47 : onSelected = null,
48 : enabled = true,
49 : destructive = false,
50 : inset = false,
51 : _kind = _MenuItemKind.separator;
52 :
53 : /// Creates a heading above a group of rows, showing [label]. It is announced
54 : /// with the menu but takes no input.
55 0 : const FossMenuItem.label(this.label, {this.inset = false})
56 : : icon = null,
57 : shortcut = null,
58 : onSelected = null,
59 : enabled = true,
60 : destructive = false,
61 : _kind = _MenuItemKind.label;
62 :
63 : /// The row's text, and the name it announces. Empty on a separator.
64 : final String label;
65 :
66 : /// A glyph at the start of the row, sized and colored by the menu. Any
67 : /// widget; no icon dependency.
68 : final Widget? icon;
69 :
70 : /// The key combination shown at the end of the row, such as `Ctrl N`.
71 : ///
72 : /// Display only: the menu never binds it. An app that wants the combination
73 : /// to work registers it itself.
74 : final String? shortcut;
75 :
76 : /// Called when the row is selected by a tap, `Enter`, or `Space`. Null
77 : /// leaves the row inert: it stays at full strength, keeps its place in the
78 : /// keyboard walk, and selecting it does nothing.
79 : final VoidCallback? onSelected;
80 :
81 : /// Whether the row accepts input. False dims it to 64% and skips it in the
82 : /// keyboard walk.
83 : final bool enabled;
84 :
85 : /// Whether the row reads as a destructive action, which colors its text and
86 : /// glyph accordingly.
87 : final bool destructive;
88 :
89 : /// Whether the row starts past the glyph column, so its text lines up with
90 : /// the rows that carry an [icon].
91 : final bool inset;
92 :
93 : final _MenuItemKind _kind;
94 :
95 : /// Whether this renders as an action row, which reports the menu-item role
96 : /// whether or not it is enabled.
97 12 : bool get _isRow => _kind == _MenuItemKind.item;
98 :
99 : /// Whether the row can take a selection: an action row that is enabled. An
100 : /// inert row qualifies, and selecting it does nothing.
101 12 : bool get _selectable => _isRow && enabled;
102 : }
|