rust-windowing / winit

Window handling library in pure Rust
https://docs.rs/winit/
Apache License 2.0
4.78k stars 892 forks source link

Add event for changes to the safe area #3911

Open madsmtm opened 3 weeks ago

madsmtm commented 3 weeks ago

https://github.com/rust-windowing/winit/pull/3890 added an API to query the safe area, but left an API for listening to changes to that value unspecified (only noting that RedrawRequested would be called if it changed).

The API could look something like this:

enum WindowEvent {
   SafeAreaChanged {
        origin: PhysicalPosition<u32>,
        size: PhysicalSize<u32>,
    }
    // ...
}

But that'd mean we have to emit it whenever the surface resizes too, which isn't so nice.

Perhaps something like the following would be better?

struct Insets {
    top: PhysicalUnit<u32>,
    left: PhysicalUnit<u32>,
    bottom: PhysicalUnit<u32>,
    right: PhysicalUnit<u32>,
}

enum WindowEvent {
   SafeAreaChanged(Insets),
    // ...
}

trait Window {
    fn safe_area_insets(&self) -> Insets;
    // ...
}

See also discussion starting from https://github.com/rust-windowing/winit/pull/3890#issuecomment-2343351828.

madsmtm commented 3 weeks ago

Responding to https://github.com/rust-windowing/winit/pull/3890#issuecomment-2343623057 here:

It doesn't have to be synced though, you should emit it only when the area changes, resize doesn't mean that it changed, because it may not apply at all.

The issue with exposing size in the event is that if the user stores that size, and they get a SurfaceResized event, the size from the previous SafeAreaChanged that they stored would now be wrong.

So we'd have to emit the SafeAreaChanged again, or state something like "SurfaceResized invalidates the safe area rectangle, you should re-query it with Window::safe_area".

If we expose the insets directly, they would never be wrong, even if the surface itself is resized.

kchibisov commented 3 weeks ago

If we expose the insets directly, they would never be wrong, even if the surface itself is resized.

Yeah, that's probably not a bad idea after all, since it's just an extra padding one could say and it'll be much easier actually, since you likely already have a padding handling of some sort in your application just to not make elements render right at the border of the window.