Skip to content

Commit e0fb72a

Browse files
authored
Merge pull request #1443 from unoplatform/dev/xygu/20250812/rv-refactor
refactor: resp-view
2 parents a13895e + 4d4224c commit e0fb72a

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

src/Uno.Toolkit.RuntimeTests/Tests/ResponsiveViewTests.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
using System.Threading.Tasks;
1+
using Windows.Foundation;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
33
using Uno.UI.RuntimeTests;
44
using Uno.Toolkit.RuntimeTests.Helpers;
5-
using Windows.Foundation;
65
using Uno.Toolkit.UI;
76

87
#if IS_WINUI
9-
using Microsoft.UI.Xaml.Controls;
108
using Microsoft.UI.Xaml.Shapes;
119
#else
12-
using Windows.UI.Xaml.Controls;
1310
using Windows.UI.Xaml.Shapes;
1411
#endif
1512

@@ -39,7 +36,7 @@ public async Task ResponsiveView_NarrowContent_TextBlock()
3936
await UnitTestUIContentHelperEx.SetContentAndWait(host);
4037

4138
host.ForceResponsiveSize(new Size(300, 400));
42-
Assert.AreEqual("Narrow", (host.Content as TextBlock)?.Text);
39+
Assert.AreEqual("Narrow", (host.GetResolvedContent() as TextBlock)?.Text);
4340
}
4441

4542
[TestMethod]
@@ -67,7 +64,7 @@ public async Task ResponsiveView_NormalContent_Rectangle()
6764
await UnitTestUIContentHelperEx.SetContentAndWait(host);
6865

6966
host.ForceResponsiveSize(new Size(600, 400));
70-
Assert.AreEqual(typeof(Rectangle), host.Content?.GetType());
67+
Assert.AreEqual(typeof(Rectangle), host.GetResolvedContent()?.GetType());
7168
}
7269

7370
[TestMethod]
@@ -109,7 +106,7 @@ public async Task ResponsiveView_NormalContent_ResponsiveLayout()
109106
await UnitTestUIContentHelperEx.SetContentAndWait(host);
110107

111108
host.ForceResponsiveSize(new Size(322, 400));
112-
Assert.AreEqual(typeof(Ellipse), host.Content?.GetType());
109+
Assert.AreEqual(typeof(Ellipse), host.GetResolvedContent()?.GetType());
113110
}
114111

115112
[TestMethod]
@@ -142,7 +139,7 @@ public async Task ResponsiveView_WidestContent_Ellipse()
142139
await UnitTestUIContentHelperEx.SetContentAndWait(host);
143140

144141
host.ForceResponsiveSize(new Size(2000, 400));
145-
Assert.AreEqual(typeof(Ellipse), host.Content?.GetType());
142+
Assert.AreEqual(typeof(Ellipse), host.GetResolvedContent()?.GetType());
146143
}
147144

148145
[TestMethod]
@@ -175,9 +172,9 @@ public async Task ResponsiveView_WideContent_SizeChanged()
175172
await UnitTestUIContentHelperEx.SetContentAndWait(host);
176173

177174
host.ForceResponsiveSize(new Size(150, 400));
178-
Assert.AreEqual(typeof(TextBlock), host.Content?.GetType());
175+
Assert.AreEqual(typeof(TextBlock), host.GetResolvedContent()?.GetType());
179176

180177
host.ForceResponsiveSize(new Size(800, 400));
181-
Assert.AreEqual(typeof(TextBox), host.Content?.GetType());
178+
Assert.AreEqual(typeof(TextBox), host.GetResolvedContent()?.GetType());
182179
}
183180
}

src/Uno.Toolkit.UI/Controls/ResponsiveView/ResponsiveView.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,24 @@
1212

1313
namespace Uno.Toolkit.UI;
1414

15-
public partial class ResponsiveView : ContentControl
15+
public partial class ResponsiveView : Control;
16+
17+
partial class ResponsiveView
18+
{
19+
private class TemplateParts
20+
{
21+
public const string ResponsiveRoot = "ResponsiveRoot";
22+
}
23+
}
24+
25+
[TemplatePart(Name = TemplateParts.ResponsiveRoot, Type = typeof(Border))]
26+
partial class ResponsiveView
1627
{
1728
public Layout? CurrentLayout { get; private set; }
1829
internal ResolvedLayout? LastResolved { get; private set; }
1930

31+
private Border? _responsiveRoot;
32+
2033
public ResponsiveView()
2134
{
2235
DefaultStyleKey = typeof(ResponsiveView);
@@ -25,6 +38,13 @@ public ResponsiveView()
2538
Unloaded += OnUnloaded;
2639
}
2740

41+
protected override void OnApplyTemplate()
42+
{
43+
base.OnApplyTemplate();
44+
45+
_responsiveRoot = GetTemplateChild(TemplateParts.ResponsiveRoot) as Border
46+
?? throw new Exception($"The template part '{TemplateParts.ResponsiveRoot}' is missing or is not of type Border.");
47+
}
2848

2949
private void OnLoaded(object sender, RoutedEventArgs e)
3050
{
@@ -66,7 +86,10 @@ private void UpdateTemplate(ResolvedLayout resolved, bool forceApplyValue = fals
6686
{
6787
if (forceApplyValue || CurrentLayout != resolved.Result)
6888
{
69-
Content = GetTemplateFor(resolved.Result)?.LoadContent() as UIElement;
89+
if (_responsiveRoot is { })
90+
{
91+
_responsiveRoot.Child = GetTemplateFor(resolved.Result)?.LoadContent() as UIElement;
92+
}
7093

7194
CurrentLayout = resolved.Result;
7295
LastResolved = resolved;
@@ -96,6 +119,8 @@ private IEnumerable<Layout> GetAvailableLayoutOptions()
96119
if (WidestTemplate != null) yield return UI.Layout.Widest;
97120
}
98121

122+
internal UIElement? GetResolvedContent() => _responsiveRoot?.Child;
123+
99124
internal ResponsiveLayout? GetAppliedLayout() =>
100125
ResponsiveLayout ??
101126
this.ResolveLocalResource<ResponsiveLayout>(ResponsiveLayout.DefaultResourceKey) ??
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:utu="using:Uno.Toolkit.UI">
4+
<Style TargetType="utu:ResponsiveView">
5+
<Setter Property="Template">
6+
<Setter.Value>
7+
<ControlTemplate TargetType="utu:ResponsiveView">
8+
<Border x:Name="ResponsiveRoot" />
9+
</ControlTemplate>
10+
</Setter.Value>
11+
</Setter>
12+
</Style>
13+
</ResourceDictionary>

0 commit comments

Comments
 (0)