Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restoring window state is not working correctly #18348

Open
MartinZikmund opened this issue Oct 1, 2024 · 3 comments
Open

Restoring window state is not working correctly #18348

MartinZikmund opened this issue Oct 1, 2024 · 3 comments
Assignees

Comments

@MartinZikmund
Copy link
Member

MartinZikmund commented Oct 1, 2024

The full screen state does not seem to work when restoring the window presenter or I am probably missing something. I created a sample project where you can see the behavior.

UnoAppWindow.zip

Window.mp4
        MainWindow = builder.Window;
        MainWindow.Closed += OnMainWindowClosed;
        RestoreWindowPosition();

#if DEBUG
        MainWindow.EnableHotReload();
#endif
        MainWindow.SetWindowIcon();

        Host = await builder.NavigateAsync<Shell>();
    }

    private void OnMainWindowClosed(object sender, WindowEventArgs args)
    {
        SaveWindowPosition();
    }

    private void SaveWindowPosition()
    {
        var settings = ApplicationData.Current.LocalSettings;
        settings.Values["WindowWidth"] = MainWindow!.AppWindow.Size.Width;
        settings.Values["WindowHeight"] = MainWindow.AppWindow.Size.Height;
        settings.Values["WindowX"] = MainWindow.AppWindow.Position.X;
        settings.Values["WindowY"] = MainWindow.AppWindow.Position.Y;
        settings.Values["Presenter"] = (int)MainWindow.AppWindow.Presenter.Kind;
    }

    private void RestoreWindowPosition()
    {
        var settings = ApplicationData.Current.LocalSettings;

        if (settings.Values.TryGetValue("WindowWidth", out var width) &&
            settings.Values.TryGetValue("WindowHeight", out var height) &&
            settings.Values.TryGetValue("WindowX", out var x) &&
            settings.Values.TryGetValue("WindowY", out var y) &&
            settings.Values.TryGetValue("Presenter", out var p))
        {
            var size = new SizeInt32 { Width = (int)width, Height = (int)height };
            var position = new PointInt32 { X = (int)x, Y = (int)y };

            MainWindow!.AppWindow.Resize(size);
            MainWindow.AppWindow.Move(position);
            MainWindow.AppWindow.SetPresenter((AppWindowPresenterKind)p);
        }
    }

Originally posted by @ArchieCoder in #18338 (reply in thread)

@MartinZikmund MartinZikmund self-assigned this Oct 1, 2024
@ArchieCoder
Copy link
Contributor

Following thread

@mobynet1
Copy link

Following thread...

One question: How effective is this when run from a multi-monitor system, the coordinates are saved, and then the next time the app opens up on a single monitor app system? I have my laptop in a docking station so this was an issue I had to solve in WPF. If you try to set the position to a point in the current monitor landscape that is out of range, what happens?

@ArchieCoder
Copy link
Contributor

@mobynet1 This is more difficult indeed. Morten does the management of it for WinUI platform with his WinUIEx library, see #18338 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants