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
use crate::{
    api, consts,
    types::{self, Uid},
};

/// Any kind of value, to be coupled with `WindowType` into a `WindowTypeValue`.
#[derive(
    Clone,
    Debug,
    PartialEq,
    PartialOrd,
    serde::Serialize,
    serde::Deserialize,
)]
pub enum Value {
    WebContents(types::OVRWebContents),
    I32(i32),
}

impl From<&WindowTypeValue> for Value {
    fn from(valued: &WindowTypeValue) -> Value {
        use WindowTypeValue::*;
        match valued {
            WebPage(v) => Value::WebContents(v.clone()),
            DesktopCapture(v) => Value::I32(*v),
            WindowCapture(v) => Value::I32(*v),
        }
    }
}

/// This is a composition of `WindowType` and `Value`,
/// used to change contents of an overlay.
#[derive(
    Clone,
    Debug,
    PartialEq,
    PartialOrd,
    serde::Serialize,
    serde::Deserialize,
)]
pub enum WindowTypeValue {
    WebPage(types::OVRWebContents),
    DesktopCapture(i32),
    WindowCapture(i32),
}

impl WindowTypeValue {
    /// Given a type kind and a value, tries to compose a `WindowTypeValue`.
    pub fn compose(
        kind: consts::WindowType,
        value: Value,
    ) -> Option<Self> {
        kind.with(value)
    }
    /// Extracts the type kind and value.
    pub fn decompose(&self) -> (consts::WindowType, Value) {
        (consts::WindowType::from(self), Value::from(self))
    }
    /// Uses this type kind and value to set an overlay content.
    ///
    /// TODO: if this presents a too high overhead, new abstractions
    /// can be introduced to call `set_contents_...` functions
    /// in a more direct manner.
    pub fn set_in_overlay(&self, uid: Uid) {
        use WindowTypeValue::*;
        unsafe {
            match self {
                WebPage(v) => api::set_contents_website(uid, v),
                DesktopCapture(v) => {
                    api::set_contents_desktop(uid, *v)
                }
                WindowCapture(v) => {
                    api::set_contents_window(uid, *v)
                }
            }
        }
    }
}