Skip to content

Commit

Permalink
feat: Implement sizing methods for ApplicationView
Browse files Browse the repository at this point in the history
  • Loading branch information
DZetko committed Jul 30, 2021
1 parent d555e13 commit 945a8c0
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@
x:Class="UITests.Windows_UI_ViewManagement.ApplicationViewSizing"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">

<StackPanel Spacing="12" Padding="12">
<controls:NumberBox Header="Width" x:Name="WidthInput" Value="500" />
<controls:NumberBox Header="Height" x:Name="HeightInput" Value="500" />
<StackPanel Padding="12" Spacing="12">
<controls:NumberBox
x:Name="WidthInput"
Header="Width"
Value="500" />
<controls:NumberBox
x:Name="HeightInput"
Header="Height"
Value="500" />
<Button Click="SetWindowSize_Click">Set window size</Button>
<Button Click="SetMinWindowSize_Click">Set min window size</Button>
<Button Click="SetPreferredLaunchViewSize_Click">Set preferred launch view size</Button>
<TextBlock>
<Run FontWeight="Bold" Text="Last SizeChanged at:" />
<Run x:Name="LastSizeTime" Text="" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,10 @@ void SetMinWindowSize_Click(System.Object sender, Windows.UI.Xaml.RoutedEventArg
WidthInput.Value,
HeightInput.Value));
}

void SetPreferredLaunchViewSize_Click(System.Object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
ApplicationView.PreferredLaunchViewSize = new Size(WidthInput.Value, HeightInput.Value);
}
}
}
12 changes: 12 additions & 0 deletions src/Uno.UI.Runtime.Skia.Gtk/GtkApplicationViewExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using Windows.Foundation;
using Windows.UI.ViewManagement;

namespace Uno.UI.Runtime.Skia
Expand Down Expand Up @@ -33,5 +34,16 @@ public bool TryEnterFullScreenMode()
GtkHost.Window.Fullscreen();
return true;
}

public bool TryResizeView(Size size)
{
GtkHost.Window.Resize((int)size.Width, (int)size.Height);
return true;
}

public void SetPreferredMinSize(Size size)
{
GtkHost.Window.SetSizeRequest((int)size.Width, (int)size.Height);
}
}
}
11 changes: 10 additions & 1 deletion src/Uno.UI.Runtime.Skia.Gtk/GtkHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using Uno.UI.Runtime.Skia.GTK.UI.Core;
using Uno.Extensions.Storage.Pickers;
using Windows.Storage.Pickers;
using Windows.UI.ViewManagement;
using Windows.Foundation;
using Uno.ApplicationModel.DataTransfer;
using Uno.UI.Runtime.Skia.GTK.Extensions.ApplicationModel.DataTransfer;

Expand Down Expand Up @@ -65,7 +67,14 @@ public void Run()

_isDispatcherThread = true;
_window = new Gtk.Window("Uno Host");
_window.SetDefaultSize(1024, 800);
Size preferredWindowSize = ApplicationView.PreferredLaunchViewSize;
if (preferredWindowSize != Size.Empty)
{
_window.SetDefaultSize((int)preferredWindowSize.Width, (int)preferredWindowSize.Height);
} else
{
_window.SetDefaultSize(1024, 800);
}
_window.SetPosition(Gtk.WindowPosition.Center);

_window.Realized += (s, e) =>
Expand Down
18 changes: 18 additions & 0 deletions src/Uno.UI.Runtime.Skia.Tizen/TizenApplicationViewExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,23 @@ public bool TryEnterFullScreenMode()
}
return false;
}

public bool TryResizeView(Size size)
{
if (this.Log().IsEnabled(LogLevel.Warning))
{
this.Log().LogWarning("Resizing windows is not yet supported on Tizen.");
}
return false;
}
public void SetPreferredMinSize()
{
if (this.Log().IsEnabled(LogLevel.Warning))
{
this.Log().LogWarning("Setting min size of windows is not yet supported on Tizen.");
}
}


}
}
13 changes: 13 additions & 0 deletions src/Uno.UI.Runtime.Skia.Wpf/WpfApplicationViewExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public string Title
private bool _isFullScreen = false;
private (WindowStyle WindowStyle, WindowState WindowState) _previousModes;

