Skip to content

Commit

Permalink
Added scrolling support to thumbnails list
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihnea Rădulescu committed Jul 17, 2024
1 parent 2b915ce commit b9549a6
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<Copyright>Copyright © Mihnea Rădulescu 2017 - 2024</Copyright>
<AssemblyVersion>1.2024.07.16</AssemblyVersion>
<AssemblyVersion>1.2024.07.17</AssemblyVersion>
<Version>$(AssemblyVersion)</Version>
<DebugType>embedded</DebugType>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace ImageFanReloaded.Core.Collections.Implementation;

public static class CollectionExtensions
{
public static bool IsIndexWithinBounds<T>(this ICollection<T> collection, int index)
=> 0 <= index && index < collection.Count;

public static bool IsIndexWithinBounds<T>(this IReadOnlyCollection<T> collection, int index)
=> 0 <= index && index < collection.Count;
=> index >= 0 && index < collection.Count;
}
2 changes: 0 additions & 2 deletions ImageFanReloaded.Core/Controls/IContentTabItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,4 @@ public interface IContentTabItem
void SetImageStatusBarText(string imageStatusBarText);

void SaveMatchingTreeViewItem(FileSystemEntryInfo selectedFileSystemEntryInfo);

bool IsThumbnailScrollViewerFocused { get; }
}
5 changes: 4 additions & 1 deletion ImageFanReloaded.Core/Keyboard/Key.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ public enum Key
I,

Plus,
Minus
Minus,

PageUp,
PageDown
}
3 changes: 3 additions & 0 deletions ImageFanReloaded.Core/Settings/IGlobalParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public interface IGlobalParameters

Key PlusKey { get; }
Key MinusKey { get; }

Key PageUpKey { get; }
Key PageDownKey { get; }

bool IsBackwardNavigationKey(Key aKey);
bool IsForwardNavigationKey(Key aKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public abstract class GlobalParametersBase : IGlobalParameters

public Key PlusKey { get; }
public Key MinusKey { get; }

public Key PageUpKey { get; }
public Key PageDownKey { get; }

public bool IsBackwardNavigationKey(Key aKey) => _backwardNavigationKeys.Contains(aKey);
public bool IsForwardNavigationKey(Key aKey) => _forwardNavigationKeys.Contains(aKey);
Expand Down Expand Up @@ -109,6 +112,9 @@ protected GlobalParametersBase(

PlusKey = Key.Plus;
MinusKey = Key.Minus;

PageUpKey = Key.PageUp;
PageDownKey = Key.PageDown;

NameComparer = operatingSystemSettings.IsWindows
? new NaturalSortingComparer(StringComparer.InvariantCultureIgnoreCase)
Expand Down
3 changes: 3 additions & 0 deletions ImageFanReloaded.Test/GlobalParametersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public void GlobalParameters_IsCorrectlyInitialized()
_globalParameters.PlusKey.Should().NotBe(Key.None);
_globalParameters.MinusKey.Should().NotBe(Key.None);

_globalParameters.PageUpKey.Should().NotBe(Key.None);
_globalParameters.PageDownKey.Should().NotBe(Key.None);

_globalParameters.NameComparer.Should().NotBeNull();

_globalParameters.ImageFileExtensions.Should().NotBeEmpty();
Expand Down
67 changes: 54 additions & 13 deletions ImageFanReloaded/Controls/ContentTabItem.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using ImageFanReloaded.Core.Collections.Implementation;
using ImageFanReloaded.Core.Controls;
using ImageFanReloaded.Core.CustomEventArgs;
using ImageFanReloaded.Core.DiscAccess;
Expand Down Expand Up @@ -45,8 +44,9 @@ public void EnableFolderTreeViewSelectedItemChanged()
public bool ShouldHandleControlKeyFunctions(KeyModifiers keyModifiers, Key keyPressing)
{
var shouldHandleKeyPressing = ShouldSwitchControlFocus(keyModifiers, keyPressing)
|| ShouldToggleRecursiveFolderAccess(keyModifiers, keyPressing)
|| ShouldHandleThumbnailSelection(keyPressing);
|| ShouldToggleRecursiveFolderAccess(keyModifiers, keyPressing)
|| ShouldHandleThumbnailSelection(keyPressing)
|| ShouldHandleThumbnailScrolling(keyPressing);

return shouldHandleKeyPressing;
}
Expand All @@ -66,6 +66,10 @@ public void HandleControlKeyFunctions(KeyModifiers keyModifiers, Key keyPressing
BringThumbnailIntoView();
DisplayImage();
}
else if (ShouldHandleThumbnailScrolling(keyPressing))
{
HandleThumbnailScrolling(keyPressing);
}
}

public void SetFolderTreeViewSelectedItem()
Expand Down Expand Up @@ -214,13 +218,13 @@ public void SaveMatchingTreeViewItem(FileSystemEntryInfo selectedFileSystemEntry
}
}
}

public bool IsThumbnailScrollViewerFocused => _thumbnailScrollViewer.IsFocused;

#region Private

private const string FakeTreeViewItemText = "Loading...";

private const int ThumbnailScrollAdvanceCount = 20;

