winrt-xaml 1.0.0

A Rust library for creating modern Windows UIs using WinRT and XAML with reactive data binding
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
//! WinRT Animation System - Storyboard and Animation types

use super::ffi::{self, XamlStoryboardHandle, XamlDoubleAnimationHandle, XamlColorAnimationHandle, XamlUIElementHandle};
use crate::error::{Error, Result};
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;

/// A WinRT Storyboard for orchestrating animations
pub struct XamlStoryboard {
    handle: XamlStoryboardHandle,
}

impl XamlStoryboard {
    /// Create a new Storyboard
    pub fn new() -> Result<Self> {
        let handle = unsafe { ffi::xaml_storyboard_create() };
        if handle.0.is_null() {
            return Err(Error::control_creation("Failed to create Storyboard"));
        }
        Ok(Self { handle })
    }

    /// Add a DoubleAnimation to the storyboard
    pub fn add_animation(&self, animation: &XamlDoubleAnimation) -> Result<()> {
        let result = unsafe {
            ffi::xaml_storyboard_add_animation(self.handle, animation.handle())
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to add animation"));
        }

        Ok(())
    }

    /// Add a ColorAnimation to the storyboard
    pub fn add_color_animation(&self, animation: &XamlColorAnimation) -> Result<()> {
        let result = unsafe {
            ffi::xaml_storyboard_add_color_animation(self.handle, animation.handle())
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to add color animation"));
        }

        Ok(())
    }

    /// Set the target UI element for all animations in this storyboard
    pub(crate) fn set_target(&self, target: XamlUIElementHandle) -> Result<()> {
        let result = unsafe {
            ffi::xaml_storyboard_set_target(self.handle, target)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to set storyboard target"));
        }

        Ok(())
    }

    /// Begin the storyboard animation
    pub fn begin(&self) -> Result<()> {
        let result = unsafe {
            ffi::xaml_storyboard_begin(self.handle)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to begin storyboard"));
        }

        Ok(())
    }

    /// Stop the storyboard animation
    pub fn stop(&self) -> Result<()> {
        let result = unsafe {
            ffi::xaml_storyboard_stop(self.handle)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to stop storyboard"));
        }

        Ok(())
    }

    /// Pause the storyboard animation
    pub fn pause(&self) -> Result<()> {
        let result = unsafe {
            ffi::xaml_storyboard_pause(self.handle)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to pause storyboard"));
        }

        Ok(())
    }

    /// Resume a paused storyboard animation
    pub fn resume(&self) -> Result<()> {
        let result = unsafe {
            ffi::xaml_storyboard_resume(self.handle)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to resume storyboard"));
        }

        Ok(())
    }

    /// Get the raw handle
    pub(crate) fn handle(&self) -> XamlStoryboardHandle {
        self.handle
    }
}

impl Default for XamlStoryboard {
    fn default() -> Self {
        Self::new().expect("Failed to create default Storyboard")
    }
}

impl Drop for XamlStoryboard {
    fn drop(&mut self) {
        if !self.handle.0.is_null() {
            unsafe {
                ffi::xaml_storyboard_destroy(self.handle);
            }
        }
    }
}

unsafe impl Send for XamlStoryboard {}
unsafe impl Sync for XamlStoryboard {}

/// A WinRT DoubleAnimation for animating numeric properties
pub struct XamlDoubleAnimation {
    handle: XamlDoubleAnimationHandle,
}

impl XamlDoubleAnimation {
    /// Create a new DoubleAnimation
    pub fn new() -> Result<Self> {
        let handle = unsafe { ffi::xaml_double_animation_create() };
        if handle.0.is_null() {
            return Err(Error::control_creation("Failed to create DoubleAnimation"));
        }
        Ok(Self { handle })
    }

    /// Create a new DoubleAnimation with builder pattern
    ///
    /// # Example
    /// ```no_run
    /// use winrt_xaml::xaml_native::XamlDoubleAnimation;
    ///
    /// let animation = XamlDoubleAnimation::builder()
    ///     .from(0.0)
    ///     .to(100.0)
    ///     .duration_ms(300)
    ///     .build()?;
    /// # Ok::<(), winrt_xaml::Error>(())
    /// ```
    pub fn builder() -> DoubleAnimationBuilder {
        DoubleAnimationBuilder::default()
    }

    /// Set the starting value
    pub fn set_from(&self, from: f64) -> Result<()> {
        let result = unsafe {
            ffi::xaml_double_animation_set_from(self.handle, from)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to set from value"));
        }

        Ok(())
    }

    /// Set the ending value
    pub fn set_to(&self, to: f64) -> Result<()> {
        let result = unsafe {
            ffi::xaml_double_animation_set_to(self.handle, to)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to set to value"));
        }

        Ok(())
    }

    /// Set the animation duration in milliseconds
    pub fn set_duration_ms(&self, milliseconds: i32) -> Result<()> {
        let result = unsafe {
            ffi::xaml_double_animation_set_duration(self.handle, milliseconds)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to set duration"));
        }

        Ok(())
    }

    /// Set the target property path
    ///
    /// # Arguments
    /// * `target` - The UI element to animate
    /// * `property_path` - Property path (e.g., "Opacity", "Width", "Height")
    pub(crate) fn set_target_property(&self, target: XamlUIElementHandle, property_path: impl AsRef<str>) -> Result<()> {
        let path_wide: Vec<u16> = OsStr::new(property_path.as_ref())
            .encode_wide()
            .chain(Some(0))
            .collect();

        let result = unsafe {
            ffi::xaml_double_animation_set_target_property(
                self.handle,
                target,
                path_wide.as_ptr(),
            )
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to set target property"));
        }

        Ok(())
    }

