Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from
Jul 3, 2024
6 changes: 6 additions & 0 deletions .changes/tauri-set-title-bar-style.md
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,7 @@ pub enum WindowMessage {
SetCursorPosition(Position),
SetIgnoreCursorEvents(bool),
SetProgressBar(ProgressBarState),
SetTitleBarStyle(tauri_utils::TitleBarStyle),
DragWindow,
ResizeDragWindow(tauri_runtime::ResizeDirection),
RequestRedraw,
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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);
}
};
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions core/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
26 changes: 26 additions & 0 deletions core/tauri/permissions/window/autogenerated/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions core/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 5 additions & 0 deletions core/tauri/src/webview/webview_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions core/tauri/src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions core/tauri/src/window/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{
#[cfg(desktop)]
mod desktop_commands {
use tauri_runtime::ResizeDirection;
use tauri_utils::TitleBarStyle;

use super::*;
use crate::{
Expand Down Expand Up @@ -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>(
Expand Down Expand Up @@ -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,
]);
Expand Down
12 changes: 12 additions & 0 deletions tooling/api/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down
Loading