orbclient 0.4.3

The Orbital Client Library
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
#[cfg(not(feature = "std"))]
use alloc::string::String;
use core::fmt::Display;
use core::str::FromStr;

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum WindowFlag {
    /// Asynchronous Event
    Async,
    /// Do not use. Reserved for background
    Back,
    /// Always on top, even taskbar
    Front,
    /// Hide the window
    Hidden,
    /// Hide window border, handle your own window dragging API
    Borderless,
    /// Maximize the window
    Maximized,
    /// Full screen the window
    Fullscreen,
    /// Window manager can resize the window
    Resizable,
    /// Window manager can enable window scaling
    Scalable,
    /// Apply blending with window behind (slower)
    Transparent,
    /// Cannot be closed, handle your own window close API
    Unclosable,
}

impl WindowFlag {
    pub const fn try_from_u64(val: u64) -> Option<Self> {
        let val = match val {
            0x0001 => WindowFlag::Async,
            0x0002 => WindowFlag::Back,
            0x0004 => WindowFlag::Front,
            0x0008 => WindowFlag::Resizable,
            0x0010 => WindowFlag::Unclosable,
            0x0020 => WindowFlag::Borderless,
            0x0040 => WindowFlag::Transparent,
            0x0080 => WindowFlag::Maximized,
            0x0100 => WindowFlag::Fullscreen,
            0x0200 => WindowFlag::Hidden,
            0x0400 => WindowFlag::Scalable,
            _ => return None,
        };
        Some(val)
    }
    pub const fn to_u64(&self) -> u64 {
        match self {
            WindowFlag::Async => 0x0001,
            WindowFlag::Back => 0x0002,
            WindowFlag::Front => 0x0004,
            WindowFlag::Resizable => 0x0008,
            WindowFlag::Unclosable => 0x0010,
            WindowFlag::Borderless => 0x0020,
            WindowFlag::Transparent => 0x0040,
            WindowFlag::Maximized => 0x0080,
            WindowFlag::Fullscreen => 0x0100,
            WindowFlag::Hidden => 0x0200,
            WindowFlag::Scalable => 0x0400,
        }
    }
    pub const fn try_from_byte(val: u8) -> Option<Self> {
        match val {
            b'a' => Some(WindowFlag::Async),
            b'b' => Some(WindowFlag::Back),
            b'f' => Some(WindowFlag::Front),
            b'h' => Some(WindowFlag::Hidden),
            b'l' => Some(WindowFlag::Borderless),
            b'm' => Some(WindowFlag::Maximized),
            b'M' => Some(WindowFlag::Fullscreen),
            b'r' => Some(WindowFlag::Resizable),
            b's' => Some(WindowFlag::Scalable),
            b't' => Some(WindowFlag::Transparent),
            b'u' => Some(WindowFlag::Unclosable),
            _ => None,
        }
    }
    const fn max_u64() -> u64 {
        // TODO: use https://github.com/rust-lang/rust/issues/73662
        WindowFlag::Scalable.to_u64()
    }
}

impl Display for WindowFlag {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            WindowFlag::Async => write!(f, "a"),
            WindowFlag::Back => write!(f, "b"),
            WindowFlag::Front => write!(f, "f"),
            WindowFlag::Hidden => write!(f, "h"),
            WindowFlag::Borderless => write!(f, "l"),
            WindowFlag::Maximized => write!(f, "m"),
            WindowFlag::Fullscreen => write!(f, "M"),
            WindowFlag::Resizable => write!(f, "r"),
            WindowFlag::Scalable => write!(f, "s"),
            WindowFlag::Transparent => write!(f, "t"),
            WindowFlag::Unclosable => write!(f, "u"),
        }
    }
}

#[derive(Debug, Default, Clone, Copy)]
pub struct WindowFlags(u64);

#[derive(Debug, Clone)]
pub struct WindowFlagsIter(u64, u64);

impl WindowFlags {
    /// New flags from array of WindowFlag
    pub const fn new(flags: &[WindowFlag]) -> Self {
        let mut iflags = 0;
        let mut i = 0;
        // Using while loop because of `const fn`
        while i < flags.len() {
            let flag = flags[i];
            i += 1;
            iflags |= flag.to_u64()
        }
        Self(iflags)
    }

