-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add `Window::set_enabled` and `Window::is_enabled` closes #6660 * license headers * fix build * fix mobile and macos * fix macos * again * unsafe * fix macos is_enabled * update example --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
- Loading branch information
1 parent
a49fc99
commit de7414a
Showing
21 changed files
with
453 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tauri-apps/api": "patch:feat" | ||
--- | ||
|
||
Add `Window::setEnabled` and `Window::isEnabled` methods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"tauri": "patch:feat" | ||
"tauri-runtime": "patch:feat" | ||
"tauri-runtime-wry": "patch:feat" | ||
--- | ||
|
||
Add `Window::set_enabled` and `Window::is_enabled` methods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
use gtk::prelude::*; | ||
#[cfg(any( | ||
target_os = "linux", | ||
target_os = "dragonfly", | ||
target_os = "freebsd", | ||
target_os = "netbsd", | ||
target_os = "openbsd" | ||
))] | ||
use tao::platform::unix::WindowExtUnix; | ||
|
||
impl super::WindowExt for tao::window::Window { | ||
fn set_enabled(&self, enabled: bool) { | ||
self.gtk_window().set_sensitive(enabled); | ||
} | ||
|
||
fn is_enabled(&self) -> bool { | ||
self.gtk_window().is_sensitive() | ||
} | ||
|
||
fn center(&self) { | ||
if let Some(monitor) = self.current_monitor() { | ||
let window_size = self.outer_size(); | ||
let new_pos = super::calculate_window_center_position(window_size, monitor); | ||
self.set_outer_position(new_pos); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
use objc2_app_kit::{NSBackingStoreType, NSWindow, NSWindowStyleMask}; | ||
use objc2_foundation::MainThreadMarker; | ||
use tao::platform::macos::WindowExtMacOS; | ||
|
||
impl super::WindowExt for tao::window::Window { | ||
// based on electron implementation | ||
// https://github.com/electron/electron/blob/15db63e26df3e3d59ce6281f030624f746518511/shell/browser/native_window_mac.mm#L474 | ||
fn set_enabled(&self, enabled: bool) { | ||
let ns_window: &NSWindow = unsafe { &*self.ns_window().cast() }; | ||
if !enabled { | ||
let frame = ns_window.frame(); | ||
let mtm = MainThreadMarker::new() | ||
.expect("`Window::set_enabled` can only be called on the main thread"); | ||
let sheet = unsafe { | ||
NSWindow::initWithContentRect_styleMask_backing_defer( | ||
mtm.alloc(), | ||
frame, | ||
NSWindowStyleMask::Titled, | ||
NSBackingStoreType::NSBackingStoreBuffered, | ||
false, | ||
) | ||
}; | ||
unsafe { sheet.setAlphaValue(0.5) }; | ||
unsafe { ns_window.beginSheet_completionHandler(&sheet, None) }; | ||
} else if let Some(attached) = unsafe { ns_window.attachedSheet() } { | ||
unsafe { ns_window.endSheet(&attached) }; | ||
} | ||
} | ||
|
||
fn is_enabled(&self) -> bool { | ||
let ns_window: &NSWindow = unsafe { &*self.ns_window().cast() }; | ||
unsafe { ns_window.attachedSheet() }.is_none() | ||
} | ||
|
||
fn center(&self) { | ||
let ns_window: &NSWindow = unsafe { &*self.ns_window().cast() }; | ||
ns_window.center(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-License-Identifier: MIT | ||
|
||
#[cfg(any( | ||
target_os = "linux", | ||
target_os = "dragonfly", | ||
target_os = "freebsd", | ||
target_os = "netbsd", | ||
target_os = "openbsd" | ||
))] | ||
mod linux; | ||
#[cfg(target_os = "macos")] | ||
mod macos; | ||
#[cfg(windows)] | ||
mod windows; | ||
|
||
pub trait WindowExt { | ||
/// Enable or disable the window | ||
/// | ||
/// ## Platform-specific: | ||
/// | ||
/// - **Android / iOS**: Unsupported. | ||
fn set_enabled(&self, enabled: bool); | ||
|
||
/// Whether the window is enabled or disabled. | ||
/// | ||
/// ## Platform-specific: | ||
/// | ||
/// - **Android / iOS**: Unsupported, always returns `true`. | ||
fn is_enabled(&self) -> bool; | ||
|
||
/// Center the window | ||
/// | ||
/// ## Platform-specific: | ||
/// | ||
/// - **Android / iOS**: Unsupported. | ||
fn center(&self) {} | ||
|
||
/// Clears the window sufrace. i.e make it it transparent. | ||
#[cfg(windows)] | ||
fn clear_surface( | ||
&self, | ||
surface: &mut softbuffer::Surface< | ||
std::sync::Arc<tao::window::Window>, | ||
std::sync::Arc<tao::window::Window>, | ||
>, | ||
); | ||
} | ||
|
||
#[cfg(mobile)] | ||
impl WindowExt for tao::window::Window { | ||
fn set_enabled(&self, _: bool) {} | ||
fn is_enabled(&self) -> bool { | ||
true | ||
} | ||
} | ||
|
||
pub fn calculate_window_center_position( | ||
window_size: tao::dpi::PhysicalSize<u32>, | ||
target_monitor: tao::monitor::MonitorHandle, | ||
) -> tao::dpi::PhysicalPosition<i32> { | ||
#[cfg(windows)] | ||
{ | ||
use ::windows::Win32::Graphics::Gdi::{GetMonitorInfoW, HMONITOR, MONITORINFO}; | ||
use tao::platform::windows::MonitorHandleExtWindows; | ||
|
||
let mut monitor_info = MONITORINFO { | ||
cbSize: std::mem::size_of::<MONITORINFO>() as u32, | ||
..Default::default() | ||
}; | ||
let hmonitor = target_monitor.hmonitor(); | ||
let status = unsafe { GetMonitorInfoW(HMONITOR(hmonitor as _), &mut monitor_info) }; | ||
if status.into() { | ||
let available_width = monitor_info.rcWork.right - monitor_info.rcWork.left; | ||
let available_height = monitor_info.rcWork.bottom - monitor_info.rcWork.top; | ||
let x = (available_width - window_size.width as i32) / 2 + monitor_info.rcWork.left; | ||
let y = (available_height - window_size.height as i32) / 2 + monitor_info.rcWork.top; | ||
return tao::dpi::PhysicalPosition::new(x, y); | ||
} | ||
} | ||
|
||
let screen_size = target_monitor.size(); | ||
let monitor_pos = target_monitor.position(); | ||
let x = (screen_size.width as i32 - window_size.width as i32) / 2 + monitor_pos.x; | ||
let y = (screen_size.height as i32 - window_size.height as i32) / 2 + monitor_pos.y; | ||
tao::dpi::PhysicalPosition::new(x, y) | ||
} |
Oops, something went wrong.