Эх сурвалжийг харах

feat(macos): support changing title bar style dynamically, close #9763 (#9788)

Jason Tsai 1 жил өмнө
parent
commit
7bc6a2a1d6

+ 6 - 0
.changes/tauri-set-title-bar-style.md

@@ -0,0 +1,6 @@
+---
+'tauri': 'patch:feat'
+'@tauri-apps/api': 'patch:feat'
+---
+
+Add a new method to set title bar style dynamically on macOS.

+ 25 - 0
core/tauri-runtime-wry/src/lib.rs

@@ -1155,6 +1155,7 @@ pub enum WindowMessage {
   SetCursorPosition(Position),
   SetIgnoreCursorEvents(bool),
   SetProgressBar(ProgressBarState),
+  SetTitleBarStyle(tauri_utils::TitleBarStyle),
   DragWindow,
   ResizeDragWindow(tauri_runtime::ResizeDirection),
   RequestRedraw,
@@ -1948,6 +1949,13 @@ impl<T: UserEvent> WindowDispatch<T> for WryWindowDispatcher<T> {
       ),
     )
   }
+
+  fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> Result<()> {
+    send_user_message(
+      &self.context,
+      Message::Window(self.window_id, WindowMessage::SetTitleBarStyle(style)),
+    )
+  }
 }
 
 #[derive(Clone)]
@@ -2872,6 +2880,23 @@ fn handle_user_message<T: UserEvent>(
           WindowMessage::SetProgressBar(progress_state) => {
             window.set_progress_bar(ProgressBarStateWrapper::from(progress_state).0);
           }
+          WindowMessage::SetTitleBarStyle(_style) => {
+            #[cfg(target_os = "macos")]
+            match _style {
+              TitleBarStyle::Visible => {
+                window.set_titlebar_transparent(false);
+                window.set_fullsize_content_view(true);
+              }
+              TitleBarStyle::Transparent => {
+                window.set_titlebar_transparent(true);
+                window.set_fullsize_content_view(false);
+              }
+              TitleBarStyle::Overlay => {
+                window.set_titlebar_transparent(true);
+                window.set_fullsize_content_view(true);
+              }
+            };
+          }
         }
       }
     }

+ 7 - 0
core/tauri-runtime/src/lib.rs

@@ -783,4 +783,11 @@ pub trait WindowDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 's
   /// - **Linux / macOS**: Progress bar is app-wide and not specific to this window. Only supported desktop environments with `libunity` (e.g. GNOME).
   /// - **iOS / Android:** Unsupported.
   fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()>;
+
+  /// Sets the title bar style. Available on macOS only.
+  ///
+  /// ## Platform-specific
+  ///
+  /// - **Linux / Windows / iOS / Android:** Unsupported.
+  fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> Result<()>;
 }

+ 1 - 0
core/tauri/build.rs

@@ -106,6 +106,7 @@ const PLUGINS: &[(&str, &[(&str, bool)])] = &[
       ("start_resize_dragging", false),
       ("set_progress_bar", false),
       ("set_icon", false),
+      ("set_title_bar_style", false),
       ("toggle_maximize", false),
       // internal
       ("internal_toggle_maximize", true),

+ 26 - 0
core/tauri/permissions/window/autogenerated/reference.md

@@ -1442,6 +1442,32 @@ Denies the set_title command without any pre-configured scope.
 <tr>
 <td>
 
+`window:allow-set-title-bar-style`
+
+</td>
+<td>
+
+Enables the set_title_bar_style command without any pre-configured scope.
+
+</td>
+</tr>
+
+<tr>
+<td>
+
+`window:deny-set-title-bar-style`
+
+</td>
+<td>
+
+Denies the set_title_bar_style command without any pre-configured scope.
+
+</td>
+</tr>
+
+<tr>
+<td>
+
 `window:allow-set-visible-on-all-workspaces`
 
 </td>

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 0
core/tauri/scripts/bundle.global.js


+ 4 - 0
core/tauri/src/test/mock_runtime.rs

@@ -933,6 +933,10 @@ impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher {
   fn set_progress_bar(&self, progress_state: ProgressBarState) -> Result<()> {
     Ok(())
   }
+
+  fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> Result<()> {
+    Ok(())
+  }
 }
 
 #[derive(Debug, Clone)]

+ 5 - 0
core/tauri/src/webview/webview_window.rs

@@ -1584,6 +1584,11 @@ impl<R: Runtime> WebviewWindow<R> {
   ) -> crate::Result<()> {
     self.webview.window().set_progress_bar(progress_state)
   }
+
+  /// Sets the title bar style. **macOS only**.
+  pub fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> crate::Result<()> {
+    self.webview.window().set_title_bar_style(style)
+  }
 }
 
 /// Desktop webview setters and actions.

+ 8 - 0
core/tauri/src/window/mod.rs

@@ -1995,6 +1995,14 @@ tauri::Builder::default()
       })
       .map_err(Into::into)
   }
+  /// Sets the title bar style. **macOS only**.
+  pub fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> crate::Result<()> {
+    self
+      .window
+      .dispatcher
+      .set_title_bar_style(style)
+      .map_err(Into::into)
+  }
 }
 
 /// Progress bar state.

+ 3 - 0
core/tauri/src/window/plugin.rs

@@ -12,6 +12,7 @@ use crate::{
 #[cfg(desktop)]
 mod desktop_commands {
   use tauri_runtime::ResizeDirection;
+  use tauri_utils::TitleBarStyle;
 
   use super::*;
   use crate::{
@@ -130,6 +131,7 @@ mod desktop_commands {
   setter!(start_resize_dragging, ResizeDirection);
   setter!(set_progress_bar, ProgressBarState);
   setter!(set_visible_on_all_workspaces, bool);
+  setter!(set_title_bar_style, TitleBarStyle);
 
   #[command(root = "crate")]
   pub async fn set_icon<R: Runtime>(
@@ -276,6 +278,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
             desktop_commands::set_progress_bar,
             desktop_commands::set_icon,
             desktop_commands::set_visible_on_all_workspaces,
+            desktop_commands::set_title_bar_style,
             desktop_commands::toggle_maximize,
             desktop_commands::internal_toggle_maximize,
           ]);

+ 12 - 0
tooling/api/src/window.ts

@@ -1623,6 +1623,18 @@ class Window {
     })
   }
 
+  /**
+   * Sets the title bar style. **macOS only**.
+   *
+   * @since 2.0.0
+   */
+  async setTitleBarStyle(style: TitleBarStyle): Promise<void> {
+    return invoke('plugin:window|set_title_bar_style', {
+      label: this.label,
+      value: style
+    })
+  }
+
   // Listeners
 
   /**

Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно