zng_ext_input/focus/
cmd.rs

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
//! Commands that control focus and [`Command`] extensions.
//!
//! [`Command`]: zng_app::event::Command

use zng_app::{
    event::{command, Command, CommandHandle, CommandInfoExt, CommandNameExt, CommandScope, EventArgs},
    shortcut::{shortcut, CommandShortcutExt},
    update::EventUpdate,
    widget::info::WidgetInfo,
};
use zng_var::{merge_var, BoxedVar};

use super::*;

command! {
    /// Represents the **focus next** action.
    pub static FOCUS_NEXT_CMD = {
        l10n!: true,
        name: "Focus Next",
        info: "Focus next focusable",
        shortcut: shortcut!(Tab),
    };

    /// Represents the **focus previous** action.
    pub static FOCUS_PREV_CMD = {
        l10n!: true,
        name: "Focus Previous",
        info: "Focus previous focusable",
        shortcut: shortcut!(SHIFT+Tab),
    };

    /// Represents the **focus/escape alt** action.
    pub static FOCUS_ALT_CMD = {
        l10n!: true,
        name: "Focus Alt",
        info: "Focus alt scope",
        shortcut: shortcut!(Alt),
    };

    /// Represents the **focus enter** action.
    pub static FOCUS_ENTER_CMD = {
        l10n!: true,
        name: "Focus Enter",
        info: "Focus child focusable",
        shortcut: [shortcut!(Enter), shortcut!(ALT+Enter)],
    };

    /// Represents the **focus exit** action.
    pub static FOCUS_EXIT_CMD = {
        l10n!: true,
        name: "Focus Exit",
        info: "Focus parent focusable, or return focus",
        shortcut: [shortcut!(Escape), shortcut!(ALT+Escape)],
    };

    /// Represents the **focus up** action.
    pub static FOCUS_UP_CMD = {
        l10n!: true,
        name: "Focus Up",
        info: "Focus closest focusable up",
        shortcut: [shortcut!(ArrowUp), shortcut!(ALT+ArrowUp)],
    };

    /// Represents the **focus down** action.
    pub static FOCUS_DOWN_CMD = {
        l10n!: true,
        name: "Focus Down",
        info: "Focus closest focusable down",
        shortcut: [shortcut!(ArrowDown), shortcut!(ALT+ArrowDown)],
    };

    /// Represents the **focus left** action.
    pub static FOCUS_LEFT_CMD = {
        l10n!: true,
        name: "Focus Left",
        info: "Focus closest focusable left",
        shortcut: [shortcut!(ArrowLeft), shortcut!(ALT+ArrowLeft)],
    };

    /// Represents the **focus right** action.
    pub static FOCUS_RIGHT_CMD = {
        l10n!: true,
        name: "Focus Right",
        info: "Focus closest focusable right",
        shortcut: [shortcut!(ArrowRight), shortcut!(ALT+ArrowRight)],
    };

    /// Represents a [`FocusRequest`] action.
    ///
    /// If this command parameter is a [`FocusRequest`] the request is made.
    pub static FOCUS_CMD;
}

pub(super) struct FocusCommands {
    next_handle: CommandHandle,
    prev_handle: CommandHandle,

    alt_handle: CommandHandle,

    up_handle: CommandHandle,
    down_handle: CommandHandle,
    left_handle: CommandHandle,
    right_handle: CommandHandle,

    exit_handle: CommandHandle,
    enter_handle: CommandHandle,

    focus_handle: CommandHandle,
}
impl FocusCommands {
    pub fn new() -> Self {
        Self {
            next_handle: FOCUS_NEXT_CMD.subscribe(false),
            prev_handle: FOCUS_PREV_CMD.subscribe(false),

            alt_handle: FOCUS_ALT_CMD.subscribe(false),

            up_handle: FOCUS_UP_CMD.subscribe(false),
            down_handle: FOCUS_DOWN_CMD.subscribe(false),
            left_handle: FOCUS_LEFT_CMD.subscribe(false),
            right_handle: FOCUS_RIGHT_CMD.subscribe(false),

            exit_handle: FOCUS_EXIT_CMD.subscribe(false),
            enter_handle: FOCUS_ENTER_CMD.subscribe(false),

            focus_handle: FOCUS_CMD.subscribe(true),
        }
    }

