cmdpal 0.3.0

Rust SDK for PowerToys Command Palette
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
//! [`IExtensionHost`] related functions and types.
//!
//! Useful for sending feedbacks to users.

use crate::bindings::*;
use crate::notify::*;
use crate::utils::ComBuilder;
use std::sync::RwLock;
use windows::Win32::Foundation::E_INVALIDARG;
use windows_core::AgileReference;
use windows_core::{ComObject, IInspectable, IUnknownImpl as _, implement};

pub(crate) static EXTENSION_HOST: RwLock<Option<AgileReference<IExtensionHost>>> =
    RwLock::new(None);

/// Struct which represents the progress state of an operation.
///
/// See [`ProgressState_Impl`] for field accessors.
///
#[doc = include_str!("./bindings_docs/IProgressState.md")]
#[implement(IProgressState, INotifyPropChanged)]
pub struct ProgressState {
    indeterminate: NotifyLock<bool>,
    percentage: NotifyLock<u32>,
    event: PropChangedEventHandler,
}

/// Builder for [`ProgressState`].
pub struct ProgressStateBuilder {
    indeterminate: bool,
    percentage: u32,
}

impl ProgressStateBuilder {
    /// Creates a new `ProgressStateBuilder`.
    pub fn new() -> Self {
        ProgressStateBuilder {
            indeterminate: true,
            percentage: 0,
        }
    }

    /// Sets the indeterminate state of the progress.
    pub fn indeterminate(mut self, indeterminate: bool) -> Self {
        self.indeterminate = indeterminate;
        self
    }

    /// Sets the progress percentage.
    pub fn percentage(mut self, percentage: u32) -> Self {
        self.percentage = percentage;
        self
    }
}

impl ComBuilder for ProgressStateBuilder {
    type Output = ProgressState;

    fn build_unmanaged(self) -> Self::Output {
        ProgressState {
            indeterminate: NotifyLock::new(self.indeterminate),
            percentage: NotifyLock::new(self.percentage),
            event: PropChangedEventHandler::new(),
        }
    }
}

impl ProgressState_Impl {
    pub(crate) fn emit_self_prop_changed(&self, prop: &str) {
        let sender: IInspectable = self.to_interface();
        let arg: IPropChangedEventArgs = PropChangedEventArgs(prop.into()).into();
        self.event.call(|handler| handler.Invoke(&sender, &arg));
    }

    /// Readonly access to [`IProgressState::IsIndeterminate`]
    ///
    #[doc = include_str!("./bindings_docs/IProgressState/IsIndeterminate.md")]
    pub fn indeterminate(&self) -> windows_core::Result<NotifyLockReadGuard<'_, bool>> {
        self.indeterminate.read()
    }

    /// Mutable access to [`IProgressState::IsIndeterminate`].
    ///
    #[doc = include_str!("./bindings_docs/IProgressState/IsIndeterminate.md")]
    ///
    /// Notifies the host about the change when dropping the guard.
    pub fn indeterminate_mut(&self) -> windows_core::Result<NotifyLockWriteGuard<'_, bool>> {
        self.indeterminate
            .write(|| self.emit_self_prop_changed("IsIndeterminate"))
    }

    /// Readonly access to [`IProgressState::ProgressPercent`]
    ///
    #[doc = include_str!("./bindings_docs/IProgressState/ProgressPercent.md")]
    pub fn percentage(&self) -> windows_core::Result<NotifyLockReadGuard<'_, u32>> {
        self.percentage.read()
    }

    /// Mutable access to [`IProgressState::ProgressPercent`].
    ///
    #[doc = include_str!("./bindings_docs/IProgressState/ProgressPercent.md")]
    ///
    /// Notifies the host about the change when dropping the guard.
    pub fn percentage_mut(&self) -> windows_core::Result<NotifyLockWriteGuard<'_, u32>> {
        self.percentage
            .write(|| self.emit_self_prop_changed("ProgressPercent"))
    }
}

