windjammer-ui 0.3.6

Cross-platform UI framework for Windjammer (Web, Desktop, Mobile)
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
//! Implementation of native platform capabilities

use super::capabilities::{Capability, CapabilityError, CapabilityResult};
use std::path::Path;
use std::sync::{Arc, Mutex};

/// Filesystem API
pub struct Filesystem {
    allowed_paths: Arc<Mutex<Vec<String>>>,
}

impl Filesystem {
    pub fn new() -> Self {
        Self {
            allowed_paths: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Request permission to access a path
    pub fn request_permission(&self, path: impl AsRef<Path>) -> CapabilityResult<()> {
        let path_str = path.as_ref().to_string_lossy().to_string();
        let mut allowed = self.allowed_paths.lock().unwrap();
        if !allowed.contains(&path_str) {
            allowed.push(path_str);
        }
        Ok(())
    }

    /// Read file contents
    pub fn read_text(&self, path: impl AsRef<Path>) -> CapabilityResult<String> {
        self.check_permission(path.as_ref())?;
        std::fs::read_to_string(path)
            .map_err(|e| CapabilityError::Error(format!("Read error: {}", e)))
    }

    /// Write text to file
    pub fn write_text(&self, path: impl AsRef<Path>, content: &str) -> CapabilityResult<()> {
        self.check_permission(path.as_ref())?;
        std::fs::write(path, content)
            .map_err(|e| CapabilityError::Error(format!("Write error: {}", e)))
    }

    /// Read binary file
    pub fn read_binary(&self, path: impl AsRef<Path>) -> CapabilityResult<Vec<u8>> {
        self.check_permission(path.as_ref())?;
        std::fs::read(path).map_err(|e| CapabilityError::Error(format!("Read error: {}", e)))
    }

    /// Write binary to file
    pub fn write_binary(&self, path: impl AsRef<Path>, data: &[u8]) -> CapabilityResult<()> {
        self.check_permission(path.as_ref())?;
        std::fs::write(path, data)
            .map_err(|e| CapabilityError::Error(format!("Write error: {}", e)))
    }

    /// Check if file exists
    pub fn exists(&self, path: impl AsRef<Path>) -> bool {
        path.as_ref().exists()
    }

    /// List directory contents
    pub fn list_dir(&self, path: impl AsRef<Path>) -> CapabilityResult<Vec<String>> {
        self.check_permission(path.as_ref())?;
        let entries = std::fs::read_dir(path)
            .map_err(|e| CapabilityError::Error(format!("Read directory error: {}", e)))?
            .filter_map(|e| e.ok())
            .map(|e| e.file_name().to_string_lossy().to_string())
            .collect();
        Ok(entries)
    }

    fn check_permission(&self, path: &Path) -> CapabilityResult<()> {
        let path_str = path.to_string_lossy().to_string();
        let allowed = self.allowed_paths.lock().unwrap();

        // Check if path or any parent is in allowed list
        for allowed_path in allowed.iter() {
            if path_str.starts_with(allowed_path) {
                return Ok(());
            }
        }

        Err(CapabilityError::PermissionDenied(Capability::Filesystem))
    }
}

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

/// GPS Location
#[derive(Debug, Clone, Copy)]
pub struct Location {
    pub latitude: f64,
    pub longitude: f64,
    pub altitude: Option<f64>,
    pub accuracy: f64,
    pub timestamp: u64,
}

/// GPS API
pub struct GPS {
    current_location: Arc<Mutex<Option<Location>>>,
    enabled: Arc<Mutex<bool>>,
}

impl GPS {
    pub fn new() -> Self {
        Self {
            current_location: Arc::new(Mutex::new(None)),
            enabled: Arc::new(Mutex::new(false)),
        }
    }

    /// Request GPS permission and enable
    pub fn enable(&self) -> CapabilityResult<()> {
        *self.enabled.lock().unwrap() = true;
        Ok(())
    }

    /// Get current location
    pub fn get_location(&self) -> CapabilityResult<Location> {
        if !*self.enabled.lock().unwrap() {
            return Err(CapabilityError::PermissionDenied(Capability::Location));
        }

        self.current_location
            .lock()
            .unwrap()
            .ok_or(CapabilityError::NotAvailable(Capability::Location))
    }

    /// Set location (for testing or simulation)
    pub fn set_location(&self, location: Location) {
        *self.current_location.lock().unwrap() = Some(location);
    }

    /// Check if GPS is enabled
    pub fn is_enabled(&self) -> bool {
        *self.enabled.lock().unwrap()
    }
}

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

/// Camera image data
#[derive(Debug, Clone)]
pub struct CameraImage {
    pub width: u32,
    pub height: u32,
    pub data: Vec<u8>,
    pub format: ImageFormat,
}

/// Image format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImageFormat {
    RGB,
    RGBA,
    JPEG,
    PNG,
}

/// Camera API
pub struct Camera {
    enabled: Arc<Mutex<bool>>,
    captured_images: Arc<Mutex<Vec<CameraImage>>>,
}

impl Camera {
    pub fn new() -> Self {
        Self {
            enabled: Arc::new(Mutex::new(false)),
            captured_images: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Request camera permission
    pub fn request_permission(&self) -> CapabilityResult<()> {
        *self.enabled.lock().unwrap() = true;
        Ok(())
    }

    /// Capture an image
    pub fn capture(&self) -> CapabilityResult<CameraImage> {
        if !*self.enabled.lock().unwrap() {
            return Err(CapabilityError::PermissionDenied(Capability::Camera));
        }

        // In real implementation, would access device camera
        // For now, return a placeholder
        let image = CameraImage {
            width: 640,
            height: 480,
            data: vec![0; 640 * 480 * 3],
            format: ImageFormat::RGB,
        };

        self.captured_images.lock().unwrap().push(image.clone());
        Ok(image)
    }

    /// Get captured images
    pub fn get_captured_images(&self) -> Vec<CameraImage> {
        self.captured_images.lock().unwrap().clone()
    }
}

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

/// Clipboard API
pub struct Clipboard {
    content: Arc<Mutex<String>>,
}

impl Clipboard {
    pub fn new() -> Self {
        Self {
            content: Arc::new(Mutex::new(String::new())),
        }
    }

    /// Read clipboard text
    pub fn read_text(&self) -> CapabilityResult<String> {
        Ok(self.content.lock().unwrap().clone())
    }

    /// Write text to clipboard
    pub fn write_text(&self, text: &str) -> CapabilityResult<()> {
        *self.content.lock().unwrap() = text.to_string();
        Ok(())
    }

    /// Check if clipboard has content
    pub fn has_content(&self) -> bool {
        !self.content.lock().unwrap().is_empty()
    }
}

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

/// Notification
#[derive(Debug, Clone)]
pub struct Notification {
    pub title: String,
    pub body: String,
    pub icon: Option<String>,
}

/// Notifications API
pub struct Notifications {
    enabled: Arc<Mutex<bool>>,
    sent_notifications: Arc<Mutex<Vec<Notification>>>,
}

impl Notifications {
    pub fn new() -> Self {
        Self {
            enabled: Arc::new(Mutex::new(false)),
            sent_notifications: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Request notification permission
    pub fn request_permission(&self) -> CapabilityResult<()> {
        *self.enabled.lock().unwrap() = true;
        Ok(())
    }

    /// Send a notification
    pub fn send(&self, notification: Notification) -> CapabilityResult<()> {
        if !*self.enabled.lock().unwrap() {
            return Err(CapabilityError::PermissionDenied(Capability::Notifications));
        }

        self.sent_notifications.lock().unwrap().push(notification);
        Ok(())
    }

    /// Get sent notifications (for testing)
    pub fn get_sent(&self) -> Vec<Notification> {
        self.sent_notifications.lock().unwrap().clone()
    }
}

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

/// Accelerometer data
#[derive(Debug, Clone, Copy)]
pub struct AccelerometerData {
    pub x: f64,
    pub y: f64,
    pub z: f64,
    pub timestamp: u64,
}

/// Accelerometer API
pub struct Accelerometer {
    enabled: Arc<Mutex<bool>>,
    current_data: Arc<Mutex<Option<AccelerometerData>>>,
}

impl Accelerometer {
    pub fn new() -> Self {
        Self {
            enabled: Arc::new(Mutex::new(false)),
            current_data: Arc::new(Mutex::new(None)),
        }
    }

    /// Start accelerometer
    pub fn start(&self) -> CapabilityResult<()> {
        *self.enabled.lock().unwrap() = true;
        Ok(())
    }

    /// Stop accelerometer
    pub fn stop(&self) {
        *self.enabled.lock().unwrap() = false;
    }

    /// Get current data
    pub fn get_data(&self) -> CapabilityResult<AccelerometerData> {
        if !*self.enabled.lock().unwrap() {
            return Err(CapabilityError::NotAvailable(Capability::Haptics));
        }

        self.current_data
            .lock()
            .unwrap()
            .ok_or(CapabilityError::NotAvailable(Capability::Haptics))
    }

    /// Set data (for testing)
    pub fn set_data(&self, data: AccelerometerData) {
        *self.current_data.lock().unwrap() = Some(data);
    }
}

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

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

    #[test]
    fn test_filesystem_permission() {
        let fs = Filesystem::new();
        assert!(fs.request_permission("/tmp").is_ok());
    }

    #[test]
    fn test_filesystem_read_write() {
        let fs = Filesystem::new();
        let path = "/tmp/test_windjammer.txt";

        fs.request_permission("/tmp").unwrap();
        fs.write_text(path, "Hello, Windjammer!").unwrap();
        let content = fs.read_text(path).unwrap();

        assert_eq!(content, "Hello, Windjammer!");

        // Clean up
        let _ = std::fs::remove_file(path);
    }

    #[test]
    fn test_filesystem_permission_denied() {
        let fs = Filesystem::new();
        let result = fs.read_text("/etc/passwd");
        assert!(matches!(
            result,
            Err(CapabilityError::PermissionDenied(Capability::Filesystem))
        ));
    }

    #[test]
    fn test_gps_enable() {
        let gps = GPS::new();
        assert!(!gps.is_enabled());

        gps.enable().unwrap();
        assert!(gps.is_enabled());
    }

    #[test]
    fn test_gps_location() {
        let gps = GPS::new();
        gps.enable().unwrap();

        let location = Location {
            latitude: 37.7749,
            longitude: -122.4194,
            altitude: Some(10.0),
            accuracy: 5.0,
            timestamp: 1234567890,
        };

        gps.set_location(location);
        let retrieved = gps.get_location().unwrap();

        assert_eq!(retrieved.latitude, 37.7749);
        assert_eq!(retrieved.longitude, -122.4194);
    }

    #[test]
    fn test_camera_permission() {
        let camera = Camera::new();
        assert!(camera.request_permission().is_ok());
    }

    #[test]
    fn test_camera_capture() {
        let camera = Camera::new();
        camera.request_permission().unwrap();

        let image = camera.capture().unwrap();
        assert_eq!(image.width, 640);
        assert_eq!(image.height, 480);
        assert_eq!(camera.get_captured_images().len(), 1);
    }

    #[test]
    fn test_clipboard() {
        let clipboard = Clipboard::new();
        assert!(!clipboard.has_content());

        clipboard.write_text("Test content").unwrap();
        assert!(clipboard.has_content());

        let content = clipboard.read_text().unwrap();
        assert_eq!(content, "Test content");
    }

    #[test]
    fn test_notifications() {
        let notifications = Notifications::new();
        notifications.request_permission().unwrap();

        let notification = Notification {
            title: "Test".to_string(),
            body: "This is a test".to_string(),
            icon: None,
        };

        notifications.send(notification).unwrap();
        assert_eq!(notifications.get_sent().len(), 1);
    }

    #[test]
    fn test_accelerometer() {
        let accel = Accelerometer::new();
        accel.start().unwrap();

        let data = AccelerometerData {
            x: 1.0,
            y: 2.0,
            z: 3.0,
            timestamp: 1234567890,
        };

        accel.set_data(data);
        let retrieved = accel.get_data().unwrap();

        assert_eq!(retrieved.x, 1.0);
        assert_eq!(retrieved.y, 2.0);
        assert_eq!(retrieved.z, 3.0);
    }
}