Skip to content

Commit 18457a3

Browse files
committed
Add option to not cache resolved tooltip labels (if they may change dynamically)
1 parent 71f939a commit 18457a3

11 files changed

Lines changed: 58 additions & 19 deletions

MonkeyLoader.Resonite.Integration/Locale/General/de.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
"MonkeyLoader.GamePacks.Resonite.Tooltip.ReferenceProxySource": "Greif eine Referenz zu {target}.",
2525

26-
"MonkeyLoader.GamePacks.Resonite.Tooltip.SlotRecord.Generic": "Greif eine Referenz zu diesem Slot, oder ziehe andere Slots hierher, um sie unter diesem einzuhängen, wobei ihre globale Position erhalten bleibt.",
26+
"MonkeyLoader.GamePacks.Resonite.Tooltip.SlotRecord.Generic": "Greif eine Referenz zum Slot {target}, oder ziehe andere Slots hierher, um sie unter diesem einzuhängen, wobei ihre globale Position erhalten bleibt.",
2727
"MonkeyLoader.GamePacks.Resonite.Tooltip.SlotRecord.Inspector": "Öffnet diesen Slot im Inspektor, greif eine Referenz zu ihm, oder ziehe andere Slots hierher, um sie unter diesem einzuhängen, wobei ihre globale Position erhalten bleibt."
2828
}
2929
}

MonkeyLoader.Resonite.Integration/Locale/General/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
"MonkeyLoader.GamePacks.Resonite.Tooltip.ReferenceProxySource": "Grab a reference to {target}.",
2525

26-
"MonkeyLoader.GamePacks.Resonite.Tooltip.SlotRecord.Generic": "Grab a reference to this slot, or drag'n'drop other slots onto here to parent them to it while preserving their global position.",
26+
"MonkeyLoader.GamePacks.Resonite.Tooltip.SlotRecord.Generic": "Grab a reference to the slot {target}, or drag'n'drop other slots onto here to parent them to it while preserving their global position.",
2727
"MonkeyLoader.GamePacks.Resonite.Tooltip.SlotRecord.Inspector": "Opens this slot in the inspector, grab a reference to it, or drag'n'drop other slots onto here to parent them to this slot while preserving their global position."
2828
}
2929
}

MonkeyLoader.Resonite.Integration/LocaleExtensions.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,20 @@ public static LocaleString AsModLocaleKey(this string key, IIdentifiable identif
7777
=> key.AsLocaleKey(format, arguments.AddModIndicator(identifiable));
7878

7979
/// <param name="continuous">Whether to assign the locale message once or continuously.</param>
80-
/// <param name="arguments">The arguments used to format the locale message.</param>
80+
/// <param name="arguments">
81+
/// The arguments used to format the locale message.<br/>
82+
/// Note that <see cref="ModLocaleStringIndicatorArgumentName">mod indicator</see> will be added to this.
83+
/// </param>
8184
/// <inheritdoc cref="AsModLocaleKey(string, IIdentifiable, string, object)"/>
8285
public static LocaleString AsModLocaleKey(this string key, IIdentifiable identifiable, bool continuous, Dictionary<string, object>? arguments = null)
8386
=> key.AsLocaleKey(continuous, arguments.AddModIndicator(identifiable));
8487

8588
/// <param name="format">An additional format string to insert the locale message into.</param>
8689
/// <param name="continuous">Whether to assign the locale message once or continuously.</param>
87-
/// <param name="arguments">The arguments used to format the locale message.</param>
90+
/// <param name="arguments">
91+
/// The arguments used to format the locale message.<br/>
92+
/// Note that <see cref="ModLocaleStringIndicatorArgumentName">mod indicator</see> will be added to this.
93+
/// </param>
8894
/// <inheritdoc cref="AsModLocaleKey(string, IIdentifiable, string, object)"/>
8995
public static LocaleString AsModLocaleKey(this string key, IIdentifiable identifiable, string? format = null, bool continuous = true, Dictionary<string, object>? arguments = null)
9096
=> key.AsLocaleKey(format!, continuous, arguments.AddModIndicator(identifiable));
@@ -238,14 +244,20 @@ public static LocaleString GetLocaleString(this IIdentifiable identifiable, stri
238244
=> identifiable.GetLocaleKey(key).AsModLocaleKey(identifiable, format, arguments);
239245

240246
/// <param name="continuous">Whether to assign the locale message once or continuously.</param>
241-
/// <param name="arguments">The arguments used to format the locale message.</param>
247+
/// <param name="arguments">
248+
/// The arguments used to format the locale message.<br/>
249+
/// Note that <see cref="ModLocaleStringIndicatorArgumentName">mod indicator</see> will be added to this.
250+
/// </param>
242251
/// <inheritdoc cref="GetLocaleString(IIdentifiable, string, string, object)"/>
243252
public static LocaleString GetLocaleString(this IIdentifiable identifiable, string key, bool continuous, Dictionary<string, object>? arguments = null)
244253
=> identifiable.GetLocaleKey(key).AsModLocaleKey(identifiable, continuous, arguments);
245254

246255
/// <param name="format">An additional format string to insert the locale message into.</param>
247256
/// <param name="continuous">Whether to assign the locale message once or continuously.</param>
248-
/// <param name="arguments">The arguments used to format the locale message.</param>
257+
/// <param name="arguments">
258+
/// The arguments used to format the locale message.<br/>
259+
/// Note that <see cref="ModLocaleStringIndicatorArgumentName">mod indicator</see> will be added to this.
260+
/// </param>
249261
/// <inheritdoc cref="GetLocaleString(IIdentifiable, string, string, object)"/>
250262
public static LocaleString GetLocaleString(this IIdentifiable identifiable, string key, string? format = null, bool continuous = true, Dictionary<string, object>? arguments = null)
251263
=> identifiable.GetLocaleKey(key).AsModLocaleKey(identifiable, format!, continuous, arguments);

MonkeyLoader.Resonite.Integration/UI/Tooltips/ButtonDelegateTooltipResolver.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ protected override void Handle(ResolveTooltipLabelEvent eventData)
7272
}
7373
}
7474