impl INotifyPropChanged_Impl for ProgressState_Impl {
    fn PropChanged(
        &self,
        handler: windows_core::Ref<
            '_,
            windows::Foundation::TypedEventHandler<
                windows_core::IInspectable,
                IPropChangedEventArgs,
            >,
        >,
    ) -> windows_core::Result<i64> {
        self.event.add(handler.ok()?)
    }

    fn RemovePropChanged(&self, token: i64) -> windows_core::Result<()> {
        self.event.remove(token);
        Ok(())
    }
}

impl IProgressState_Impl for ProgressState_Impl {
    fn IsIndeterminate(&self) -> windows_core::Result<bool> {
        self.indeterminate.read().map(|x| *x)
    }

    fn ProgressPercent(&self) -> windows_core::Result<u32> {
        self.percentage.read().map(|x| *x)
    }
}

/// Rust idiomatic representation of [`crate::bindings::MessageState`]
///
#[doc = include_str!("./bindings_docs/MessageState.md")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MessageState {
    #[doc = include_str!("./bindings_docs/MessageState/Info.md")]
    Info,
    #[doc = include_str!("./bindings_docs/MessageState/Success.md")]
    Success,
    #[doc = include_str!("./bindings_docs/MessageState/Warning.md")]
    Warning,
    #[doc = include_str!("./bindings_docs/MessageState/Error.md")]
    Error,
}

impl TryFrom<crate::bindings::MessageState> for MessageState {
    type Error = windows_core::Error;
    fn try_from(value: crate::bindings::MessageState) -> Result<Self, windows_core::Error> {
        match value {
            crate::bindings::MessageState::Error => Ok(MessageState::Error),
            crate::bindings::MessageState::Warning => Ok(MessageState::Warning),
            crate::bindings::MessageState::Info => Ok(MessageState::Info),
            crate::bindings::MessageState::Success => Ok(MessageState::Success),
            _ => Err(E_INVALIDARG.into()),
        }
    }
}

impl From<MessageState> for crate::bindings::MessageState {
    fn from(value: MessageState) -> Self {
        match value {
            MessageState::Error => crate::bindings::MessageState::Error,
            MessageState::Warning => crate::bindings::MessageState::Warning,
            MessageState::Info => crate::bindings::MessageState::Info,
            MessageState::Success => crate::bindings::MessageState::Success,
        }
    }
}

/// Rust idiomatic representation of [`crate::bindings::StatusContext`]
///
#[doc = include_str!("./bindings_docs/StatusContext.md")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StatusContext {
    #[doc = include_str!("./bindings_docs/StatusContext/Page.md")]
    Page,
    #[doc = include_str!("./bindings_docs/StatusContext/Extension.md")]
    Extension,
}

impl TryFrom<crate::bindings::StatusContext> for StatusContext {
    type Error = windows_core::Error;
    fn try_from(value: crate::bindings::StatusContext) -> Result<Self, windows_core::Error> {
        match value {
            crate::bindings::StatusContext::Page => Ok(StatusContext::Page),
            crate::bindings::StatusContext::Extension => Ok(StatusContext::Extension),
            _ => Err(E_INVALIDARG.into()),
        }
    }
}

impl From<StatusContext> for crate::bindings::StatusContext {
    fn from(value: StatusContext) -> Self {
        match value {
            StatusContext::Page => crate::bindings::StatusContext::Page,
            StatusContext::Extension => crate::bindings::StatusContext::Extension,
        }
    }
}

/// Struct which represents a status message.
///
/// See [`StatusMessage_Impl`] for field accessors.
///
#[doc = include_str!("./bindings_docs/IStatusMessage.md")]
#[implement(IStatusMessage, INotifyPropChanged)]
pub struct StatusMessage {
    state: NotifyLock<MessageState>,
    progress: NotifyLock<ComObject<ProgressState>>,
    message: NotifyLock<windows_core::HSTRING>,
    event: PropChangedEventHandler,
}

/// Builder for [`StatusMessage`].
pub struct StatusMessageBuilder {
    state: MessageState,
    progress: ComObject<ProgressState>,
    message: windows_core::HSTRING,
}

