tauri-plugin-decor 1.0.1

Opinionated window decoration controls for Tauri apps.
Documentation
use tauri::plugin::{Builder, TauriPlugin};
use tauri::{Manager, Runtime};

use crate::commands;
use crate::decor::Decor;

#[derive(Clone)]
pub struct DecorPluginBuilder {
    #[cfg(any(target_os = "windows", target_os = "linux"))]
    titlebar_height: u32,
    #[cfg(any(target_os = "windows", target_os = "linux"))]
    button_width: u32,
    #[cfg(any(target_os = "windows", target_os = "linux"))]
    controls_scale: f64,
    #[cfg(any(target_os = "windows", target_os = "linux"))]
    auto_titlebar: bool,
    #[cfg(target_os = "windows")]
    snap_overlay_delay_ms: u64,
    #[cfg(any(target_os = "windows", target_os = "linux"))]
    close_hover_bg: String,
    #[cfg(any(target_os = "windows", target_os = "linux"))]
    button_hover_bg: String,
    #[cfg(target_os = "macos")]
    traffic_inset_x: f64,
    #[cfg(target_os = "macos")]
    traffic_inset_y: f64,
    #[cfg(target_os = "macos")]
    traffic_spacing: f64,
    #[cfg(target_os = "macos")]
    traffic_scale: f64,
}

impl Default for DecorPluginBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl DecorPluginBuilder {
    pub fn new() -> Self {
        Self {
            #[cfg(any(target_os = "windows", target_os = "linux"))]
            titlebar_height: 32,
            #[cfg(any(target_os = "windows", target_os = "linux"))]
            button_width: 46,
            #[cfg(any(target_os = "windows", target_os = "linux"))]
            controls_scale: 1.0,
            #[cfg(any(target_os = "windows", target_os = "linux"))]
            auto_titlebar: false,
            #[cfg(target_os = "windows")]
            snap_overlay_delay_ms: 10,
            #[cfg(any(target_os = "windows", target_os = "linux"))]
            close_hover_bg: "rgba(196,43,28,1)".into(),
            #[cfg(any(target_os = "windows", target_os = "linux"))]
            button_hover_bg: "rgba(0,0,0,0.2)".into(),
            #[cfg(target_os = "macos")]
            traffic_inset_x: 12.0,
            #[cfg(target_os = "macos")]
            traffic_inset_y: 16.0,
            #[cfg(target_os = "macos")]
            traffic_spacing: 20.0,
            #[cfg(target_os = "macos")]
            traffic_scale: 1.0,
        }
    }

    pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
        self.apply_to_config();

        Builder::new("decor")
            .invoke_handler(tauri::generate_handler![
                commands::show_snap_overlay,
                commands::update_style
            ])
            .setup(|app, _| {
                app.manage(Decor::new(app.clone()));
                Ok(())
            })
            .on_page_load(|win, _| crate::dispatch::on_page_load(win))
            .on_window_ready(crate::dispatch::on_window_ready)
            .build()
    }

    #[cfg(any(target_os = "windows", target_os = "linux"))]
    fn apply_to_config(&self) {
        crate::config::store(
            self.titlebar_height,
            self.button_width,
            self.controls_scale,
            self.auto_titlebar,
            self.close_hover_bg.clone(),
            self.button_hover_bg.clone(),
        );
        #[cfg(target_os = "windows")]
        crate::config::store_snap(self.snap_overlay_delay_ms);
    }

    #[cfg(target_os = "macos")]
    fn apply_to_config(&self) {
        crate::config::store_traffic(
            self.traffic_inset_x,
            self.traffic_inset_y,
            self.traffic_spacing,
            self.traffic_scale,
        );
    }

    #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
    fn apply_to_config(&self) {}
}

#[cfg(any(target_os = "windows", target_os = "linux"))]
impl DecorPluginBuilder {
    pub fn controls_height(mut self, height: f64) -> Self {
        self.titlebar_height = height.round().max(0.0) as u32;
        self
    }

    pub fn controls_inset_x(self, _inset: f64) -> Self {
        self
    }

    pub fn controls_spacing(self, _spacing: f64) -> Self {
        self
    }

    pub fn controls_scale(mut self, scale: f64) -> Self {
        self.controls_scale = scale;
        self
    }

    pub fn controls_button_width(mut self, width: u32) -> Self {
        self.button_width = width;
        self
    }

    pub fn controls_close_hover_bg(mut self, color: impl Into<String>) -> Self {
        self.close_hover_bg = color.into();
        self
    }

    pub fn controls_button_hover_bg(mut self, color: impl Into<String>) -> Self {
        self.button_hover_bg = color.into();
        self
    }

    pub fn controls_auto_titlebar(mut self, auto: bool) -> Self {
        self.auto_titlebar = auto;
        self
    }
}

#[cfg(target_os = "macos")]
impl DecorPluginBuilder {
    pub fn controls_height(mut self, height: f64) -> Self {
        self.traffic_inset_y = height;
        self
    }

    pub fn controls_inset_x(mut self, inset: f64) -> Self {
        self.traffic_inset_x = inset;
        self
    }

    pub fn controls_spacing(mut self, spacing: f64) -> Self {
        self.traffic_spacing = spacing;
        self
    }

    pub fn controls_scale(mut self, scale: f64) -> Self {
        self.traffic_scale = scale;
        self
    }

    pub fn controls_button_width(self, _width: u32) -> Self {
        self
    }

    pub fn controls_close_hover_bg(self, _color: impl Into<String>) -> Self {
        self
    }

    pub fn controls_button_hover_bg(self, _color: impl Into<String>) -> Self {
        self
    }

    pub fn controls_auto_titlebar(self, _auto: bool) -> Self {
        self
    }
}

#[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
impl DecorPluginBuilder {
    pub fn controls_height(self, _height: f64) -> Self {
        self
    }
    pub fn controls_inset_x(self, _inset: f64) -> Self {
        self
    }
    pub fn controls_spacing(self, _spacing: f64) -> Self {
        self
    }
    pub fn controls_scale(self, _scale: f64) -> Self {
        self
    }
    pub fn controls_button_width(self, _width: u32) -> Self {
        self
    }
    pub fn controls_close_hover_bg(self, _color: impl Into<String>) -> Self {
        self
    }
    pub fn controls_button_hover_bg(self, _color: impl Into<String>) -> Self {
        self
    }
    pub fn controls_auto_titlebar(self, _auto: bool) -> Self {
        self
    }
}

#[cfg(target_os = "windows")]
impl DecorPluginBuilder {
    pub fn snap_overlay_delay_ms(mut self, delay: u64) -> Self {
        self.snap_overlay_delay_ms = delay;
        self
    }
}

#[cfg(not(target_os = "windows"))]
impl DecorPluginBuilder {
    pub fn snap_overlay_delay_ms(self, _delay: u64) -> Self {
        self
    }
}