diff --git a/winui-learning/winui-learning-3/App.xaml b/winui-learning/winui-learning-3/App.xaml index 1c576aa..c89e7c8 100644 --- a/winui-learning/winui-learning-3/App.xaml +++ b/winui-learning/winui-learning-3/App.xaml @@ -3,7 +3,7 @@ x:Class="winui_learning_3.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" - xmlns:local="using:winui_learning_2"> + xmlns:local="using:winui_learning_3"> diff --git a/winui-learning/winui-learning-3/MainWindow.xaml b/winui-learning/winui-learning-3/MainWindow.xaml index e8cb520..902ad0b 100644 --- a/winui-learning/winui-learning-3/MainWindow.xaml +++ b/winui-learning/winui-learning-3/MainWindow.xaml @@ -14,7 +14,6 @@ - @@ -26,14 +25,13 @@ - - + diff --git a/winui-learning/winui-learning-3/MainWindow.xaml.cs b/winui-learning/winui-learning-3/MainWindow.xaml.cs index 04918d6..cbfd1d5 100644 --- a/winui-learning/winui-learning-3/MainWindow.xaml.cs +++ b/winui-learning/winui-learning-3/MainWindow.xaml.cs @@ -14,9 +14,9 @@ public sealed partial class MainWindow : Window private void AppTitleBar_BackRequested(TitleBar titleBar, object args) { - if (rootFrame.CanGoBack) + if (RootFrame.CanGoBack) { - rootFrame.GoBack(); + RootFrame.GoBack(); } } } diff --git a/winui-learning/winui-learning-3/Models/FileDto.cs b/winui-learning/winui-learning-3/Models/FileDto.cs new file mode 100644 index 0000000..cd19f18 --- /dev/null +++ b/winui-learning/winui-learning-3/Models/FileDto.cs @@ -0,0 +1,10 @@ +namespace winui_learning_3.Models; + +public class FileDto +{ + public required string AbsolutePath { get; set; } + public required FileTypeEnum Type { get; set; } + public required string Name { get; set; } + public string? Content { get; set; } + public string? Extension { get; set; } +} \ No newline at end of file diff --git a/winui-learning/winui-learning-3/Models/FileTypeEnum.cs b/winui-learning/winui-learning-3/Models/FileTypeEnum.cs new file mode 100644 index 0000000..93d2824 --- /dev/null +++ b/winui-learning/winui-learning-3/Models/FileTypeEnum.cs @@ -0,0 +1,13 @@ +using System.Net.Mime; + +namespace winui_learning_3.Models; + +public enum FileTypeEnum +{ + Text, + Link, + Image, + Video, + Audio, + Other +} \ No newline at end of file diff --git a/winui-learning/winui-learning-3/Models/FolderContentDto.cs b/winui-learning/winui-learning-3/Models/FolderContentDto.cs new file mode 100644 index 0000000..61f15c3 --- /dev/null +++ b/winui-learning/winui-learning-3/Models/FolderContentDto.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.IO; + +namespace winui_learning_3.Models; + +public class FolderContentDto +{ + public required string AbsolutePath { get; set; } + public ObservableCollection Children { get; set; } = []; + public List Files { get; set; } = []; + + public string Name + { + get + { + var name = Path.GetFileName(AbsolutePath.TrimEnd(Path.DirectorySeparatorChar)); + return string.IsNullOrEmpty(name) ? AbsolutePath : name; // "C:\" has no file name + } + } +} \ No newline at end of file diff --git a/winui-learning/winui-learning-3/Services/FolderContentService.cs b/winui-learning/winui-learning-3/Services/FolderContentService.cs new file mode 100644 index 0000000..9b40145 --- /dev/null +++ b/winui-learning/winui-learning-3/Services/FolderContentService.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Frozen; +using System.IO; +using System.Linq; +using winui_learning_3.Models; + +namespace winui_learning_3.Services; + +public class FolderContentService +{ + private static readonly FrozenSet TextExt = + new[] { ".txt", ".md", ".cs", ".json", ".xml", ".csv", ".log", ".xaml", ".html", ".css", ".js" } + .ToFrozenSet(StringComparer.OrdinalIgnoreCase); + + private static readonly FrozenSet ImageExt = + new[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".svg", ".ico" } + .ToFrozenSet(StringComparer.OrdinalIgnoreCase); + + private static readonly FrozenSet VideoExt = + new[] { ".mp4", ".mkv", ".avi", ".mov", ".webm" } + .ToFrozenSet(StringComparer.OrdinalIgnoreCase); + + private static readonly FrozenSet AudioExt = + new[] { ".mp3", ".wav", ".flac", ".ogg", ".m4a" } + .ToFrozenSet(StringComparer.OrdinalIgnoreCase); + + public static FolderContentDto GetFolderContent(string? path) + { + path ??= Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + var directory = new DirectoryInfo(path); + + try + { + return new FolderContentDto + { + AbsolutePath = directory.FullName, + Files = [.. directory.EnumerateFiles().Select(ToFileDto)], + Children = + [ + .. directory.EnumerateDirectories().Select(d => new FolderContentDto + { + AbsolutePath = d.FullName, + Files = [], + Children = [] + }) + ] + }; + } + catch (UnauthorizedAccessException ex) + { + throw new UnauthorizedAccessException($"Access to the path '{path}' is denied.", ex); + } + } + + private static FileDto ToFileDto(FileInfo file) => new() + { + AbsolutePath = file.FullName, + Name = file.Name, + Extension = file.Extension, + Type = GetFileType(file.Extension) + }; + + private static FileTypeEnum GetFileType(string extension) => extension switch + { + _ when TextExt.Contains(extension) => FileTypeEnum.Text, + _ when ImageExt.Contains(extension) => FileTypeEnum.Image, + _ when VideoExt.Contains(extension) => FileTypeEnum.Video, + _ when AudioExt.Contains(extension) => FileTypeEnum.Audio, + ".lnk" or ".url" => FileTypeEnum.Link, + _ => FileTypeEnum.Other + }; +} \ No newline at end of file diff --git a/winui-learning/winui-learning-3/Views/HomePage.xaml b/winui-learning/winui-learning-3/Views/HomePage.xaml index c29ca26..42ad218 100644 --- a/winui-learning/winui-learning-3/Views/HomePage.xaml +++ b/winui-learning/winui-learning-3/Views/HomePage.xaml @@ -7,4 +7,67 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:models="using:winui_learning_3.Models" mc:Ignorable="d"> - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/winui-learning/winui-learning-3/Views/HomePage.xaml.cs b/winui-learning/winui-learning-3/Views/HomePage.xaml.cs index 5355fd1..6b19c68 100644 --- a/winui-learning/winui-learning-3/Views/HomePage.xaml.cs +++ b/winui-learning/winui-learning-3/Views/HomePage.xaml.cs @@ -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 _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( + 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 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); } diff --git a/winui-learning/winui-learning-3/winui-learning-3.csproj b/winui-learning/winui-learning-3/winui-learning-3.csproj index 994ce2f..09b4140 100644 --- a/winui-learning/winui-learning-3/winui-learning-3.csproj +++ b/winui-learning/winui-learning-3/winui-learning-3.csproj @@ -51,8 +51,6 @@ - -