Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Resources and Style to Popup #1351

Merged
merged 38 commits into from
Oct 22, 2023
Merged
Changes from 14 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d57ff37
Add Resources and Style to Popup
cat0363 Aug 17, 2023
d9cbd1c
Fix of MergedStyle constructor arguments
cat0363 Aug 21, 2023
552c2de
Merge branch 'main' into Issue-1345
cat0363 Aug 21, 2023
6542bb1
Merge branch 'main' into Issue-1345
cat0363 Aug 22, 2023
2175e47
Merge branch 'main' into Issue-1345
cat0363 Aug 25, 2023
5438a14
Merge branch 'main' into Issue-1345
brminnick Aug 27, 2023
da98d47
Merge branch 'main' into Issue-1345
cat0363 Aug 27, 2023
49bfbd1
Merge branch 'main' into Issue-1345
cat0363 Aug 28, 2023
7210afc
Merge branch 'main' into Issue-1345
cat0363 Aug 30, 2023
6e448b1
Merge branch 'main' into Issue-1345
cat0363 Sep 7, 2023
bc350ba
Merge branch 'main' into Issue-1345
brminnick Sep 7, 2023
23c6af6
Merge branch 'main' into Issue-1345
cat0363 Sep 7, 2023
0b16d1c
Merge branch 'main' into Issue-1345
cat0363 Sep 8, 2023
657fb24
Merge branch 'main' into Issue-1345
cat0363 Sep 10, 2023
c89cff9
Update src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs
cat0363 Sep 10, 2023
ea19278
Update src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs
cat0363 Sep 10, 2023
a913656
Update src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs
cat0363 Sep 10, 2023
d5c0876
Update src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs
cat0363 Sep 10, 2023
791b34d
Update src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs
cat0363 Sep 10, 2023
3ae9e4d
Add sample page and correct MergedStyle argument
cat0363 Sep 12, 2023
d350bb7
Update samples/CommunityToolkit.Maui.Sample/CommunityToolkit.Maui.Sam…
cat0363 Sep 12, 2023
aebbbc3
Merge branch 'main' into Issue-1345
cat0363 Sep 14, 2023
f79779b
Update Sample App Naming
brminnick Sep 23, 2023
0f71139
Update Popup.shared.cs
brminnick Sep 23, 2023
d9b7e5a
Small performance optimization
brminnick Sep 23, 2023
d7c14da
Merge branch 'main' into Issue-1345
brminnick Sep 23, 2023
573f803
Add support for Dynamic style inheritance
cat0363 Sep 25, 2023
bbf7103
Modifying the sample and adding unsubscribe
cat0363 Sep 26, 2023
3976034
Update samples/CommunityToolkit.Maui.Sample/ViewModels/Views/ViewsGal…
cat0363 Sep 26, 2023
05fbe61
Added XML docs to the popup samples
bijington Sep 29, 2023
a086fb8
Update docs PR syntax
brminnick Sep 29, 2023
bb0e931
`dotnet format`
brminnick Sep 29, 2023
e7e1e8b
Merge branch 'main' into Issue-1345
brminnick Sep 29, 2023
5ba07cf
Merge branch 'main' into Issue-1345
cat0363 Oct 4, 2023
4281b89
Merge branch 'main' into Issue-1345
bijington Oct 14, 2023
14c7e9b
Merge branch 'main' into Issue-1345
brminnick Oct 21, 2023
a0f21d3
Merge branch 'main' into Issue-1345
brminnick Oct 21, 2023
44d7e24
Merge branch 'main' into Issue-1345
brminnick Oct 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 119 additions & 2 deletions src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
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;