private readonly IList<IThumbnailBox> _thumbnailBoxCollection;

private int _maxThumbnailIndex;
Expand Down Expand Up @@ -304,7 +308,7 @@ private void OnImageChanged(object? sender, ImageChangedEventArgs e)
var imageView = e.ImageView;
var increment = e.Increment;

if (AdvanceSelectedThumbnailByIncrement(increment))
if (AdvanceFromSelectedThumbnail(increment))
{
imageView.SetImage(_selectedThumbnailBox!.ImageFile!, _folderAccessType.IsRecursive());
}
Expand All @@ -323,26 +327,37 @@ private void SelectThumbnailBox(IThumbnailBox thumbnailBox)
}
}

private bool AdvanceSelectedThumbnailByIncrement(int increment)
private bool AdvanceFromSelectedThumbnail(int increment)
{
var canAdvanceToNewSelectedThumbnailIndex = false;

if (_selectedThumbnailBox is not null)
{
var newSelectedThumbnailIndex = _selectedThumbnailIndex + increment;

if (_thumbnailBoxCollection.IsIndexWithinBounds(newSelectedThumbnailIndex))
if (newSelectedThumbnailIndex < 0)
{
newSelectedThumbnailIndex = 0;
}
else if (newSelectedThumbnailIndex >= _thumbnailBoxCollection.Count)
{
newSelectedThumbnailIndex = _thumbnailBoxCollection.Count - 1;
}

if (_selectedThumbnailIndex != newSelectedThumbnailIndex)
{
canAdvanceToNewSelectedThumbnailIndex = true;

UnselectThumbnail();

_selectedThumbnailIndex = newSelectedThumbnailIndex;
_selectedThumbnailBox = _thumbnailBoxCollection[_selectedThumbnailIndex];

SelectThumbnail();

return true;
}
}

return false;
return canAdvanceToNewSelectedThumbnailIndex;
}

private void BringThumbnailIntoView() => _selectedThumbnailBox?.BringThumbnailIntoView();
Expand Down Expand Up @@ -443,6 +458,17 @@ private bool ShouldHandleThumbnailSelection(Key keyPressing)
return false;
}

private bool ShouldHandleThumbnailScrolling(Key keyPressing)
{
if (_selectedThumbnailBox is not null &&
(keyPressing == GlobalParameters!.PageUpKey || keyPressing == GlobalParameters!.PageDownKey))
{
return true;
}

return false;
}

private void SwitchControlFocus()
{
if (_folderTreeView.SelectedItem is null)
Expand Down Expand Up @@ -484,17 +510,32 @@ private void ToggleRecursiveFolderAccess(KeyModifiers keyModifiers)
RaiseFolderChangedEvent();
}

private void HandleThumbnailScrolling(Key keyPressing)
{
if (_selectedThumbnailBox is not null)
{
if (keyPressing == GlobalParameters!.PageUpKey)
{
AdvanceFromSelectedThumbnail(-ThumbnailScrollAdvanceCount);
}
else if (keyPressing == GlobalParameters!.PageDownKey)
{
AdvanceFromSelectedThumbnail(ThumbnailScrollAdvanceCount);
}
}
}

private void ThumbnailScrollViewerOnKeyPressing(object? sender, Avalonia.Input.KeyEventArgs e)
{
var keyPressing = e.Key.ToCoreKey();

if (GlobalParameters!.IsBackwardNavigationKey(keyPressing))
{
AdvanceSelectedThumbnailByIncrement(-1);
AdvanceFromSelectedThumbnail(-1);
}
else if (GlobalParameters!.IsForwardNavigationKey(keyPressing))
{
AdvanceSelectedThumbnailByIncrement(1);
AdvanceFromSelectedThumbnail(1);
}

e.Handled = true;
Expand Down
2 changes: 1 addition & 1 deletion ImageFanReloaded/Controls/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private bool ShouldNavigateToNextTab(KeyModifiers keyModifiers, Key keyPressing)
private bool ShouldDisplayHelp(Key keyPressing) => keyPressing == GlobalParameters!.F1Key;

private bool ShouldAllowKeyPressingEventPropagation(IContentTabItem contentTabItem, Key keyPressing)
=> contentTabItem.IsThumbnailScrollViewerFocused || GlobalParameters!.IsNavigationKey(keyPressing);
=> GlobalParameters!.IsNavigationKey(keyPressing);

private bool HasAtLeastOneContentTabItem()
{
Expand Down
3 changes: 3 additions & 0 deletions ImageFanReloaded/Keyboard/KeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public static ImageFanReloaded.Core.Keyboard.Key ToCoreKey(this Avalonia.Input.K
Avalonia.Input.Key.Add => Core.Keyboard.Key.Plus,
Avalonia.Input.Key.OemMinus => Core.Keyboard.Key.Minus,
Avalonia.Input.Key.Subtract => Core.Keyboard.Key.Minus,

Avalonia.Input.Key.PageUp => Core.Keyboard.Key.PageUp,
Avalonia.Input.Key.PageDown => Core.Keyboard.Key.PageDown,

_ => Core.Keyboard.Key.None
};
Expand Down

0 comments on commit b9549a6

Please sign in to comment.