Skip to content

Commit

Permalink
Simplify ResourceAccessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Mar 9, 2021
1 parent ee4def8 commit 6a332f0
Show file tree
Hide file tree
Showing 83 changed files with 37 additions and 259 deletions.
20 changes: 0 additions & 20 deletions ModernWpf.Controls/Common/ResourceHelper.cs

This file was deleted.

3 changes: 2 additions & 1 deletion ModernWpf.Controls/NavigationView/NavigationView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public partial class NavigationView : ContentControl, IControlProtected

static readonly Size c_infSize = new Size(double.PositiveInfinity, double.PositiveInfinity);

private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(NavigationView));

/*
~NavigationView()
{
Expand Down Expand Up @@ -214,7 +216,6 @@ void UnhookEventsAndClearFields(bool isFromDestructor = false)

static NavigationView()
{
ResourceHelper.Initialize();
DefaultStyleKeyProperty.OverrideMetadata(typeof(NavigationView), new FrameworkPropertyMetadata(typeof(NavigationView)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public class NavigationViewItemAutomationPeer :
ISelectionItemProvider,
IExpandCollapseProvider
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(NavigationView));

public NavigationViewItemAutomationPeer(NavigationViewItem owner) :
base(owner)
{
ResourceHelper.Initialize();
}

protected override string GetNameCore()
Expand Down
3 changes: 2 additions & 1 deletion ModernWpf.Controls/NumberBox/NumberBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public partial class NumberBox : Control
const double c_popupShadowDepth = 16.0;
const string c_numberBoxPopupShadowDepthName = "NumberBoxPopupShadowDepth";

private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(NumberBox));

static NumberBox()
{
ResourceHelper.Initialize();
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumberBox), new FrameworkPropertyMetadata(typeof(NumberBox)));
}

Expand Down
3 changes: 2 additions & 1 deletion ModernWpf.Controls/PersonPicture/PersonPicture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ namespace ModernWpf.Controls
{
public partial class PersonPicture : Control
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(PersonPicture));

static PersonPicture()
{
ResourceHelper.Initialize();
DefaultStyleKeyProperty.OverrideMetadata(typeof(PersonPicture), new FrameworkPropertyMetadata(typeof(PersonPicture)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ namespace ModernWpf.Automation.Peers
{
public class ProgressRingAutomationPeer : FrameworkElementAutomationPeer
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(ProgressRing));

public ProgressRingAutomationPeer(ProgressRing owner) : base(owner)
{
ResourceHelper.Initialize();
}

protected override string GetClassNameCore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public class RatingControlAutomationPeer :
IValueProvider,
IRangeValueProvider
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(RatingControl));

public RatingControlAutomationPeer(RatingControl owner)
: base(owner)
{
ResourceHelper.Initialize();
}

protected override string GetLocalizedControlTypeCore()
Expand Down
3 changes: 2 additions & 1 deletion ModernWpf.Controls/SplitButton/SplitButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ namespace ModernWpf.Controls
{
public class SplitButton : ContentControl, ICommandSource
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(SplitButton));

static SplitButton()
{
ResourceHelper.Initialize();
DefaultStyleKeyProperty.OverrideMetadata(typeof(SplitButton), new FrameworkPropertyMetadata(typeof(SplitButton)));
}

Expand Down
168 changes: 15 additions & 153 deletions ModernWpf/Common/ResourceAccessor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Collections.Generic;
using System.Reflection;
using System;
using System.Resources;

namespace ModernWpf
{
internal static class ResourceAccessor
internal class ResourceAccessor
{
#region Resource Keys

Expand Down Expand Up @@ -161,163 +160,26 @@ internal static class ResourceAccessor

#endregion

#region RESX Specific workarounds
private readonly Type _controlType;
private ResourceManager _resourceManager;

internal static Assembly modernWpfAssembly = typeof(ResourceAccessor).Assembly;
// Would be set by the ResourceHelper (present in the ModernWpf.Controls assembly)
internal static Assembly modernWpfControlsAssembly;

private const string ProgressBarResourcesName = "ModernWpf.ProgressBar.Strings.Resources";
private const string TextContextMenuResourcesName = "ModernWpf.Controls.TextContextMenu.Strings.Resources";

private const string NavigationViewResourcesName = "ModernWpf.Controls.NavigationView.Strings.Resources";
private const string NumberBoxResourcesName = "ModernWpf.Controls.NumberBox.Strings.Resources";
private const string PersonPictureResourcesName = "ModernWpf.Controls.PersonPicture.Strings.Resources";
private const string ProgressRingResourcesName = "ModernWpf.Controls.ProgressRing.Strings.Resources";
private const string RatingControlResourcesName = "ModernWpf.Controls.RatingControl.Strings.Resources";
private const string SplitButtonResourcesName = "ModernWpf.Controls.SplitButton.Strings.Resources";

/// <summary>
/// Used to map each resource keys to their respective <strong>*.resources files</strong> in the <strong>ModernWpf</strong> assembly.
/// </summary>
/// <remarks>
/// Update this map every time the resources are modified.
/// </remarks>
private static readonly Dictionary<string, string> resourceMapsM = new()
{
// ProgressBar resources
{ SR_ProgressBarErrorStatus, ProgressBarResourcesName },
{ SR_ProgressBarIndeterminateStatus, ProgressBarResourcesName },
{ SR_ProgressBarPausedStatus, ProgressBarResourcesName },

// TextContextMenu resources
{ SR_ProofingMenuItemLabel, TextContextMenuResourcesName },
{ SR_TextCommandDescriptionBold, TextContextMenuResourcesName },
{ SR_TextCommandDescriptionCopy, TextContextMenuResourcesName },
{ SR_TextCommandDescriptionCut, TextContextMenuResourcesName },
{ SR_TextCommandDescriptionItalic, TextContextMenuResourcesName },
{ SR_TextCommandDescriptionPaste, TextContextMenuResourcesName },
{ SR_TextCommandDescriptionRedo, TextContextMenuResourcesName },
{ SR_TextCommandDescriptionSelectAll, TextContextMenuResourcesName },
{ SR_TextCommandDescriptionUnderline, TextContextMenuResourcesName },
{ SR_TextCommandDescriptionUndo, TextContextMenuResourcesName },
{ SR_TextCommandKeyboardAcceleratorKeyBold, TextContextMenuResourcesName },
{ SR_TextCommandKeyboardAcceleratorKeyCopy, TextContextMenuResourcesName },
{ SR_TextCommandKeyboardAcceleratorKeyCut, TextContextMenuResourcesName },
{ SR_TextCommandKeyboardAcceleratorKeyItalic, TextContextMenuResourcesName },
{ SR_TextCommandKeyboardAcceleratorKeyPaste, TextContextMenuResourcesName },
{ SR_TextCommandKeyboardAcceleratorKeyRedo, TextContextMenuResourcesName },
{ SR_TextCommandKeyboardAcceleratorKeySelectAll, TextContextMenuResourcesName },
{ SR_TextCommandKeyboardAcceleratorKeyUnderline, TextContextMenuResourcesName },
{ SR_TextCommandKeyboardAcceleratorKeyUndo, TextContextMenuResourcesName },
{ SR_TextCommandLabelBold, TextContextMenuResourcesName },
{ SR_TextCommandLabelCopy, TextContextMenuResourcesName },
{ SR_TextCommandLabelCut, TextContextMenuResourcesName },
{ SR_TextCommandLabelItalic, TextContextMenuResourcesName },
{ SR_TextCommandLabelPaste, TextContextMenuResourcesName },
{ SR_TextCommandLabelRedo, TextContextMenuResourcesName },
{ SR_TextCommandLabelSelectAll, TextContextMenuResourcesName },
{ SR_TextCommandLabelUnderline, TextContextMenuResourcesName },
{ SR_TextCommandLabelUndo, TextContextMenuResourcesName },
};

/// <summary>
/// Used to map each resource keys to their respective <strong>*.resources files</strong> in the <strong>ModernWpf.Controls</strong> assembly.
/// </summary>
/// <remarks>
/// Update this map every time the resources are modified.
/// </remarks>
private static readonly Dictionary<string, string> resourceMapsMC = new()
public ResourceAccessor(Type controlType)
{
// NavigationView resources
{ SR_NavigationButtonClosedName, NavigationViewResourcesName },
{ SR_NavigationButtonOpenName, NavigationViewResourcesName },
{ SR_NavigationViewItemDefaultControlName, NavigationViewResourcesName },
{ SR_SettingsButtonName, NavigationViewResourcesName },
{ SR_NavigationViewSearchButtonName, NavigationViewResourcesName },
{ SR_NavigationBackButtonName, NavigationViewResourcesName },
{ SR_NavigationBackButtonToolTip, NavigationViewResourcesName },
{ SR_NavigationCloseButtonName, NavigationViewResourcesName },
{ SR_NavigationOverflowButtonName, NavigationViewResourcesName },
{ SR_NavigationOverflowButtonText, NavigationViewResourcesName },
{ SR_NavigationOverflowButtonToolTip, NavigationViewResourcesName },

// NumberBox resources
{ SR_NumberBoxDownSpinButtonName, NumberBoxResourcesName },
{ SR_NumberBoxUpSpinButtonName, NumberBoxResourcesName },

// PersonPicture resources
{ SR_BadgeItemPlural1, PersonPictureResourcesName },
{ SR_BadgeItemPlural2, PersonPictureResourcesName },
{ SR_BadgeItemPlural3, PersonPictureResourcesName },
{ SR_BadgeItemPlural4, PersonPictureResourcesName },
{ SR_BadgeItemPlural5, PersonPictureResourcesName },
{ SR_BadgeItemPlural6, PersonPictureResourcesName },
{ SR_BadgeItemPlural7, PersonPictureResourcesName },
{ SR_BadgeItemSingular, PersonPictureResourcesName },
{ SR_BadgeItemTextOverride, PersonPictureResourcesName },
{ SR_BadgeIcon, PersonPictureResourcesName },
{ SR_BadgeIconTextOverride, PersonPictureResourcesName },
{ SR_PersonName, PersonPictureResourcesName },
{ SR_GroupName, PersonPictureResourcesName },

// ProgressRing resources
{ SR_ProgressRingIndeterminateStatus, ProgressRingResourcesName },
{ SR_ProgressRingName, ProgressRingResourcesName },

// RatingControl resources
{ SR_BasicRatingString, RatingControlResourcesName },
{ SR_CommunityRatingString, RatingControlResourcesName },
{ SR_RatingsControlName, RatingControlResourcesName },
{ SR_RatingControlName, RatingControlResourcesName },
{ SR_RatingUnset, RatingControlResourcesName },
{ SR_RatingLocalizedControlType, RatingControlResourcesName },

// SplitButton resources
{ SR_SplitButtonSecondaryButtonName, SplitButtonResourcesName },
};

/// <summary>
/// Used to cache <see cref="ResourceManager"/> instances associated with a particular
/// <strong>*.resources file</strong> (generated from a *.resx file)
/// </summary>
private static readonly Dictionary<string, ResourceManager> resourceManagers = new();

#endregion
_controlType = controlType ?? throw new ArgumentNullException(nameof(controlType));
}

public static string GetLocalizedStringResource(string resourceName)
public string GetLocalizedStringResource(string resourceName)
{
string _resourcesFilePath;

string GetStringResource(string resourcesFilePath, Assembly assembly)
{
if (!resourceManagers.TryGetValue(resourcesFilePath, out ResourceManager resourceManager))
{
resourceManager = new(resourcesFilePath, assembly);
resourceManagers.Add(resourcesFilePath, resourceManager);
}

if (resourceManager != null)
{
return resourceManager.GetString(resourceName);
}

return string.Empty;
}

// Tries to get the string resource from the ModernWpf assembly
if (resourceMapsM.TryGetValue(resourceName, out _resourcesFilePath))
{
return GetStringResource(_resourcesFilePath, modernWpfAssembly);
}

// Tries to get the string resource from the ModernWpf.Controls assembly
if (resourceMapsMC.TryGetValue(resourceName, out _resourcesFilePath))
if (_resourceManager is null)
{
return GetStringResource(_resourcesFilePath, modernWpfControlsAssembly);
var assembly = _controlType.Assembly;
var assemblyName = assembly.GetName().Name;
var controlName = _controlType.Name;
var baseName = $"{assemblyName}.{controlName}.Strings.Resources";
_resourceManager = new ResourceManager(baseName, assembly);
}

return string.Empty;
return _resourceManager.GetString(resourceName);
}
}
}
2 changes: 2 additions & 0 deletions ModernWpf/ProgressBar/ProgressBarAutomationPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace ModernWpf.Automation.Peers
/// </summary>
public class ProgressBarAutomationPeer : RangeBaseAutomationPeer, IRangeValueProvider
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(ProgressBar));

/// <summary>
/// Initializes a new instance of the ProgressBarAutomationPeer class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace ModernWpf.Controls
/// </summary>
public class TextContextMenu : ContextMenu
{
private static readonly ResourceAccessor ResourceAccessor = new ResourceAccessor(typeof(TextContextMenu));

private static readonly CommandBinding _selectAllBinding;
private static readonly CommandBinding _undoBinding;
private static readonly CommandBinding _redoBinding;
Expand Down
38 changes: 0 additions & 38 deletions tools/WinUIResourcesConverter/CodeGen.cs

This file was deleted.

5 changes: 0 additions & 5 deletions tools/WinUIResourcesConverter/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@
</ui:SimpleStackPanel>
<ui:ProgressRing x:Name="ProgressRing1" Width="100" Height="100" />
<ui:ProgressBar x:Name="ProgressBar1" VerticalAlignment="Top" />
<Expander IsExpanded="False" Header="Generated Code" Background="{DynamicResource NavigationViewExpandedPaneBackground}"
Padding="10" VerticalAlignment="Bottom" ExpandDirection="Up">
<TextBox x:Name="TbCodeGen" VerticalScrollBarVisibility="Auto" AcceptsReturn="True"
ui:ControlHelper.Description="Append these to the ModernWpf\Common\ResourceAccessor.cs in the proper places" />
</Expander>
</Grid>
<ui:SplitView.Pane>
<Grid>
Expand Down
Loading

0 comments on commit 6a332f0

Please sign in to comment.