Vibenutý 😔
This commit is contained in:
1 parent
d06a9f332b
commit
8cc27e6b8e
10 files changed
+295
-12
No files matched your search
@@ -7,4 +7,67 @@
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:models="using:winui_learning_3.Models"
|
||||
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 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