    /// New flags from u64. Unknown constants will be saved as it is but not appear in the iterator.
    pub const fn from_u64(flags: u64) -> Self {
        Self(flags)
    }

    /// Encode flags into u64
    pub const fn to_64(&self) -> u64 {
        self.0
    }

    /// Clear flags
    pub fn clear(&mut self) {
        self.0 = 0;
    }

    /// Check if flags contains a flag
    pub const fn contains(&self, flag: WindowFlag) -> bool {
        self.0 & flag.to_u64() > 0
    }

    /// Add a flag into flags
    pub fn push(&mut self, flag: WindowFlag) {
        self.0 |= flag.to_u64();
    }

    /// Remove a flag from flags
    pub fn remove(&mut self, flag: WindowFlag) {
        self.0 &= !(flag.to_u64());
    }
}

impl FromStr for WindowFlags {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut iflags = 0;
        for (i, c) in s.bytes().enumerate() {
            let Some(ch) = WindowFlag::try_from_byte(c) else {
                // return remaining flags
                return Err(s.get(i..).unwrap_or("").into());
            };
            iflags |= ch.to_u64()
        }
        Ok(Self(iflags))
    }
}

impl<'a, const N: usize> From<&[WindowFlag; N]> for WindowFlags {
    fn from(value: &[WindowFlag; N]) -> Self {
        Self::new(value)
    }
}

impl Display for WindowFlags {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut i = 1;
        while i <= WindowFlag::max_u64() {
            let Some(flag) = WindowFlag::try_from_u64(i) else {
                continue;
            };
            if self.contains(flag) {
                write!(f, "{}", flag)?;
            }
            i *= 2;
        }
        Ok(())
    }
}

impl IntoIterator for WindowFlags {
    type Item = WindowFlag;
    type IntoIter = WindowFlagsIter;

    fn into_iter(self) -> WindowFlagsIter {
        WindowFlagsIter(self.0, 0)
    }
}

impl Iterator for WindowFlagsIter {
    type Item = WindowFlag;

    fn next(&mut self) -> Option<Self::Item> {
        while 1 << self.1 <= WindowFlag::max_u64() {
            let r = self.0 & (1 << self.1);
            self.1 += 1;
            if r == 0 {
                continue;
            }
            if let Some(flag) = WindowFlag::try_from_u64(r) {
                return Some(flag);
            }
        }
        return None;
    }
}

#[derive(Clone, Copy, Debug)]
pub enum SurfaceFlag {}

#[derive(Clone, Copy, Debug)]
pub enum Mode {
    Blend,     //Composite
    Overwrite, //Replace
}

pub enum WindowDragKind {
    None,
    Move,
    ResizeLeft,
    ResizeRight,
    ResizeBottom,
    ResizeTop,
}

impl Display for WindowDragKind {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            WindowDragKind::Move => write!(f, "M"),
            WindowDragKind::ResizeLeft => write!(f, "L"),
            WindowDragKind::ResizeRight => write!(f, "R"),
            WindowDragKind::ResizeBottom => write!(f, "B"),
            WindowDragKind::ResizeTop => write!(f, "T"),
            WindowDragKind::None => write!(f, "0"),
        }
    }
}

impl WindowDragKind {
    pub const fn try_from_u8(val: i64) -> Option<Self> {
        match val {
            0 => Some(WindowDragKind::None),
            1 => Some(WindowDragKind::Move),
            2 => Some(WindowDragKind::ResizeLeft),
            3 => Some(WindowDragKind::ResizeRight),
            4 => Some(WindowDragKind::ResizeBottom),
            5 => Some(WindowDragKind::ResizeTop),
            _ => None,
        }
    }
    pub const fn to_u8(&self) -> i64 {
        match self {
            WindowDragKind::None => 0,
            WindowDragKind::Move => 1,
            WindowDragKind::ResizeLeft => 2,
            WindowDragKind::ResizeRight => 3,
            WindowDragKind::ResizeBottom => 4,
            WindowDragKind::ResizeTop => 5,
        }
    }
    #[allow(unused)]
    pub(crate) fn to_orbital_cmd(&self) -> &'static [u8] {
        match self {
            WindowDragKind::None => b"D,0",
            WindowDragKind::Move => b"D,M",
            WindowDragKind::ResizeLeft => b"D,L",
            WindowDragKind::ResizeRight => b"D,R",
            WindowDragKind::ResizeBottom => b"D,B",
            WindowDragKind::ResizeTop => b"D,T",
        }
    }
}
impl FromStr for WindowDragKind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "" | "M" => Ok(WindowDragKind::Move),
            "L" => Ok(WindowDragKind::ResizeLeft),
            "R" => Ok(WindowDragKind::ResizeRight),
            "B" => Ok(WindowDragKind::ResizeBottom),
            "T" => Ok(WindowDragKind::ResizeTop),
            _ => Err(s.into()),
        }
    }
}

