Skip to content

feat: add set_visible_on_all_workspaces, closes #185 #666

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

Merged
merged 4 commits into from
Jan 11, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changes/set_visible_on_all_workspaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

Add `WindowBuilder::with_visible_on_all_workspaces` and `Window::set_visible_on_all_workspaces`.
7 changes: 7 additions & 0 deletions src/platform_impl/linux/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ impl<T: 'static> EventLoop<T> {
window.set_skip_taskbar_hint(skip);
window.set_skip_pager_hint(skip)
}
WindowRequest::SetVisibleOnAllWorkspaces(visible) => {
if visible {
window.stick();
} else {
window.unstick();
}
}
WindowRequest::CursorIcon(cursor) => {
if let Some(gdk_window) = window.window() {
let display = window.display();
Expand Down
14 changes: 14 additions & 0 deletions src/platform_impl/linux/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ impl Window {
window.set_keep_above(attributes.always_on_top);
}

if attributes.visible_on_all_workspaces {
window.stick();
}

if let Some(icon) = attributes.window_icon {
window.set_icon(Some(&icon.inner.into()));
}
Expand Down Expand Up @@ -660,6 +664,15 @@ impl Window {
self.menu_bar.get_visible()
}

pub fn set_visible_on_all_workspaces(&self, visible: bool) {
if let Err(e) = self.window_requests_tx.send((
self.window_id,
WindowRequest::SetVisibleOnAllWorkspaces(visible),
)) {
log::warn!("Fail to send visible on all workspaces request: {}", e);
}
}

pub fn set_cursor_icon(&self, cursor: CursorIcon) {
if let Err(e) = self
.window_requests_tx
Expand Down Expand Up @@ -844,6 +857,7 @@ pub enum WindowRequest {
Menu((Option<MenuItem>, Option<MenuId>)),
SetMenu((Option<menu::Menu>, AccelGroup, gtk::MenuBar)),
GlobalHotKey(u16),
SetVisibleOnAllWorkspaces(bool),
}

pub fn hit_test(window: &gdk::Window, cx: f64, cy: f64) -> WindowEdge {
Expand Down
20 changes: 18 additions & 2 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use crate::{
use cocoa::{
appkit::{
self, CGFloat, NSApp, NSApplication, NSApplicationPresentationOptions, NSColor, NSEvent,
NSRequestUserAttentionType, NSScreen, NSView, NSWindow, NSWindowButton, NSWindowOrderingMode,
NSWindowStyleMask,
NSRequestUserAttentionType, NSScreen, NSView, NSWindow, NSWindowButton,
NSWindowCollectionBehavior, NSWindowOrderingMode, NSWindowStyleMask,
},
base::{id, nil},
foundation::{
Expand Down Expand Up @@ -519,6 +519,7 @@ impl UnownedWindow {
let visible = win_attribs.visible;
let focused = win_attribs.focused;
let decorations = win_attribs.decorations;
let visible_on_all_workspaces = win_attribs.visible_on_all_workspaces;
let inner_rect = win_attribs
.inner_size
.map(|size| size.to_physical(scale_factor));
Expand Down Expand Up @@ -551,6 +552,7 @@ impl UnownedWindow {

// Set fullscreen mode after we setup everything
window.set_fullscreen(fullscreen);
window.set_visible_on_all_workspaces(visible_on_all_workspaces);

// Setting the window as key has to happen *after* we set the fullscreen
// state, since otherwise we'll briefly see the window at normal size
Expand Down Expand Up @@ -1360,6 +1362,20 @@ impl UnownedWindow {
let _: () = msg_send![*self.ns_window, setSharingType: !enabled as i32];
}
}

pub fn set_visible_on_all_workspaces(&self, visible: bool) {
unsafe {
let mut collection_behavior = self.ns_window.collectionBehavior();
if visible {
collection_behavior |=
NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces;
} else {
collection_behavior &=
!NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces;
};
self.ns_window.setCollectionBehavior_(collection_behavior)
}
}
}

impl WindowExtMacOS for UnownedWindow {
Expand Down
29 changes: 29 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ pub struct WindowAttributes {
///
/// - **iOS / Android / Linux:** Unsupported.
pub content_protection: bool,

/// Sets whether the window should be visible on all workspaces.
///
/// ## Platform-specific
///
/// - **iOS / Android / Windows:** Unsupported.
pub visible_on_all_workspaces: bool,
}

impl Default for WindowAttributes {
Expand Down Expand Up @@ -270,6 +277,7 @@ impl Default for WindowAttributes {
preferred_theme: None,
focused: true,
content_protection: false,
visible_on_all_workspaces: false,
}
}
}
Expand Down Expand Up @@ -505,6 +513,17 @@ impl WindowBuilder {
self
}

/// Sets whether the window should be visible on all workspaces.
///
/// ## Platform-specific
///
/// - **iOS / Android / Windows:** Unsupported.
#[inline]
pub fn with_visible_on_all_workspaces(mut self, visible: bool) -> WindowBuilder {
self.window.visible_on_all_workspaces = visible;
self
}

/// Builds the window.
///
/// Possible causes of error include denied permission, incompatible system, and lack of memory.
Expand Down Expand Up @@ -1089,6 +1108,16 @@ impl Window {
#[cfg(any(target_os = "macos", target_os = "windows"))]
self.window.set_content_protection(enabled);
}

/// Sets whether the window should be visible on all workspaces.
///
/// ## Platform-specific
///
/// - **iOS / Android / Windows:** Unsupported.
pub fn set_visible_on_all_workspaces(&self, #[allow(unused)] visible: bool) {
#[cfg(any(target_os = "macos", target_os = "linux"))]
self.window.set_visible_on_all_workspaces(visible)
}
}

/// Cursor functions.
Expand Down