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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
// Copyright 2019 The Druid Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Custom commands.

use std::any::{self, Any};
use std::{
    marker::PhantomData,
    sync::{Arc, Mutex},
};

use crate::{WidgetId, WindowId};

/// The identity of a [`Selector`].
///
/// [`Selector`]: struct.Selector.html
pub(crate) type SelectorSymbol = &'static str;

/// An identifier for a particular command.
///
/// This should be a unique string identifier.
/// Having multiple selectors with the same identifier but different payload
/// types is not allowed and can cause [`Command::get`] and [`get_unchecked`] to panic.
///
/// The type parameter `T` specifies the command's payload type.
/// See [`Command`] for more information.
///
/// Certain `Selector`s are defined by druid, and have special meaning
/// to the framework; these are listed in the [`druid::commands`] module.
///
/// [`Command`]: struct.Command.html
/// [`Command::get`]: struct.Command.html#method.get
/// [`get_unchecked`]: struct.Command.html#method.get_unchecked
/// [`druid::commands`]: commands/index.html
#[derive(Debug, PartialEq, Eq)]
pub struct Selector<T = ()>(SelectorSymbol, PhantomData<T>);

/// An arbitrary command.
///
/// A `Command` consists of a [`Selector`], that indicates what the command is
/// and what type of payload it carries, as well as the actual payload.
///
/// If the payload can't or shouldn't be cloned,
/// wrapping it with [`SingleUse`] allows you to `take` the payload.
/// The [`SingleUse`] docs give an example on how to do this.
///
/// Generic payloads can be achieved with `Selector<Box<dyn Any>>`.
/// In this case it could make sense to use utility functions to construct
/// such commands in order to maintain as much static typing as possible.
/// The [`EventCtx::new_window`] method is an example of this.
///
/// # Examples
/// ```
/// use druid::{Command, Selector, Target};
///
/// let selector = Selector::new("process_rows");
/// let rows = vec![1, 3, 10, 12];
/// let command = Command::new(selector, rows, Target::Auto);
///
/// assert_eq!(command.get(selector), Some(&vec![1, 3, 10, 12]));
/// ```
///
/// [`EventCtx::new_window`]: struct.EventCtx.html#method.new_window
/// [`SingleUse`]: struct.SingleUse.html
/// [`Selector`]: struct.Selector.html
#[derive(Debug, Clone)]
pub struct Command {
    symbol: SelectorSymbol,
    payload: Arc<dyn Any>,
    target: Target,
}

/// A message passed up the tree from a [`Widget`] to its ancestors.
///
/// In the course of handling an event, a [`Widget`] may change some internal
/// state that is of interest to one of its ancestors. In this case, the widget
/// may submit a [`Notification`].
///
/// In practice, a [`Notification`] is very similar to a [`Command`]; the
/// main distinction relates to delivery. [`Command`]s are delivered from the
/// root of the tree down towards the target, and this delivery occurs after
/// the originating event call has returned. [`Notification`]s are delivered *up*
/// the tree, and this occurs *during* event handling; immediately after the
/// child widget's [`event`] method returns, the notification will be delivered
/// to the child's parent, and then the parent's parent, until the notification
/// is handled.
///
/// [`Widget`]: crate::Widget
/// [`event`]: crate::Widget::event
#[derive(Clone)]
pub struct Notification {
    symbol: SelectorSymbol,
    payload: Arc<dyn Any>,
    source: WidgetId,
}

/// A wrapper type for [`Command`] payloads that should only be used once.
///
/// This is useful if you have some resource that cannot be
/// cloned, and you wish to send it to another widget.
///
/// # Examples
/// ```
/// use druid::{Command, Selector, SingleUse, Target};
///
/// struct CantClone(u8);
///
/// let selector = Selector::new("use-once");
/// let num = CantClone(42);
/// let command = Command::new(selector, SingleUse::new(num), Target::Auto);
///
/// let payload: &SingleUse<CantClone> = command.get_unchecked(selector);
/// if let Some(num) = payload.take() {
///     // now you own the data
///     assert_eq!(num.0, 42);
/// }
///
/// // subsequent calls will return `None`
/// assert!(payload.take().is_none());
/// ```
///
/// [`Command`]: struct.Command.html
pub struct SingleUse<T>(Mutex<Option<T>>);

