Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an event to detect the screen changes #15637

Open
walterlv opened this issue May 7, 2024 · 2 comments
Open

Add an event to detect the screen changes #15637

walterlv opened this issue May 7, 2024 · 2 comments
Labels
api API addition enhancement

Comments

@walterlv
Copy link
Contributor

walterlv commented May 7, 2024

Is your feature request related to a problem? Please describe.

Supposed that we are developing such an app:

  1. It has a floating icon on the desktop/screen
  2. The icon will reposition and rescale itself when the screen resolution or scaling is changed

Avalonia 11.1.0 now only provide an event of Window.ScalingChanged that will be raised when the screen scaling is changed. But if the screen resolution is changed, it provide nothing. So our desktop icon doesn't know when to reposition it.

For example, the desktop icon is position at the top-right corner of the screen displaying in 3840x2160 and now the user changes the resolution to 1920x1080, the icon will be out of the screen. (Note: If the window is an override redirect window, the desktop window manager will not reposition it.)

Describe the solution you'd like

  1. Add an event Changed to Avalonia.Controls.Screens class
  2. The event should be raised when the screen resolution, scaling, orientation, layout, count, etc. are changed

Describe alternatives you've considered

  1. Add a native callback into the X11Properties class, which is the same as what in the Win32Properties class.

Additional context

image

@maxkatz6 maxkatz6 added the api API addition label May 7, 2024
@lindexi
Copy link
Contributor

lindexi commented May 10, 2024

Thank you. This feature is exactly what I need and look forward to adding it.

@walterlv
Copy link
Contributor Author

Before we get the framework support, we can use this work-around:

using var display = XDisplay.Open(0);
var window = XDefaultRootWindow(display);

XSelectInput(display, window, (nint)(ExposureMask | StructureNotifyMask));

while (_isRunning)
{
    XNextEvent(display, out var e);

    if (e.type is XEventName.ConfigureNotify)
    {
        var configureEvent = e.ConfigureEvent;
        var bounds = new PixelRect(configureEvent.x, configureEvent.y, configureEvent.width, configureEvent.height);
        if (bounds != _lastScreenBounds)
        {
            _lastScreenBounds = bounds;
            RaiseScreenChanged(bounds);
        }
    }
}

We'are trying to know the changes of the root window and we assume that the position and the size of it equals to the screen position and size.

Just run this code above on a long-running task and we'll get the screen resolution changed event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api API addition enhancement
Projects
None yet
Development

No branches or pull requests

3 participants