spell_framework/
configure.rs

1use slint::platform::software_renderer::TargetPixel;
2use smithay_client_toolkit::shell::wlr_layer::{Anchor, KeyboardInteractivity, Layer};
3use std::cell::Cell;
4
5/// Unused Internal struct representation of a pixel, it is similar to slint's
6/// representation of [pixel]() but implement few more trait. Currently, redundent
7#[derive(Default)]
8pub struct Rgba8Pixel {
9    pub a: u8,
10    pub r: u8,
11    pub g: u8,
12    pub b: u8,
13}
14
15impl Rgba8Pixel {
16    pub fn new(a: u8, r: u8, g: u8, b: u8) -> Self {
17        Rgba8Pixel { a, r, g, b }
18    }
19}
20
21impl TargetPixel for Rgba8Pixel {
22    fn blend(&mut self, color: slint::platform::software_renderer::PremultipliedRgbaColor) {
23        let a: u16 = (u8::MAX - color.alpha) as u16;
24        // self.a = a as u8;
25        let out_a = color.alpha as u16 + (self.a as u16 * (255 - color.alpha) as u16) / 255;
26        self.a = out_a as u8;
27        self.r = (self.r as u16 * a / 255) as u8 + color.red;
28        self.g = (self.g as u16 * a / 255) as u8 + color.green;
29        self.b = (self.b as u16 * a / 255) as u8 + color.blue;
30    }
31
32    fn from_rgb(red: u8, green: u8, blue: u8) -> Self {
33        let a = 0xFF;
34        Self::new(a, red, green, blue)
35    }
36
37    fn background() -> Self {
38        // TODO This needs to be decided to see how it should be 0xFF or 0x00;
39        // I think there is a bug in slint which is causing the leak of This
40        // value.
41        let a: u8 = 0x00;
42        Self::new(a, 0, 0, 0)
43    }
44}
45
46impl std::marker::Copy for Rgba8Pixel {}
47impl std::clone::Clone for Rgba8Pixel {
48    fn clone(&self) -> Self {
49        *self
50    }
51}
52
53/// WindowConf is an essential struct passed on to widget constructor functions (like [invoke_spell](crate::wayland_adapter::SpellWin::invoke_spell))
54/// for defining the specifications of the widget.
55///
56/// ## Panics
57///
58/// event_loop will panic if 0 is provided as width and height.
59#[derive(Debug, Clone)]
60pub struct WindowConf {
61    /// Defines the widget width in pixels. On setting values greater than the provided pixels of
62    /// monitor, the widget offsets from monitor's prectangular monitor space. It is important to
63    /// note that the value should be the maximum width the widget will ever attain, not the
64    /// current width in case of resizeable widgets.
65    pub width: u32,
66    /// Defines the widget height in pixels. On setting values greater than the provided pixels of
67    /// monitor, the widget offsets from monitor's prectangular monitor space. It is important to
68    /// note that the value should be the maximum width the widget will ever attain, not the
69    /// current width in case of resizeable widgets.
70    pub height: u32,
71    /// Defines the Anchors to which the window needs to be attached. View [`Anchor`] for
72    /// related explaination of usage. If both values are None, then widget is displayed in the
73    /// center of screen.
74    pub anchor: (Option<Anchor>, Option<Anchor>),
75    /// Defines the margin of widget from monitor edges, negative values make the widget go outside
76    /// of monitor pixels if anchored to some edge(s). Otherwise, the widget moves to the opposite
77    /// direction to the given pixels.
78    pub margin: (i32, i32, i32, i32),
79    /// Defines the possible layer on which to define the widget. View [`Layer`] for more details.
80    pub layer_type: Layer,
81    /// Defines the relation of widget with Keyboard. View [`KeyboardInteractivity`] for more
82    /// details.
83    pub board_interactivity: Cell<KeyboardInteractivity>,
84    /// Defines if the widget is exclusive of not, if marked true, further laying of widgets in the
85    /// same area is avoided. View [wayland docs](https://wayland.app/protocols/wlr-layer-shell-unstable-v1#zwlr_layer_surface_v1:request:set_exclusive_zone)
86    /// for details. By default, spell takes the value of width if anchored at top or bottom and
87    /// height if anchored left or right and set that to the exclusive zone.
88    /// TOTEST, define the case when widget is resizeable.
89    pub exclusive_zone: bool,
90}
91
92impl WindowConf {
93    /// constructor method for initialising an instance of WindowConf.
94    #[allow(clippy::too_many_arguments)]
95    pub fn new(
96        max_width: u32,
97        max_height: u32,
98        anchor: (Option<Anchor>, Option<Anchor>),
99        margin: (i32, i32, i32, i32),
100        layer_type: Layer,
101        board_interactivity: KeyboardInteractivity,
102        exclusive_zone: bool,
103    ) -> Self {
104        WindowConf {
105            width: max_width,
106            height: max_height,
107            anchor,
108            margin,
109            layer_type,
110            board_interactivity: Cell::new(board_interactivity),
111            exclusive_zone,
112        }
113    }
114}
115
116// TODO this will be made public when multiple widgets in the same layer is supported.
117// Likely it will be easy after the resize action is implemented
118#[allow(dead_code)]
119pub enum LayerConf {
120    Window(WindowConf),
121    Windows(Vec<WindowConf>),
122    Lock(u32, u32),
123}