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

This commit is contained in:
Jason Tsai 2024-07-03 10:36:51 +08:00 committed by GitHub
parent a7354f9a81
commit 7bc6a2a1d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 98 additions and 1 deletions

View File

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

View File

@ -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);
}
};
}
}
}
}

View File

@ -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<()>;
}

View File

@ -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),

View File

@ -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>

File diff suppressed because one or more lines are too long

View File

@ -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)]

View File

@ -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.

View File

@ -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.

View File

@ -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,
]);

View File

@ -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
/**