Skip to content

Commit

Permalink
feat(ResExpr): add support for attached dp
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Dec 8, 2023
1 parent 0a71da0 commit a06b723
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,45 @@
<DataTemplate>
<StackPanel Spacing="20">

<!-- string literal -->
<TextBlock Text="Text test" FontWeight="Bold" />
<TextBlock Text="{utu:Responsive Narrow='Narrow Threshold 300', Normal='Normal Threshold 600', Wide='Wide Threshold 800'}" />

<!-- primitive literal -->
<TextBlock Text="FontSize test" FontWeight="Bold" />
<TextBlock Text="Normal 15 Wide 25" FontSize="{utu:Responsive Normal=15, Wide=25}" />

<!-- enum literal -->
<TextBlock Text="Orientation test | Normal=Vertical | Wide=Horizontal" FontWeight="Bold" />
<StackPanel Orientation="{utu:Responsive Normal=Vertical, Wide=Horizontal}">
<TextBlock Text="A" />
<TextBlock Text="B" />
<TextBlock Text="C" />
</StackPanel>

<TextBlock Text="Text test" FontWeight="Bold" />
<TextBlock Text="{utu:Responsive Narrow='Narrow Threshold 300', Normal='Normal Threshold 600', Wide='Wide Threshold 800'}" />

<TextBlock Text="FontSize test" FontWeight="Bold" />
<TextBlock Text="Normal 15 Wide 25" FontSize="{utu:Responsive Normal=15, Wide=25}" />

<!-- xaml parsable object -->
<TextBlock Text="Color test | Normal Red | Wide Blue" FontWeight="Bold" />
<Border Width="30"
Height="30"
HorizontalAlignment="Left"
Background="{utu:Responsive Normal=Red,
Wide=Blue}" />

<!-- attached property -->
<TextBlock Text="Grid.Column test: Narrowest=0, 1, 0, 1, Widest=0" FontWeight="Bold" />
<Grid Width="100"
Height="50"
Background="SkyBlue"
HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<Border Grid.Column="{utu:Responsive Narrowest=0, Narrow=1, Normal=0, Wide=1, Widest=0}" Background="Pink" />
</Grid>

<!-- layout overriding -->
<TextBlock Text="Custom values override" FontWeight="Bold" />
<StackPanel>
<TextBlock Text="Global Override:" />
Expand Down
11 changes: 4 additions & 7 deletions src/Uno.Toolkit.UI/Markup/ResponsiveExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Logging;
using Windows.Foundation;
using Uno.Extensions;
using Uno.Logging;
using System.Diagnostics.CodeAnalysis;
using System.Linq;



#if IS_WINUI
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -85,7 +82,7 @@ private void BindToEvents(IXamlServiceProvider serviceProvider)
if (serviceProvider.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget pvt &&
pvt.TargetObject is FrameworkElement target &&
pvt.TargetProperty is ProvideValueTargetProperty pvtp &&
target.FindDependencyProperty($"{pvtp.Name}Property") is DependencyProperty dp)
pvtp.DeclaringType.FindDependencyProperty($"{pvtp.Name}Property") is DependencyProperty dp)
{
TargetWeakRef = new WeakReference(target);
_targetProperty = dp;
Expand All @@ -95,11 +92,11 @@ private void BindToEvents(IXamlServiceProvider serviceProvider)
#else
pvtp.Type;
#endif

// here, we need to bind to two events:
// 1. Window.SizeChanged for obvious reason
// 2. Control.Loaded because the initial value(result of ProvideValue) is resolved without the inherited .resources
// which may define a different DefaultResponsiveLayout resource somewhere along the visual tree, so we need to rectify that.

ResponsiveHelper.GetForCurrentView().Register(this);
target.Loaded += OnTargetLoaded;

Expand Down Expand Up @@ -158,7 +155,7 @@ private void UpdateBindingIfNeeded(ResponsiveHelper? helper = null, bool forceAp
var helper = ResponsiveHelper.GetForCurrentView();
var resolved = helper.ResolveLayout(GetAppliedLayout(), GetAvailableLayoutOptions());
var value = GetValueFor(resolved.Result);

CurrentValue = value;
CurrentLayout = resolved.Result;
LastResolved = resolved;
Expand Down

0 comments on commit a06b723

Please sign in to comment.