Essentials icon indicating copy to clipboard operation
Essentials copied to clipboard

Add Application/Screen Size API

Open KSemenenko opened this issue 7 years ago • 12 comments

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.

KSemenenko avatar Jul 17 '18 13:07 KSemenenko

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..

jamesmontemagno avatar Jul 17 '18 15:07 jamesmontemagno

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 avatar Dec 11 '18 05:12 Redth

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

mattleibow avatar Dec 13 '18 23:12 mattleibow

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.

barrysohl avatar Aug 26 '19 22:08 barrysohl

+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.

SmartmanApps avatar Nov 03 '19 06:11 SmartmanApps

Yeah this would be good to add.

Redth avatar Nov 15 '19 21:11 Redth

@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 avatar Nov 17 '19 15:11 pictos

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

mattleibow avatar Nov 17 '19 16:11 mattleibow

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

pictos avatar Nov 17 '19 16:11 pictos

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.

Jared-Aus avatar Nov 26 '19 07:11 Jared-Aus

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.

DDHSchmidt avatar Jul 01 '20 09:07 DDHSchmidt

Would love to be able to get the height of the usable part of the screen. Many modern Android devices no longer have hardware buttons so "home", "back button" etc are on screen, taking up some of the full screen height.

Would also be good to know how tall the navigation, tab bar, etc are in Xamarin.Forms Shell, then I can make layouts truly responsive. It's very hit and miss at the moment. You can't get Shell.Current.Height; until after the screen is already rendering.

Some elements on screen like 3rd party plugins only work by specifying a HeightRequest, so need to bind that to a value that can be calculated as a proportion of the remaining screen height or it looks very inconsistent across devices.

c0ff33-b34n avatar Feb 14 '22 14:02 c0ff33-b34n