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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! Types for displaying the details tab.
//!
//! `Details` is displayed in a tab occupying the right side of UI,
//! displaying additional information about the selected item.

use crate::icon::IconInfo;
use crate::utils::{OkOrEmpty, assert_send_sync, map_array};
use crate::{bindings::*, utils::ComBuilder};
use windows_core::{AgileReference, HSTRING};
use windows_core::{ComObject, Result, implement};

/// Represents a tag for classification.
///
/// See: [`ITag`]
///
#[doc = include_str!("./bindings_docs/ITag.md")]
#[implement(ITag)]
pub struct Tag {
    icon: Option<ComObject<IconInfo>>,
    text: HSTRING,
    foreground: Option<Color>,
    background: Option<Color>,
    tooltip: HSTRING,
}

/// Builder for [`Tag`].
pub struct TagBuilder {
    icon: Option<ComObject<IconInfo>>,
    text: Option<HSTRING>,
    foreground: Option<Color>,
    background: Option<Color>,
    tooltip: Option<HSTRING>,
}

impl TagBuilder {
    /// Creates a builder.
    pub fn new() -> Self {
        TagBuilder {
            icon: None,
            text: None,
            foreground: None,
            background: None,
            tooltip: None,
        }
    }

    /// Sets the icon for the tag.
    ///
    #[doc = include_str!("./bindings_docs/ITag/Icon.md")]
    pub fn icon(mut self, icon: ComObject<IconInfo>) -> Self {
        self.icon = Some(icon);
        self
    }

    /// Sets the text for the tag.
    ///
    #[doc = include_str!("./bindings_docs/ITag/Text.md")]
    pub fn text(mut self, text: impl Into<HSTRING>) -> Self {
        self.text = Some(text.into());
        self
    }

    /// Sets the foreground color for the tag.
    ///
    #[doc = include_str!("./bindings_docs/ITag/Foreground.md")]
    pub fn foreground(mut self, color: Color) -> Self {
        self.foreground = Some(color);
        self
    }

    /// Sets the background color for the tag.
    ///
    #[doc = include_str!("./bindings_docs/ITag/Background.md")]
    pub fn background(mut self, color: Color) -> Self {
        self.background = Some(color);
        self
    }

    /// Sets the hover tooltip for the tag.
    ///
    #[doc = include_str!("./bindings_docs/ITag/ToolTip.md")]
    pub fn tooltip(mut self, tooltip: impl Into<HSTRING>) -> Self {
        self.tooltip = Some(tooltip.into());
        self
    }
}

impl ComBuilder for TagBuilder {
    type Output = Tag;
    fn build_unmanaged(self) -> Tag {
        Tag {
            icon: self.icon,
            text: self.text.unwrap_or_else(|| HSTRING::new()),
            foreground: self.foreground,
            background: self.background,
            tooltip: self.tooltip.unwrap_or_else(|| HSTRING::new()),
        }
    }
}

impl Default for TagBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ITag_Impl for Tag_Impl {
    fn Icon(&self) -> Result<crate::bindings::IIconInfo> {
        self.icon
            .as_ref()
            .map(|icon| icon.to_interface())
            .ok_or_empty()
    }

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

    fn Foreground(&self) -> Result<OptionalColor> {
        Ok(self.foreground.into())
    }

    fn Background(&self) -> Result<OptionalColor> {
        Ok(self.background.into())
    }

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

/// Represents a collection of tags in details.
///
/// See: [`IDetailsTags`]
///
#[doc = include_str!("./bindings_docs/IDetailsTags.md")]
#[implement(IDetailsTags, IDetailsData)]
pub struct DetailsTags {
    tags: Vec<ComObject<Tag>>,
}

/// Builder for [`DetailsTags`].
pub struct DetailsTagsBuilder {
    tags: Vec<ComObject<Tag>>,
}

impl DetailsTagsBuilder {
    /// Creates a new builder.
    pub fn new() -> Self {
        DetailsTagsBuilder { tags: Vec::new() }
    }

    /// Adds a tag to the collection.
    pub fn add_tag(mut self, tag: ComObject<Tag>) -> Self {
        self.tags.push(tag);
        self
    }

