|
| 1 | +//! Startup flags for window chrome that must be applied before the webview exists. |
| 2 | +//! |
| 3 | +//! Acrylic + Overlay CSD is shaped for macOS/Windows. On Wayland, WebKitGTK |
| 4 | +//! cannot composite an alpha surface (no protocol support), and GTK CSD shows |
| 5 | +//! as a black bar. These flags rewrite `tauri.conf.json` window fields in |
| 6 | +//! memory so the window is created opaque and undecorated. |
| 7 | +
|
| 8 | +use tauri::utils::config::{Color, WindowConfig}; |
| 9 | +use tauri::TitleBarStyle; |
| 10 | + |
| 11 | +/// Cream fill used by the acrylic tint, with a fully opaque alpha. |
| 12 | +const OPAQUE_BACKGROUND: Color = Color(250, 244, 237, 255); |
| 13 | + |
| 14 | +pub const USAGE: &str = "\ |
| 15 | +PinkCode - desktop GUI for Grok Build |
| 16 | +
|
| 17 | +Usage: |
| 18 | + PinkCode [options] |
| 19 | +
|
| 20 | +Options: |
| 21 | + --disable-csd Hide client-side decorations (fixes the black |
| 22 | + title bar on Wayland compositors such as niri) |
| 23 | + --disable-transparency Opaque window (WebKitGTK has no Wayland |
| 24 | + protocol for alpha compositing) |
| 25 | + -h, --help Show this help |
| 26 | +"; |
| 27 | + |
| 28 | +#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 29 | +pub struct Flags { |
| 30 | + pub disable_csd: bool, |
| 31 | + pub disable_transparency: bool, |
| 32 | +} |
| 33 | + |
| 34 | +pub enum Action { |
| 35 | + Run(Flags), |
| 36 | + Help, |
| 37 | +} |
| 38 | + |
| 39 | +impl Flags { |
| 40 | + /// GTK reads `GTK_CSD` at init; `set_decorations(false)` after the window |
| 41 | + /// exists is not enough to stop the black CSD bar. |
| 42 | + pub fn prepare_env(self) { |
| 43 | + if self.disable_csd { |
| 44 | + #[cfg(target_os = "linux")] |
| 45 | + { |
| 46 | + std::env::set_var("GTK_CSD", "0"); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + pub fn apply(self, config: &mut tauri::Config) { |
| 52 | + if !self.disable_csd && !self.disable_transparency { |
| 53 | + return; |
| 54 | + } |
| 55 | + for win in &mut config.app.windows { |
| 56 | + self.apply_window(win); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fn apply_window(self, win: &mut WindowConfig) { |
| 61 | + if self.disable_csd { |
| 62 | + win.decorations = false; |
| 63 | + win.hidden_title = false; |
| 64 | + win.title_bar_style = TitleBarStyle::Visible; |
| 65 | + } |
| 66 | + if self.disable_transparency { |
| 67 | + win.transparent = false; |
| 68 | + win.window_effects = None; |
| 69 | + win.background_color = Some(OPAQUE_BACKGROUND); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +/// Unknown args are ignored so WebKit / `tauri dev` extra flags still work. |
| 75 | +pub fn parse<I, S>(args: I) -> Action |
| 76 | +where |
| 77 | + I: IntoIterator<Item = S>, |
| 78 | + S: AsRef<str>, |
| 79 | +{ |
| 80 | + let mut flags = Flags::default(); |
| 81 | + for arg in args.into_iter().skip(1) { |
| 82 | + match arg.as_ref() { |
| 83 | + "-h" | "--help" => return Action::Help, |
| 84 | + "--disable-csd" => flags.disable_csd = true, |
| 85 | + "--disable-transparency" => flags.disable_transparency = true, |
| 86 | + _ => {} |
| 87 | + } |
| 88 | + } |
| 89 | + Action::Run(flags) |
| 90 | +} |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use super::*; |
| 95 | + |
| 96 | + fn flags_of(args: &[&str]) -> Flags { |
| 97 | + match parse(args.iter().copied()) { |
| 98 | + Action::Run(flags) => flags, |
| 99 | + Action::Help => panic!("expected Run"), |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn no_args_leaves_defaults() { |
| 105 | + assert_eq!(flags_of(&["PinkCode"]), Flags::default()); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn each_flag_and_both() { |
| 110 | + assert_eq!( |
| 111 | + flags_of(&["PinkCode", "--disable-csd"]), |
| 112 | + Flags { |
| 113 | + disable_csd: true, |
| 114 | + disable_transparency: false, |
| 115 | + } |
| 116 | + ); |
| 117 | + assert_eq!( |
| 118 | + flags_of(&["PinkCode", "--disable-transparency"]), |
| 119 | + Flags { |
| 120 | + disable_csd: false, |
| 121 | + disable_transparency: true, |
| 122 | + } |
| 123 | + ); |
| 124 | + assert_eq!( |
| 125 | + flags_of(&["PinkCode", "--disable-csd", "--disable-transparency"]), |
| 126 | + Flags { |
| 127 | + disable_csd: true, |
| 128 | + disable_transparency: true, |
| 129 | + } |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn help_and_unknown_args() { |
| 135 | + assert!(matches!(parse(["PinkCode", "--help"]), Action::Help)); |
| 136 | + assert_eq!( |
| 137 | + flags_of(&["PinkCode", "--something-webkit-passes"]), |
| 138 | + Flags::default() |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn apply_rewrites_overlay_acrylic_window() { |
| 144 | + let mut win = WindowConfig { |
| 145 | + transparent: true, |
| 146 | + decorations: true, |
| 147 | + hidden_title: true, |
| 148 | + title_bar_style: TitleBarStyle::Overlay, |
| 149 | + window_effects: Some(Default::default()), |
| 150 | + background_color: Some(Color(0, 0, 0, 0)), |
| 151 | + ..Default::default() |
| 152 | + }; |
| 153 | + Flags { |
| 154 | + disable_csd: true, |
| 155 | + disable_transparency: true, |
| 156 | + } |
| 157 | + .apply_window(&mut win); |
| 158 | + assert!(!win.decorations); |
| 159 | + assert!(!win.hidden_title); |
| 160 | + assert_eq!(win.title_bar_style, TitleBarStyle::Visible); |
| 161 | + assert!(!win.transparent); |
| 162 | + assert!(win.window_effects.is_none()); |
| 163 | + assert_eq!(win.background_color, Some(OPAQUE_BACKGROUND)); |
| 164 | + } |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn apply_is_a_no_op_without_flags() { |
| 168 | + let mut win = WindowConfig { |
| 169 | + transparent: true, |
| 170 | + decorations: true, |
| 171 | + ..Default::default() |
| 172 | + }; |
| 173 | + Flags::default().apply_window(&mut win); |
| 174 | + assert!(win.transparent); |
| 175 | + assert!(win.decorations); |
| 176 | + } |
| 177 | +} |
0 commit comments