Vibenutý 😔
This commit is contained in:
1 parent
d06a9f332b
commit
8cc27e6b8e
10 files changed
+295
-12
No files matched your search
@@ -3,7 +3,7 @@
|
|||||||
x:Class="winui_learning_3.App"
|
x:Class="winui_learning_3.App"
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:local="using:winui_learning_2">
|
xmlns:local="using:winui_learning_3">
|
||||||
<Application.Resources>
|
<Application.Resources>
|
||||||
<ResourceDictionary>
|
<ResourceDictionary>
|
||||||
<ResourceDictionary.MergedDictionaries>
|
<ResourceDictionary.MergedDictionaries>
|
||||||
|
|||||||
@@ -14,7 +14,6 @@
|
|||||||
</Window.SystemBackdrop>
|
</Window.SystemBackdrop>
|
||||||
|
|
||||||
<Grid>
|
<Grid>
|
||||||
|
|
||||||
<Grid.RowDefinitions>
|
<Grid.RowDefinitions>
|
||||||
<!-- Title bar -->
|
<!-- Title bar -->
|
||||||
<RowDefinition Height="Auto"></RowDefinition>
|
<RowDefinition Height="Auto"></RowDefinition>
|
||||||
@@ -26,14 +25,13 @@
|
|||||||
<TitleBar x:Name="AppTitleBar"
|
<TitleBar x:Name="AppTitleBar"
|
||||||
Title="file adventurer"
|
Title="file adventurer"
|
||||||
IsBackButtonVisible="True"
|
IsBackButtonVisible="True"
|
||||||
IsBackButtonEnabled="{x:Bind rootFrame.CanGoBack, Mode=OneWay}"
|
IsBackButtonEnabled="{x:Bind RootFrame.CanGoBack, Mode=OneWay}"
|
||||||
BackRequested="AppTitleBar_BackRequested">
|
BackRequested="AppTitleBar_BackRequested">
|
||||||
<TitleBar.IconSource>
|
<TitleBar.IconSource>
|
||||||
<FontIconSource Glyph=""/>
|
<FontIconSource Glyph=""/>
|
||||||
</TitleBar.IconSource>
|
</TitleBar.IconSource>
|
||||||
</TitleBar>
|
</TitleBar>
|
||||||
|
|
||||||
<Frame x:Name="rootFrame" Grid.Row="1"/>
|
<Frame x:Name="RootFrame" Grid.Row="1" SourcePageType="views:HomePage"/>
|
||||||
<!-- SourcePageType="views:AllNotesPage"/> -->
|
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
@@ -14,9 +14,9 @@ public sealed partial class MainWindow : Window
|
|||||||
|
|
||||||
private void AppTitleBar_BackRequested(TitleBar titleBar, object args)
|
private void AppTitleBar_BackRequested(TitleBar titleBar, object args)
|
||||||
{
|
{
|
||||||
if (rootFrame.CanGoBack)
|
if (RootFrame.CanGoBack)
|
||||||
{
|
{
|
||||||
rootFrame.GoBack();
|
RootFrame.GoBack();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using System.Net.Mime;
|
||||||
|
|
||||||
|
namespace winui_learning_3.Models;
|
||||||
|
|
||||||
|
public enum FileTypeEnum
|
||||||
|
{
|
||||||
|
Text,
|
||||||
|
Link,
|
||||||
|
Image,
|
||||||
|
Video,
|
||||||
|
Audio,
|
||||||
|
Other
|
||||||
|
}
|
||||||
@@ -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<FolderContentDto> Children { get; set; } = [];
|
||||||
|
public List<FileDto> 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<string> TextExt =
|
||||||
|
new[] { ".txt", ".md", ".cs", ".json", ".xml", ".csv", ".log", ".xaml", ".html", ".css", ".js" }
|
||||||
|
.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
private static readonly FrozenSet<string> ImageExt =
|
||||||
|
new[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".svg", ".ico" }
|
||||||
|
.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
private static readonly FrozenSet<string> VideoExt =
|
||||||
|
new[] { ".mp4", ".mkv", ".avi", ".mov", ".webm" }
|
||||||
|
.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
private static readonly FrozenSet<string> 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
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -7,4 +7,67 @@
|
|||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:models="using:winui_learning_3.Models"
|
xmlns:models="using:winui_learning_3.Models"
|
||||||
mc:Ignorable="d">
|
mc:Ignorable="d">
|
||||||
</Page>
|
|
||||||
|
<Page.Resources>
|
||||||
|
<!-- Tree node: a folder -->
|
||||||
|
<DataTemplate x:Key="FolderNodeTemplate" x:DataType="models:FolderContentDto">
|
||||||
|
<TreeViewItem ItemsSource="{x:Bind Children}"
|
||||||
|
Content="{x:Bind Name}"
|
||||||
|
HasUnrealizedChildren="True" />
|
||||||
|
</DataTemplate>
|
||||||
|
|
||||||
|
<!-- Right side: a file row -->
|
||||||
|
<DataTemplate x:Key="FileTemplate" x:DataType="models:FileDto">
|
||||||
|
<Grid Padding="0,10" ColumnSpacing="12">
|
||||||
|
<Grid.ColumnDefinitions>
|
||||||
|
<ColumnDefinition Width="Auto" />
|
||||||
|
<ColumnDefinition Width="*" />
|
||||||
|
<ColumnDefinition Width="Auto" />
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
|
<FontIcon Glyph="" FontSize="16" />
|
||||||
|
<TextBlock Grid.Column="1" Text="{x:Bind Name}"
|
||||||
|
TextTrimming="CharacterEllipsis" VerticalAlignment="Center" />
|
||||||
|
<TextBlock Grid.Column="2" Text="{x:Bind Extension}"
|
||||||
|
Opacity="0.6" VerticalAlignment="Center" />
|
||||||
|
</Grid>
|
||||||
|
</DataTemplate>
|
||||||
|
</Page.Resources>
|
||||||
|
|
||||||
|
<Grid ColumnSpacing="0">
|
||||||
|
<Grid.ColumnDefinitions>
|
||||||
|
<ColumnDefinition Width="280" MinWidth="160" />
|
||||||
|
<ColumnDefinition Width="*" />
|
||||||
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
|
<!-- LEFT: drives + folders -->
|
||||||
|
<Border BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
|
||||||
|
BorderThickness="0,0,1,0">
|
||||||
|
<TreeView x:Name="FolderTree"
|
||||||
|
ItemTemplate="{StaticResource FolderNodeTemplate}"
|
||||||
|
SelectionMode="Single"
|
||||||
|
Expanding="FolderTree_Expanding"
|
||||||
|
ItemInvoked="FolderTree_ItemInvoked" />
|
||||||
|
</Border>
|
||||||
|
|
||||||
|
<!-- RIGHT: path bar + content -->
|
||||||
|
<Grid Grid.Column="1" RowSpacing="8" Padding="12">
|
||||||
|
<Grid.RowDefinitions>
|
||||||
|
<RowDefinition Height="Auto" />
|
||||||
|
<RowDefinition Height="*" />
|
||||||
|
</Grid.RowDefinitions>
|
||||||
|
|
||||||
|
<AutoSuggestBox x:Name="PathBox"
|
||||||
|
PlaceholderText="Type a path, e.g. C:\Users"
|
||||||
|
QueryIcon="Forward"
|
||||||
|
GotFocus="PathBox_GotFocus"
|
||||||
|
TextChanged="PathBox_TextChanged"
|
||||||
|
SuggestionChosen="PathBox_SuggestionChosen"
|
||||||
|
QuerySubmitted="PathBox_QuerySubmitted" />
|
||||||
|
|
||||||
|
<ListView x:Name="FileList" Grid.Row="1"
|
||||||
|
ItemTemplate="{StaticResource FileTemplate}"
|
||||||
|
SelectionMode="Single" />
|
||||||
|
</Grid>
|
||||||
|
</Grid>
|
||||||
|
</Page>
|
||||||
@@ -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 Microsoft.UI.Xaml.Controls;
|
||||||
|
using winui_learning_3.Models;
|
||||||
|
using winui_learning_3.Services;
|
||||||
|
|
||||||
namespace winui_learning_3.Views;
|
namespace winui_learning_3.Views;
|
||||||
|
|
||||||
public sealed partial class HomePage : Page
|
public sealed partial class HomePage : Page
|
||||||
{
|
{
|
||||||
|
private const int MaxHistory = 5;
|
||||||
|
private readonly List<string> _history = [];
|
||||||
|
|
||||||
public HomePage()
|
public HomePage()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
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);
|
||||||
}
|
}
|
||||||
@@ -51,8 +51,6 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Helpers\" />
|
<Folder Include="Helpers\" />
|
||||||
<Folder Include="Models\" />
|
|
||||||
<Folder Include="Services\" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
|
|||||||
Reference in new issue
Block a user