impl StatusMessageBuilder {
    /// Creates a new `StatusMessageBuilder`.
    pub fn new() -> Self {
        StatusMessageBuilder {
            state: MessageState::Info,
            progress: ProgressStateBuilder::new().build(),
            message: windows_core::HSTRING::default(),
        }
    }

    /// Sets the state of the status message.
    pub fn state(mut self, state: MessageState) -> Self {
        self.state = state;
        self
    }

    /// Sets the progress of the status message.
    pub fn progress(mut self, progress: ComObject<ProgressState>) -> Self {
        self.progress = progress;
        self
    }

    /// Sets the message of the status message.
    pub fn message(mut self, message: windows_core::HSTRING) -> Self {
        self.message = message;
        self
    }

    /// Builds the `StatusMessage`.
    pub fn build(self) -> StatusMessage {
        StatusMessage {
            state: NotifyLock::new(self.state),
            progress: NotifyLock::new(self.progress),
            message: NotifyLock::new(self.message),
            event: PropChangedEventHandler::new(),
        }
    }
}

impl StatusMessage_Impl {
    pub(crate) fn emit_self_prop_changed(&self, prop: &str) {
        let sender: IInspectable = self.to_interface();
        let arg: IPropChangedEventArgs = PropChangedEventArgs(prop.into()).into();
        self.event.call(|handler| handler.Invoke(&sender, &arg));
    }

    /// Readonly access to [`IStatusMessage::State`]
    ///
    #[doc = include_str!("./bindings_docs/IStatusMessage/State.md")]
    pub fn state(&self) -> windows_core::Result<NotifyLockReadGuard<'_, MessageState>> {
        self.state.read()
    }

    /// Mutable access to [`IStatusMessage::State`].
    ///
    #[doc = include_str!("./bindings_docs/IStatusMessage/State.md")]
    ///
    /// Notifies the host about the change when dropping the guard.
    pub fn state_mut(&self) -> windows_core::Result<NotifyLockWriteGuard<'_, MessageState>> {
        self.state.write(|| self.emit_self_prop_changed("State"))
    }

    /// Readonly access to [`IStatusMessage::Progress`]
    ///
    #[doc = include_str!("./bindings_docs/IStatusMessage/Progress.md")]
    pub fn progress(
        &self,
    ) -> windows_core::Result<NotifyLockReadGuard<'_, ComObject<ProgressState>>> {
        self.progress.read()
    }

    /// Mutable access to [`IStatusMessage::Progress`].
    ///
    #[doc = include_str!("./bindings_docs/IStatusMessage/Progress.md")]
    ///
    /// Notifies the host about the change when dropping the guard.
    pub fn progress_mut(
        &self,
    ) -> windows_core::Result<NotifyLockWriteGuard<'_, ComObject<ProgressState>>> {
        self.progress
            .write(|| self.emit_self_prop_changed("Progress"))
    }

    /// Readonly access to [`IStatusMessage::Message`]
    ///
    #[doc = include_str!("./bindings_docs/IStatusMessage/Message.md")]
    pub fn message(&self) -> windows_core::Result<NotifyLockReadGuard<'_, windows_core::HSTRING>> {
        self.message.read()
    }

    /// Mutable access to [`IStatusMessage::Message`].
    ///
    #[doc = include_str!("./bindings_docs/IStatusMessage/Message.md")]
    ///
    /// Notifies the host about the change when dropping the guard.
    pub fn message_mut(
        &self,
    ) -> windows_core::Result<NotifyLockWriteGuard<'_, windows_core::HSTRING>> {
        self.message
            .write(|| self.emit_self_prop_changed("Message"))
    }
}

impl IStatusMessage_Impl for StatusMessage_Impl {
    fn State(&self) -> windows_core::Result<crate::bindings::MessageState> {
        self.state.read().map(|x| x.clone().into())
    }

    fn Progress(&self) -> windows_core::Result<IProgressState> {
        self.progress.read().map(|x| x.to_interface())
    }

    fn Message(&self) -> windows_core::Result<windows_core::HSTRING> {
        self.message.read().map(|x| x.clone())
    }
}