/// <summary>
/// Represents a small View that pops up at front the Page. Implements <see cref="IPopup"/>.
/// </summary>
[ContentProperty(nameof(Content))]
public partial class Popup : Element, IPopup, IWindowController, IPropertyPropagationController
public partial class Popup : Element, IPopup, IWindowController, IPropertyPropagationController, IResourcesProvider, IStyleSelectable
{
/// <summary>
/// Backing BindableProperty for the <see cref="Content"/> property.
Expand Down Expand Up @@ -40,13 +42,21 @@ public partial class Popup : Element, IPopup, IWindowController, IPropertyPropag
/// </summary>
public static readonly BindableProperty HorizontalOptionsProperty = BindableProperty.Create(nameof(HorizontalOptions), typeof(LayoutAlignment), typeof(Popup), LayoutAlignment.Center);

/// <summary>
/// Backing BindableProperty for the <see cref="Style" /> property.
/// </summary>
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<Popup>> platformConfigurationRegistry;
readonly MergedStyle mergedStyle;

TaskCompletionSource popupDismissedTaskCompletionSource = new();
TaskCompletionSource<object?> resultTaskCompletionSource = new();
Window? window;
ResourceDictionary? resources;
bool IResourcesProvider.IsResourcesCreated => resources != null;
cat0363 marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Instantiates a new instance of <see cref="Popup"/>.
Expand All @@ -55,7 +65,8 @@ public Popup()
{
platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<Popup>>(() => new(this));

VerticalOptions = HorizontalOptions = LayoutAlignment.Center;
bijington marked this conversation as resolved.
Show resolved Hide resolved
var type = GetType();
mergedStyle = new MergedStyle(type == typeof(Popup) ? type : type.BaseType, this);
}

/// <summary>
Expand Down Expand Up @@ -153,6 +164,15 @@ public bool CanBeDismissedByTappingOutsideOfPopup
set => SetValue(CanBeDismissedByTappingOutsideOfPopupProperty, value);
}

/// <summary>
/// Gets or sets the <see cref="Style"/> of the Popup.
/// </summary>
public Style Style
{
get { return (Style)GetValue(StyleProperty); }
set { SetValue(StyleProperty, value); }
}

/// <summary>
/// Gets or sets the <see cref="View"/> anchor.
/// </summary>
Expand All @@ -179,6 +199,56 @@ public Window? Window
}
}

/// <summary>
/// Property that represent Resources of Popup.
/// </summary>
public ResourceDictionary Resources
{
get
{
if (resources != null)
cat0363 marked this conversation as resolved.
Show resolved Hide resolved
{
return resources;
}

resources = new ResourceDictionary();
((IResourceDictionary)resources).ValuesChanged += OnResourcesChanged;
return resources;
}
set
{
if (resources == value)
{
return;
}

OnPropertyChanging();
if (resources != null)
cat0363 marked this conversation as resolved.
Show resolved Hide resolved
{
((IResourceDictionary)resources).ValuesChanged -= OnResourcesChanged;
}

resources = value;
OnResourcesChanged(value);
if (resources != null)
cat0363 marked this conversation as resolved.
Show resolved Hide resolved
{
((IResourceDictionary)resources).ValuesChanged += OnResourcesChanged;
}

OnPropertyChanged();
}
}

/// <summary>
/// Property that represent Style Class of Popup.
/// </summary>
[System.ComponentModel.TypeConverter(typeof(ListStringTypeConverter))]
public IList<string> StyleClass
{
get { return mergedStyle.StyleClass; }
set { mergedStyle.StyleClass = value; }
}

/// <summary>
/// Gets or sets the result that will return when user taps outside of the Popup.
/// </summary>
Expand Down Expand Up @@ -301,4 +371,51 @@ IReadOnlyList<IVisualTreeElement> IVisualTreeElement.GetVisualChildren() =>
Content is null
? Enumerable.Empty<IVisualTreeElement>().ToList()
: new List<IVisualTreeElement> { Content };

internal override void OnParentResourcesChanged(IEnumerable<KeyValuePair<string, object>> values)
{
if (values == null)
cat0363 marked this conversation as resolved.
Show resolved Hide resolved
{
return;
}

if (!((IResourcesProvider)this).IsResourcesCreated || Resources.Count == 0)
{
base.OnParentResourcesChanged(values);
return;
}

var innerKeys = new HashSet<string>(StringComparer.Ordinal);
var changedResources = new List<KeyValuePair<string, object>>();
foreach (KeyValuePair<string, object> c in Resources)
{
innerKeys.Add(c.Key);
}

foreach (KeyValuePair<string, object> 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<Style>;
var parentStyle = value.Value as List<Style>;
if (innerStyle is not null)
{
var mergedClassStyles = new List<Style>(innerStyle);
if (parentStyle is not null)
{
mergedClassStyles.AddRange(parentStyle);
}
changedResources.Add(new KeyValuePair<string, object>(value.Key, mergedClassStyles));
}
}
}
if (changedResources.Count != 0)
{
OnResourcesChanged(changedResources);
}
}
}