    /// Sets the tags for the collection.
    pub fn tags(mut self, tags: Vec<ComObject<Tag>>) -> Self {
        self.tags = tags;
        self
    }
}

impl ComBuilder for DetailsTagsBuilder {
    type Output = DetailsTags;
    fn build_unmanaged(self) -> DetailsTags {
        DetailsTags { tags: self.tags }
    }
}

impl Default for DetailsTagsBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl IDetailsData_Impl for DetailsTags_Impl {}

impl IDetailsTags_Impl for DetailsTags_Impl {
    fn Tags(&self) -> Result<windows_core::Array<ITag>> {
        Ok(map_array(&self.tags, |x| x.to_interface::<ITag>().into()))
    }
}

/// Represents a hyperlink.
///
/// See: [`IDetailsLink`]
///
#[doc = include_str!("./bindings_docs/IDetailsLink.md")]
#[implement(IDetailsLink, IDetailsData)]
pub struct DetailsLink {
    text: HSTRING,
    link: windows::Foundation::Uri,
}

/// Builder for [`DetailsLink`].
pub struct DetailsLinkBuilder {
    text: Option<HSTRING>,
    link: windows::Foundation::Uri,
}

impl DetailsLinkBuilder {
    /// Creates a new builder.
    pub fn new(link: windows::Foundation::Uri) -> Self {
        DetailsLinkBuilder { text: None, link }
    }

    /// Try to create a new builder from a string link.
    pub fn try_new(link: impl Into<HSTRING>) -> Result<Self> {
        let uri = windows::Foundation::Uri::CreateUri(&link.into())?;
        Ok(DetailsLinkBuilder {
            text: None,
            link: uri,
        })
    }

    /// Sets the display text for the link automatically based on the URI, if not already set.
    pub fn auto_text(mut self) -> Result<Self> {
        if self.text.is_none() {
            self.text = Some(self.link.ToString()?);
        }
        Ok(self)
    }

    /// Sets the display text for the link.
    pub fn text(mut self, text: impl Into<HSTRING>) -> Self {
        self.text = Some(text.into());
        self
    }
}

impl ComBuilder for DetailsLinkBuilder {
    type Output = DetailsLink;
    fn build_unmanaged(self) -> DetailsLink {
        DetailsLink {
            text: self.text.unwrap_or_else(|| HSTRING::new()),
            link: self.link,
        }
    }
}

impl IDetailsData_Impl for DetailsLink_Impl {}

impl IDetailsLink_Impl for DetailsLink_Impl {
    fn Text(&self) -> Result<windows_core::HSTRING> {
        Ok(self.text.clone())
    }

    fn Link(&self) -> Result<windows::Foundation::Uri> {
        Ok(self.link.clone())
    }
}

/// Represents a command that can be executed from details tab.
///
/// See: [`IDetailsCommands`]
///
#[doc = include_str!("./bindings_docs/IDetailsCommands.md")]
#[implement(IDetailsCommands, IDetailsData)]
pub struct DetailsCommands {
    commands: Vec<AgileReference<ICommand>>,
}

impl DetailsCommands {
    /// Creates a new unmanaged instance of `DetailsCommand` with the specified commands.
    pub fn try_new_unmanaged(commands: &[ICommand]) -> Result<Self> {
        let agile_commands = commands
            .iter()
            .map(|cmd| AgileReference::new(cmd))
            .collect::<Result<Vec<_>>>()?;
        Ok(Self {
            commands: agile_commands,
        })
    }

    /// Creates a new reference-counted COM object for `DetailsCommand` with the specified commands.
    pub fn try_new(commands: &[ICommand]) -> Result<ComObject<Self>> {
        Ok(ComObject::new(Self::try_new_unmanaged(commands)?))
    }

    /// Creates a new unmanaged instance of `DetailsCommand` with the specified commands.
    pub fn new_unmanaged(commands: &[AgileReference<ICommand>]) -> Self {
        Self {
            commands: Vec::from(commands),
        }
    }

    /// Creates a new reference-counted COM object for `DetailsCommand` with the specified commands.
    pub fn new(commands: &[AgileReference<ICommand>]) -> ComObject<Self> {
        ComObject::new(Self::new_unmanaged(commands))
    }
}

impl IDetailsData_Impl for DetailsCommands_Impl {}

impl IDetailsCommands_Impl for DetailsCommands_Impl {
    fn Commands(&self) -> windows_core::Result<windows_core::Array<ICommand>> {
        Ok(map_array(&self.commands, |cmd| cmd.resolve().ok()))
    }
}

/// Represents a separator in details tab.
///
/// See: [`IDetailsSeparator`]
///
/// Details contents lay out vertically,
/// this separator could be used to visually separate different sections of details.
///
#[doc = include_str!("./bindings_docs/IDetailsSeparator.md")]
#[implement(IDetailsSeparator, IDetailsData)]
pub struct DetailsSeparator;

impl DetailsSeparator {
    pub fn new() -> ComObject<Self> {
        ComObject::new(Self)
    }
}

impl IDetailsData_Impl for DetailsSeparator_Impl {}

impl IDetailsSeparator_Impl for DetailsSeparator_Impl {}

/// Represents a collection of all possible detail data types.
pub enum DetailsData {
    Tags(ComObject<DetailsTags>),
    Link(ComObject<DetailsLink>),
    Commands(ComObject<DetailsCommands>),
    Separator(ComObject<DetailsSeparator>),
}

impl From<&DetailsData> for IDetailsData {
    fn from(data: &DetailsData) -> Self {
        match data {
            DetailsData::Tags(tags) => tags.to_interface(),
            DetailsData::Link(link) => link.to_interface(),
            DetailsData::Commands(commands) => commands.to_interface(),
            DetailsData::Separator(separator) => separator.to_interface(),
        }
    }
}

/// Represents a detail metadata that can be used in [`Details`].
///
/// See: [`IDetailsElement`]
///
#[doc = include_str!("./bindings_docs/IDetailsElement.md")]
#[implement(IDetailsElement)]
pub struct DetailsElement {
    key: HSTRING,
    data: DetailsData,
}

impl DetailsElement {
    /// Creates a new unmanaged instance of `DetailsElement` with the specified key and data.
    pub fn new_unmanaged(key: impl Into<HSTRING>, data: DetailsData) -> Self {
        DetailsElement {
            key: key.into(),
            data,
        }
    }

