Unique-Digital-Resources / my_rust_gui

my experimental rust gui framework
Apache License 2.0
2 stars 0 forks source link

Error when resizing the window #1

Open Unique-Digital-Resources opened 7 months ago

Unique-Digital-Resources commented 7 months ago

When I resize the window, sometimes get this message: my_gui_v0.01/target/debug/examples/simple_window` mouse pixel: Color(4294967295) thread 'main' panicked at 'assertion failed: p.x >= 0 && p.x < self.width()', /home/admin/.cargo/registry/src/index.crates.io-6f17d22bba15001f/skia-safe-0.68.0/src/core/pixmap.rs:161:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

After some modifications I got this: thread 'main' panicked at 'index out of bounds: the len is 1114280 but the index is 1114282', ....../my_gui_v0.01/src/uwindow.rs:147:69 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

After some other modifications : thread 'main' panicked at 'index out of bounds: the len is 787920 but the index is 787922', ......./my_gui_v0.01/src/uwindow.rs:147:69 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

leorsousa05 commented 7 months ago

This error is not happening on my code, could you send me the code you're trying to do with it?

leorsousa05 commented 7 months ago

So i was trying to fix the code and looks like the pixmap variable on the uwindow.rs is broken. The variable has a length and the length of the variable (that is a &[u8]) is smaller then you are actually trying to access on:

for index in 0..(width * height) as usize {
 buffer[index] = pixmap[index * 4 + 2] as u32
| (pixmap[index * 4 + 1] as u32) << 8
| (pixmap[index * 4] as u32) << 16;
}
leorsousa05 commented 7 months ago

So i tried removing this block of code and it started working properly... When you have free time, post an explanation of what this does!

Unique-Digital-Resources commented 7 months ago

Screencast 2024-02-11 20 08 30.webm

As shown in the video, when you change the size of the window, the location of the button changes as required, but sometimes the error mentioned previously appears. I think I understood the cause of the problem. When the size of the window is reduced, the canvas or the buffer does not shrink with it at the same speed. Therefore, when the mouse touches the button, which is what happened in the video, the size of the buffer has not yet decreased, so the window suddenly disappears due to this error.

Unique-Digital-Resources commented 7 months ago

So i tried removing this block of code and it started working properly... When you have free time, post an explanation of what this does!

I appreciate your suggestion, but I don't think this is the desired solution The block you removed is responsible for converting the skia pixmap to the a buffer that is displayed on the screen

The problem lies in this block:

WindowEvent::CursorMoved { position,device_id , modifiers } 
                    => {
                        let x = position.x as i32;
                        let y = position.y as i32;

                        if (x <=  screenoff_buffer.image_info().bounds().width()) &&(y <= screenoff_buffer.image_info().bounds().height()) && (x>0) && (y>0){
                            _current_pixel = screenoff_buffer.peek_pixels().unwrap().get_color((x,y));
                        }
                        if last_pixel != _current_pixel {
                            last_pixel = _current_pixel;
                            println!("mouse pixel: {last_pixel:?}");

The current possible solution is to add a condition to prevent the pixel color from being read when the window size is changed, but the problem remains that the buffer with the window does not change quickly enough (I do not know if this is due to the LXDE desktop environment for Linux, or due to a problem with the code structure).

I have added some information in the read.me file if you want to know briefly how the code works