/// A type of media that registered into clipboard or DND system.
/// Text, File, Uri is designed for simple implementation.
/// For programs that wants to send custom MIME, use Data.
/// Cannot support multiple kind of data at the moment.
#[derive(Clone, Copy, Debug)]
pub enum MediaKind {
    /// A regular text
    Text,
    /// A file path (can be multiple, \n separated)
    File,
    /// A URI path (HTTP or internal identifier)
    Uri,
    /// A data URI
    Data,
    /// Do not use
    Any,
}

impl MediaKind {
    pub const fn from_i64(val: i64) -> Self {
        match val {
            1 => MediaKind::Text,
            2 => MediaKind::File,
            3 => MediaKind::Uri,
            4 => MediaKind::Data,
            _ => MediaKind::Any,
        }
    }
    pub const fn to_i64(&self) -> i64 {
        match self {
            MediaKind::Text => 1,
            MediaKind::File => 2,
            MediaKind::Uri => 3,
            MediaKind::Data => 4,
            MediaKind::Any => 1,
        }
    }
    pub fn to_mime(&self, data: &str) -> String {
        const DEFAULT_MIME: &str = "application/octet-stream";
        match self {
            MediaKind::Text => "text/plain".into(),
            MediaKind::File => "text/uri-list".into(),
            MediaKind::Uri => "text/x-uri".into(),
            MediaKind::Data => {
                if let Some(rest) = data.strip_prefix("data:") {
                    if let Some((prefix, _)) = rest.split_once(&[';', ',', '\n']) {
                        if prefix.len() < 100 {
                            return prefix.into();
                        }
                    }
                }
                DEFAULT_MIME.into()
            }
            MediaKind::Any => DEFAULT_MIME.into(),
        }
    }
}

impl Display for MediaKind {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            MediaKind::Text => write!(f, "t"),
            MediaKind::File => write!(f, "f"),
            MediaKind::Uri => write!(f, "u"),
            MediaKind::Data => write!(f, "d"),
            MediaKind::Any => write!(f, " "),
        }
    }
}

impl FromStr for MediaKind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "t" => Ok(MediaKind::Text),
            "f" => Ok(MediaKind::File),
            "u" => Ok(MediaKind::Uri),
            "d" => Ok(MediaKind::Data),
            " " => Ok(MediaKind::Any),
            _ => Err(s.into()),
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub enum ClipboardAction {
    Copy,
    Cut,
    Paste,
}

impl ClipboardAction {
    pub const fn try_from_u8(val: u8) -> Option<Self> {
        match val {
            1 => Some(ClipboardAction::Copy),
            2 => Some(ClipboardAction::Cut),
            3 => Some(ClipboardAction::Paste),
            _ => None,
        }
    }
    pub const fn to_u8(&self) -> u8 {
        match self {
            ClipboardAction::Copy => 1,
            ClipboardAction::Cut => 2,
            ClipboardAction::Paste => 3,
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub enum DragAction {
    Copy,
    Move,
    Link,
    None,
}

impl DragAction {
    pub const fn try_from_u8(val: u8) -> Option<Self> {
        match val {
            1 => Some(DragAction::Copy),
            2 => Some(DragAction::Move),
            4 => Some(DragAction::Link),
            0 => Some(DragAction::None),
            _ => None,
        }
    }
    pub const fn to_u8(&self) -> u8 {
        match self {
            DragAction::Copy => 1,
            DragAction::Move => 2,
            DragAction::Link => 4,
            DragAction::None => 0,
        }
    }
}