public void SetPreferredMinSize(Windows.Foundation.Size size)
{
_mainWpfWindow.MinWidth = size.Width;
_mainWpfWindow.MinHeight = size.Height;
}

public bool TryResizeView(Windows.Foundation.Size size)
{
_mainWpfWindow.Width = size.Width;
_mainWpfWindow.Height = size.Height;
return true;
}

public bool TryEnterFullScreenMode()
{
if (_isFullScreen || _mainWpfWindow.WindowStyle == WindowStyle.None)
Expand Down
8 changes: 8 additions & 0 deletions src/Uno.UI.Runtime.Skia.Wpf/WpfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using WpfCanvas = System.Windows.Controls.Canvas;
using WpfControl = System.Windows.Controls.Control;
using WpfFrameworkPropertyMetadata = System.Windows.FrameworkPropertyMetadata;
using Windows.UI.ViewManagement;
using Uno.UI.Xaml;
using Uno.UI.Runtime.Skia.Wpf;
using Uno.ApplicationModel.DataTransfer;
Expand Down Expand Up @@ -134,6 +135,13 @@ bool EnqueueNative(DispatcherQueuePriority priority, DispatcherQueueHandler call
WpfApplication.Current.Deactivated += Current_Deactivated;
WpfApplication.Current.MainWindow.StateChanged += MainWindow_StateChanged;

Windows.Foundation.Size preferredWindowSize = ApplicationView.PreferredLaunchViewSize;
if (preferredWindowSize != Windows.Foundation.Size.Empty)
{
WpfApplication.Current.MainWindow.Width = (int)preferredWindowSize.Width;
WpfApplication.Current.MainWindow.Height = (int)preferredWindowSize.Height;
}

SizeChanged += WpfHost_SizeChanged;
Loaded += WpfHost_Loaded;
}
Expand Down
14 changes: 12 additions & 2 deletions src/Uno.UI/UI/Xaml/Window.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,18 @@ public sealed partial class Window
public Window()
{
var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled | NSWindowStyle.Miniaturizable;
var rect = new CoreGraphics.CGRect(100, 100, 1024, 768);
_window = new Uno.UI.Controls.Window(rect, style, NSBackingStore.Buffered, false);

Foundation.Size preferredWindowSize = ApplicationView.PreferredLaunchViewSize;
if (preferredWindowSize != Foundation.Size.Empty)
{
var rect = new CoreGraphics.CGRect(100, 100, (int)preferredWindowSize.Width, (int)preferredWindowSize.Height);
_window = new Uno.UI.Controls.Window(rect, style, NSBackingStore.Buffered, false);
}
else
{
var rect = new CoreGraphics.CGRect(100, 100, 1024, 768);
_window = new Uno.UI.Controls.Window(rect, style, NSBackingStore.Buffered, false);
}

_mainController = ViewControllerGenerator?.Invoke() ?? new RootViewController();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static bool TerminateAppOnFinalViewClose
}
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
#if false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static global::Windows.Foundation.Size PreferredLaunchViewSize
{
Expand Down Expand Up @@ -240,14 +240,14 @@ public void ShowStandardSystemOverlays()
global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.UI.ViewManagement.ApplicationView", "void ApplicationView.ShowStandardSystemOverlays()");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || false
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || false || __NETSTD_REFERENCE__ || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__")]
public bool TryResizeView( global::Windows.Foundation.Size value)
{
throw new global::System.NotImplementedException("The member bool ApplicationView.TryResizeView(Size value) is not implemented in Uno.");
}
#endif
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || false
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || false || __NETSTD_REFERENCE__ || false
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__")]
public void SetPreferredMinSize( global::Windows.Foundation.Size minSize)
{
Expand Down Expand Up @@ -322,7 +322,7 @@ public static bool TryUnsnapToFullscreen()
#if __ANDROID__ || __IOS__ || NET461 || __WASM__ || __SKIA__ || __NETSTD_REFERENCE__ || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "NET461", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public static int GetApplicationViewIdForWindow( global::Windows.UI.Core.ICoreWindow window)
{
{
throw new global::System.NotImplementedException("The member int ApplicationView.GetApplicationViewIdForWindow(ICoreWindow window) is not implemented in Uno.");
}
#endif
Expand Down
29 changes: 29 additions & 0 deletions src/Uno.UWP/UI/ViewManagement/ApplicationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
using Uno.Logging;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Windows.Storage;

namespace Windows.UI.ViewManagement
{
public partial class ApplicationView
: IApplicationViewSpanningRects
{
private const string PreferredLaunchViewWidthKey = "__Uno.PreferredLaunchViewSizeKey.Width";
private const string PreferredLaunchViewHeightKey = "__Uno.PreferredLaunchViewSizeKey.Height";

private static ApplicationView _instance = new ApplicationView();

private ApplicationViewTitleBar _titleBar = new ApplicationViewTitleBar();
Expand Down Expand Up @@ -91,6 +95,31 @@ public ApplicationViewMode ViewMode
}
}

public static Size PreferredLaunchViewSize
{
get
{
if (ApplicationData.Current.LocalSettings.Values.TryGetValue(PreferredLaunchViewWidthKey, out var widthObject) &&
ApplicationData.Current.LocalSettings.Values.TryGetValue(PreferredLaunchViewHeightKey, out var heightObject))
{
double width = (double)widthObject;
double height = (double)heightObject;

return new Size(width, height);
}
return Size.Empty;
}

set
{
double width = value.Width;
double height = value.Height;

ApplicationData.Current.LocalSettings.Values[PreferredLaunchViewWidthKey] = width;
ApplicationData.Current.LocalSettings.Values[PreferredLaunchViewHeightKey] = height;
}
}

public bool IsViewModeSupported(ApplicationViewMode viewMode)
{
if (viewMode == ApplicationViewMode.Default)
Expand Down
8 changes: 7 additions & 1 deletion src/Uno.UWP/UI/ViewManagement/ApplicationView.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Windows.UI.ViewManagement
partial class ApplicationView
{
private string _title = string.Empty;
private Size _preferredMinSize = Size.Empty;

internal void SetCoreBounds(NSWindow keyWindow, Foundation.Rect windowBounds)
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public string Title
}
}

public bool IsFullScreen
public bool IsFullScreen
{
get
{
Expand Down Expand Up @@ -72,6 +73,10 @@ public bool TryResizeView(Size value)
{
VerifyKeyWindowInitialized();

if (value.Width < _preferredMinSize.Width || value.Height < _preferredMinSize.Height)
{
return false;
}
var window = NSApplication.SharedApplication.KeyWindow;
var frame = window.Frame;
frame.Size = value;
Expand All @@ -85,6 +90,7 @@ public void SetPreferredMinSize(Size minSize)

var window = NSApplication.SharedApplication.KeyWindow;
window.MinSize = new CGSize(minSize.Width, minSize.Height);
_preferredMinSize = minSize;
}

internal void SyncTitleWithWindow(NSWindow window)
Expand Down
21 changes: 21 additions & 0 deletions src/Uno.UWP/UI/ViewManagement/ApplicationView.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
using Uno.Foundation.Extensibility;
using Uno.Disposables;
using Windows.ApplicationModel;
using Windows.Storage;

namespace Windows.UI.ViewManagement
{
partial class ApplicationView : IApplicationViewEvents
{
private readonly IApplicationViewExtension _applicationViewExtension;
private Size _preferredMinSize = Size.Empty;

public ApplicationView()
{
Expand All @@ -32,6 +34,21 @@ public string Title
public bool TryEnterFullScreenMode() => _applicationViewExtension.TryEnterFullScreenMode();

public void ExitFullScreenMode() => _applicationViewExtension.ExitFullScreenMode();

public bool TryResizeView(Size value)
{
if (value.Width < _preferredMinSize.Width || value.Height < _preferredMinSize.Height)
{
return false;
}
return _applicationViewExtension.TryResizeView(value);
}

public void SetPreferredMinSize(Size minSize)
{
_applicationViewExtension.SetPreferredMinSize(minSize);
_preferredMinSize = minSize;
}
}

internal interface IApplicationViewExtension
Expand All @@ -41,6 +58,10 @@ internal interface IApplicationViewExtension
bool TryEnterFullScreenMode();

void ExitFullScreenMode();

bool TryResizeView(Size size);

void SetPreferredMinSize(Size minSize);
}

internal interface IApplicationViewEvents
Expand Down

0 comments on commit 945a8c0

Please sign in to comment.