75-
arguments.Add(LocaleExtensions.ModLocaleStringIndicatorArgumentName, string.Empty);
7675
eventData.Label = localeKey.AsModLocaleKey(Mod, arguments: arguments);
76+
eventData.ShouldCacheLabel = false; // The provided arguments' values may change between invocations
7777

7878
if (TooltipConfig.Instance.EnableDebugButtonData)
79-
Logger.Debug($"LocaleKey: {eventData.Label.Value.content}".Yield().Concat(eventData.Label.Value.arguments.Select(item => $"\"{item.Key}\" = \"{item.Value}\"")));
79+
Logger.Debug($"LocaleKey: {localeKey}".Yield().Concat(arguments.Select(item => $"\"{item.Key}\" = \"{item.Value}\"")));
8080
}
8181
}
8282
}

MonkeyLoader.Resonite.Integration/UI/Tooltips/CommentTooltipResolver.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ public static bool TryGetTooltipLabel(IButton button, [NotNullWhen(true)] out st
4747
/// <inheritdoc/>
4848
protected override void Handle(ResolveTooltipLabelEvent eventData)
4949
{
50-
if (TryGetTooltipLabel(eventData.Button, out var label))
51-
eventData.Label = label;
50+
if (!TryGetTooltipLabel(eventData.Button, out var label))
51+
return;
52+
53+
eventData.Label = label;
54+
eventData.ShouldCacheLabel = true;
5255
}
5356

5457
private static bool IsTooltipComment(Comment comment)

MonkeyLoader.Resonite.Integration/UI/Tooltips/HyperlinkTooltipResolver.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,14 @@ public static bool TryGetTooltipLabel(IButton button, [NotNullWhen(true)] out Lo
8484
/// <inheritdoc/>
8585
protected override void Handle(ResolveTooltipLabelEvent eventData)
8686
{
87-
if (TryGetTooltipLabel(eventData.Button, out var label, eventData.Label))
88-
eventData.Label = label;
87+
if (!TryGetTooltipLabel(eventData.Button, out var label, eventData.Label))
88+
return;
89+
90+
// Otherwise, keep prior caching
91+
if (!eventData.HasLabel)
92+
eventData.ShouldCacheLabel = true;
93+
94+
eventData.Label = label;
8995
}
9096
}
9197
}

MonkeyLoader.Resonite.Integration/UI/Tooltips/ReferenceProxySourceTooltipResolver.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ internal sealed class ReferenceProxySourceTooltipResolver : ResoniteCancelableEv
1414

1515
public override bool SkipCanceled => true;
1616