impl INotifyPropChanged_Impl for StatusMessage_Impl {
    fn PropChanged(
        &self,
        handler: windows_core::Ref<
            '_,
            windows::Foundation::TypedEventHandler<
                windows_core::IInspectable,
                IPropChangedEventArgs,
            >,
        >,
    ) -> windows_core::Result<i64> {
        self.event.add(handler.ok()?)
    }

    fn RemovePropChanged(&self, token: i64) -> windows_core::Result<()> {
        self.event.remove(token);
        Ok(())
    }
}

/// Struct which represents a log message.
///
#[doc = include_str!("./bindings_docs/ILogMessage.md")]
#[implement(ILogMessage)]
#[derive(Debug, Clone)]
pub struct LogMessage {
    #[doc = include_str!("./bindings_docs/ILogMessage/State.md")]
    pub state: MessageState,
    #[doc = include_str!("./bindings_docs/ILogMessage/Message.md")]
    pub message: windows_core::HSTRING,
}

impl LogMessage {
    /// Creates a new `LogMessage`.
    pub fn new(state: MessageState, message: windows_core::HSTRING) -> Self {
        LogMessage { state, message }
    }

    /// Creates a new `LogMessage` that represents an informational message.
    pub fn info(message: windows_core::HSTRING) -> Self {
        LogMessage::new(MessageState::Info, message)
    }

    /// Creates a new `LogMessage` that represents a success message.
    pub fn success(message: windows_core::HSTRING) -> Self {
        LogMessage::new(MessageState::Success, message)
    }

    /// Creates a new `LogMessage` that represents a warning message.
    pub fn warning(message: windows_core::HSTRING) -> Self {
        LogMessage::new(MessageState::Warning, message)
    }

    /// Creates a new `LogMessage` that represents an error message.
    pub fn error(message: windows_core::HSTRING) -> Self {
        LogMessage::new(MessageState::Error, message)
    }

    /// Logs the message to the host.
    ///
    /// The message will be logged into the log file, yet not shown to the user.
    ///
    /// This method is a convenient wrapper around the [`log_message`] function,
    /// it consumes `self`, so clone the struct if you want to log it multiple times.
    ///
    pub fn log(self) {
        let ilog: ILogMessage = self.into();
        log_message(ilog);
    }
}

impl ILogMessage_Impl for LogMessage_Impl {
    fn State(&self) -> windows_core::Result<crate::bindings::MessageState> {
        Ok(self.state.clone().into())
    }

    fn Message(&self) -> windows_core::Result<windows_core::HSTRING> {
        Ok(self.message.clone())
    }
}

/// Sets the global extension host reference.
///
/// This function is automatically called
/// when [`crate::cmd_provider::CommandProvider`] is initialized by the Command Palette.
pub fn set_ext_host(host: &IExtensionHost) {
    let reference = AgileReference::new(host).unwrap();
    if let Ok(mut lock) = EXTENSION_HOST.write() {
        *lock = Some(reference);
    }
}

/// Shows a status message to the user.
///
/// The status message will appear as a "pop-up" in the Command Palette.
pub fn show_status(message: ComObject<StatusMessage>, context: StatusContext) {
    if let Ok(lock) = EXTENSION_HOST.read() {
        if let Some(host) = lock.as_ref().and_then(|x| x.resolve().ok()) {
            let _ = host.ShowStatus(message.as_interface(), context.into());
        }
    }
}

/// Hides a status message.
pub fn hide_status(message: ComObject<StatusMessage>) {
    if let Ok(lock) = EXTENSION_HOST.read() {
        if let Some(host) = lock.as_ref().and_then(|x| x.resolve().ok()) {
            let _ = host.HideStatus(message.as_interface());
        }
    }
}

/// Logs a message to the host.
///
/// The message will be logged into the log file, yet not shown to the user.
///
/// Consider [`LogMessage::log`] method for a more idiomatic way to log messages.
pub fn log_message(message: impl std::borrow::Borrow<ILogMessage>) {
    if let Ok(lock) = EXTENSION_HOST.read() {
        if let Some(host) = lock.as_ref().and_then(|x| x.resolve().ok()) {
            let _ = host.LogMessage(message.borrow());
        }
    }
}