Skip to content

Commit e1dd1c8

Browse files
committed
Add default pagination methods to core package with defaults controlled by ML config
1 parent aa9aae0 commit e1dd1c8

5 files changed

Lines changed: 108 additions & 7 deletions

File tree

MonkeyLoader.Resonite.Core/UI/ContextMenuPaginationExtensions.cs

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ public static class ContextMenuPaginationExtensions
1414
{
1515
private static readonly ConditionalWeakTable<ContextMenu, PaginationInfo> _paginationInfosByContextMenu = [];
1616

17+
internal static Func<int> GetDefaultMaxItems { get; set; } = static () => 14;
18+
19+
internal static Func<bool> GetLimitContextMenuItems { get; set; } = static () => true;
20+
21+
/// <summary>
22+
/// Gets the current default maximum number of non-pagination items to show per page, not including the paging buttons.<br/>
23+
/// This means that up to <c>value + 2</c> non-pagination items will be displayed without pagination.
24+
/// </summary>
25+
/// <remarks>
26+
/// This value may change between calls.
27+
/// </remarks>
28+
public static int DefaultMaxItems => GetDefaultMaxItems();
29+
30+
/// <summary>
31+
/// Gets whether the number of items in the context menu should be limited and pagination buttons added for anything beyond that.
32+
/// </summary>
33+
/// <remarks>
34+
/// This value may change between calls.
35+
/// </remarks>
36+
public static bool LimitContextMenuItems => GetLimitContextMenuItems();
37+
1738
/// <inheritdoc cref="AddPagination(ContextMenu, int, int, out ContextMenuItem, out ContextMenuItem)"/>
1839
public static void AddPagination(this ContextMenu contextMenu, int maxItems, int currentPage = 0)
1940
=> contextMenu.AddPagination(maxItems, currentPage, out _, out _);
@@ -43,6 +64,75 @@ public static void AddPagination(this ContextMenu contextMenu, int maxItems, int
4364
throw new InvalidOperationException("Failed to add pagination!");
4465
}
4566

67+
/// <remarks>
68+
/// This version automatically uses the <see cref="DefaultMaxItems">DefaultMaxItems</see> for pagination, if wanted by the user.
69+
/// </remarks>
70+
/// <inheritdoc cref="AddPagination(ContextMenu, int, int, out ContextMenuItem, out ContextMenuItem)"/>
71+
public static void AddDefaultPagination(this ContextMenu contextMenu, int currentPage = 0)
72+
{
73+
if (LimitContextMenuItems)
74+
contextMenu.AddPagination(DefaultMaxItems, currentPage, out _, out _);
75+
}
76+
77+
/// <remarks>
78+
/// This version automatically uses the <see cref="DefaultMaxItems">DefaultMaxItems</see> for pagination, if wanted by the user.
79+
/// </remarks>
80+
/// <inheritdoc cref="AddPagination(ContextMenu, int, int, out ContextMenuItem, out ContextMenuItem)"/>
81+
public static void AddDefaultPagination(this ContextMenu contextMenu, int currentPage,
82+
out ContextMenuItem? back, out ContextMenuItem? forward)
83+
{
84+
back = null;
85+
forward = null;
86+
87+
if (LimitContextMenuItems)
88+
contextMenu.AddPagination(DefaultMaxItems, currentPage, out back, out forward);
89+
}
90+
91+
/// <remarks>
92+
/// This version automatically uses the <see cref="DefaultMaxItems">DefaultMaxItems</see> for pagination, if wanted by the user.
93+
/// </remarks>
94+
/// <inheritdoc cref="TryAddPagination(ContextMenu, int, int)"/>
95+
public static bool TryAddDefaultPagination(this ContextMenu contextMenu, int currentPage = 0)
96+
{
97+
if (!LimitContextMenuItems)
98+
return false;
99+
100+
return contextMenu.TryAddPagination(DefaultMaxItems, currentPage);
101+
}
102+
103+
/// <remarks>
104+
/// This version automatically uses the <see cref="DefaultMaxItems">DefaultMaxItems</see> for pagination, if wanted by the user.
105+
/// </remarks>
106+
/// <inheritdoc cref="TryAddPagination(ContextMenu, int, int, out ContextMenuItem?, out ContextMenuItem?)"/>
107+
public static bool TryAddDefaultPagination(this ContextMenu contextMenu, int currentPage,
108+
[NotNullWhen(true)] out ContextMenuItem? back, [NotNullWhen(true)] out ContextMenuItem? forward)
109+
{
110+
back = null;
111+
forward = null;
112+
113+
if (!LimitContextMenuItems)
114+
return false;
115+
116+
return contextMenu.TryAddPagination(DefaultMaxItems, currentPage, out back, out forward);
117+
}
118+
119+
/// <remarks>
120+
/// This version automatically uses the <see cref="DefaultMaxItems">DefaultMaxItems</see> for pagination, if wanted by the user.
121+
/// </remarks>
122+
/// <inheritdoc cref="TryAddPagination(ContextMenu, ref int, ref int, bool, out ContextMenuItem?, out ContextMenuItem?)"/>
123+
public static bool TryAddDefaultPagination(this ContextMenu contextMenu, out int maxItems, ref int currentPage, bool forceNew,
124+
[NotNullWhen(true)] out ContextMenuItem? back, [NotNullWhen(true)] out ContextMenuItem? forward)
125+
{
126+
back = null;
127+
forward = null;
128+
maxItems = DefaultMaxItems;
129+
130+
if (!LimitContextMenuItems)
131+
return false;
132+
133+
return contextMenu.TryAddPagination(ref maxItems, ref currentPage, forceNew, out back, out forward);
134+
}
135+
46136
/// <summary>
47137
/// Tries to add pagination with the given configuration to this context menu.<br/>
48138
/// If there already is pagination, nothing happens.
@@ -60,7 +150,7 @@ public static bool TryAddPagination(this ContextMenu contextMenu, int maxItems,
60150
/// <param name="currentPage">The page that should be shown from the start. Can be any integer value, even negative.</param>
61151
/// <inheritdoc cref="TryAddPagination(ContextMenu, ref int, ref int, bool, out ContextMenuItem?, out ContextMenuItem?)"/>
62152
public static bool TryAddPagination(this ContextMenu contextMenu, int maxItems, int currentPage,
63-
[NotNullWhen(true)] out ContextMenuItem? back, [NotNullWhen(true)] out ContextMenuItem? forward)
153+
[NotNullWhen(true)] out ContextMenuItem? back, [NotNullWhen(true)] out ContextMenuItem? forward)
64154
=> contextMenu.TryAddPagination(ref maxItems, ref currentPage, false, out back, out forward);
65155

66156
/// <summary>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ internal sealed class ContextMenuInjector : ResoniteAsyncEventSourceMonkey<Conte
1111
{
1212
protected override bool OnLoaded()
1313
{
14+
ContextMenuPaginationExtensions.GetDefaultMaxItems = static () => ContextMenusConfig.Instance.ContextMenuItemLimit;
15+
ContextMenuPaginationExtensions.GetLimitContextMenuItems = static () => ContextMenusConfig.Instance.LimitContextMenuItems;
16+
1417
ContextMenuItemsGenerationEvent.AddConcreteEvent<InspectorMemberActions>(static contextMenu => new InspectorMemberActionsMenuItemsGenerationEvent(contextMenu), true);
1518

1619
ContextMenuItemsGenerationEvent.AddConcreteEvent(typeof(FieldDriveReceiver<>), DriveReceiverMenuItemsGenerationEvent.CreateForDriveReceiver);

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ public void CloseContextMenu()
188188
/// </summary>
189189
/// <remarks>
190190
/// If <paramref name="options"/>.<see cref="ContextMenuOptions.keepPosition">keepPosition</see>
191-
/// is <c>true</c> the last position will be kept instead.
191+
/// is <c>true</c> the last position will be kept instead.<br/>
192+
/// This will automatically <see cref="ContextMenuPaginationExtensions.TryAddPagination(ContextMenu, int, int)">
193+
/// add pagination</see> to the opened ContextMenu if the user's
194+
/// <see cref="ContextMenusConfig.LimitContextMenuItems">settings</see> call for it.
192195
/// </remarks>
193196
/// <param name="pointer">
194197
/// The slot that the <see cref="FrooxEngine.ContextMenu"/> will be centered at.<br/>
@@ -197,7 +200,15 @@ public void CloseContextMenu()
197200
/// <param name="options">The additional options for opening the menu.</param>
198201
/// <returns>The opened <see cref="FrooxEngine.ContextMenu"/>, or <see langword="null"/> if it failed to open.</returns>
199202
public async Task<ContextMenu?> OpenContextMenuAsync(Slot pointer, ContextMenuOptions options = default)
200-
=> await ContextMenu.OpenMenu(Summoner, pointer, options) ? ContextMenu : null;
203+
{
204+
if (!await ContextMenu.OpenMenu(Summoner, pointer, options))
205+
return null;
206+
207+
if (ContextMenusConfig.Instance.LimitContextMenuItems)
208+
ContextMenu.TryAddPagination(ContextMenusConfig.Instance.ContextMenuItemLimit);
209+
210+
return ContextMenu;
211+
}
201212

202213
/// <summary>
203214
/// Opens the <see cref="SummoningUser">SummoningUser</see>'s <see cref="FrooxEngine.ContextMenu"/>

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using FrooxEngine;
2-
using MonkeyLoader.Configuration;
1+
using MonkeyLoader.Configuration;
32
using MonkeyLoader.Resonite.Configuration;
4-
using MonkeyLoader.Resonite.DataFeeds.Settings;
53

64
namespace MonkeyLoader.Resonite.UI.ContextMenus
75
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using HarmonyLib;
66
using MonkeyLoader.Logging;
77
using MonkeyLoader.Resonite.Events;
8-
using System.Collections.Immutable;
98
using System.Runtime.CompilerServices;
109

1110
using static FrooxEngine.InteractionHandler;

0 commit comments

Comments
 (0)