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
use std::{cell::RefCell, rc::Rc};

use smithay_client_toolkit::{
    reexports::client::{
        protocol::{
            wl_compositor::WlCompositor, wl_subcompositor::WlSubcompositor,
            wl_subsurface::WlSubsurface, wl_surface::WlSurface,
        },
        Attached, DispatchData,
    },
    window::FrameRequest,
};

use crate::{surface, Inner, Location};

pub enum DecorationPartKind {
    Header,
    Top,
    Left,
    Right,
    Bottom,
    None,
}

#[derive(Debug)]
pub struct Decoration {
    pub header: Part,

    pub top: Part,
    pub left: Part,
    pub right: Part,
    pub bottom: Part,
}

impl Decoration {
    pub fn iter(&self) -> [&Part; 5] {
        [
            &self.header,
            &self.top,
            &self.left,
            &self.right,
            &self.bottom,
        ]
    }

    pub fn hide_decoration(&self) {
        for p in self.iter() {
            p.surface.attach(None, 0, 0);
            p.surface.commit();
        }
    }

    pub fn hide_borders(&self) {
        for p in self.iter().iter().skip(1) {
            p.surface.attach(None, 0, 0);
            p.surface.commit();
        }
    }
}

#[derive(Default, Debug)]
pub(crate) struct Parts {
    decoration: Option<Decoration>,
}

impl Parts {
    pub fn add_decorations(
        &mut self,
        parent: &WlSurface,
        compositor: &Attached<WlCompositor>,
        subcompositor: &Attached<WlSubcompositor>,
        inner: Rc<RefCell<Inner>>,
    ) {
        if self.decoration.is_none() {
            let header = Part::new(parent, compositor, subcompositor, Some(inner));
            let top = Part::new(parent, compositor, subcompositor, None);
            let left = Part::new(parent, compositor, subcompositor, None);
            let right = Part::new(parent, compositor, subcompositor, None);
            let bottom = Part::new(parent, compositor, subcompositor, None);

            self.decoration = Some(Decoration {
                header,
                top,
                left,
                right,
                bottom,
            });
        }
    }

    pub fn remove_decorations(&mut self) {
        self.decoration = None;
    }

    pub fn hide_decorations(&self) {
        if let Some(decor) = self.decoration.as_ref() {
            decor.hide_decoration();
        }
    }

    pub fn decoration(&self) -> Option<&Decoration> {
        self.decoration.as_ref()
    }

    pub fn find_decoration_part(&self, surface: &WlSurface) -> DecorationPartKind {
        if let Some(decor) = self.decoration() {
            if surface.as_ref().equals(decor.header.surface.as_ref()) {
                DecorationPartKind::Header
            } else if surface.as_ref().equals(decor.top.surface.as_ref()) {
                DecorationPartKind::Top
            } else if surface.as_ref().equals(decor.bottom.surface.as_ref()) {
                DecorationPartKind::Bottom
            } else if surface.as_ref().equals(decor.left.surface.as_ref()) {
                DecorationPartKind::Left
            } else if surface.as_ref().equals(decor.right.surface.as_ref()) {
                DecorationPartKind::Right
            } else {
                DecorationPartKind::None
            }
        } else {
            DecorationPartKind::None
        }
    }

    pub fn find_surface(&self, surface: &WlSurface) -> Location {
        if let Some(decor) = self.decoration() {
            if surface.as_ref().equals(decor.header.surface.as_ref()) {
                Location::Head
            } else if surface.as_ref().equals(decor.top.surface.as_ref()) {
                Location::Top
            } else if surface.as_ref().equals(decor.bottom.surface.as_ref()) {
                Location::Bottom
            } else if surface.as_ref().equals(decor.left.surface.as_ref()) {
                Location::Left
            } else if surface.as_ref().equals(decor.right.surface.as_ref()) {
                Location::Right
            } else {
                Location::None
            }
        } else {
            Location::None
        }
    }
}

#[derive(Debug)]
pub struct Part {
    pub surface: WlSurface,
    pub subsurface: WlSubsurface,
}

impl Part {
    fn new(
        parent: &WlSurface,
        compositor: &Attached<WlCompositor>,
        subcompositor: &Attached<WlSubcompositor>,
        inner: Option<Rc<RefCell<Inner>>>,
    ) -> Part {
        let surface = if let Some(inner) = inner {
            surface::setup_surface(
                compositor.create_surface(),
                Some(move |dpi, surface: WlSurface, ddata: DispatchData| {
                    surface.set_buffer_scale(dpi);
                    surface.commit();
                    (inner.borrow_mut().implem)(FrameRequest::Refresh, 0, ddata);
                }),
            )
        } else {
            surface::setup_surface(
                compositor.create_surface(),
                Some(move |dpi, surface: WlSurface, _ddata: DispatchData| {
                    surface.set_buffer_scale(dpi);
                    surface.commit();
                }),
            )
        };

        let surface = surface.detach();

        let subsurface = subcompositor.get_subsurface(&surface, parent);

        Part {
            surface,
            subsurface: subsurface.detach(),
        }
    }

    pub fn scale(&self) -> u32 {
        surface::get_surface_scale_factor(&self.surface) as u32
    }
}

impl Drop for Part {
    fn drop(&mut self) {
        self.subsurface.destroy();
        self.surface.destroy();
    }
}