Skip to content

Commit

Permalink
feat(core): add shadow APIs (#6206)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog authored Feb 7, 2023
1 parent 17f2676 commit a81750d
Show file tree
Hide file tree
Showing 28 changed files with 950 additions and 739 deletions.
5 changes: 5 additions & 0 deletions .changes/shadow-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'api': minor
---

Added the `shadow` option when creating a window and `setShadow` function.
5 changes: 5 additions & 0 deletions .changes/shadow-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-utils': minor
---

Added the `shadow` option to the window configuration and `set_shadow` option to the `window` allow list.
7 changes: 7 additions & 0 deletions .changes/shadow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'tauri': minor
'tauri-runtime-wry': minor
'tauri-runtime': minor
---

Added the `shadow` option when creating a window and `Window::set_shadow`.
13 changes: 13 additions & 0 deletions core/config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"setMinSize": false,
"setPosition": false,
"setResizable": false,
"setShadow": false,
"setSize": false,
"setSkipTaskbar": false,
"setTitle": false,
Expand Down Expand Up @@ -393,6 +394,7 @@
"setMinSize": false,
"setPosition": false,
"setResizable": false,
"setShadow": false,
"setSize": false,
"setSkipTaskbar": false,
"setTitle": false,
Expand Down Expand Up @@ -684,6 +686,11 @@
"string",
"null"
]
},
"shadow": {
"description": "Whether or not the window has shadow.\n\n## Platform-specific\n\n- **Windows:** - `false` has no effect on decorated window, shadow are always ON. - `true` will make ndecorated window have a 1px white border, and on Windows 11, it will have a rounded corners. - **Linux:** Unsupported.",
"default": false,
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -1699,6 +1706,7 @@
"setMinSize": false,
"setPosition": false,
"setResizable": false,
"setShadow": false,
"setSize": false,
"setSkipTaskbar": false,
"setTitle": false,
Expand Down Expand Up @@ -2031,6 +2039,11 @@
"default": false,
"type": "boolean"
},
"setShadow": {
"description": "Allows setting the shadow flag of the window.",
"default": false,
"type": "boolean"
},
"setAlwaysOnTop": {
"description": "Allows setting the always_on_top flag of the window.",
"default": false,
Expand Down
8 changes: 4 additions & 4 deletions core/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ rand = "0.8"
raw-window-handle = "0.5"

[target."cfg(windows)".dependencies]
webview2-com = "0.19.1"
webview2-com = "0.22"

[target."cfg(windows)".dependencies.windows]
version = "0.39.0"
features = [ "Win32_Foundation" ]
[target."cfg(windows)".dependencies.windows]
version = "0.44"
features = [ "Win32_Foundation" ]

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
gtk = { version = "0.16", features = [ "v3_24" ] }
Expand Down
29 changes: 28 additions & 1 deletion core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,8 @@ impl WindowBuilder for WindowBuilderWrapper {
.maximized(config.maximized)
.always_on_top(config.always_on_top)
.skip_taskbar(config.skip_taskbar)
.theme(config.theme);
.theme(config.theme)
.shadow(config.shadow);

if let (Some(min_width), Some(min_height)) = (config.min_width, config.min_height) {
window = window.min_inner_size(min_width, min_height);
Expand Down Expand Up @@ -849,6 +850,18 @@ impl WindowBuilder for WindowBuilderWrapper {
self
}

fn shadow(#[allow(unused_mut)] mut self, _enable: bool) -> Self {
#[cfg(windows)]
{
self.inner = self.inner.with_undecorated_shadow(_enable);
}
#[cfg(target_os = "macos")]
{
self.inner = self.inner.with_has_shadow(_enable);
}
self
}

#[cfg(windows)]
fn parent_window(mut self, parent: HWND) -> Self {
self.inner = self.inner.with_parent_window(parent);
Expand Down Expand Up @@ -1066,6 +1079,7 @@ pub enum WindowMessage {
Hide,
Close,
SetDecorations(bool),
SetShadow(bool),
SetAlwaysOnTop(bool),
SetSize(Size),
SetMinSize(Option<Size>),
Expand Down Expand Up @@ -1430,6 +1444,13 @@ impl<T: UserEvent> Dispatch<T> for WryDispatcher<T> {
)
}

fn set_shadow(&self, enable: bool) -> Result<()> {
send_user_message(
&self.context,
Message::Window(self.window_id, WindowMessage::SetShadow(enable)),
)
}

fn set_always_on_top(&self, always_on_top: bool) -> Result<()> {
send_user_message(
&self.context,
Expand Down Expand Up @@ -2420,6 +2441,12 @@ fn handle_user_message<T: UserEvent>(
panic!("cannot handle `WindowMessage::Close` on the main thread")
}
WindowMessage::SetDecorations(decorations) => window.set_decorations(decorations),
WindowMessage::SetShadow(_enable) => {
#[cfg(windows)]
window.set_undecorated_shadow(_enable);
#[cfg(target_os = "macos")]
window.set_has_shadow(_enable);
}
WindowMessage::SetAlwaysOnTop(always_on_top) => window.set_always_on_top(always_on_top),
WindowMessage::SetSize(size) => {
window.set_inner_size(SizeWrapper::from(size).0);
Expand Down
8 changes: 4 additions & 4 deletions core/tauri-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ raw-window-handle = "0.5"
rand = "0.8"

[target."cfg(windows)".dependencies]
webview2-com = "0.19.1"
webview2-com = "0.22"

[target."cfg(windows)".dependencies.windows]
version = "0.39.0"
features = [ "Win32_Foundation" ]
[target."cfg(windows)".dependencies.windows]
version = "0.44"
features = [ "Win32_Foundation" ]

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
gtk = { version = "0.16", features = [ "v3_24" ] }
Expand Down
5 changes: 4 additions & 1 deletion core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,12 @@ pub trait Dispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + 'static
/// Closes the window.
fn close(&self) -> Result<()>;

/// Updates the hasDecorations flag.
/// Updates the decorations flag.
fn set_decorations(&self, decorations: bool) -> Result<()>;

/// Updates the shadow flag.
fn set_shadow(&self, enable: bool) -> Result<()>;

/// Updates the window alwaysOnTop flag.
fn set_always_on_top(&self, always_on_top: bool) -> Result<()>;

Expand Down
12 changes: 12 additions & 0 deletions core/tauri-runtime/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ pub trait WindowBuilder: WindowBuilderBase {
#[must_use]
fn skip_taskbar(self, skip: bool) -> Self;

/// Sets whether or not the window has shadow.
///
/// ## Platform-specific
///
/// - **Windows:**
/// - `false` has no effect on decorated window, shadows are always ON.
/// - `true` will make ndecorated window have a 1px white border,
/// and on Windows 11, it will have a rounded corners.
/// - **Linux:** Unsupported.
#[must_use]
fn shadow(self, enable: bool) -> Self;

/// Sets a parent to the window to be created.
///
/// A child window has the WS_CHILD style and is confined to the client area of its parent window.
Expand Down
2 changes: 1 addition & 1 deletion core/tauri-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ infer = "0.7"
heck = "0.4"

[target."cfg(windows)".dependencies.windows]
version = "0.39.0"
version = "0.44.0"
features = [
"implement",
"Win32_Foundation",
Expand Down
Loading

0 comments on commit a81750d

Please sign in to comment.