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

Enable Window Sizing #4942

Merged
merged 68 commits into from
Aug 28, 2022
Merged

Enable Window Sizing #4942

merged 68 commits into from
Aug 28, 2022

Conversation

mattleibow
Copy link
Member

@mattleibow mattleibow commented Feb 26, 2022

Description of Change

Add the ability to set window size and read size and position.

Depends on:

Known Issues:

Platforms:

  • Windows
    • X\Y
    • Width\Height
    • Minimum Width\Height
    • Maximum Width\Height
    • Get window size and updates
    • SizeChanged event
  • Mac Catalyst (macOS)
    • X\Y (not supported)
    • Width\Height (not supported)
    • Minimum Width\Height
    • Maximum Width\Height
    • Get window size and updates
    • SizeChanged event

Additions made

One notable difference in API is that the views have a Width/HeightRequest and then a Width/Height for the actual bounds. Window does not need this because no matter what the user sets, that is the value. And, if the user doe not set any value, the OS will assign one. You cannot have a window without bounds. Views have bounds too, but there is a layout system that interprets it. For example, a view can be set to fill a space in a Grid, and then when moved to a new cell, it resizes. Window is the opposite - the OS ensures that the size is set and nothing updates it unless the user resizes it.

Core:

interface IWindow {
    // "request" values
    double X { get; }
    double Y { get; }
    double Width { get; }
    double Height { get; }
    double MinimumWidth { get; set; }
    double MinimumHeight { get; set; }
    double MaximumWidth { get; set; }
    double MaximumHeight { get; set; }

    // platform value
    void FrameChanged(Rect frame);
}

Controls:

class Window {
    // bindable properties
    public static readonly BindableProperty XProperty;
    public static readonly BindableProperty YProperty;
    public static readonly BindableProperty WidthProperty;
    public static readonly BindableProperty HeightProperty;
    public static readonly BindableProperty MinimumWidthProperty;
    public static readonly BindableProperty MinimumHeightProperty;
    public static readonly BindableProperty MaximumWidthProperty;
    public static readonly BindableProperty MaximumHeightProperty;

    // properties
    public double X { get; set; }
    public double Y { get; set; }
    public double Width { get; set; }
    public double Height { get; set; }
    public double MinimumWidth { get; set; }
    public double MinimumHeight { get; set; }
    public double MaximumWidth { get; set; }
    public double MaximumHeight { get; set; }

    // events
    public event EventHandler? SizeChanged;

Usage:

const int newWidth = 800;
const int newHeight = 600;

// get screen size
var disp = DeviceDisplay.Current.MainDisplayInfo;

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

// resize
Window.Width = newWidth;
Window.Height = newHeight;

NOTES

Mac Catalyst does not support window sizing or positioning, however sizing could happen via setting both the min and max sizes on the window. This will then trigger a resize and you can then reset back to mon=0 and max=maxvalue.

Not perfect, but works.

Window.MinimumWidth = 700;
Window.MaximumWidth = 700;

Window.MinimumHeight = 500;
Window.MaximumHeight = 500;

// dispatcher is used to give the window time to actually resize
Dispatcher.Dispatch(() =>
{
	Window.MinimumWidth = 0;
	Window.MaximumWidth = double.PositiveInfinity;
	Window.MinimumHeight = 0;
	Window.MaximumHeight = double.PositiveInfinity;
});

PR Checklist

  • Targets the correct branch
  • Tests are passing (or failures are unrelated)
  • Targets a single property for a single control (or intertwined few properties)
  • Adds the property to the appropriate interface
  • Avoids any changes not essential to the handler property
  • Adds the mapping to the PropertyMapper in the handler
  • Adds the mapping method to the WinUI, Android, iOS, and Standard aspects of the handler
  • Implements the actual property updates (usually in extension methods in the Platform section of Core)
  • Adds an example of the property to the sample project (MainPage)
  • Adds the property to the stub class
  • Implements basic property tests in DeviceTests

@Eilon Eilon added the legacy-area-desktop Windows / WinUI / Project Reunion & Mac Catalyst / macOS specifics (Menus & other Controls)) label Feb 28, 2022
@akanieski
Copy link

Will this work allow me to set window size to a fixed value while targeting Windows? What about MacOS? If I set MainPage.WidthRequest or MainPage.HeightRequest it gets the below.. is there some other way to achieve this? Or is this PR something I should look out for?
image

@mattleibow
Copy link
Member Author

That might be a bug because the root page always fills the window... Right, @hartez?

Maybe we also need a min/max on the window? And maybe the option to set flags, like the nosizing and toolwindow... Maybe these are for separate PRs.

Need to figure out the wiggle first.

And yeah, macos should respect bounds too. I don't think I implemented it yet as the Windows wiggle put a stop to that. I'll get back to it as soon as p14 is out the door.

@mattleibow mattleibow requested a review from Redth August 3, 2022 21:47
@mattleibow
Copy link
Member Author

After much playing around and learning about WinUI I see we now have a nice API that works well - trick is to use AppWindow.

@mattleibow mattleibow changed the title [WIP] Enable Window Sizing Enable Window Sizing Aug 3, 2022
@PureWeen PureWeen dismissed their stale review August 27, 2022 16:04

Slight changes to Frame needed

@mattleibow mattleibow closed this Aug 27, 2022
@mattleibow mattleibow deleted the dev/window-bounds branch August 27, 2022 17:10
@mattleibow mattleibow restored the dev/window-bounds branch August 27, 2022 17:11
@mattleibow mattleibow reopened this Aug 27, 2022
@mattleibow mattleibow merged commit b818a95 into main Aug 28, 2022
@mattleibow mattleibow deleted the dev/window-bounds branch August 28, 2022 15:13
@davidbuckleyni
Copy link

In winforms days we also had screen position this could be centered minimised and maximised in the more modern word be great if we could that and tell which montior to start a particular screen on ?

@mattleibow
Copy link
Member Author

In winforms days we also had screen position this could be centered minimised and maximised in the more modern word be great if we could that and tell which montior to start a particular screen on ?

Could you open an issue on the screens? I assume you would be looking to not only say screen 1 or 2 but also see what screens are available and their size?

@CartBlanche
Copy link
Contributor

@mattleibow in which version of MAUI is this fix available in? When will it be public (my Maui.Controls.Window object doesn't contain Width, Height etc properties)?

@Redth
Copy link
Member

Redth commented Oct 30, 2022

This arrives in .NET 7

Already available in RC2, soon in GA :)

@CartBlanche
Copy link
Contributor

@Redth thanks!

@BlueHippoGithub
Copy link

Right now the general solution for setting the window size of a maui application seems to be by calling .Resize on the AppWindow at window creation. This however, causes a "flicker" because it resizes after window creation.

Will this have the same issue of flickering or will the window size be set properly before showing it?

@Inrego
Copy link

Inrego commented Apr 3, 2023

This marks #7592 as completed. Is restoring values also implemented? Every time I open my app on Windows, it shows up on my main screen with the same size, ignoring the location and size I used last session.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 23, 2023
@Eilon Eilon added the t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK) label May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
fixed-in-7.0.0-rc.1.6683 legacy-area-desktop Windows / WinUI / Project Reunion & Mac Catalyst / macOS specifics (Menus & other Controls)) platform/macOS 🍏 macOS / Mac Catalyst platform/windows 🪟 t/desktop The issue relates to desktop scenarios (MacOS/MacCatalyst/Windows/WinUI/WinAppSDK) t/enhancement ☀️ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to set app launch window size
10 participants