Skip to content

Commit

Permalink
refactor: always on bottom
Browse files Browse the repository at this point in the history
Co-Authored-By: Amr Bashir <[email protected]>
  • Loading branch information
henry40408 and amrbashir committed Aug 21, 2022
1 parent 2e74c58 commit 3f4feba
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 51 deletions.
5 changes: 0 additions & 5 deletions .changes/always-below-bottom.md

This file was deleted.

5 changes: 5 additions & 0 deletions .changes/always-on-bottom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": "patch"
---

Implement "always on bottom" as contrary to "always on top".
8 changes: 4 additions & 4 deletions examples/window_debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ fn main() {
eprintln!(" (V) Toggle visibility");
eprintln!(" (X) Toggle maximized");
eprintln!(" (T) Toggle always on top");
eprintln!(" (B) Toggle always below bottom");
eprintln!(" (B) Toggle always on bottom");

let mut always_below_bottom = false;
let mut always_on_bottom = false;
let mut always_on_top = false;
let mut visible = true;

Expand Down Expand Up @@ -129,8 +129,8 @@ fn main() {
window.set_always_on_top(always_on_top);
}
"b" => {
always_below_bottom = !always_below_bottom;
window.set_always_below_bottom(always_below_bottom);
always_on_bottom = !always_on_bottom;
window.set_always_on_bottom(always_on_bottom);
}
_ => (),
},
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ impl Window {

pub fn set_decorations(&self, _decorations: bool) {}

pub fn set_always_below_bottom(&self, _always_below_bottom: bool) {}
pub fn set_always_on_bottom(&self, _always_on_bottom: bool) {}

pub fn set_always_on_top(&self, _always_on_top: bool) {}

Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/ios/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ impl Inner {
warn!("`Window::set_decorations` is ignored on iOS")
}

pub fn set_always_below_bottom(&self, _always_below_bottom: bool) {
warn!("`Window::set_always_below_bottom` is ignored on iOS")
pub fn set_always_on_bottom(&self, _always_on_bottom: bool) {
warn!("`Window::set_always_on_bottom` is ignored on iOS")
}

pub fn set_always_on_top(&self, _always_on_top: bool) {
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/linux/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ impl<T: 'static> EventLoop<T> {
None => window.unfullscreen(),
},
WindowRequest::Decorations(decorations) => window.set_decorated(decorations),
WindowRequest::AlwaysBelowBottom(always_below_bottom) => {
window.set_keep_below(always_below_bottom)
WindowRequest::AlwaysOnBottom(always_on_bottom) => {
window.set_keep_below(always_on_bottom)
}
WindowRequest::AlwaysOnTop(always_on_top) => window.set_keep_above(always_on_top),
WindowRequest::WindowIcon(window_icon) => {
Expand Down
17 changes: 10 additions & 7 deletions src/platform_impl/linux/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,15 @@ impl Window {
window.set_visible(attributes.visible);
window.set_decorated(attributes.decorations);

if attributes.always_below_bottom && !attributes.always_on_top {
window.set_keep_below(attributes.always_below_bottom);
}
if attributes.always_on_top && !attributes.always_below_bottom {
if attributes.always_on_bottom && attributes.always_on_top {
log::warn!(
"Always on top and always on bottom, are both specified, you should only set one."
);
} else if attributes.always_on_bottom || attributes.always_on_top {
window.set_keep_below(attributes.always_on_bottom);
window.set_keep_above(attributes.always_on_top);
}

if let Some(icon) = attributes.window_icon {
window.set_icon(Some(&icon.inner.into()));
}
Expand Down Expand Up @@ -557,10 +560,10 @@ impl Window {
}
}

pub fn set_always_below_bottom(&self, always_below_bottom: bool) {
pub fn set_always_on_bottom(&self, always_on_bottom: bool) {
if let Err(e) = self.window_requests_tx.send((
self.window_id,
WindowRequest::AlwaysBelowBottom(always_below_bottom),
WindowRequest::AlwaysOnBottom(always_on_bottom),
)) {
log::warn!("Fail to send always on bottom request: {}", e);
}
Expand Down Expand Up @@ -771,7 +774,7 @@ pub enum WindowRequest {
DragWindow,
Fullscreen(Option<Fullscreen>),
Decorations(bool),
AlwaysBelowBottom(bool),
AlwaysOnBottom(bool),
AlwaysOnTop(bool),
WindowIcon(Option<Icon>),
UserAttention(Option<UserAttentionType>),
Expand Down
12 changes: 6 additions & 6 deletions src/platform_impl/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,14 @@ fn create_window(
ns_window.setMovableByWindowBackground_(YES);
}

if attrs.always_on_top && !attrs.always_below_bottom {
if attrs.always_on_top && attrs.always_on_bottom {
log::warn!("Always on top and always on bottom, are both specified, you should only set one.");
} else if attrs.always_on_top {
let _: () = msg_send![
*ns_window,
setLevel: ffi::NSWindowLevel::NSFloatingWindowLevel
];
}

if attrs.always_below_bottom && !attrs.always_on_top {
} else if attrs.always_on_bottom {
let _: () = msg_send![
*ns_window,
setLevel: ffi::NSWindowLevel::BelowNormalWindowLevel
Expand Down Expand Up @@ -1133,8 +1133,8 @@ impl UnownedWindow {
}

#[inline]
pub fn set_always_below_bottom(&self, always_below_bottom: bool) {
let level = if always_below_bottom {
pub fn set_always_on_bottom(&self, always_on_bottom: bool) {
let level = if always_on_bottom {
ffi::NSWindowLevel::BelowNormalWindowLevel
} else {
ffi::NSWindowLevel::NSNormalWindowLevel
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ unsafe fn public_window_callback_inner<T: 'static>(
}

let window_flags = window_state.window_flags;
if window_flags.contains(WindowFlags::ALWAYS_BELOW_BOTTOM) {
if window_flags.contains(WindowFlags::ALWAYS_ON_BOTTOM) {
let window_pos = &mut *(lparam.0 as *mut WINDOWPOS);
window_pos.hwndInsertAfter = HWND_BOTTOM;
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,13 +656,13 @@ impl Window {
}

#[inline]
pub fn set_always_below_bottom(&self, always_below_bottom: bool) {
pub fn set_always_on_bottom(&self, always_on_bottom: bool) {
let window = self.window.clone();
let window_state = Arc::clone(&self.window_state);

self.thread_executor.execute_in_thread(move || {
WindowState::set_window_flags(window_state.lock(), window.0, |f| {
f.set(WindowFlags::ALWAYS_BELOW_BOTTOM, always_below_bottom)
f.set(WindowFlags::ALWAYS_ON_BOTTOM, always_on_bottom)
});
});
}
Expand Down
26 changes: 13 additions & 13 deletions src/platform_impl/windows/window_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ bitflags! {
}
bitflags! {
pub struct WindowFlags: u32 {
const RESIZABLE = 1 << 0;
const DECORATIONS = 1 << 1;
const VISIBLE = 1 << 2;
const ON_TASKBAR = 1 << 3;
const ALWAYS_ON_TOP = 1 << 4;
const NO_BACK_BUFFER = 1 << 5;
const TRANSPARENT = 1 << 6;
const CHILD = 1 << 7;
const MAXIMIZED = 1 << 8;
const POPUP = 1 << 14;
const ALWAYS_BELOW_BOTTOM = 1 << 16;
const RESIZABLE = 1 << 0;
const DECORATIONS = 1 << 1;
const VISIBLE = 1 << 2;
const ON_TASKBAR = 1 << 3;
const ALWAYS_ON_TOP = 1 << 4;
const NO_BACK_BUFFER = 1 << 5;
const TRANSPARENT = 1 << 6;
const CHILD = 1 << 7;
const MAXIMIZED = 1 << 8;
const POPUP = 1 << 14;
const ALWAYS_ON_BOTTOM = 1 << 16;

/// Marker flag for fullscreen. Should always match `WindowState::fullscreen`, but is
/// included here to make masking easier.
Expand Down Expand Up @@ -298,11 +298,11 @@ impl WindowFlags {
}
}

if diff.contains(WindowFlags::ALWAYS_BELOW_BOTTOM) {
if diff.contains(WindowFlags::ALWAYS_ON_BOTTOM) {
unsafe {
SetWindowPos(
window,
match new.contains(WindowFlags::ALWAYS_BELOW_BOTTOM) {
match new.contains(WindowFlags::ALWAYS_ON_BOTTOM) {
true => HWND_BOTTOM,
false => HWND_NOTOPMOST,
},
Expand Down
19 changes: 11 additions & 8 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub struct WindowAttributes {
/// Whether the window should always be on bottom of other windows.
///
/// The default is `false`.
pub always_below_bottom: bool,
pub always_on_bottom: bool,

/// The window icon.
///
Expand Down Expand Up @@ -224,7 +224,7 @@ impl Default for WindowAttributes {
transparent: false,
decorations: true,
always_on_top: false,
always_below_bottom: false,
always_on_bottom: false,
window_icon: None,
window_menu: None,
preferred_theme: None,
Expand Down Expand Up @@ -369,12 +369,13 @@ impl WindowBuilder {

/// Sets whether or not the window will always be below other windows.
///
/// See [`Window::set_always_below_bottom`] for details.
/// See [`Window::set_always_on_bottom`] for details.
///
/// [`Window::set_always_below_bottom`]: crate::window::Window::set_always_below_bottom
/// [`Window::set_always_on_bottom`]: crate::window::Window::set_always_on_bottom
#[inline]
pub fn with_always_below_bottom(mut self, always_below_bottom: bool) -> Self {
self.window.always_below_bottom = always_below_bottom;
pub fn with_always_on_bottom(mut self, always_on_bottom: bool) -> Self {
self.window.always_on_top = false;
self.window.always_on_bottom = always_on_bottom;
self
}

Expand All @@ -385,6 +386,7 @@ impl WindowBuilder {
/// [`Window::set_always_on_top`]: crate::window::Window::set_always_on_top
#[inline]
pub fn with_always_on_top(mut self, always_on_top: bool) -> Self {
self.window.always_on_bottom = false;
self.window.always_on_top = always_on_top;
self
}
Expand Down Expand Up @@ -805,10 +807,11 @@ impl Window {
///
/// ## Platform-specific
///
/// - **Windows**: There is no guarantee that the window will be the bottom most but it will try to be.
/// - **iOS / Android:** Unsupported.
#[inline]
pub fn set_always_below_bottom(&self, always_below_bottom: bool) {
self.window.set_always_below_bottom(always_below_bottom)
pub fn set_always_on_bottom(&self, always_on_bottom: bool) {
self.window.set_always_on_bottom(always_on_bottom)
}

/// Change whether or not the window will always be on top of other windows.
Expand Down

0 comments on commit 3f4feba

Please sign in to comment.