    /// Creates a new reference-counted COM object for `DetailsElement` with the specified key and data.
    pub fn new(key: impl Into<HSTRING>, data: DetailsData) -> ComObject<Self> {
        Self::new_unmanaged(key, data).into()
    }
}

impl IDetailsElement_Impl for DetailsElement_Impl {
    fn Key(&self) -> Result<windows_core::HSTRING> {
        Ok(self.key.clone())
    }

    fn Data(&self) -> Result<IDetailsData> {
        Ok((&self.data).into())
    }
}

/// Represents the details tab that can be displayed in a page.
///
/// See: [`IDetails`]
///
#[doc = include_str!("./bindings_docs/IDetails.md")]
#[implement(IDetails)]
pub struct Details {
    hero_image: Option<ComObject<IconInfo>>,
    title: HSTRING,
    body: HSTRING,
    metadata: Vec<ComObject<DetailsElement>>,
}

/// Builder for [`Details`].
pub struct DetailsBuilder {
    hero_image: Option<ComObject<IconInfo>>,
    title: Option<HSTRING>,
    body: Option<HSTRING>,
    metadata: Vec<ComObject<DetailsElement>>,
}

impl DetailsBuilder {
    /// Creates a new builder.
    pub fn new() -> Self {
        DetailsBuilder {
            hero_image: None,
            title: None,
            body: None,
            metadata: Vec::new(),
        }
    }

    /// Sets the hero image for the details.
    pub fn hero_image(mut self, hero_image: ComObject<IconInfo>) -> Self {
        self.hero_image = Some(hero_image);
        self
    }

    /// Sets the title for the details.
    pub fn title(mut self, title: impl Into<HSTRING>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Sets the body text for the details.
    pub fn body(mut self, body: impl Into<HSTRING>) -> Self {
        self.body = Some(body.into());
        self
    }

    /// Sets the metadata (elements) for the details.
    pub fn metadata(mut self, metadata: Vec<ComObject<DetailsElement>>) -> Self {
        self.metadata = metadata;
        self
    }

    /// Adds a metadata (element) to the details.
    pub fn add_metadata(mut self, element: ComObject<DetailsElement>) -> Self {
        self.metadata.push(element);
        self
    }

    /// Adds a metadata (element) without a key to the details.
    pub fn add_unnamed_metadata(mut self, data: DetailsData) -> Self {
        let element = DetailsElement::new_unmanaged(HSTRING::new(), data);
        self.metadata.push(ComObject::new(element));
        self
    }
}

impl ComBuilder for DetailsBuilder {
    type Output = Details;
    fn build_unmanaged(self) -> Details {
        Details {
            hero_image: self.hero_image,
            title: self.title.unwrap_or_else(|| HSTRING::new()),
            body: self.body.unwrap_or_else(|| HSTRING::new()),
            metadata: self.metadata,
        }
    }
}

impl Default for DetailsBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl IDetails_Impl for Details_Impl {
    fn HeroImage(&self) -> Result<crate::bindings::IIconInfo> {
        self.hero_image
            .as_ref()
            .map(|icon| icon.to_interface())
            .ok_or_empty()
    }

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

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

    fn Metadata(&self) -> Result<windows_core::Array<IDetailsElement>> {
        Ok(map_array(&self.metadata, |x| {
            x.to_interface::<IDetailsElement>().into()
        }))
    }
}

const _: () = assert_send_sync::<DetailsData>();
const _: () = assert_send_sync::<ComObject<Details>>();