Skip to content

Commit 6dc3c17

Browse files
committed
Add config key enabled source component to sync the generated item(s)' enabled state with a config key
1 parent 4339141 commit 6dc3c17

4 files changed

Lines changed: 70 additions & 28 deletions

File tree

MonkeyLoader.Resonite.Integration/Configuration/ConfigKeyCustomDataFeedItems.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@ namespace MonkeyLoader.Resonite.Configuration
99
/// Represents a basic config key component which generate <see cref="DataFeedItem"/>s
1010
/// for the config key to represent themselves, rather than requiring default handling.
1111
/// </summary>
12+
/// <inheritdoc cref="IConfigKeyCustomDataFeedItems{T}"/>
1213
public abstract class ConfigKeyCustomDataFeedItems<T> : IConfigKeyCustomDataFeedItems<T>
1314
{
15+
/// <inheritdoc/>
16+
public IDefiningConfigKey<T> ConfigKey { get; private set; } = null!;
17+
1418
/// <summary>
15-
/// Gets the config item that <see cref="DataFeedItem"/>s will be generated for.
19+
/// Creates a new instance of this component in derived classes.
1620
/// </summary>
17-
public IDefiningConfigKey<T> ConfigKey { get; private set; } = null!;
21+
/// <inheritdoc cref="ConfigKeyCustomDataFeedItems{T}"/>
22+
protected ConfigKeyCustomDataFeedItems()
23+
{ }
1824

1925
/// <inheritdoc/>
2026
public abstract IAsyncEnumerable<DataFeedItem> Enumerate(IReadOnlyList<string> path, IReadOnlyList<string> groupKeys, string? searchPhrase, object? viewData);
@@ -44,6 +50,17 @@ protected virtual void OnInitialize(IDefiningConfigKey<T> entity)
4450
/// Defines the interface for config key components which generate <see cref="DataFeedItem"/>s
4551
/// for the config key to represent themselves, rather than requiring default handling.
4652
/// </summary>
53+
/// <remarks>
54+
/// Beware, that only the first of these components will be used,
55+
/// should a <see cref="IDefiningConfigKey{T}">config key</see> have multiple.
56+
/// </remarks>
57+
/// <typeparam name="T">The type of the config item's value.</typeparam>
4758
public interface IConfigKeyCustomDataFeedItems<T> : IConfigKeyComponent<IDefiningConfigKey<T>>, ICustomDataFeedItems
48-
{ }
59+
{
60+
/// <summary>
61+
/// Gets the config item that <see cref="DataFeedItem"/>s will be
62+
/// <see cref="ICustomDataFeedItems.Enumerate">enumerated</see> for.
63+
/// </summary>
64+
public IDefiningConfigKey<T> ConfigKey { get; }
65+
}
4966
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using FrooxEngine;
2+
using MonkeyLoader.Configuration;
3+
using MonkeyLoader.Resonite.DataFeeds.Settings;
4+
5+
namespace MonkeyLoader.Resonite.Configuration
6+
{
7+
/// <summary>
8+
/// Implements a <see cref="IConfigKeyCustomDataFeedItems{T}">custom data feed items component</see>
9+
/// that will use another config key as the <see cref="ConfigToSettingsExtensions.WithEnabledSource">enabled source</see>
10+
/// for the default items generated for the <see cref="ConfigKeyCustomDataFeedItems{T}.ConfigKey">ConfigKey</see>.
11+
/// </summary>
12+
/// <typeparam name="T">The type of the config key </typeparam>
13+
/// <inheritdoc/>
14+
public sealed class ConfigKeyEnabledSource<T> : ConfigKeyCustomDataFeedItems<T>
15+
{
16+
/// <summary>
17+
/// Gets the config item that will be used as the
18+
/// <see cref="ConfigToSettingsExtensions.WithEnabledSource{TDataFeedItem}(TDataFeedItem, IDefiningConfigKey{bool})">enabled source</see>
19+
/// for the default items generated for the <see cref="ConfigKeyCustomDataFeedItems{T}.ConfigKey">ConfigKey</see>.
20+
/// </summary>
21+
public IDefiningConfigKey<bool> EnabledSource { get; }
22+
23+
/// <summary>
24+
/// Creates a new instance of this <see cref="IConfigKeyCustomDataFeedItems{T}">custom data feed items component</see>
25+
/// that will use the given <see cref="IDefiningConfigKey{T}">config key</see> as the
26+
/// <see cref="ConfigToSettingsExtensions.WithEnabledSource{TDataFeedItem}(TDataFeedItem, IDefiningConfigKey{bool})">enabled source</see>
27+
/// for the default items generated for the <see cref="ConfigKeyCustomDataFeedItems{T}.ConfigKey">ConfigKey</see>.
28+
/// </summary>
29+
/// <param name="enabledSource"></param>
30+
/// <inheritdoc cref="ConfigKeyCustomDataFeedItems{T}"/>
31+
public ConfigKeyEnabledSource(IDefiningConfigKey<bool> enabledSource)
32+
{
33+
EnabledSource = enabledSource;
34+
}
35+
36+
/// <inheritdoc/>
37+
public override async IAsyncEnumerable<DataFeedItem> Enumerate(IReadOnlyList<string> path, IReadOnlyList<string> groupKeys, string? searchPhrase, object? viewData)
38+
{
39+
await foreach (var feedItem in ConfigKey.EnumerateDefaultItemsAsync(path, groupKeys))
40+
yield return feedItem.WithEnabledSource(EnabledSource);
41+
}
42+
}
43+
}

MonkeyLoader.Resonite.Integration/UI/ContextMenus/ContextMenusConfig.cs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ public sealed class ContextMenusConfig : SingletonConfigSection<ContextMenusConf
3434
{
3535
_itemLimitSubgroup,
3636
new ConfigKeyPriority(8),
37-
new ConfigKeyRange<int>(3, 32),
38-
new LimitContextMenuItemsToggleLink()
37+
new ConfigKeyRange<int>(3, 32)
3938
};
4039

4140
private readonly DefiningConfigKey<bool> _limitContextMenuItems = new("LimitContextMenuItems", "Limit the number of items shown in the context menu. If the configured number is exceeded, the menu will get paginated.", () => true)
@@ -47,8 +46,7 @@ public sealed class ContextMenusConfig : SingletonConfigSection<ContextMenusConf
4746
private readonly DefiningConfigKey<bool> _showResetScaleWithToggle = new("ShowResetScaleWithToggle", "Show the reset scale action alongside the scaling toggle when it is always allowed.", () => true)
4847
{
4948
_scalingSubgroup,
50-
new ConfigKeyPriority(6),
51-
new AlwaysAllowScaleToggleLink()
49+
new ConfigKeyPriority(6)
5250
};
5351

5452
private readonly DefiningConfigKey<bool> _showSaveLocation = new("ShowSaveLocation", "Show the current inventory path when trying to save something.", () => true)
@@ -108,28 +106,11 @@ public sealed class ContextMenusConfig : SingletonConfigSection<ContextMenusConf
108106
/// <inheritdoc/>
109107
public override Version Version { get; } = new(1, 1, 0);
110108

111-
private sealed class AlwaysAllowScaleToggleLink : ConfigKeyCustomDataFeedItems<bool>
112-
{
113-
public override async IAsyncEnumerable<DataFeedItem> Enumerate(IReadOnlyList<string> path, IReadOnlyList<string> groupKeys, string? searchPhrase, object? viewData)
114-
{
115-
await foreach (var item in ConfigKey.EnumerateDefaultItemsAsync(path, [.. groupKeys]))
116-
{
117-
item.InitEnabled(field => field.SetupConfigKeyField(Instance._alwaysAllowScaleToggle));
118-
yield return item;
119-
}
120-
}
121-
}
122-
123-
private sealed class LimitContextMenuItemsToggleLink : ConfigKeyCustomDataFeedItems<int>
109+
/// <inheritdoc/>
110+
public ContextMenusConfig()
124111
{
125-
public override async IAsyncEnumerable<DataFeedItem> Enumerate(IReadOnlyList<string> path, IReadOnlyList<string> groupKeys, string? searchPhrase, object? viewData)
126-
{
127-
await foreach (var item in ConfigKey.EnumerateDefaultItemsAsync(path, groupKeys))
128-
{
129-
item.InitEnabled(field => field.SetupConfigKeyField(Instance._limitContextMenuItems));
130-
yield return item;
131-
}
132-
}
112+
_contextMenuItemLimit.Components.Add(new ConfigKeyEnabledSource<int>(_limitContextMenuItems));
113+
_showResetScaleWithToggle.Components.Add(new ConfigKeyEnabledSource<bool>(_alwaysAllowScaleToggle));
133114
}
134115
}
135116
}

MonkeyLoader.Resonite.Integration/UI/ContextMenus/InteractionHandlerContextMenuInjector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using FrooxEngine.CommonAvatar;
44
using FrooxEngine.UIX;
55
using HarmonyLib;
6+
67
using static FrooxEngine.InteractionHandler;
78

89
namespace MonkeyLoader.Resonite.UI.ContextMenus

0 commit comments

Comments
 (0)