17-
public static bool TryGetTooltipLabel(IButton button, [NotNullWhen(true)] out LocaleString? label)
17+
public static bool TryGetTooltipLabel(IButton button, [NotNullWhen(true)] out LocaleString? label, out bool shouldCache)
1818
{
1919
label = null;
20+
shouldCache = true;
2021

2122
if (button.Slot.GetComponent<ReferenceProxySource>() is not ReferenceProxySource proxySource)
2223
return false;
@@ -31,20 +32,22 @@ public static bool TryGetTooltipLabel(IButton button, [NotNullWhen(true)] out Lo
3132
return TryGetInspectorTooltipLabel(proxySource.Reference.Target, out label);
3233

3334
default:
35+
shouldCache = false;
3436
label = Mod.GetLocaleString("Tooltip.ReferenceProxySource", "target", proxySource.Reference.Target.GetReferenceLabel());
3537
return true;
3638
}
3739
}
3840

3941
protected override void Handle(ResolveTooltipLabelEvent eventData)
4042
{
41-
if (!TryGetTooltipLabel(eventData.Button, out var label))
43+
if (!TryGetTooltipLabel(eventData.Button, out var label, out var shouldCache))
4244
return;
4345

4446
if (!TooltipConfig.Instance.EnableDebugButtonData && !label.Value.HasMessageInCurrent())
4547
return;
4648

4749
eventData.Label = label;
50+
eventData.ShouldCacheLabel = shouldCache;
4851

4952
if (TooltipConfig.Instance.EnableDebugButtonData)
5053
Logger.Debug(() => $"LocaleKey: {eventData.Label.Value.content}");

MonkeyLoader.Resonite.Integration/UI/Tooltips/ResolveTooltipLabelEvent.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public LocaleString? Label
4747
}
4848
}
4949

50+
/// <summary>
51+
/// Gets or sets whether the tooltip <see cref="Label">label</see> for this
52+
/// <see cref="Button">button</see> should be cached by the <see cref="TooltipManager"/>.
53+
/// </summary>
54+
/// <value>
55+
/// <see langword="true"/> if the <see cref="Label">label</see> should be cached by
56+
/// the <see cref="TooltipManager"/>; otherwise, <see langword="false"/>.
57+
/// </value>
58+
public bool ShouldCacheLabel { get; set; } = true;
59+
5060
internal ResolveTooltipLabelEvent(IButton button, ButtonEventData buttonEventData)
5161
{
5262
Button = button;

MonkeyLoader.Resonite.Integration/UI/Tooltips/SlotRecordTooltipResolver.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ internal sealed class SlotRecordTooltipResolver : ResoniteCancelableEventHandler
1313

1414
public override bool SkipCanceled => true;
1515

16-
public static bool TryGetTooltipLabel(IButton button, [NotNullWhen(true)] out LocaleString? label)
16+
public static bool TryGetTooltipLabel(IButton button, [NotNullWhen(true)] out LocaleString? label, out bool shouldCache)
1717
{
1818
label = null;
19+
shouldCache = true;
1920

2021
if (button.Slot.GetComponent<SlotRecord>() is not SlotRecord slotRecord)
2122
return false;
@@ -33,17 +34,19 @@ public static bool TryGetTooltipLabel(IButton button, [NotNullWhen(true)] out Lo
3334
return true;
3435

3536
default:
36-
label = Mod.GetLocaleString("Tooltip.SlotRecord.Generic");
37+
shouldCache = false;
38+
label = Mod.GetLocaleString("Tooltip.SlotRecord.Generic", "target", targetSlot.GetReferenceLabel());
3739
return true;
3840
}
3941
}
4042

4143
protected override void Handle(ResolveTooltipLabelEvent eventData)
4244
{
43-
if (!TryGetTooltipLabel(eventData.Button, out var label))
45+
if (!TryGetTooltipLabel(eventData.Button, out var label, out var shouldCache))
4446
return;
4547

4648
eventData.Label = label;
49+
eventData.ShouldCacheLabel = shouldCache;
4750
}
4851
}
4952
}

MonkeyLoader.Resonite.Integration/UI/Tooltips/TooltipManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ public static bool TryResolveTooltipLabel(IButton button, ButtonEventData button
268268

269269
if (!label.Value.isLocaleKey || label.Value.HasMessageInCurrent())
270270
{
271-
RegisterLabelForButton(button, label.Value);
271+
if (eventData.ShouldCacheLabel)
272+
RegisterLabelForButton(button, label.Value);
273+
272274
return true;
273275
}
274276

0 commit comments

Comments
 (0)