generated from unoplatform/template
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(responsive): add support for non-FrameworkElement
- Loading branch information
Showing
5 changed files
with
285 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
|
||
#if IS_WINUI | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Controls; | ||
using Microsoft.UI.Xaml.Documents; | ||
#else | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml.Documents; | ||
#endif | ||
|
||
namespace Uno.Toolkit.UI; | ||
|
||
public static class ResponsiveBehavior | ||
{ | ||
#region DependencyProperty: IsEnabled | ||
|
||
public static DependencyProperty IsEnabledProperty { [DynamicDependency(nameof(GetIsEnabled))] get; } = DependencyProperty.RegisterAttached( | ||
"IsEnabled", | ||
typeof(bool), | ||
typeof(ResponsiveBehavior), | ||
new PropertyMetadata(default(bool), OnIsEnabledChanged)); | ||
|
||
[DynamicDependency(nameof(SetIsEnabled))] | ||
public static bool GetIsEnabled(DependencyObject obj) => (bool)obj.GetValue(IsEnabledProperty); | ||
[DynamicDependency(nameof(GetIsEnabled))] | ||
public static void SetIsEnabled(DependencyObject obj, bool value) => obj.SetValue(IsEnabledProperty, value); | ||
|
||
#endregion | ||
|
||
internal static bool IsChildSupported(DependencyObject? child) => child switch | ||
{ | ||
ColumnDefinition or RowDefinition => true, | ||
Inline => true, | ||
|
||
_ => false, | ||
}; | ||
|
||
private static void OnIsEnabledChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (sender is Grid g) | ||
{ | ||
g.Loaded += OnGridLoaded; | ||
} | ||
else if (sender is TextBlock tb) | ||
{ | ||
tb.Loaded += OnTextBlockLoaded; | ||
} | ||
else | ||
{ | ||
throw new NotSupportedException($"ResponsiveBehavior is not supported on '{sender.GetType()}'."); | ||
} | ||
} | ||
|
||
private static void OnGridLoaded(object sender, RoutedEventArgs e) | ||
{ | ||
if (sender is not Grid host) return; | ||
|
||
var markups = Enumerable | ||
.Concat<DependencyObject>(host.ColumnDefinitions, host.RowDefinitions) | ||
.SelectMany(ResponsiveExtension.GetAllInstancesFor); | ||
foreach (var markup in markups) | ||
{ | ||
markup.InitializeByProxy(host); | ||
} | ||
} | ||
|
||
private static void OnTextBlockLoaded(object sender, RoutedEventArgs e) | ||
{ | ||
if (sender is not TextBlock host) return; | ||
|
||
// There is actually be more elements than what's defined in the xaml; | ||
// these come from the whitespaces... and, they are fine. | ||
var markups = FlattenInlines(host) | ||
.SelectMany(ResponsiveExtension.GetAllInstancesFor); | ||
foreach (var markup in markups) | ||
{ | ||
markup.InitializeByProxy(host); | ||
} | ||
} | ||
|
||
private static IEnumerable<Inline> FlattenInlines(TextBlock tb) => FlattenInlines(tb.Inlines); | ||
private static IEnumerable<Inline> FlattenInlines(InlineCollection inlines) | ||
{ | ||
foreach (var inline in inlines) | ||
{ | ||
yield return inline; | ||
|
||
if (inline is Span span) | ||
{ | ||
foreach (var nested in FlattenInlines(span.Inlines)) | ||
{ | ||
yield return nested; | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.