Skip to content

Commit 221f061

Browse files
committed
feat: Warn when Window constructor is used in Uno Platform targets
1 parent 01016bf commit 221f061

File tree

9 files changed

+40
-35
lines changed

9 files changed

+40
-35
lines changed

src/SolutionTemplate/UnoSolutionTemplate/Shared/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
5454
}
5555
#endif
5656

57-
#if NET5_0 && WINDOWS
57+
#if NET5_0 && WINDOWS && !HAS_UNO
5858
_window = new Window();
5959
_window.Activate();
6060
#else

src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_Window.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Given_Window
1515
public void When_CreateNewWindow()
1616
{
1717
// This used to crash on wasm which was trying to create a second D&D extension
18-
var sut = new Window();
18+
var sut = new Window(true);
1919
}
2020
#endif
2121
}

src/Uno.UI/UI/Xaml/Window.Android.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ public sealed partial class Window
2525
{
2626
private Border _rootBorder;
2727

28-
public Window()
28+
partial void InitPlatform()
2929
{
3030
Dispatcher = CoreDispatcher.Main;
3131
CoreWindow = CoreWindow.GetOrCreateForCurrentThread();
3232

3333
CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBarChanged
3434
+= RaiseNativeSizeChanged;
35-
36-
InitializeCommon();
3735
}
3836

3937
internal Thickness Insets { get; set; }

src/Uno.UI/UI/Xaml/Window.Desktop.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@ public sealed partial class Window
1717
{
1818
private bool _isActive;
1919

20-
public Window()
20+
partial void InitPlatform()
2121
{
2222
CoreWindow = CoreWindow.GetOrCreateForCurrentThread();
23-
24-
InitializeCommon();
2523
}
2624

2725
partial void InternalActivate()

src/Uno.UI/UI/Xaml/Window.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Windows.UI.Xaml
2222
public sealed partial class Window
2323
{
2424
private static Window _current;
25-
25+
2626
private UIElement _content;
2727
private RootVisual _rootVisual;
2828

@@ -36,6 +36,32 @@ public sealed partial class Window
3636
public global::Microsoft.UI.Dispatching.DispatcherQueue DispatcherQueue { get; } = global::Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
3737
#endif
3838

39+
#if HAS_UNO_WINUI
40+
public Window() : this(false)
41+
{
42+
}
43+
#endif
44+
45+
internal Window(bool internalUse)
46+
{
47+
if (!internalUse)
48+
{
49+
if (this.Log().IsEnabled(LogLevel.Warning))
50+
{
51+
this.Log().LogWarning(
52+
"Creating a secondary Window instance is currently not supported in Uno Platform targets. " +
53+
"Use the Window.Current property instead (you can use #if HAS_UNO to differentiate " +
54+
"between Uno Platform targets and Windows App SDK).");
55+
}
56+
}
57+
58+
InitPlatform();
59+
60+
InitializeCommon();
61+
}
62+
63+
partial void InitPlatform();
64+
3965
#pragma warning disable 67
4066
/// <summary>
4167
/// Occurs when the window has successfully been activated.
@@ -96,7 +122,7 @@ public UIElement Content
96122
oldRoot.SizeChanged -= RootSizeChanged;
97123
}
98124
}
99-
125+
100126
if (value != null)
101127
{
102128
value.IsWindowRoot = true;
@@ -106,7 +132,7 @@ public UIElement Content
106132

107133
if (value is FrameworkElement newRoot)
108134
{
109-
newRoot.SizeChanged += RootSizeChanged;
135+
newRoot.SizeChanged += RootSizeChanged;
110136
}
111137

112138
oldContent?.XamlRoot?.NotifyChanged();
@@ -282,7 +308,7 @@ internal IDisposable RegisterBackgroundChangedEvent(EventHandler handler)
282308
(h as EventHandler)?.Invoke(s, (EventArgs)e)
283309
);
284310

285-
private static Window InternalGetCurrentWindow() => _current ??= new Window();
311+
private static Window InternalGetCurrentWindow() => _current ??= new Window(true);
286312

287313
private UIElement InternalGetContent() => _content!;
288314

src/Uno.UI/UI/Xaml/Window.iOS.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public sealed partial class Window
3131
/// </summary>
3232
public static Func<RootViewController> ViewControllerGenerator { get; set; }
3333

34-
public Window()
34+
partial void InitPlatform()
3535
{
3636
_nativeWindow = new Uno.UI.Controls.Window();
3737

@@ -45,7 +45,6 @@ public Window()
4545
CoreWindow = new CoreWindow(_nativeWindow);
4646

4747
_nativeWindow.SetOwner(CoreWindow);
48-
InitializeCommon();
4948
}
5049

5150
internal Uno.UI.Controls.Window NativeWindow => _nativeWindow;

src/Uno.UI/UI/Xaml/Window.macOS.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public sealed partial class Window
3131
/// </summary>
3232
public static Func<RootViewController> ViewControllerGenerator { get; set; }
3333

34-
public Window()
34+
partial void InitPlatform()
3535
{
3636
var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled | NSWindowStyle.Miniaturizable;
3737

@@ -55,8 +55,6 @@ public Window()
5555
CoreWindow = new CoreWindow(_window);
5656

5757
_window.CoreWindowEvents = CoreWindow;
58-
59-
InitializeCommon();
6058
}
6159

6260
internal NSWindow NativeWindow => _window;

src/Uno.UI/UI/Xaml/Window.skia.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,12 @@ public sealed partial class Window
1919
// private ScrollViewer _rootScrollViewer;
2020
private Border? _rootBorder;
2121

22-
public Window()
23-
{
24-
Init();
25-
26-
Compositor = new Compositor();
27-
}
28-
29-
public void Init()
22+
partial void InitPlatform()
3023
{
3124
Dispatcher = CoreDispatcher.Main;
3225
CoreWindow = CoreWindow.GetOrCreateForCurrentThread();
3326

34-
InitializeCommon();
27+
Compositor = new Compositor();
3528
}
3629

3730
internal void OnNativeSizeChanged(Size size)
@@ -54,7 +47,7 @@ internal void OnNativeSizeChanged(Size size)
5447
}
5548
}
5649

57-
public Compositor Compositor { get; }
50+
public Compositor Compositor { get; private set; }
5851

5952
private void InternalSetContent(UIElement content)
6053
{

src/Uno.UI/UI/Xaml/Window.wasm.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@ public sealed partial class Window
2929
private Border _rootBorder;
3030
private bool _invalidateRequested;
3131

32-
public Window()
33-
{
34-
Init();
35-
36-
InitializeCommon();
37-
}
38-
39-
private void Init()
32+
partial void InitPlatform()
4033
{
4134
Dispatcher = CoreDispatcher.Main;
4235
CoreWindow = CoreWindow.GetOrCreateForCurrentThread();

0 commit comments

Comments
 (0)