workflow_egui/
device.rs

1use crate::imports::*;
2
3#[derive(Default, Clone, Copy, Debug, Eq, PartialEq)]
4pub enum Orientation {
5    #[default]
6    Landscape,
7    Portrait,
8}
9
10#[derive(Default, Clone)]
11pub struct Device {
12    pub mobile_device: bool,
13    pub mobile_forced: bool,
14    pub orientation: Orientation,
15    pub orientation_forced: Option<Orientation>,
16    pub screen_size: Vec2,
17}
18
19impl Device {
20    pub fn set_screen_size(&mut self, rect: &Rect) {
21        let size = rect.size();
22
23        if size.x * 3. < size.y * 2. || size.x < 540.0 {
24            self.orientation = Orientation::Portrait;
25        } else {
26            self.orientation = Orientation::Landscape;
27        }
28
29        self.screen_size = rect.size();
30    }
31
32    pub fn orientation(&self) -> Orientation {
33        self.orientation_forced.unwrap_or(self.orientation)
34    }
35
36    pub fn is_mobile(&self) -> bool {
37        self.mobile_device || self.mobile_forced
38    }
39
40    pub fn toggle_portrait(&mut self) {
41        if self.orientation_forced.is_none() {
42            self.orientation_forced = Some(Orientation::Portrait);
43        } else {
44            self.orientation_forced = None;
45        }
46    }
47
48    pub fn toggle_mobile(&mut self) {
49        self.mobile_forced = !self.mobile_forced;
50    }
51
52    pub fn is_single_pane(&self) -> bool {
53        workflow_core::runtime::is_chrome_extension()
54            || self.mobile_forced
55            || self.mobile_device
56            || self.orientation() == Orientation::Portrait
57    }
58
59    #[inline]
60    pub fn is_desktop(&self) -> bool {
61        !self.is_single_pane()
62    }
63
64    pub fn force_orientation(&mut self, orientation: Option<Orientation>) {
65        self.orientation_forced = orientation;
66    }
67}