From 15bce84a4edfb99a79490cc1598abc583a988461 Mon Sep 17 00:00:00 2001 From: phillvancejr Date: Fri, 28 Aug 2020 15:14:49 -0400 Subject: [PATCH] Added Topmost (always on top) functionality to Windows (#159) * Added always on top functionality for Windows * Added always on top functionality for Windows * Made topmost method in Window public so that the topmost attribute can be defined either at or after creation time as opposed to just at creation time. This allows the topmost functionality to be toggled during the programs runtime as opposed to only at window creation --- src/lib.rs | 18 ++++++++++++++++++ src/os/macos/mod.rs | 5 +++++ src/os/posix/mod.rs | 5 +++++ src/os/windows/mod.rs | 38 +++++++++++++++++++------------------- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0e01b03..0f46ca2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -352,6 +352,24 @@ impl Window { self.0.set_position(x, y) } + /// + /// Makes the window the topmost window and makes it stay always on top. This is useful if you + /// want the window to float above all over windows + /// + /// # Examples + /// + /// ```no_run + /// # use minifb::*; + /// # let mut window = Window::new("Test", 640, 400, WindowOptions::default()).unwrap(); + /// // Makes the window always on top + /// window.topmost(true); + /// ``` + /// + #[inline] + pub fn topmost(&self, topmost: bool) { + self.0.topmost(topmost) + } + /// /// Sets the background color that is used with update_with_buffer. /// In some cases there will be a blank area around the buffer depending on the ScaleMode that has been set. diff --git a/src/os/macos/mod.rs b/src/os/macos/mod.rs index ab29490..6ec1d05 100644 --- a/src/os/macos/mod.rs +++ b/src/os/macos/mod.rs @@ -410,6 +410,11 @@ impl Window { unsafe { mfb_set_position(self.window_handle, x as i32, y as i32) } } + #[inline] + pub fn topmost(&self, topmost: bool) { + unsafe { mfb_topmost(self.window_handle, topmost) } + } + pub fn get_size(&self) -> (usize, usize) { ( self.shared_data.width as usize, diff --git a/src/os/posix/mod.rs b/src/os/posix/mod.rs index d1428b0..1309867 100644 --- a/src/os/posix/mod.rs +++ b/src/os/posix/mod.rs @@ -129,6 +129,11 @@ impl Window { } } + pub fn topmost(&self, _topmost: bool) { + // We will just do nothing until it is implemented so that nothing breaks + () + } + pub fn get_size(&self) -> (usize, usize) { match *self { #[cfg(feature = "x11")] diff --git a/src/os/windows/mod.rs b/src/os/windows/mod.rs index 36c77ef..b4c8eb9 100644 --- a/src/os/windows/mod.rs +++ b/src/os/windows/mod.rs @@ -646,6 +646,25 @@ impl Window { } } + #[inline] + pub fn topmost(&self, topmost: bool) { + unsafe { + winuser::SetWindowPos( + self.window.unwrap(), + if topmost == true { + winuser::HWND_TOPMOST + } else { + winuser::HWND_TOP + }, + 0, + 0, + 0, + 0, + winuser::SWP_SHOWWINDOW | winuser::SWP_NOSIZE | winuser::SWP_NOMOVE, + ) + }; + } + #[inline] pub fn get_size(&self) -> (usize, usize) { (self.width as usize, self.height as usize) @@ -990,25 +1009,6 @@ impl Window { Some(t) } } - - #[inline] - fn topmost(&self, topmost: bool) { - unsafe { - winuser::SetWindowPos( - self.window.unwrap(), - if topmost == true { - winuser::HWND_TOPMOST - } else { - winuser::HWND_TOP - }, - 0, - 0, - 0, - 0, - winuser::SWP_SHOWWINDOW | winuser::SWP_NOSIZE | winuser::SWP_NOMOVE, - ); - } - } } #[derive(Clone)]