/// The target of a [`Command`].
///
/// [`Command`]: struct.Command.html
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Target {
    /// The target is the top-level application.
    ///
    /// The `Command` will be delivered to all open windows, and all widgets
    /// in each window. Delivery will stop if the event is [`handled`].
    ///
    /// [`handled`]: struct.EventCtx.html#set_handled
    Global,
    /// The target is a specific window.
    ///
    /// The `Command` will be delivered to all widgets in that window.
    /// Delivery will stop if the event is [`handled`].
    ///
    /// [`handled`]: struct.EventCtx.html#set_handled
    Window(WindowId),
    /// The target is a specific widget.
    Widget(WidgetId),
    /// The target will be determined automatically.
    ///
    /// How this behaves depends on the context used to submit the command.
    /// Each `submit_command` function should have documentation about the specific behavior.
    Auto,
}

/// Commands with special meaning, defined by druid.
///
/// See [`Command`] for more info.
///
/// [`Command`]: ../struct.Command.html
pub mod sys {
    use std::any::Any;

    use super::Selector;
    use crate::{FileDialogOptions, FileInfo, SingleUse, WindowConfig};

    /// Quit the running application. This command is handled by the druid library.
    pub const QUIT_APP: Selector = Selector::new("druid-builtin.quit-app");

    /// Hide the application. (mac only?)
    pub const HIDE_APPLICATION: Selector = Selector::new("druid-builtin.menu-hide-application");

    /// Hide all other applications. (mac only?)
    pub const HIDE_OTHERS: Selector = Selector::new("druid-builtin.menu-hide-others");

    /// The selector for a command to create a new window.
    pub(crate) const NEW_WINDOW: Selector<SingleUse<Box<dyn Any>>> =
        Selector::new("druid-builtin.new-window");

    /// The selector for a command to close a window.
    ///
    /// The command must target a specific window.
    /// When calling `submit_command` on a `Widget`s context, passing `None` as target
    /// will automatically target the window containing the widget.
    pub const CLOSE_WINDOW: Selector = Selector::new("druid-builtin.close-window");

    /// Close all windows.
    pub const CLOSE_ALL_WINDOWS: Selector = Selector::new("druid-builtin.close-all-windows");

    /// The selector for a command to bring a window to the front, and give it focus.
    ///
    /// The command must target a specific window.
    /// When calling `submit_command` on a `Widget`s context, passing `None` as target
    /// will automatically target the window containing the widget.
    pub const SHOW_WINDOW: Selector = Selector::new("druid-builtin.show-window");

    /// Apply the configuration payload to an existing window. The target should be a WindowId.
    pub const CONFIGURE_WINDOW: Selector<WindowConfig> =
        Selector::new("druid-builtin.configure-window");

    /// Display a context (right-click) menu. The payload must be the [`ContextMenu`]
    /// object to be displayed.
    ///
    /// [`ContextMenu`]: ../struct.ContextMenu.html
    pub(crate) const SHOW_CONTEXT_MENU: Selector<Box<dyn Any>> =
        Selector::new("druid-builtin.show-context-menu");

    /// The selector for a command to set the window's menu. The payload should
    /// be a [`MenuDesc`] object.
    ///
    /// [`MenuDesc`]: ../struct.MenuDesc.html
    pub(crate) const SET_MENU: Selector<Box<dyn Any>> = Selector::new("druid-builtin.set-menu");

    /// Show the application preferences.
    pub const SHOW_PREFERENCES: Selector = Selector::new("druid-builtin.menu-show-preferences");

    /// Show the application about window.
    pub const SHOW_ABOUT: Selector = Selector::new("druid-builtin.menu-show-about");