    pub fn update_enabled(&mut self, nav: FocusNavAction) {
        self.next_handle.set_enabled(nav.contains(FocusNavAction::NEXT));
        self.prev_handle.set_enabled(nav.contains(FocusNavAction::PREV));

        self.alt_handle.set_enabled(nav.contains(FocusNavAction::ALT));

        self.up_handle.set_enabled(nav.contains(FocusNavAction::UP));
        self.down_handle.set_enabled(nav.contains(FocusNavAction::DOWN));
        self.left_handle.set_enabled(nav.contains(FocusNavAction::LEFT));
        self.right_handle.set_enabled(nav.contains(FocusNavAction::RIGHT));

        self.exit_handle.set_enabled(nav.contains(FocusNavAction::EXIT));
        self.enter_handle.set_enabled(nav.contains(FocusNavAction::ENTER));
    }

    pub fn event_preview(&mut self, update: &EventUpdate) {
        macro_rules! handle {
            ($($CMD:ident($handle:ident) => $method:ident,)+) => {$(
                if let Some(args) = $CMD.on(update) {
                    args.handle(|args| {
                        if args.enabled && self.$handle.is_enabled() {
                            FOCUS.$method();
                        } else {
                            FOCUS.on_disabled_cmd();
                        }
                    });
                    return;
                }
            )+};
        }
        handle! {
            FOCUS_NEXT_CMD(next_handle) => focus_next,
            FOCUS_PREV_CMD(prev_handle) => focus_prev,
            FOCUS_ALT_CMD(alt_handle) => focus_alt,
            FOCUS_UP_CMD(up_handle) => focus_up,
            FOCUS_DOWN_CMD(down_handle) => focus_down,
            FOCUS_LEFT_CMD(left_handle) => focus_left,
            FOCUS_RIGHT_CMD(right_handle) => focus_right,
            FOCUS_ENTER_CMD(enter_handle) => focus_enter,
            FOCUS_EXIT_CMD(exit_handle) => focus_exit,
        }

        if let Some(args) = FOCUS_CMD.on(update) {
            if let Some(req) = args.param::<FocusRequest>() {
                args.handle_enabled(&self.focus_handle, |_| {
                    FOCUS.focus(*req);
                });
            }
        }
    }
}

/// Focus extension methods for commands.
pub trait CommandFocusExt {
    /// Gets a command variable with `self` scoped to the focused (non-alt) widget or app.
    ///
    /// The scope is [`alt_return`] if is set, otherwise it is [`focused`], otherwise the
    /// command is not scoped (app scope). This means that you can bind the command variable to
    /// a menu or toolbar button inside an *alt-scope* without losing track of the intended target
    /// of the command.
    ///
    /// [`alt_return`]: FOCUS::alt_return
    /// [`focused`]: FOCUS::focused
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # zng_app::command! { pub static PASTE_CMD; }
    /// # use zng_ext_input::focus::cmd::CommandFocusExt as _;
    /// # use zng_var::*;
    /// # fn main() {
    /// let paste_in_focused_cmd = PASTE_CMD.focus_scoped();
    /// let is_enabled = paste_in_focused_cmd.flat_map(|c| c.is_enabled());
    /// paste_in_focused_cmd.get().notify();
    /// # }
    /// ```
    fn focus_scoped(self) -> BoxedVar<Command>;

    /// Gets a command variable with `self` scoped to the output of `map`.
    ///
    /// The `map` closure is called every time the non-alt focused widget changes, that is the [`alt_return`] or
    /// the [`focused`]. The closure input is the [`WidgetInfo`] for the focused widget and the output must be
    /// a [`CommandScope`] for the command.
    ///
    /// [`alt_return`]: FOCUS::alt_return
    /// [`focused`]: FOCUS::focused
    /// [`WidgetInfo`]: zng_app::widget::info::WidgetInfo
    /// [`CommandScope`]: zng_app::event::CommandScope
    fn focus_scoped_with(self, map: impl FnMut(Option<WidgetInfo>) -> CommandScope + Send + 'static) -> BoxedVar<Command>;
}

impl CommandFocusExt for Command {
    fn focus_scoped(self) -> BoxedVar<Command> {
        let cmd = self.scoped(CommandScope::App);
        merge_var!(FOCUS.alt_return(), FOCUS.focused(), move |alt, f| {
            match alt.as_ref().or(f.as_ref()) {
                Some(p) => cmd.scoped(p.widget_id()),
                None => cmd,
            }
        })
        .boxed()
    }

    fn focus_scoped_with(self, mut map: impl FnMut(Option<WidgetInfo>) -> CommandScope + Send + 'static) -> BoxedVar<Command> {
        let cmd = self.scoped(CommandScope::App);
        merge_var!(FOCUS.alt_return(), FOCUS.focused(), |alt, f| {
            match alt.as_ref().or(f.as_ref()) {
                Some(p) => WINDOWS.widget_tree(p.window_id()).ok()?.get(p.widget_id()),
                None => None,
            }
        })
        .map(move |w| cmd.scoped(map(w.clone())))
        .boxed()
    }
}