Vibenutý 😔
This commit is contained in:
1 parent
d06a9f332b
commit
8cc27e6b8e
10 files changed
+295
-12
No files matched your search
@@ -1,16 +1,124 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using winui_learning_3.Models;
|
||||
using winui_learning_3.Services;
|
||||
|
||||
namespace winui_learning_3.Views;
|
||||
|
||||
public sealed partial class HomePage : Page
|
||||
{
|
||||
private const int MaxHistory = 5;
|
||||
private readonly List<string> _history = [];
|
||||
|
||||
public HomePage()
|
||||
{
|
||||
InitializeComponent();
|
||||
LoadRoots();
|
||||
}
|
||||
|
||||
protected override async void OnNavigatedTo(Microsoft.UI.Xaml.Navigation.NavigationEventArgs e)
|
||||
private void LoadRoots()
|
||||
{
|
||||
base.OnNavigatedTo(e);
|
||||
foreach (var drive in DriveInfo.GetDrives().Where(d => d.IsReady))
|
||||
{
|
||||
FolderTree.RootNodes.Clear(); // not needed with ItemsSource, shown for clarity
|
||||
}
|
||||
|
||||
FolderTree.ItemsSource = new ObservableCollection<FolderContentDto>(
|
||||
DriveInfo.GetDrives()
|
||||
.Where(d => d.IsReady)
|
||||
.Select(d => new FolderContentDto { AbsolutePath = d.Name }));
|
||||
}
|
||||
|
||||
// ---- Tree ----
|
||||
private void FolderTree_Expanding(TreeView sender, TreeViewExpandingEventArgs args)
|
||||
{
|
||||
if (args.Item is not FolderContentDto folder || folder.Children.Count > 0) return;
|
||||
|
||||
try
|
||||
{
|
||||
var loaded = FolderContentService.GetFolderContent(folder.AbsolutePath);
|
||||
foreach (var child in loaded.Children) folder.Children.Add(child);
|
||||
}
|
||||
catch (UnauthorizedAccessException) { /* leave empty */ }
|
||||
|
||||
args.Node.HasUnrealizedChildren = false;
|
||||
}
|
||||
|
||||
private void FolderTree_ItemInvoked(TreeView sender, TreeViewItemInvokedEventArgs args)
|
||||
{
|
||||
if (args.InvokedItem is FolderContentDto folder)
|
||||
NavigateTo(folder.AbsolutePath);
|
||||
}
|
||||
|
||||
// ---- Navigation ----
|
||||
private void NavigateTo(string path)
|
||||
{
|
||||
if (!Directory.Exists(path)) return;
|
||||
|
||||
try
|
||||
{
|
||||
var content = FolderContentService.GetFolderContent(path);
|
||||
FileList.ItemsSource = content.Files;
|
||||
PathBox.Text = content.AbsolutePath;
|
||||
AddToHistory(content.AbsolutePath);
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
FileList.ItemsSource = null; // or show an InfoBar
|
||||
}
|
||||
}
|
||||
|
||||
private void AddToHistory(string path)
|
||||
{
|
||||
_history.RemoveAll(p => string.Equals(p, path, StringComparison.OrdinalIgnoreCase));
|
||||
_history.Insert(0, path);
|
||||
if (_history.Count > MaxHistory) _history.RemoveRange(MaxHistory, _history.Count - MaxHistory);
|
||||
}
|
||||
|
||||
// ---- Path box: history + hints ----
|
||||
private void PathBox_GotFocus(object sender, RoutedEventArgs e)
|
||||
{
|
||||
PathBox.ItemsSource = _history.ToList();
|
||||
PathBox.IsSuggestionListOpen = _history.Count > 0;
|
||||
}
|
||||
|
||||
private void PathBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
|
||||
{
|
||||
if (args.Reason != AutoSuggestionBoxTextChangeReason.UserInput) return;
|
||||
|
||||
sender.ItemsSource = string.IsNullOrWhiteSpace(sender.Text)
|
||||
? _history.ToList()
|
||||
: GetHints(sender.Text);
|
||||
}
|
||||
|
||||
private static List<string> GetHints(string text)
|
||||
{
|
||||
try
|
||||
{
|
||||
var endsWithSep = text.EndsWith(Path.DirectorySeparatorChar);
|
||||
var parent = endsWithSep ? text : Path.GetDirectoryName(text);
|
||||
var prefix = endsWithSep ? "" : Path.GetFileName(text);
|
||||
|
||||
if (string.IsNullOrEmpty(parent) || !Directory.Exists(parent)) return [];
|
||||
|
||||
return new DirectoryInfo(parent)
|
||||
.EnumerateDirectories()
|
||||
.Where(d => d.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
||||
.Select(d => d.FullName)
|
||||
.Take(8)
|
||||
.ToList();
|
||||
}
|
||||
catch { return []; } // bad path chars, access denied, etc.
|
||||
}
|
||||
|
||||
private void PathBox_SuggestionChosen(AutoSuggestBox sender, AutoSuggestBoxSuggestionChosenEventArgs args)
|
||||
=> sender.Text = args.SelectedItem?.ToString() ?? sender.Text;
|
||||
|
||||
private void PathBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
|
||||
=> NavigateTo(args.ChosenSuggestion?.ToString() ?? args.QueryText);
|
||||
}
|
||||
Reference in new issue
Block a user