    /// Get the raw handle
    pub(crate) fn handle(&self) -> XamlDoubleAnimationHandle {
        self.handle
    }
}

impl Default for XamlDoubleAnimation {
    fn default() -> Self {
        Self::new().expect("Failed to create default DoubleAnimation")
    }
}

impl Drop for XamlDoubleAnimation {
    fn drop(&mut self) {
        if !self.handle.0.is_null() {
            unsafe {
                ffi::xaml_double_animation_destroy(self.handle);
            }
        }
    }
}

unsafe impl Send for XamlDoubleAnimation {}
unsafe impl Sync for XamlDoubleAnimation {}

/// Builder for DoubleAnimation
#[derive(Default)]
pub struct DoubleAnimationBuilder {
    from: Option<f64>,
    to: Option<f64>,
    duration_ms: Option<i32>,
}

impl DoubleAnimationBuilder {
    /// Set the starting value
    pub fn from(mut self, value: f64) -> Self {
        self.from = Some(value);
        self
    }

    /// Set the ending value
    pub fn to(mut self, value: f64) -> Self {
        self.to = Some(value);
        self
    }

    /// Set the duration in milliseconds
    pub fn duration_ms(mut self, milliseconds: i32) -> Self {
        self.duration_ms = Some(milliseconds);
        self
    }

    /// Build the animation
    pub fn build(self) -> Result<XamlDoubleAnimation> {
        let animation = XamlDoubleAnimation::new()?;

        if let Some(from) = self.from {
            animation.set_from(from)?;
        }

        if let Some(to) = self.to {
            animation.set_to(to)?;
        }

        if let Some(duration) = self.duration_ms {
            animation.set_duration_ms(duration)?;
        }

        Ok(animation)
    }
}

/// A WinRT ColorAnimation for animating color properties
pub struct XamlColorAnimation {
    handle: XamlColorAnimationHandle,
}

impl XamlColorAnimation {
    /// Create a new ColorAnimation
    pub fn new() -> Result<Self> {
        let handle = unsafe { ffi::xaml_color_animation_create() };
        if handle.0.is_null() {
            return Err(Error::control_creation("Failed to create ColorAnimation"));
        }
        Ok(Self { handle })
    }

    /// Create a new ColorAnimation with builder pattern
    pub fn builder() -> ColorAnimationBuilder {
        ColorAnimationBuilder::default()
    }

    /// Set the starting color (ARGB format)
    pub fn set_from(&self, from: u32) -> Result<()> {
        let result = unsafe {
            ffi::xaml_color_animation_set_from(self.handle, from)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to set from color"));
        }

        Ok(())
    }

    /// Set the ending color (ARGB format)
    pub fn set_to(&self, to: u32) -> Result<()> {
        let result = unsafe {
            ffi::xaml_color_animation_set_to(self.handle, to)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to set to color"));
        }

        Ok(())
    }

    /// Set the animation duration in milliseconds
    pub fn set_duration_ms(&self, milliseconds: i32) -> Result<()> {
        let result = unsafe {
            ffi::xaml_color_animation_set_duration(self.handle, milliseconds)
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to set duration"));
        }

        Ok(())
    }

    /// Set the target property path
    ///
    /// # Arguments
    /// * `target` - The UI element to animate
    /// * `property_path` - Property path (e.g., "(Button.Background).(SolidColorBrush.Color)")
    pub(crate) fn set_target_property(&self, target: XamlUIElementHandle, property_path: impl AsRef<str>) -> Result<()> {
        let path_wide: Vec<u16> = OsStr::new(property_path.as_ref())
            .encode_wide()
            .chain(Some(0))
            .collect();

        let result = unsafe {
            ffi::xaml_color_animation_set_target_property(
                self.handle,
                target,
                path_wide.as_ptr(),
            )
        };

        if result != 0 {
            return Err(Error::invalid_operation("Failed to set target property"));
        }

        Ok(())
    }

    /// Get the raw handle
    pub(crate) fn handle(&self) -> XamlColorAnimationHandle {
        self.handle
    }
}

impl Default for XamlColorAnimation {
    fn default() -> Self {
        Self::new().expect("Failed to create default ColorAnimation")
    }
}

impl Drop for XamlColorAnimation {
    fn drop(&mut self) {
        if !self.handle.0.is_null() {
            unsafe {
                ffi::xaml_color_animation_destroy(self.handle);
            }
        }
    }
}

unsafe impl Send for XamlColorAnimation {}
unsafe impl Sync for XamlColorAnimation {}

/// Builder for ColorAnimation
#[derive(Default)]
pub struct ColorAnimationBuilder {
    from: Option<u32>,
    to: Option<u32>,
    duration_ms: Option<i32>,
}

impl ColorAnimationBuilder {
    /// Set the starting color (ARGB format, e.g., 0xFFFF0000 for red)
    pub fn from(mut self, color: u32) -> Self {
        self.from = Some(color);
        self
    }

    /// Set the ending color (ARGB format)
    pub fn to(mut self, color: u32) -> Self {
        self.to = Some(color);
        self
    }

    /// Set the duration in milliseconds
    pub fn duration_ms(mut self, milliseconds: i32) -> Self {
        self.duration_ms = Some(milliseconds);
        self
    }

    /// Build the animation
    pub fn build(self) -> Result<XamlColorAnimation> {
        let animation = XamlColorAnimation::new()?;

        if let Some(from) = self.from {
            animation.set_from(from)?;
        }

        if let Some(to) = self.to {
            animation.set_to(to)?;
        }

        if let Some(duration) = self.duration_ms {
            animation.set_duration_ms(duration)?;
        }

        Ok(animation)
    }
}