| 1 | using CascadeIDE.Contracts; |
| 2 | |
| 3 | namespace CascadeIDE.Features.Shell.Application; |
| 4 | |
| 5 | /// <summary>Выбор строки в палитре команд без привязки к <c>ObservableCollection</c>.</summary> |
| 6 | [PresentationProjection] |
| 7 | public static class CommandPaletteSelectionProjection |
| 8 | { |
| 9 | /// <summary>После полного пересчёта списка: первая строка или «нет выбора».</summary> |
| 10 | public static int InitialSelectedIndex(int entryCount) => entryCount > 0 ? 0 : -1; |
| 11 | |
| 12 | /// <summary>Если индекс ушёл за хвост после добавления/удаления строк — зажать на последнюю валидную.</summary> |
| 13 | public static int ClampUpperOrKeep(int selectedIndex, int entryCount) |
| 14 | { |
| 15 | if (entryCount <= 0) |
| 16 | return -1; |
| 17 | return selectedIndex >= entryCount ? Math.Max(0, entryCount - 1) : selectedIndex; |
| 18 | } |
| 19 | |
| 20 | public static bool TryMoveCircular(int currentIndex, int delta, int entryCount, out int nextIndex) |
| 21 | { |
| 22 | nextIndex = currentIndex; |
| 23 | if (entryCount <= 0) |
| 24 | return false; |
| 25 | var next = currentIndex + delta; |
| 26 | if (next < 0) |
| 27 | next = entryCount - 1; |
| 28 | else if (next >= entryCount) |
| 29 | next = 0; |
| 30 | nextIndex = next; |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | /// <summary>PgUp/PgDn: шаг по <paramref name="pageStep"/> с зажатием в диапазоне.</summary> |
| 35 | public static bool TryPageMove(int currentIndex, int directionSign, int pageStep, int entryCount, out int nextIndex) |
| 36 | { |
| 37 | nextIndex = currentIndex; |
| 38 | if (entryCount <= 0) |
| 39 | return false; |
| 40 | var step = pageStep * Math.Sign(directionSign); |
| 41 | if (step == 0) |
| 42 | return false; |
| 43 | nextIndex = Math.Clamp(currentIndex + step, 0, entryCount - 1); |
| 44 | return true; |
| 45 | } |
| 46 | } |
| 47 | |