WinUI-Gallery icon indicating copy to clipboard operation
WinUI-Gallery copied to clipboard

Resize the window on startup

Open HEIC-to-JPEG-Dev opened this issue 1 year ago • 13 comments

Issue type

enhancement

Which version of the app?

WinUI 3 Gallery

Description

On small monitors/laptops etc, the launch window default size is probably acceptable. On larger monitors (ultra wide for example) it is massive, 3824 x 1002.

Request that a max size is set 1366 x 768, or that it remembers the size it was last set to (like UWP did).

Screenshots

No response

Windows version

Windows 11 23H2 (22631)

Additional context

No response

HEIC-to-JPEG-Dev avatar Aug 15 '24 08:08 HEIC-to-JPEG-Dev

Remembering the size should be easy.

MGGSK avatar Aug 15 '24 09:08 MGGSK

I don't think setting a maximum size is a good idea because it can be too low (1366 x 768 is too low for most users) and in that case maximizing the window will cause issues.

MGGSK avatar Aug 29 '24 16:08 MGGSK

I don't think setting a maximum size is a good idea because it can be too low (1366 x 768 is too low for most users) and in that case maximizing the window will cause issues.

Is that from imperical evidence ? Whatever the size, just get the app to remember it's size and location, it currently starts at nearly 4000 pixels wide every time.

HEIC-to-JPEG-Dev avatar Oct 16 '24 06:10 HEIC-to-JPEG-Dev

I don't think setting a maximum size is a good idea because it can be too low (1366 x 768 is too low for most users) and in that case maximizing the window will cause issues.

Is that from imperical evidence ? Whatever the size, just get the app to remember it's size and location, it currently starts at nearly 4000 pixels wide every time.

Its not from imperical evidence. The average screen resulution is 1920x1080 which is too large for a maximum size of 1366x768. I will make a pull request that adds this feature (Just without the maximum window size).

MGGSK avatar Oct 16 '24 13:10 MGGSK

@niels9001 @marcelwgn

I’ve been experimenting with this and I believe I can take it on. You can check my approach in this repo. If it looks good, you can assign me this.

Zakariathr22 avatar Oct 07 '25 08:10 Zakariathr22

@Zakariathr22 I saw that these APIs were included in 1.8-exp:

Image

Wonder if we could get the logic out of the box with these APIs?

https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.windowing.appwindow.savecurrentplacement?view=windows-app-sdk-2.0-experimental

niels9001 avatar Oct 07 '25 12:10 niels9001

@Zakariathr22 I saw that these APIs were included in 1.8-exp:

Image Wonder if we could get the logic out of the box with these APIs?

https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.windowing.appwindow.savecurrentplacement?view=windows-app-sdk-2.0-experimental

The docs are a bit unclear, and we’re not sure if it will actually be released.

Zakariathr22 avatar Oct 07 '25 14:10 Zakariathr22

@Zakariathr22 I saw that these APIs were included in 1.8-exp: Image Wonder if we could get the logic out of the box with these APIs? https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.windowing.appwindow.savecurrentplacement?view=windows-app-sdk-2.0-experimental

The docs are a bit unclear, and we’re not sure if it will actually be released.

They have existed since v1.7-exp and only work in that version. They don’t work in v1.8-exp or v2.0-exp — or at least, I haven’t been able to use them there.

v1.7 APIs: EnablePlacementPersistence EnablePlacementPersistence GetCurrentPlacement SaveCurrentPlacement

v1.8 APIs: GetCurrentPlacement PersistedStateId PlacementRestorationBehavior SaveCurrentPlacement SaveCurrentPlacementForAllPersistedStateIds SetCurrentPlacement

ghost1372 avatar Oct 07 '25 15:10 ghost1372

They have existed since v1.7-exp and only work in that version. They don’t work in v1.8-exp or v2.0-exp — or at least, I haven’t been able to use them there.

v1.7 APIs: EnablePlacementPersistence EnablePlacementPersistence GetCurrentPlacement SaveCurrentPlacement

v1.8 APIs: GetCurrentPlacement PersistedStateId PlacementRestorationBehavior SaveCurrentPlacement SaveCurrentPlacementForAllPersistedStateIds SetCurrentPlacement

I haven’t been able to use them too 😕 so I have created an issue about this:

  • https://github.com/microsoft/WindowsAppSDK/issues/5896

Zakariathr22 avatar Oct 08 '25 08:10 Zakariathr22

They have existed since v1.7-exp and only work in that version.

They actually works in all version after that exp (including formal release) As long as you cast the objRef (thisPtr) to IAppWindowExperimental then you can use them anyway.

Here is a working example:

    private static void EnablePlacementRestoration(Window window)
    {
		// Unsafe.As<IWinRTObject> is just a little faster than ((IWinRTObject)window.AppWindow)
        IObjectReference objRefAppWindowExperimental = Unsafe.As<IWinRTObject>(window.AppWindow).NativeObject.As<IUnknownVftbl>(IAppWindowExperimentalMethods.IID);
        IAppWindowExperimentalMethods.set_PlacementRestorationBehavior(objRefAppWindowExperimental, PlacementRestorationBehavior.All);

        string windowName = TypeNameHelper.GetTypeDisplayName(window);
        byte[] data = CryptographicOperations.HashData(HashAlgorithmName.MD5, Encoding.UTF8.GetBytes(windowName));
        Guid guid = MemoryMarshal.AsRef<Guid>(data);
        IAppWindowExperimentalMethods.set_PersistedStateId(objRefAppWindowExperimental, guid);
    }

Lightczx avatar Oct 13 '25 08:10 Lightczx

If you are using C# 14, you can even write a extension(AppWindow) to make PlacementRestorationBehavior/PersistedStateId be extension properties instead of odd extension method.

Something like:

public static class AppWindowExtensions
{
    extension(AppWindow window)
    {
        public PlacementRestorationBehavior PlacementRestorationBehavior
        {
            get...
            set
            {
                IObjectReference objRefAppWindowExperimental = Unsafe.As<IWinRTObject>(window.AppWindow).NativeObject.As<IUnknownVftbl>(IAppWindowExperimentalMethods.IID);
                IAppWindowExperimentalMethods.set_PlacementRestorationBehavior(objRefAppWindowExperimental, value);
            }
        }
    }
}

then you can just myWindow.AppWindow.PlacementRestorationBehavior = PlacementRestorationBehavior.All

Lightczx avatar Oct 13 '25 09:10 Lightczx

@Lightczx That was a very clever workaround, but I’d rather not use an approach that relies on unsafe casts and manual interface access. It’s a lot of low-level plumbing just to call an API, and that makes it more painful than it should be.

Zakariathr22 avatar Oct 13 '25 13:10 Zakariathr22

@Lightczx That was a very clever workaround, but I’d rather not use an approach that relies on unsafe casts and manual interface access. It’s a lot of low-level plumbing just to call an API, and that makes it more painful than it should be.

Believe it or not, that's how the cswinrt projection calling all winrt api under the 'projection' layer, you can just open your vs and goto your app.xaml.cs and select Application class then press F12, you gonna see bunch code like what I provided, I just copied them out and did some minor changes. There are also interesting apis like ExpPointerPoint,ContentExternalOutputLink,ContentExternalBackdropLink are marked as experimental, but I think avalonia,files-community and other great project are using them. Since microsoft not willing to put experimental api to formal release, I just had to do it myself.

Lightczx avatar Oct 13 '25 15:10 Lightczx