Skip to main content

tauri_plugin_snap_layout/
lib.rs

1use tauri::{plugin::TauriPlugin, Manager, Runtime};
2
3mod desktop;
4mod error;
5
6pub use desktop::Snap;
7pub use error::{Error, Result};
8
9#[cfg(windows)]
10#[path = "lib_win_impl.rs"]
11mod platform;
12
13#[cfg(windows)]
14pub use platform::SnapCursor;
15
16#[cfg(not(windows))]
17#[path = "lib_dummy_impl.rs"]
18mod platform;
19
20#[cfg(not(windows))]
21/// Note: This is a no-op implementation for non-Windows platforms.
22/// This enum is provided to ensure your cross-platform code compiles.
23#[derive(Debug, Clone, Copy, PartialEq)]
24pub enum SnapCursor {
25    Arrow,
26    Hand,
27}
28
29pub struct SnapLayoutArea {
30    inner: platform::AreaBuilder,
31}
32
33impl Default for SnapLayoutArea {
34    fn default() -> Self {
35        Self::new()
36    }
37}
38
39impl SnapLayoutArea {
40    pub fn new() -> Self {
41        Self {
42            inner: platform::AreaBuilder::new(),
43        }
44    }
45    pub fn button_id(mut self, id: impl Into<String>) -> Self {
46        self.inner = self.inner.button_id(id);
47        self
48    }
49    pub fn padding_left(mut self, padding_left: i32) -> Self {
50        self.inner = self.inner.padding_left(padding_left);
51        self
52    }
53    pub fn padding_right(mut self, padding_right: i32) -> Self {
54        self.inner = self.inner.padding_right(padding_right);
55        self
56    }
57    pub fn padding_top(mut self, padding_top: i32) -> Self {
58        self.inner = self.inner.padding_top(padding_top);
59        self
60    }
61    pub fn padding_bottom(mut self, padding_bottom: i32) -> Self {
62        self.inner = self.inner.padding_bottom(padding_bottom);
63        self
64    }
65    pub fn padding_all(mut self, padding_all: i32) -> Self {
66        self.inner = self.inner.padding_all(padding_all);
67        self
68    }
69    pub fn display(mut self, is_display: bool) -> Self {
70        self.inner = self.inner.display(is_display);
71        self
72    }
73    pub fn debug_color(mut self, color: impl Into<String>) -> Self {
74        self.inner = self.inner.debug_color(color);
75        self
76    }
77    pub fn cursor(mut self, cursor: SnapCursor) -> Self {
78        self.inner = self.inner.cursor(cursor);
79        self
80    }
81    pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
82        self.inner.build()
83    }
84}
85
86pub fn init() -> SnapLayoutArea {
87    SnapLayoutArea::new()
88}
89
90pub trait SnapExt<R: Runtime> {
91    fn snap(&self) -> &Snap<R>;
92}
93impl<R: Runtime, T: Manager<R>> SnapExt<R> for T {
94    fn snap(&self) -> &Snap<R> {
95        self.state::<Snap<R>>().inner()
96    }
97}