    /// Show all applications.
    pub const SHOW_ALL: Selector = Selector::new("druid-builtin.menu-show-all");

    /// Show the new file dialog.
    pub const NEW_FILE: Selector = Selector::new("druid-builtin.menu-file-new");

    /// When submitted by the application, a file picker dialog will be shown to the user,
    /// and an [`OPEN_FILE`] command will be sent if a path is chosen.
    ///
    /// [`OPEN_FILE`]: constant.OPEN_FILE.html
    pub const SHOW_OPEN_PANEL: Selector<FileDialogOptions> =
        Selector::new("druid-builtin.menu-file-open");

    /// Sent when the user cancels an open file panel.
    pub const OPEN_PANEL_CANCELLED: Selector = Selector::new("druid-builtin.open-panel-cancelled");

    /// Open a path, must be handled by the application.
    ///
    /// [`FileInfo`]: ../struct.FileInfo.html
    pub const OPEN_FILE: Selector<FileInfo> = Selector::new("druid-builtin.open-file-path");

    /// When submitted by the application, the system will show the 'save as' panel,
    /// and if a path is selected the system will issue a [`SAVE_FILE`] command
    /// with the selected path as the payload.
    ///
    /// [`SAVE_FILE`]: constant.SAVE_FILE.html
    pub const SHOW_SAVE_PANEL: Selector<FileDialogOptions> =
        Selector::new("druid-builtin.menu-file-save-as");

    /// Sent when the user cancels a save file panel.
    pub const SAVE_PANEL_CANCELLED: Selector = Selector::new("druid-builtin.save-panel-cancelled");

    /// Save the current path.
    ///
    /// The application should save its data, to a path that should be determined by the
    /// application. Usually, this will be the most recent path provided by a [`SAVE_FILE_AS`]
    /// or [`OPEN_FILE`] command.
    pub const SAVE_FILE: Selector<()> = Selector::new("druid-builtin.save-file");

    /// Save to a given location.
    ///
    /// This command is emitted by druid whenever a save file dialog successfully completes. The
    /// application should save its data to the path proved, and should store the path in order to
    /// handle [`SAVE_FILE`] commands in the future.
    ///
    /// The path might be a file or a directory, so always check whether it matches your
    /// expectations.
    pub const SAVE_FILE_AS: Selector<FileInfo> = Selector::new("druid-builtin.save-file-as");

    /// Show the print-setup window.
    pub const PRINT_SETUP: Selector = Selector::new("druid-builtin.menu-file-print-setup");

    /// Show the print dialog.
    pub const PRINT: Selector = Selector::new("druid-builtin.menu-file-print");

    /// Show the print preview.
    pub const PRINT_PREVIEW: Selector = Selector::new("druid-builtin.menu-file-print");

    /// Cut the current selection.
    pub const CUT: Selector = Selector::new("druid-builtin.menu-cut");

    /// Copy the current selection.
    pub const COPY: Selector = Selector::new("druid-builtin.menu-copy");

    /// Paste.
    pub const PASTE: Selector = Selector::new("druid-builtin.menu-paste");

    /// Undo.
    pub const UNDO: Selector = Selector::new("druid-builtin.menu-undo");

    /// Redo.
    pub const REDO: Selector = Selector::new("druid-builtin.menu-redo");
}

impl Selector<()> {
    /// A selector that does nothing.
    pub const NOOP: Selector = Selector::new("");

    /// Turns this into a command with the specified [`Target`].
    ///
    /// [`Target`]: enum.Target.html
    pub fn to(self, target: impl Into<Target>) -> Command {
        Command::from(self).to(target.into())
    }
}

impl<T> Selector<T> {
    /// Create a new `Selector` with the given string.
    pub const fn new(s: &'static str) -> Selector<T> {
        Selector(s, PhantomData)
    }

    /// Returns the `SelectorSymbol` identifying this `Selector`.
    pub(crate) const fn symbol(self) -> SelectorSymbol {
        self.0
    }
}

