Skip to content

Commit

Permalink
that was so weird to implement
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianLars committed Jan 12, 2025
1 parent 02af143 commit ba941fa
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 19 deletions.
34 changes: 21 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ opt-level = "s"
[patch.crates-io]
schemars_derive = { git = 'https://github.com/tauri-apps/schemars.git', branch = 'feat/preserve-description-newlines' }
tauri = { path = "./crates/tauri" }
wry = { git = "https://github.com/tauri-apps/wry" }
tao = { git = "https://github.com/tauri-apps/tao" }
2 changes: 1 addition & 1 deletion crates/tauri-cli/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@
]
},
"trafficLightPosition": {
"description": "The position of the window controls on macOS.\n Requires titleBarStyle: Overlay and decorations: true.",
"description": "The position of the window controls on macOS.\n\n Requires titleBarStyle: Overlay and decorations: true.",
"anyOf": [
{
"$ref": "#/definitions/LogicalPosition"
Expand Down
32 changes: 30 additions & 2 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,9 @@ impl WindowBuilder for WindowBuilderWrapper {
if let Some(identifier) = &config.tabbing_identifier {
window = window.tabbing_identifier(identifier);
}
if let Some(position) = &config.traffic_light_position {
window = window.traffic_light_position(position.x, position.y);
}
}

#[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
Expand Down Expand Up @@ -1069,8 +1072,10 @@ impl WindowBuilder for WindowBuilderWrapper {
}

#[cfg(target_os = "macos")]
fn traffic_light_position(mut self, position: Position) -> Self {
self.inner = self.inner.with_traffic_light_inset(position);
fn traffic_light_position(mut self, x: f64, y: f64) -> Self {
self.inner = self
.inner
.with_traffic_light_inset(TaoLogicalPosition::new(x, y));
self
}

Expand Down Expand Up @@ -1276,6 +1281,7 @@ pub enum WindowMessage {
SetOverlayIcon(Option<TaoIcon>),
SetProgressBar(ProgressBarState),
SetTitleBarStyle(tauri_utils::TitleBarStyle),
SetTrafficLightPosition(Position),
SetTheme(Option<Theme>),
SetBackgroundColor(Option<Color>),
DragWindow,
Expand Down Expand Up @@ -2179,6 +2185,16 @@ impl<T: UserEvent> WindowDispatch<T> for WryWindowDispatcher<T> {
)
}

fn set_traffic_light_position(&self, position: Position) -> Result<()> {
send_user_message(
&self.context,
Message::Window(
self.window_id,
WindowMessage::SetTrafficLightPosition(position),
),
)
}

fn set_theme(&self, theme: Option<Theme>) -> Result<()> {
send_user_message(
&self.context,
Expand Down Expand Up @@ -3186,6 +3202,10 @@ fn handle_user_message<T: UserEvent>(
}
};
}
WindowMessage::SetTrafficLightPosition(_position) => {
#[cfg(target_os = "macos")]
window.set_traffic_light_inset(_position);
}
WindowMessage::SetTheme(theme) => {
window.set_theme(match theme {
Some(Theme::Light) => Some(TaoTheme::Light),
Expand Down Expand Up @@ -4366,6 +4386,14 @@ fn create_webview<T: UserEvent>(
}
}

#[cfg(target_os = "macos")]
{
use wry::WebViewBuilderExtDarwin;
if let Some(position) = &webview_attributes.traffic_light_position {
webview_builder = webview_builder.with_traffic_light_inset(*position);
}
}

webview_builder = webview_builder.with_ipc_handler(create_ipc_handler(
kind,
window_id.clone(),
Expand Down
11 changes: 10 additions & 1 deletion crates/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub enum RunEvent<T: UserEvent> {
Resumed,
/// Emitted when all of the event loop's input events have been processed and redraw processing is about to begin.
///
/// This event is useful as a place to put your code that should be run after all state-changing events have been handled and you want to do stuff (updating state, performing calculations, etc) that happens as the main body of your event loop.
/// This event is useful as a place to put your code that should be run after all state-changing events have been handled and you want to do stuff (updating state, performing calculations, etc) that happens as the "main body" of your event loop.
MainEventsCleared,
/// Emitted when the user wants to open the specified resource with the app.
#[cfg(any(target_os = "macos", target_os = "ios"))]
Expand Down Expand Up @@ -847,6 +847,15 @@ pub trait WindowDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 's
/// - **Linux / Windows / iOS / Android:** Unsupported.
fn set_title_bar_style(&self, style: tauri_utils::TitleBarStyle) -> Result<()>;

/// Change the position of the window controls. Available on macOS only.
///
/// Requires titleBarStyle: Overlay and decorations: true.
///
/// ## Platform-specific
///
/// - **Linux / Windows / iOS / Android:** Unsupported.
fn set_traffic_light_position(&self, position: Position) -> Result<()>;

/// Sets the theme for this window.
///
/// ## Platform-specific
Expand Down
22 changes: 22 additions & 0 deletions crates/tauri-runtime/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ pub struct WebviewAttributes {
pub use_https_scheme: bool,
pub devtools: Option<bool>,
pub background_color: Option<Color>,
pub traffic_light_position: Option<dpi::Position>,
}

impl From<&WindowConfig> for WebviewAttributes {
Expand All @@ -230,6 +231,13 @@ impl From<&WindowConfig> for WebviewAttributes {
{
builder = builder.transparent(config.transparent);
}
#[cfg(target_os = "macos")]
{
if let Some(position) = &config.traffic_light_position {
builder =
builder.traffic_light_position(dpi::LogicalPosition::new(position.x, position.y).into());
}
}
builder = builder.accept_first_mouse(config.accept_first_mouse);
if !config.drag_drop_enabled {
builder = builder.disable_drag_drop_handler();
Expand Down Expand Up @@ -279,6 +287,7 @@ impl WebviewAttributes {
use_https_scheme: false,
devtools: None,
background_color: None,
traffic_light_position: None,
}
}

Expand Down Expand Up @@ -444,6 +453,19 @@ impl WebviewAttributes {
self.background_color = Some(color);
self
}

/// Change the position of the window controls. Available on macOS only.
///
/// Requires titleBarStyle: Overlay and decorations: true.
///
/// ## Platform-specific
///
/// - **Linux / Windows / iOS / Android:** Unsupported.
#[must_use]
pub fn traffic_light_position(mut self, position: dpi::Position) -> Self {
self.traffic_light_position = Some(position);
self
}
}

/// IPC handler.
Expand Down
7 changes: 7 additions & 0 deletions crates/tauri-runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,13 @@ pub trait WindowBuilder: WindowBuilderBase {
#[must_use]
fn title_bar_style(self, style: tauri_utils::TitleBarStyle) -> Self;

/// Change the position of the window controls on macOS.
///
/// Requires titleBarStyle: Overlay and decorations: true.
#[cfg(target_os = "macos")]
#[must_use]
fn traffic_light_position(self, x: f64, y: f64) -> Self;

/// Hide the window title.
#[cfg(target_os = "macos")]
#[must_use]
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri-schema-generator/schemas/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@
]
},
"trafficLightPosition": {
"description": "The position of the window controls on macOS.\n Requires titleBarStyle: Overlay and decorations: true.",
"description": "The position of the window controls on macOS.\n\n Requires titleBarStyle: Overlay and decorations: true.",
"anyOf": [
{
"$ref": "#/definitions/LogicalPosition"
Expand Down
1 change: 1 addition & 0 deletions crates/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,7 @@ pub struct WindowConfig {
#[serde(default, alias = "title-bar-style")]
pub title_bar_style: TitleBarStyle,
/// The position of the window controls on macOS.
///
/// Requires titleBarStyle: Overlay and decorations: true.
#[serde(default, alias = "traffic-light-position")]
pub traffic_light_position: Option<LogicalPosition>,
Expand Down
9 changes: 9 additions & 0 deletions crates/tauri/src/test/mock_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,11 @@ impl WindowBuilder for MockWindowBuilder {
self
}

#[cfg(target_os = "macos")]
fn traffic_light_position(self, x: f64, y: f64) -> Self {
self
}

#[cfg(target_os = "macos")]
fn hidden_title(self, transparent: bool) -> Self {
self
Expand Down Expand Up @@ -993,6 +998,10 @@ impl<T: UserEvent> WindowDispatch<T> for MockWindowDispatcher {
Ok(())
}

fn set_traffic_light_position(&self, position: Position) -> Result<()> {
Ok(())
}

fn set_size_constraints(
&self,
constraints: tauri_runtime::window::WindowSizeConstraints,
Expand Down
4 changes: 3 additions & 1 deletion examples/helloworld/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"width": 800,
"height": 600,
"resizable": true,
"fullscreen": false
"fullscreen": false,
"titleBarStyle": "Overlay",
"trafficLightPosition": { "x": 100, "y": 100 }
}
],
"security": {
Expand Down

0 comments on commit ba941fa

Please sign in to comment.