Browse Source

Allow batch updating resources.

feature/resource-batch-update
Steven Kirk 6 years ago
parent
commit
704d929e30
  1. 4
      samples/ControlCatalog/MainView.xaml.cs
  2. 18
      src/Avalonia.Controls/Application.cs
  3. 14
      src/Avalonia.Styling/Controls/IResourceHost.cs
  4. 18
      src/Avalonia.Styling/StyledElement.cs

4
samples/ControlCatalog/MainView.xaml.cs

@ -35,6 +35,8 @@ namespace ControlCatalog
var themes = this.Find<ComboBox>("Themes"); var themes = this.Find<ComboBox>("Themes");
themes.SelectionChanged += (sender, e) => themes.SelectionChanged += (sender, e) =>
{ {
((IResourceHost)Application.Current).BeginBatchUpdate();
switch (themes.SelectedIndex) switch (themes.SelectedIndex)
{ {
case 0: case 0:
@ -50,6 +52,8 @@ namespace ControlCatalog
Application.Current.Styles[0] = App.DefaultDark; Application.Current.Styles[0] = App.DefaultDark;
break; break;
} }
((IResourceHost)Application.Current).EndBatchUpdate();
}; };
var decorations = this.Find<ComboBox>("Decorations"); var decorations = this.Find<ComboBox>("Decorations");

18
src/Avalonia.Controls/Application.cs

@ -43,6 +43,7 @@ namespace Avalonia
private Styles _styles; private Styles _styles;
private IResourceDictionary _resources; private IResourceDictionary _resources;
private bool _notifyingResourcesChanged; private bool _notifyingResourcesChanged;
private int _resourceBatchUpdate;
private Action<IReadOnlyList<IStyle>> _stylesAdded; private Action<IReadOnlyList<IStyle>> _stylesAdded;
private Action<IReadOnlyList<IStyle>> _stylesRemoved; private Action<IReadOnlyList<IStyle>> _stylesRemoved;
@ -199,9 +200,24 @@ namespace Avalonia
Styles.TryGetResource(key, out value); Styles.TryGetResource(key, out value);
} }
/// <inheritdoc/>
void IResourceHost.BeginBatchUpdate() => ++_resourceBatchUpdate;
/// <inheritdoc/>
void IResourceHost.EndBatchUpdate()
{
if (_resourceBatchUpdate > 0 && --_resourceBatchUpdate == 0)
{
ResourcesChanged?.Invoke(this, ResourcesChangedEventArgs.Empty);
}
}
void IResourceHost.NotifyHostedResourcesChanged(ResourcesChangedEventArgs e) void IResourceHost.NotifyHostedResourcesChanged(ResourcesChangedEventArgs e)
{ {
ResourcesChanged?.Invoke(this, e); if (_resourceBatchUpdate == 0)
{
ResourcesChanged?.Invoke(this, e);
}
} }
void IStyleHost.StylesAdded(IReadOnlyList<IStyle> styles) void IStyleHost.StylesAdded(IReadOnlyList<IStyle> styles)

14
src/Avalonia.Styling/Controls/IResourceHost.cs

@ -17,6 +17,20 @@ namespace Avalonia.Controls
/// </summary> /// </summary>
event EventHandler<ResourcesChangedEventArgs> ResourcesChanged; event EventHandler<ResourcesChangedEventArgs> ResourcesChanged;
/// <summary>
/// Begin batch updating resources hosted by this element.
/// </summary>
/// <remarks>
/// After calling this method, <see cref="ResourcesChanged"/> will not be raised until
/// <see cref="EndBatchUpdate"/> is called.
/// </remarks>
void BeginBatchUpdate();
/// <summary>
/// End batch updating resources hosted by this element.
/// </summary>
void EndBatchUpdate();
/// <summary> /// <summary>
/// Notifies the resource host that one or more of its hosted resources has changed. /// Notifies the resource host that one or more of its hosted resources has changed.
/// </summary> /// </summary>

18
src/Avalonia.Styling/StyledElement.cs

@ -67,6 +67,7 @@ namespace Avalonia
private List<IStyleInstance>? _appliedStyles; private List<IStyleInstance>? _appliedStyles;
private ITemplatedControl? _templatedParent; private ITemplatedControl? _templatedParent;
private bool _dataContextUpdating; private bool _dataContextUpdating;
private int _resourceBatchUpdate;
/// <summary> /// <summary>
/// Initializes static members of the <see cref="StyledElement"/> class. /// Initializes static members of the <see cref="StyledElement"/> class.
@ -371,6 +372,18 @@ namespace Avalonia
/// <inheritdoc/> /// <inheritdoc/>
void ILogical.NotifyResourcesChanged(ResourcesChangedEventArgs e) => NotifyResourcesChanged(e); void ILogical.NotifyResourcesChanged(ResourcesChangedEventArgs e) => NotifyResourcesChanged(e);
/// <inheritdoc/>
void IResourceHost.BeginBatchUpdate() => ++_resourceBatchUpdate;
/// <inheritdoc/>
void IResourceHost.EndBatchUpdate()
{
if (_resourceBatchUpdate > 0 && --_resourceBatchUpdate == 0)
{
NotifyResourcesChanged();
}
}
/// <inheritdoc/> /// <inheritdoc/>
void IResourceHost.NotifyHostedResourcesChanged(ResourcesChangedEventArgs e) => NotifyResourcesChanged(e); void IResourceHost.NotifyHostedResourcesChanged(ResourcesChangedEventArgs e) => NotifyResourcesChanged(e);
@ -799,6 +812,11 @@ namespace Avalonia
ResourcesChangedEventArgs? e = null, ResourcesChangedEventArgs? e = null,
bool propagate = true) bool propagate = true)
{ {
if (_resourceBatchUpdate > 0)
{
return;
}
if (ResourcesChanged is object) if (ResourcesChanged is object)
{ {
e ??= ResourcesChangedEventArgs.Empty; e ??= ResourcesChangedEventArgs.Empty;

Loading…
Cancel
Save