impl<T: Any> Selector<T> {
    /// Convenience method for [`Command::new`] with this selector.
    ///
    /// If the payload is `()` there is no need to call this,
    /// as `Selector<()>` implements `Into<Command>`.
    ///
    /// By default, the command will have [`Target::Auto`].
    /// The [`Command::to`] method can be used to override this.
    ///
    /// [`Command::new`]: struct.Command.html#method.new
    /// [`Command::to`]: struct.Command.html#method.to
    /// [`Target::Auto`]: enum.Target.html#variant.Auto
    pub fn with(self, payload: T) -> Command {
        Command::new(self, payload, Target::Auto)
    }
}

impl Command {
    /// Create a new `Command` with a payload and a [`Target`].
    ///
    /// [`Selector::with`] can be used to create `Command`s more conveniently.
    ///
    /// If you do not need a payload, [`Selector`] implements `Into<Command>`.
    ///
    /// [`Selector`]: struct.Selector.html
    /// [`Selector::with`]: struct.Selector.html#method.with
    /// [`Target`]: enum.Target.html
    pub fn new<T: Any>(selector: Selector<T>, payload: T, target: impl Into<Target>) -> Self {
        Command {
            symbol: selector.symbol(),
            payload: Arc::new(payload),
            target: target.into(),
        }
    }

    /// Used to create a `Command` from the types sent via an `ExtEventSink`.
    pub(crate) fn from_ext(symbol: SelectorSymbol, payload: Box<dyn Any>, target: Target) -> Self {
        Command {
            symbol,
            payload: payload.into(),
            target,
        }
        .default_to(Target::Global)
    }

    /// A helper method for creating a `Notification` from a `Command`.
    ///
    /// This is slightly icky; it lets us do `SOME_SELECTOR.with(SOME_PAYLOAD)`
    /// (which generates a command) and then privately convert it to a
    /// notification.
    pub(crate) fn into_notification(self, source: WidgetId) -> Notification {
        Notification {
            symbol: self.symbol,
            payload: self.payload,
            source,
        }
    }

    /// Set the `Command`'s [`Target`].
    ///
    /// [`Command::target`] can be used to get the current [`Target`].
    ///
    /// [`Command::target`]: #method.target
    /// [`Target`]: enum.Target.html
    pub fn to(mut self, target: impl Into<Target>) -> Self {
        self.target = target.into();
        self
    }

    /// Set the correct default target when target is `Auto`.
    pub(crate) fn default_to(mut self, target: Target) -> Self {
        self.target.default(target);
        self
    }

    /// Returns the `Command`'s [`Target`].
    ///
    /// [`Command::to`] can be used to change the [`Target`].
    ///
    /// [`Command::to`]: #method.to
    /// [`Target`]: enum.Target.html
    pub fn target(&self) -> Target {
        self.target
    }

    /// Returns `true` if `self` matches this `selector`.
    pub fn is<T>(&self, selector: Selector<T>) -> bool {
        self.symbol == selector.symbol()
    }

    /// Returns `Some(&T)` (this `Command`'s payload) if the selector matches.
    ///
    /// Returns `None` when `self.is(selector) == false`.
    ///
    /// Alternatively you can check the selector with [`is`] and then use [`get_unchecked`].
    ///
    /// # Panics
    ///
    /// Panics when the payload has a different type, than what the selector is supposed to carry.
    /// This can happen when two selectors with different types but the same key are used.
    ///
    /// [`is`]: #method.is
    /// [`get_unchecked`]: #method.get_unchecked
    pub fn get<T: Any>(&self, selector: Selector<T>) -> Option<&T> {
        if self.symbol == selector.symbol() {
            Some(self.payload.downcast_ref().unwrap_or_else(|| {
                panic!(
                    "The selector \"{}\" exists twice with different types. See druid::Command::get for more information",
                    selector.symbol()
                );
            }))
        } else {
            None
        }
    }

