emoon / rust_minifb

Cross platfrom window and framebuffer crate for Rust
MIT License
1k stars 95 forks source link

window.set_position() has a weird behavior #297

Closed DevAlone closed 1 year ago

DevAlone commented 2 years ago
use std::cmp::{max, min};
use minifb::{Key, MouseButton, MouseMode, ScaleMode, Window, WindowOptions};
use rand::Rng;

const WIDTH: usize = 640 / 2;
const HEIGHT: usize = 360 / 2;

fn main() {
    let mut window = Window::new(
        "WTF?",
        WIDTH,
        HEIGHT,
        WindowOptions {
            resize: true,
            scale_mode: ScaleMode::UpperLeft,
            ..WindowOptions::default()
        },
    ).expect("Unable to create window");

    while window.is_open() && !window.is_key_down(Key::Escape) {
        let window_pos = window.get_position();
        println!("window pos is {window_pos:?}");

        window.get_keys_released().iter().for_each(|key| match key {
            Key::Enter => {
                let window_pos = window.get_position();
                window.set_position(window_pos.0 + 10, window_pos.1);
            },
            _ => (),
        });

        window.update();
    }
}

Expected behavior:

The window jumps 10 pixels to the right

Current behaviour

The window jumped 28 pixels to the right and 110 pixels to the bottom

Wut?

emoon commented 2 years ago

Which platform/OS?

DevAlone commented 2 years ago

Which platform/OS?

Well, I'm running it in an LXC container. The host is KDE neon User - 5.24 aka Ubuntu 20.04 with both KDE and Gnome on Xorg. The container is Ubuntu 22.04, but it shouldn't matter at all.

emoon commented 2 years ago

Alright. I will have look

emoon commented 2 years ago

So I tried this locally on my Linux machine. Running Ubuntu 21.04 and i3 (I set the Window to floating) and the code above works just as expected for me so I'm not sure what is going on.

AndreasOM commented 2 years ago

get_window position was a very crude hack, that was not very well tested. Especially on Linux.

Most systems disagree between setting the window position (usually without window decorations), and getting the window position (usually with some window decorations).

This will more details on which desktop, window manager, etc is used. It might not ever work 100% on all combinations.

If you have a detailed repro I might give it a shot again.

emoon commented 1 year ago

Closing until there is some more info about the issue. Please re-open if needed.