Add getter for window position. (#276)

* + Add getter for window position (macos, windows).

+ Add getter STUB for window position (posix x11/wayland).

* Implement getter for window position (posix x11).

* Remove todo from getter for window position and return 0,0 (posix wayland).
This commit is contained in:
Andreas Neukoetter 2022-02-03 09:01:00 +00:00 committed by GitHub
parent c25b5d114a
commit 032d616bbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 116 additions and 1 deletions

View file

@ -397,6 +397,24 @@ impl Window {
self.0.set_position(x, y)
}
///
/// Gets the position of the window. This is useful if you want
/// to store the position of the window across sessions
///
/// # Examples
///
/// ```no_run
/// # use minifb::*;
/// # let mut window = Window::new("Test", 640, 400, WindowOptions::default()).unwrap();
/// // Retrieves the current window position
/// let (x,y) = window.get_position();
/// ```
///
#[inline]
pub fn get_position(&self) -> (isize, isize) {
self.0.get_position()
}
///
/// 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

View file

@ -559,6 +559,23 @@ void mfb_set_position(void* window, int x, int y)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void mfb_get_position(const void* window, int *px, int *py)
{
OSXWindow* win = (OSXWindow*)window;
const NSRect rectW = [win frame];
if( px != NULL ) {
*px = rectW.origin.x;
}
if( py != NULL ) {
const NSRect msf = [[NSScreen mainScreen] frame];
const float height = msf.size.height;
const float h = rectW.size.height;
*py = height - ( rectW.origin.y + h ); // origin is from bottom
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int mfb_should_close(void* window)
{
OSXWindow* win = (OSXWindow*)window;

View file

@ -175,6 +175,7 @@ extern "C" {
buf_stride: u32,
);
fn mfb_set_position(window: *mut c_void, x: i32, y: i32);
fn mfb_get_position(window: *const c_void, x: *mut i32, y: *mut i32);
fn mfb_set_key_callback(
window: *mut c_void,
target: *mut c_void,
@ -414,6 +415,15 @@ impl Window {
unsafe { mfb_set_position(self.window_handle, x as i32, y as i32) }
}
#[inline]
pub fn get_position(&self) -> (isize, isize) {
let (mut x, mut y) = (0, 0);
unsafe {
mfb_get_position(self.window_handle, &mut x, &mut y);
}
(x as isize, y as isize)
}
#[inline]
pub fn topmost(&self, topmost: bool) {
unsafe { mfb_topmost(self.window_handle, topmost) }

View file

@ -142,6 +142,15 @@ impl Window {
}
}
pub fn get_position(&self) -> (isize, isize) {
match *self {
#[cfg(feature = "x11")]
Window::X11(ref w) => w.get_position(),
#[cfg(feature = "wayland")]
Window::Wayland(ref w) => w.get_position(),
}
}
pub fn topmost(&self, _topmost: bool) {
// We will just do nothing until it is implemented so that nothing breaks
}

View file

@ -647,6 +647,16 @@ impl Window {
.set_geometry((x as i32, y as i32), (self.width, self.height));
}
pub fn get_position(&self) -> (isize, isize) {
let (mut x, mut y) = (0, 0);
unsafe {
// todo!("get_position");
}
(x as isize, y as isize)
}
pub fn set_rate(&mut self, rate: Option<Duration>) {
self.update_rate.set_rate(rate);
}

View file

@ -25,7 +25,8 @@ use crate::mouse_handler;
use super::common::Menu;
use x11_dl::xlib::{
KeyPressMask, KeyReleaseMask, KeySym, Status, XEvent, XIMPreeditNothing, XIMStatusNothing,
XKeyEvent, XNClientWindow, XNFocusWindow, XNInputStyle, XrmDatabase, XIC, XIM,
XKeyEvent, XNClientWindow, XNFocusWindow, XNInputStyle, XWindowAttributes, XrmDatabase, XIC,
XIM,
};
// NOTE: the x11-dl crate does not define Button6 or Button7
@ -680,6 +681,37 @@ impl Window {
}
}
#[inline]
pub fn get_position(&self) -> (isize, isize) {
let (x, y);
let (mut nx, mut ny) = (0, 0);
// Create dummy window for child_return value
let mut dummy_window: mem::MaybeUninit<Window> = mem::MaybeUninit::uninit();
let mut attributes: mem::MaybeUninit<XWindowAttributes> = mem::MaybeUninit::uninit();
unsafe {
let root = (self.d.lib.XDefaultRootWindow)(self.d.display);
(self.d.lib.XGetWindowAttributes)(self.d.display, self.handle, attributes.as_mut_ptr());
x = attributes.assume_init().x;
y = attributes.assume_init().y;
(self.d.lib.XTranslateCoordinates)(
self.d.display,
self.handle,
root,
x,
y,
&mut nx,
&mut ny,
dummy_window.as_mut_ptr() as *mut u64,
);
}
(nx as isize, ny as isize)
}
#[inline]
pub fn get_size(&self) -> (usize, usize) {
(self.width as usize, self.height as usize)

View file

@ -700,6 +700,25 @@ impl Window {
}
}
#[inline]
pub fn get_position(&self) -> (isize, isize) {
let (mut x, mut y) = (0, 0);
unsafe {
let mut rect = windef::RECT {
left: 0,
right: 0,
top: 0,
bottom: 0,
};
if winuser::GetWindowRect(self.window.unwrap(), &mut rect) != 0 {
x = rect.left;
y = rect.top;
}
}
(x as isize, y as isize)
}
#[inline]
pub fn topmost(&self, topmost: bool) {
unsafe {