Essentials: Add Application/Screen Size API

Created on 17 Jul 2018  路  11Comments  路  Source: xamarin/Essentials

Currently the DeviceDisplay is for the actual device, not the app. It would be nice to have an API to get the application size, or current view size if you will.

Reference:

//Original Comment to consider
I think that "Device Display Information" does not have enough information about SafeArea on the iPhone.
It contains "metrics.Orientation" and I think that it would be nice to get "metrics.SafeArea", which would be of the type Thickness.

needs-specifications up-for-grabs

Most helpful comment

I think this is really UI and not the device itself. I have a proposal for App Display Information. I will update your issue here to apply to that..

All 11 comments

I think this is really UI and not the device itself. I have a proposal for App Display Information. I will update your issue here to apply to that..

We are closing this issue since no response was received. Please open a new issue referring to this one if you have more information.

@Redth your bot is too aggressive with the closing ;)

Is there any update on this one? Hardware screen size works great, but unless I am missing something, I still see no way to get the "app" screen size on Android.

+1 for this feature. I want to adjust what's displayed in my UI according to the actual app window size. I just tried using the DeviceDisplay in Essentials thinking this is what it would tell me, but found it gives the actual screen size, not the size of your app window - unless I'm missing something, I don't see anything in Essentials to get the current app window size.

Yeah this would be good to add.

@Redth, I did some tests and I thought of this spec:

(The events were inspired from DeviceDisplay implementation)

class AppInfo
{
      // ...
      public static WindowSize WindowAppSize() => PlatformWindowAppSize();
      public static event EventHandler<WindowSizeChangedEventArgs> WindowSizeInfoChanged
        {
            add
            {
                var wasRunning = WindowSizeChangedInternal != null;

                WindowSizeChangedInternal += value;

                if (!wasRunning && WindowSizeChangedInternal != null)
                {
                    SetCurrent(GetWindowSizeInfo());
                    StartScreenMetricsListeners();
                }
            }

            remove
            {
                var wasRunning = WindowSizeChangedInternal != null;

                WindowSizeChangedInternal -= value;

                if (wasRunning && WindowSizeChangedInternal == null)
                    StopScreenMetricsListeners();
            }
        }

        static void OnWindowSizeChanged(DisplayInfo metrics)
            => OnWindowSizeChanged(new WindowSizeEventArgs(metrics));

        static void OnWindowSizeChanged(WindowSizeEventArgs e)
        {
            if (!currentMetrics.Equals(e.WindowSize))
            {
                SetCurrent(e.WindowSize);
                WindowSizeChangedInternal?.Invoke(null, e);
            }
        }
}
public class WindowSizeChangedEventArgs : EventArgs
    {
        public WindowSizeChangedEventArgs(WindowSize windowSize) =>
            WindowSize = windowSize;

        public WindowSize WindowSize { get; }
    }
public readonly struct WindowSize : IEquitable<WindowSize>
{
      public double Width {get;}
      public double Height {get;}

     public WindowSize(double width, double height)
     {
            Width = widht;
            Height = height;
     }

     // Overrides implementations .... 
}

For Android I still need to find an away without using an override in MainActivity.cs.

@pictos That looks like a decent API. I would open a PR and start work. Then we can talk API features and iterate.

@mattleibow I'll do some clean up in the code, and open a PR.

I'm happy to see progress on this as I also need to see the application/window dimensions along with a changed event like with DisplayInfo. I think it's more important than ever given the growing emphasis on iPad split screen behaviour.

I'd also love to see this realized. Anything preventing the last pull request from being merged into the main branch?
The only thing missing in the last pull request as far as I can see would be the "WindowSizeInfoChanged" as suggested above. That would be a cool extra for us as it would allow us to track if we're running in Multitasking mode or not.

Was this page helpful?
0 / 5 - 0 ratings