-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve flexibility with build-in controls localization (#13773)
* Avoid hardcoding strings in DateTimePicker cs files * Add invariant resources * Implement ResourceProvider for flexibility of localization with custom resource provider * Fix these weird tests * Seal some ResourceDictionary extension points * Replace "Locale" with "String"
- Loading branch information
Showing
21 changed files
with
268 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System; | ||
using Avalonia.Styling; | ||
|
||
namespace Avalonia.Controls; | ||
|
||
/// <summary> | ||
/// Base implementation for IResourceProvider interface. | ||
/// Includes Owner property management. | ||
/// </summary> | ||
public abstract class ResourceProvider : IResourceProvider | ||
{ | ||
private IResourceHost? _owner; | ||
|
||
public ResourceProvider() | ||
{ | ||
} | ||
|
||
public ResourceProvider(IResourceHost owner) | ||
{ | ||
_owner = owner; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public abstract bool HasResources { get; } | ||
|
||
/// <inheritdoc/> | ||
public abstract bool TryGetResource(object key, ThemeVariant? theme, out object? value); | ||
|
||
/// <inheritdoc/> | ||
public IResourceHost? Owner | ||
{ | ||
get => _owner; | ||
private set | ||
{ | ||
if (_owner != value) | ||
{ | ||
_owner = value; | ||
OwnerChanged?.Invoke(this, EventArgs.Empty); | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public event EventHandler? OwnerChanged; | ||
|
||
protected void RaiseResourcesChanged() | ||
{ | ||
Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); | ||
} | ||
|
||
/// <summary> | ||
/// Handles when owner was added. | ||
/// Base method implementation raises <see cref="IResourceHost.NotifyHostedResourcesChanged"/>, if this provider has any resources. | ||
/// </summary> | ||
/// <param name="owner">New owner.</param> | ||
protected virtual void OnAddOwner(IResourceHost owner) | ||
{ | ||
if (HasResources) | ||
{ | ||
owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Handles when owner was removed. | ||
/// Base method implementation raises <see cref="IResourceHost.NotifyHostedResourcesChanged"/>, if this provider has any resources. | ||
/// </summary> | ||
/// <param name="owner">Old owner.</param> | ||
protected virtual void OnRemoveOwner(IResourceHost owner) | ||
{ | ||
if (HasResources) | ||
{ | ||
owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); | ||
} | ||
} | ||
|
||
void IResourceProvider.AddOwner(IResourceHost owner) | ||
{ | ||
owner = owner ?? throw new ArgumentNullException(nameof(owner)); | ||
|
||
if (Owner != null) | ||
{ | ||
throw new InvalidOperationException("The ResourceDictionary already has a parent."); | ||
} | ||
|
||
Owner = owner; | ||
|
||
OnAddOwner(owner); | ||
} | ||
|
||
void IResourceProvider.RemoveOwner(IResourceHost owner) | ||
{ | ||
owner = owner ?? throw new ArgumentNullException(nameof(owner)); | ||
|
||
if (Owner == owner) | ||
{ | ||
Owner = null; | ||
|
||
OnRemoveOwner(owner); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.