Skip to content

[Windows] Allow early use of DeviceDisplay.Current.MainDisplayInfo - #14521

Merged
rmarinho merged 3 commits into
mainfrom
foda/MainDisplayInfo
Apr 12, 2023
Merged

rmarinho merged 3 commits into
mainfrom
foda/MainDisplayInfo

Conversation

@Foda

@Foda Foda commented Apr 11, 2023

Copy link
Copy Markdown
Contributor

Description of Change

Currently (on Windows), DeviceDisplay.Current.MainDisplayInfo doesn't get set until the first window is activated/shown on the screen since it uses the active window to gather this info.

This PR fixes that by setting the active window as soon as possible using the OnPlatformWindowSubclassed lifecycle event. This lifecycle event happens after we have a valid HWND but before the window is "technically" active. I don't think this will cause any issues (???).

As a result, Windows users can now do things like override Application.CreateWindow to set the initial window position and size before it's shown without needing to pinvoke:

class App : Application
{
    protected override Window CreateWindow(IActivationState activationState)
    {
        Window window = new Window(new NavigationPage(new MainPage()));

        var disp = DeviceDisplay.Current.MainDisplayInfo;
        var newWidth = disp.Width / 2;
        var newHeight = disp.Height / 2;

        // center the window
        window.X = (disp.Width / disp.Density - newWidth) / 2;
        window.Y = (disp.Height / disp.Density - newHeight) / 2;

        return window;
    }
}

Issues Fixed

Fixes #6976

Mike Corsaro added 2 commits April 11, 2023 14:49
…able ASAP

This commit adds `OnPlatformWindowInitialized` to the WindowStateManager for Windows. This function sets the active window via the `OnPlatformWindowSubclassed` lifecycle event which happens before `OnActivated`, which is possible because we get a valid hWND in the `MauiWinUIWindow` constructor. As a result, users can now override `Application.CreateWindow` to set the window position and size before it's shown.
@Foda Foda added t/bug Something isn't working platform/windows area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info labels Apr 11, 2023

@jfversluis jfversluis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a lot of people will be happy with this one, thank you!

@rmarinho

Copy link
Copy Markdown
Member

Can we name the files for test like the other ones, no underscores please.

Co-authored-by: Rui Marinho <me@ruimarinho.net>
@rmarinho
rmarinho merged commit 13e66e0 into main Apr 12, 2023
@rmarinho
rmarinho deleted the foda/MainDisplayInfo branch April 12, 2023 15:41
@Foda

Foda commented Apr 12, 2023

Copy link
Copy Markdown
Contributor Author

Thanks for applying the code review suggestions @jfversluis!

@kt215

kt215 commented Apr 15, 2023

Copy link
Copy Markdown

How will I know which maui-windows workload version this fix will be included in? The latest version is 7.0.81/7.0.100

@gerneio

gerneio commented Apr 28, 2023

Copy link
Copy Markdown

Would also like to know what version this fix will be applied to? Is this being saved for .NET 8? Hopefully will be delivered sooner. Can confirm that it's still an issue in the latest maui-windows workload version.

@mattleibow mattleibow added the backport/suggested The PR author or issue review has suggested that the change should be backported. label Apr 29, 2023
@mattleibow

mattleibow commented Apr 29, 2023 •

Copy link
Copy Markdown
Member

I wonder if we should actually take a different approach. If there is no main window yet, then we can just use the main monitor on the system?

I suppose the event is useful generally as it forms the same thing as an activity OnCreate...

@hartez hartez added the backport/NO This change should not be backported. It may break customers. label May 31, 2023
@dinther

dinther commented Aug 13, 2023 •

Copy link
Copy Markdown

It's day two of me exploring .NET Maui ... And I run into fundamental stuff like this!

The workaround solution offered here #6976 (comment)
is of no use as it assumes a displayDensity of 1

When will the fix be released?

Here is another workaround. My issue is the redraw of the window that appears as flashing. But setting the window size to 0 in createWindow and then to the proper size and location in the Loaded event handler of the Page works.

App.xaml.cs

namespace MyApp
{
    public partial class App : Application
    {
        double displayWidth;
        public App()
        {
            InitializeComponent();
            MainPage = new AppShell();
        }
        protected override Window CreateWindow(IActivationState activationState)
        {
            Window window = base.CreateWindow(activationState);
            // Manipulate Window object
            window.Width = 0;
            window.Height = 0;
            return window;
        }
    }
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyApp.MainPage"
             Loaded="OnPageLoaded">

             ....
using Microsoft.Maui.Controls;

namespace MyApp
{
    public partial class MainPage : ContentPage
    {
        int count = 0;

        public MainPage()
        {
            InitializeComponent();
        }

        private void OnPageLoaded(object sender, EventArgs e)
        {
            // Get display size
            var displayInfo = DeviceDisplay.Current.MainDisplayInfo;
            Window.Width = 1700;
            Window.Height = 1500;
            // Center the window
            Window.X = (displayInfo.Width / displayInfo.Density - Window.Width) / 2;
            Window.Y = (displayInfo.Height / displayInfo.Density - Window.Height) / 2;
        }

         ....

@github-actions github-actions Bot locked and limited conversation to collaborators Dec 12, 2023
@samhouts samhouts added the fixed-in-8.0.0-preview.4.8333 Look for this fix in 8.0.0-preview.4.8333! label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-essentials Essentials: Device, Display, Connectivity, Secure Storage, Sensors, App Info backport/NO This change should not be backported. It may break customers. backport/suggested The PR author or issue review has suggested that the change should be backported. fixed-in-8.0.0-preview.4.8333 Look for this fix in 8.0.0-preview.4.8333! platform/windows t/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DeviceDisplay.Current.MainDisplayInfo not available on windows

10 participants