From d57ff378e5d7b6ec1d9db175bbc99232ac92a32b Mon Sep 17 00:00:00 2001 From: cat0363 Date: Thu, 17 Aug 2023 14:30:23 +0900 Subject: [PATCH 01/18] Add Resources and Style to Popup --- .../Views/Popup/Popup.shared.cs | 120 +++++++++++++++++- 1 file changed, 118 insertions(+), 2 deletions(-) diff --git a/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs b/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs index d75929ce0..70ad906ac 100644 --- a/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs +++ b/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs @@ -1,6 +1,8 @@ using CommunityToolkit.Maui.Core; using Microsoft.Maui.Controls.Internals; +using Microsoft.Maui.Controls.StyleSheets; using LayoutAlignment = Microsoft.Maui.Primitives.LayoutAlignment; +using Style = Microsoft.Maui.Controls.Style; namespace CommunityToolkit.Maui.Views; @@ -8,7 +10,7 @@ namespace CommunityToolkit.Maui.Views; /// Represents a small View that pops up at front the Page. Implements . /// [ContentProperty(nameof(Content))] -public partial class Popup : Element, IPopup, IWindowController, IPropertyPropagationController +public partial class Popup : Element, IPopup, IWindowController, IPropertyPropagationController, IResourcesProvider, IStyleSelectable { /// /// Backing BindableProperty for the property. @@ -40,13 +42,21 @@ public partial class Popup : Element, IPopup, IWindowController, IPropertyPropag /// public static readonly BindableProperty HorizontalOptionsProperty = BindableProperty.Create(nameof(HorizontalOptions), typeof(LayoutAlignment), typeof(Popup), LayoutAlignment.Center); + /// + /// Backing BindableProperty for the property. + /// + public static readonly BindableProperty StyleProperty = BindableProperty.Create(nameof(Style), typeof(Style), typeof(Popup), default(Style), propertyChanged: (bindable, oldValue, newValue) => ((Popup)bindable).mergedStyle.Style = (Style)newValue); + readonly WeakEventManager dismissWeakEventManager = new(); readonly WeakEventManager openedWeakEventManager = new(); readonly Lazy> platformConfigurationRegistry; + readonly MergedStyle mergedStyle; TaskCompletionSource popupDismissedTaskCompletionSource = new(); TaskCompletionSource resultTaskCompletionSource = new(); Window? window; + ResourceDictionary? resources; + bool IResourcesProvider.IsResourcesCreated => resources != null; /// /// Instantiates a new instance of . @@ -55,7 +65,7 @@ public Popup() { platformConfigurationRegistry = new Lazy>(() => new(this)); - VerticalOptions = HorizontalOptions = LayoutAlignment.Center; + mergedStyle = new MergedStyle(GetType().BaseType, this); } /// @@ -153,6 +163,15 @@ public bool CanBeDismissedByTappingOutsideOfPopup set => SetValue(CanBeDismissedByTappingOutsideOfPopupProperty, value); } + /// + /// Gets or sets the of the Popup. + /// + public Style Style + { + get { return (Style)GetValue(StyleProperty); } + set { SetValue(StyleProperty, value); } + } + /// /// Gets or sets the anchor. /// @@ -179,6 +198,56 @@ public Window? Window } } + /// + /// Property that represent Resources of Popup. + /// + public ResourceDictionary Resources + { + get + { + if (resources != null) + { + return resources; + } + + resources = new ResourceDictionary(); + ((IResourceDictionary)resources).ValuesChanged += OnResourcesChanged; + return resources; + } + set + { + if (resources == value) + { + return; + } + + OnPropertyChanging(); + if (resources != null) + { + ((IResourceDictionary)resources).ValuesChanged -= OnResourcesChanged; + } + + resources = value; + OnResourcesChanged(value); + if (resources != null) + { + ((IResourceDictionary)resources).ValuesChanged += OnResourcesChanged; + } + + OnPropertyChanged(); + } + } + + /// + /// Property that represent Style Class of Popup. + /// + [System.ComponentModel.TypeConverter(typeof(ListStringTypeConverter))] + public IList StyleClass + { + get { return mergedStyle.StyleClass; } + set { mergedStyle.StyleClass = value; } + } + /// /// Gets or sets the result that will return when user taps outside of the Popup. /// @@ -301,4 +370,51 @@ IReadOnlyList IVisualTreeElement.GetVisualChildren() => Content is null ? Enumerable.Empty().ToList() : new List { Content }; + + internal override void OnParentResourcesChanged(IEnumerable> values) + { + if (values == null) + { + return; + } + + if (!((IResourcesProvider)this).IsResourcesCreated || Resources.Count == 0) + { + base.OnParentResourcesChanged(values); + return; + } + + var innerKeys = new HashSet(StringComparer.Ordinal); + var changedResources = new List>(); + foreach (KeyValuePair c in Resources) + { + innerKeys.Add(c.Key); + } + + foreach (KeyValuePair value in values) + { + if (innerKeys.Add(value.Key)) + { + changedResources.Add(value); + } + else if (value.Key.StartsWith(Style.StyleClassPrefix, StringComparison.Ordinal)) + { + var innerStyle = Resources[value.Key] as List + + + + + + diff --git a/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs b/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs index 65543b4fd..fa38acc80 100644 --- a/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs +++ b/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs @@ -128,6 +128,7 @@ public partial class AppShell : Shell CreateViewModelMapping(), CreateViewModelMapping(), CreateViewModelMapping(), + CreateViewModelMapping(), }); public AppShell() => InitializeComponent(); diff --git a/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj b/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj index 549161162..deaa785f5 100644 --- a/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj +++ b/samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sample.csproj @@ -76,6 +76,51 @@ + + + ApplyToDerivedTypesPopup.xaml + + + StyleInheritancePopup.xaml + + + ExplicitStylePopup.xaml + + + ImplicitStylePopup.xaml + + + DynamicStylePopup.xaml + + + StyleClassPopup.xaml + + + + + + MSBuild:Compile + + + MSBuild:Compile + + + MSBuild:Compile + + + MSBuild:Compile + + + MSBuild:Compile + + + MSBuild:Compile + + + MSBuild:Compile + + + android-arm;android-arm64;android-x86;android-x64 diff --git a/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs b/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs index fe6d14c15..daa10889a 100644 --- a/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs +++ b/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs @@ -191,6 +191,7 @@ static void RegisterViewsAndViewModels(in IServiceCollection services) services.AddTransientWithShellRoute(); services.AddTransientWithShellRoute(); services.AddTransientWithShellRoute(); + services.AddTransientWithShellRoute(); // Add Popups services.AddTransient(); diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupStylePage.xaml b/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupStylePage.xaml new file mode 100644 index 000000000..f0cead110 --- /dev/null +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupStylePage.xaml @@ -0,0 +1,46 @@ + + + + + + [ContentProperty(nameof(Content))] -public partial class Popup : Element, IPopup, IWindowController, IPropertyPropagationController, IResourcesProvider, IStyleSelectable +public partial class Popup : Element, IPopup, IWindowController, IPropertyPropagationController, IResourcesProvider, IStyleSelectable, IStyleElement { /// /// Backing BindableProperty for the property. From bbf7103f49af6554203f9b8b88fe69a15f14de95 Mon Sep 17 00:00:00 2001 From: cat0363 Date: Tue, 26 Sep 2023 16:24:41 +0900 Subject: [PATCH 14/18] Modifying the sample and adding unsubscribe --- samples/CommunityToolkit.Maui.Sample/App.xaml | 4 ++-- .../CommunityToolkit.Maui.Sample.csproj | 12 ------------ .../Pages/Views/Popup/StylePopupPage.xaml | 14 +++++++------- .../ViewModels/Views/Popup/StylePopupViewModel.cs | 14 +++++++------- .../Views/Popups/ExplicitStylePopup.xaml | 2 +- .../Views/Popups/ImplicitStylePopup.xaml | 2 +- .../Views/Popup/Popup.shared.cs | 4 ++++ 7 files changed, 22 insertions(+), 30 deletions(-) diff --git a/samples/CommunityToolkit.Maui.Sample/App.xaml b/samples/CommunityToolkit.Maui.Sample/App.xaml index bac0b3548..a51300737 100644 --- a/samples/CommunityToolkit.Maui.Sample/App.xaml +++ b/samples/CommunityToolkit.Maui.Sample/App.xaml @@ -9,7 +9,7 @@ - -