Skip to content

Commit

Permalink
Feature: Added support for displaying recently entered paths (files-c…
Browse files Browse the repository at this point in the history
  • Loading branch information
hishitetsu authored Dec 27, 2023
1 parent 3752486 commit 496e03f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 25 deletions.
6 changes: 6 additions & 0 deletions src/Files.App/Services/Settings/GeneralSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public List<string> LastCrashedTabList
set => Set(value);
}

public List<string> PathHistoryList
{
get => Get<List<string>>(null);
set => Set(value);
}

public DateTimeFormats DateTimeFormat
{
get => Get(DateTimeFormats.Application);
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Services/Settings/UserSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public override object ExportSettings()
// Remove session settings
export.Remove(nameof(GeneralSettingsService.LastSessionTabList));
export.Remove(nameof(GeneralSettingsService.LastCrashedTabList));
export.Remove(nameof(GeneralSettingsService.PathHistoryList));

return JsonSettingsSerializer.SerializeToJson(export);
}
Expand Down
84 changes: 59 additions & 25 deletions src/Files.App/ViewModels/UserControls/ToolbarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Files.App.ViewModels.UserControls
{
public class ToolbarViewModel : ObservableObject, IAddressToolbar, IDisposable
{
private const int MAX_SUGGESTIONS = 10;

private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();

private readonly IDialogService _dialogService = Ioc.Default.GetRequiredService<IDialogService>();
Expand Down Expand Up @@ -707,6 +709,7 @@ await DialogDisplayHelper.ShowDialogAsync("CommandNotExecutable".GetLocalizedRes
{
if (currentInput.Equals("Home", StringComparison.OrdinalIgnoreCase) || currentInput.Equals("Home".GetLocalizedResource(), StringComparison.OrdinalIgnoreCase))
{
SavePathToHistory("Home");
shellPage.NavigateHome();
}
else
Expand All @@ -732,10 +735,12 @@ await DialogDisplayHelper.ShowDialogAsync("CommandNotExecutable".GetLocalizedRes
return;
}
var pathToNavigate = resFolder.Result?.Path ?? currentInput;
SavePathToHistory(pathToNavigate);
shellPage.NavigateToPath(pathToNavigate);
}
else if (isFtp)
{
SavePathToHistory(currentInput);
shellPage.NavigateToPath(currentInput);
}
else // Not a folder or inaccessible
Expand Down Expand Up @@ -776,6 +781,18 @@ await DialogDisplayHelper.ShowDialogAsync("InvalidItemDialogTitle".GetLocalizedR
}
}

private void SavePathToHistory(string path)
{
var pathHistoryList = UserSettingsService.GeneralSettingsService.PathHistoryList?.ToList() ?? new List<string>();
pathHistoryList.Remove(path);
pathHistoryList.Insert(0, path);

if (pathHistoryList.Count > MAX_SUGGESTIONS)
UserSettingsService.GeneralSettingsService.PathHistoryList = pathHistoryList.RemoveFrom(MAX_SUGGESTIONS + 1);
else
UserSettingsService.GeneralSettingsService.PathHistoryList = pathHistoryList;
}

private static async Task<bool> LaunchApplicationFromPath(string currentInput, string workingDir)
{
var trimmedInput = currentInput.Trim();
Expand All @@ -791,9 +808,9 @@ private static async Task<bool> LaunchApplicationFromPath(string currentInput, s
return await LaunchHelper.LaunchAppAsync(fileName, arguments, workingDir);
}

public async Task SetAddressBarSuggestionsAsync(AutoSuggestBox sender, IShellPage shellpage, int maxSuggestions = 7)
public async Task SetAddressBarSuggestionsAsync(AutoSuggestBox sender, IShellPage shellpage)
{
if (!string.IsNullOrWhiteSpace(sender.Text) && shellpage.FilesystemViewModel is not null)
if (sender.Text is not null && shellpage.FilesystemViewModel is not null)
{
if (!await SafetyExtensions.IgnoreExceptions(async () =>
{
Expand All @@ -818,37 +835,54 @@ public async Task SetAddressBarSuggestionsAsync(AutoSuggestBox sender, IShellPag
{
IsCommandPaletteOpen = false;
var currentInput = sender.Text;
var isFtp = FtpHelpers.IsFtpPath(currentInput);
currentInput = NormalizePathInput(currentInput, isFtp);
var expandedPath = StorageFileExtensions.GetResolvedPath(currentInput, isFtp);
var folderPath = PathNormalization.GetParentDir(expandedPath) ?? expandedPath;
StorageFolderWithPath folder = await shellpage.FilesystemViewModel.GetFolderWithPathFromPathAsync(folderPath);

if (folder is null)
return false;

var currPath = await folder.GetFoldersWithPathAsync(Path.GetFileName(expandedPath), (uint)maxSuggestions);
if (currPath.Count >= maxSuggestions)
if (string.IsNullOrWhiteSpace(currentInput) || currentInput == "Home")
{
suggestions = currPath.Select(x => new NavigationBarSuggestionItem()
// Load previously entered path
var pathHistoryList = UserSettingsService.GeneralSettingsService.PathHistoryList;
if (pathHistoryList is not null)
{
Text = x.Path,
PrimaryDisplay = x.Item.DisplayName
}).ToList();
suggestions = pathHistoryList.Select(x => new NavigationBarSuggestionItem()
{
Text = x,
PrimaryDisplay = x
}).ToList();
}
}
else if (currPath.Any())
else
{
var subPath = await currPath.First().GetFoldersWithPathAsync((uint)(maxSuggestions - currPath.Count));
suggestions = currPath.Select(x => new NavigationBarSuggestionItem()
var isFtp = FtpHelpers.IsFtpPath(currentInput);
currentInput = NormalizePathInput(currentInput, isFtp);
var expandedPath = StorageFileExtensions.GetResolvedPath(currentInput, isFtp);
var folderPath = PathNormalization.GetParentDir(expandedPath) ?? expandedPath;
StorageFolderWithPath folder = await shellpage.FilesystemViewModel.GetFolderWithPathFromPathAsync(folderPath);

if (folder is null)
return false;

var currPath = await folder.GetFoldersWithPathAsync(Path.GetFileName(expandedPath), (uint)MAX_SUGGESTIONS);
if (currPath.Count >= MAX_SUGGESTIONS)
{
suggestions = currPath.Select(x => new NavigationBarSuggestionItem()
{
Text = x.Path,
PrimaryDisplay = x.Item.DisplayName
}).ToList();
}
else if (currPath.Any())
{
Text = x.Path,
PrimaryDisplay = x.Item.DisplayName
}).Concat(
subPath.Select(x => new NavigationBarSuggestionItem()
var subPath = await currPath.First().GetFoldersWithPathAsync((uint)(MAX_SUGGESTIONS - currPath.Count));
suggestions = currPath.Select(x => new NavigationBarSuggestionItem()
{
Text = x.Path,
PrimaryDisplay = PathNormalization.Combine(currPath.First().Item.DisplayName, x.Item.DisplayName)
})).ToList();
PrimaryDisplay = x.Item.DisplayName
}).Concat(
subPath.Select(x => new NavigationBarSuggestionItem()
{
Text = x.Path,
PrimaryDisplay = PathNormalization.Combine(currPath.First().Item.DisplayName, x.Item.DisplayName)
})).ToList();
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Files.Core/Services/Settings/IGeneralSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public interface IGeneralSettingsService : IBaseSettingsService, INotifyProperty
/// </summary>
List<string> LastCrashedTabList { get; set; }

/// <summary>
/// A list containing paths previously entered in the path bar.
/// </summary>
List<string> PathHistoryList { get; set; }

/// <summary>
/// Gets or sets a value indicating which date and time format to use.
/// </summary>
Expand Down

0 comments on commit 496e03f

Please sign in to comment.