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: traffic light position #12366

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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" }
32 changes: 32 additions & 0 deletions crates/tauri-cli/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,17 @@
}
]
},
"trafficLightPosition": {
"description": "The position of the window controls on macOS.\n\n Requires titleBarStyle: Overlay and decorations: true.",
"anyOf": [
{
"$ref": "#/definitions/LogicalPosition"
},
{
"type": "null"
}
]
},
"hiddenTitle": {
"description": "If `true`, sets the window title to be hidden on macOS.",
"default": false,
Expand Down Expand Up @@ -584,6 +595,27 @@
}
]
},
"LogicalPosition": {
"description": "Position coordinates struct.",
"type": "object",
"required": [
"x",
"y"
],
"properties": {
"x": {
"description": "X coordinate.",
"type": "number",
"format": "double"
},
"y": {
"description": "Y coordinate.",
"type": "number",
"format": "double"
}
},
"additionalProperties": false
},
"WindowEffectsConfig": {
"description": "The window effects configuration object",
"type": "object",
Expand Down
8 changes: 2 additions & 6 deletions crates/tauri-cli/src/helpers/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,11 @@ mod sys {
}

pub(super) fn error_contended(err: &Error) -> bool {
err
.raw_os_error()
.map_or(false, |x| x == ERROR_LOCK_VIOLATION as i32)
err.raw_os_error() == Some(ERROR_LOCK_VIOLATION as i32)
}

pub(super) fn error_unsupported(err: &Error) -> bool {
err
.raw_os_error()
.map_or(false, |x| x == ERROR_INVALID_FUNCTION as i32)
err.raw_os_error() == Some(ERROR_INVALID_FUNCTION as i32)
}

pub(super) fn unlock(file: &File) -> Result<()> {
Expand Down
34 changes: 34 additions & 0 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 @@ -1068,6 +1071,14 @@ impl WindowBuilder for WindowBuilderWrapper {
self
}

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

#[cfg(target_os = "macos")]
fn hidden_title(mut self, hidden: bool) -> Self {
self.inner = self.inner.with_title_hidden(hidden);
Expand Down Expand Up @@ -1270,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 @@ -2173,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 @@ -3180,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 @@ -4360,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
32 changes: 32 additions & 0 deletions crates/tauri-schema-generator/schemas/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,17 @@
}
]
},
"trafficLightPosition": {
"description": "The position of the window controls on macOS.\n\n Requires titleBarStyle: Overlay and decorations: true.",
"anyOf": [
{
"$ref": "#/definitions/LogicalPosition"
},
{
"type": "null"
}
]
},
"hiddenTitle": {
"description": "If `true`, sets the window title to be hidden on macOS.",
"default": false,
Expand Down Expand Up @@ -584,6 +595,27 @@
}
]
},
"LogicalPosition": {
"description": "Position coordinates struct.",
"type": "object",
"required": [
"x",
"y"
],
"properties": {
"x": {
"description": "X coordinate.",
"type": "number",
"format": "double"
},
"y": {
"description": "Y coordinate.",
"type": "number",
"format": "double"
}
},
"additionalProperties": false
},
"WindowEffectsConfig": {
"description": "The window effects configuration object",
"type": "object",
Expand Down
Loading
Loading