Resize the window on startup
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
Remembering the size should be easy.
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.
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.
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).
@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 I saw that these APIs were included in 1.8-exp:
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
@Zakariathr22 I saw that these APIs were included in 1.8-exp:
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 I saw that these APIs were included in 1.8-exp:
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
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
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);
}
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 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.
@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.
Wonder if we could get the logic out of the box with these APIs?