    /// Returns a reference to this `Command`'s payload.
    ///
    /// If the selector has already been checked with [`is`], then `get_unchecked` can be used safely.
    /// Otherwise you should use [`get`] instead.
    ///
    /// # Panics
    ///
    /// Panics when `self.is(selector) == false`.
    ///
    /// Panics when the payload has a different type, than what the selector is supposed to carry.
    /// This can happen when two selectors with different types but the same key are used.
    ///
    /// [`is`]: #method.is
    /// [`get`]: #method.get
    pub fn get_unchecked<T: Any>(&self, selector: Selector<T>) -> &T {
        self.get(selector).unwrap_or_else(|| {
            panic!(
                "Expected selector \"{}\" but the command was \"{}\".",
                selector.symbol(),
                self.symbol
            )
        })
    }
}

impl Notification {
    /// Returns `true` if `self` matches this [`Selector`].
    pub fn is<T>(&self, selector: Selector<T>) -> bool {
        self.symbol == selector.symbol()
    }

    /// Returns the payload for this [`Selector`], if the selector matches.
    ///
    /// # Panics
    ///
    /// Panics when the payload has a different type, than what the selector
    /// is supposed to carry. This can happen when two selectors with different
    /// types but the same key are used.
    ///
    /// [`is`]: #method.is
    pub fn get<T: Any>(&self, selector: Selector<T>) -> Option<&T> {
        if self.symbol == selector.symbol() {
            Some(self.payload.downcast_ref().unwrap_or_else(|| {
                panic!(
                    "The selector \"{}\" exists twice with different types. \
                    See druid::Command::get for more information",
                    selector.symbol()
                );
            }))
        } else {
            None
        }
    }

    /// The [`WidgetId`] of the [`Widget`] that sent this [`Notification`].
    ///
    /// [`Widget`]: crate::Widget
    pub fn source(&self) -> WidgetId {
        self.source
    }
}

impl<T: Any> SingleUse<T> {
    /// Create a new single-use payload.
    pub fn new(data: T) -> Self {
        SingleUse(Mutex::new(Some(data)))
    }

    /// Takes the value, leaving a None in its place.
    pub fn take(&self) -> Option<T> {
        self.0.lock().unwrap().take()
    }
}

impl From<Selector> for Command {
    fn from(selector: Selector) -> Command {
        Command {
            symbol: selector.symbol(),
            payload: Arc::new(()),
            target: Target::Auto,
        }
    }
}

impl<T> std::fmt::Display for Selector<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "Selector(\"{}\", {})", self.0, any::type_name::<T>())
    }
}

// This has do be done explicitly, to avoid the Copy bound on `T`.
// See https://doc.rust-lang.org/std/marker/trait.Copy.html#how-can-i-implement-copy .
impl<T> Copy for Selector<T> {}
impl<T> Clone for Selector<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl Target {
    /// If `self` is `Auto` it will be replaced with `target`.
    pub(crate) fn default(&mut self, target: Target) {
        if self == &Target::Auto {
            *self = target;
        }
    }
}

impl From<WindowId> for Target {
    fn from(id: WindowId) -> Target {
        Target::Window(id)
    }
}

impl From<WidgetId> for Target {
    fn from(id: WidgetId) -> Target {
        Target::Widget(id)
    }
}

impl Into<Option<Target>> for WindowId {
    fn into(self) -> Option<Target> {
        Some(Target::Window(self))
    }
}

impl Into<Option<Target>> for WidgetId {
    fn into(self) -> Option<Target> {
        Some(Target::Widget(self))
    }
}

impl std::fmt::Debug for Notification {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "Notification: Selector {} from {:?}",
            self.symbol, self.source
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn get_payload() {
        let sel = Selector::new("my-selector");
        let payload = vec![0, 1, 2];
        let command = Command::new(sel, payload, Target::Auto);
        assert_eq!(command.get(sel), Some(&vec![0, 1, 2]));
    }

    #[test]
    fn selector_is_send_and_sync() {
        fn assert_send_sync<T: Send + Sync>() {}

        assert_send_sync::<Selector>();
    }
}