From f30dcf5ef41d54c588d7b42c48be8d941abba72e Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 8 Jan 2017 09:57:10 -0500 Subject: Initial upload --- ShiftOS.Wpf/Applications/FileDialog.xaml.cs | 285 ++++++++++++++++++++++++++++ 1 file changed, 285 insertions(+) create mode 100644 ShiftOS.Wpf/Applications/FileDialog.xaml.cs (limited to 'ShiftOS.Wpf/Applications/FileDialog.xaml.cs') diff --git a/ShiftOS.Wpf/Applications/FileDialog.xaml.cs b/ShiftOS.Wpf/Applications/FileDialog.xaml.cs new file mode 100644 index 0000000..820ceeb --- /dev/null +++ b/ShiftOS.Wpf/Applications/FileDialog.xaml.cs @@ -0,0 +1,285 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using ShiftOS.Objects.ShiftFS; +using ShiftOS.Engine; + +namespace ShiftOS.Wpf.Applications +{ + /// + /// Interaction logic for FileDialog.xaml + /// + public partial class FileDialog : UserControl, IShiftOSWindow + { + public FileDialog(string[] filters, FileOpenerStyle style, Action callback) + { + InitializeComponent(); + Filters = filters; + OpenerStyle = style; + Callback = callback; + } + + public string[] Filters { get; set; } + public FileOpenerStyle OpenerStyle { get; set; } + public Action Callback { get; set; } + + private string currentFolder = "0:"; + + public void OnLoad() + { + Reset(); + foreach(var f in Filters) + { + filter.Items.Add(f); + } + filter.SelectedIndex = 0; + } + + public string GetParent(string path) + { + string[] pathlist = path.Split(new[] { "/" }, StringSplitOptions.RemoveEmptyEntries); + if (pathlist.Length > 1) + { + if (path.EndsWith("/")) + { + path = path.Remove(path.Length - 1, 1); + } + path = path.Remove(path.LastIndexOf('/'), path.Length - path.LastIndexOf('/')); + return path; + } + else + { + return "__system"; + } + } + + public void Reset() + { + if (currentFolder != "__system") + this.SetTitle("File Skimmer - " + currentFolder); + else + this.SetTitle("File Skimmer"); + + lbfiles.Children.Clear(); + if (currentFolder == "__system") + { + foreach (var dir in Utils.Mounts) + { + var sp = new StackPanel(); + sp.Width = 50; + sp.Margin = new Thickness(5); + var label = new TextBlock(); + label.Text = dir.Name; + label.TextWrapping = TextWrapping.Wrap; + label.TextAlignment = TextAlignment.Center; + + var img = new Image(); + img.Width = 42; + img.Height = 42; + img.Source = FileSkimmerBackend.GetImage(Utils.Mounts.IndexOf(dir) + ":").ToBitmapImage(); + + sp.Children.Add(img); + sp.Children.Add(label); + + sp.PreviewMouseLeftButtonDown += (o, a) => + { + if (a.ClickCount == 2) + { + ChangeDirectory(Utils.Mounts.IndexOf(dir) + ":"); + } + }; + + lbfiles.Children.Add(sp); + + } + } + else + { + var __up = CreateUpOneDirectory(); + lbfiles.Children.Add(__up); + + foreach (var dir in Utils.GetDirectories(currentFolder)) + { + var sp = new StackPanel(); + sp.Margin = new Thickness(5); + sp.Width = 50; + var label = new TextBlock(); + label.Text = Utils.GetDirectoryInfo(dir).Name; + label.TextWrapping = TextWrapping.Wrap; + label.TextAlignment = TextAlignment.Center; + + var img = new Image(); + img.Width = 42; + img.Height = 42; + img.Source = FileSkimmerBackend.GetImage(dir).ToBitmapImage(); + + sp.Children.Add(img); + sp.Children.Add(label); + + sp.PreviewMouseLeftButtonDown += (o, a) => + { + if (a.ClickCount == 2) + { + ChangeDirectory(dir); + } + }; + + lbfiles.Children.Add(sp); + + } + + foreach (var dir in Utils.GetFiles(currentFolder)) + { + if (dir.EndsWith(filter.SelectedItem.ToString())) + { + var sp = new StackPanel(); + sp.Margin = new Thickness(5); + sp.Width = 50; + var label = new TextBlock(); + label.Text = Utils.GetFileInfo(dir).Name; + label.TextWrapping = TextWrapping.Wrap; + label.TextAlignment = TextAlignment.Center; + + var img = new Image(); + img.Width = 42; + img.Height = 42; + img.Source = FileSkimmerBackend.GetImage(dir).ToBitmapImage(); + + sp.Children.Add(img); + sp.Children.Add(label); + + sp.PreviewMouseLeftButtonDown += (o, a) => + { + if (a.ClickCount == 2) + { + if(OpenerStyle == FileOpenerStyle.Open) + { + Callback?.Invoke(dir); + AppearanceManager.Close(this); + } + } + else + { + txtfilename.Text = Utils.GetFileInfo(dir).Name; + } + }; + + lbfiles.Children.Add(sp); + } + } + } + } + + public void ChangeDirectory(string path) + { + currentFolder = path; + Reset(); + } + + private StackPanel CreateUpOneDirectory() + { + var sp = new StackPanel(); + sp.Margin = new Thickness(5); + sp.Width = 50; + var label = new TextBlock(); + label.Text = "Up one directory"; + label.TextWrapping = TextWrapping.Wrap; + label.TextAlignment = TextAlignment.Center; + + var img = new Image(); + img.Width = 42; + img.Height = 42; + img.Source = FileSkimmerBackend.GetImage("__upone").ToBitmapImage(); + + sp.Children.Add(img); + sp.Children.Add(label); + + sp.PreviewMouseLeftButtonDown += (o, a) => + { + if (a.ClickCount == 2) + { + GoUp(); + } + }; + + return sp; + } + + public void GoUp() + { + if (currentFolder.EndsWith(":")) + { + currentFolder = "__system"; + Reset(); + } + else + { + currentFolder = GetParent(currentFolder); + Reset(); + } + } + + public void OnSkinLoad() + { + } + + public bool OnUnload() + { + return true; + } + + public void OnUpgrade() + { + + } + + + private void btnconfirm_Click(object sender, RoutedEventArgs e) + { + if (!string.IsNullOrWhiteSpace(txtfilename.Text)) + { + if (!txtfilename.Text.EndsWith(filter.SelectedItem.ToString())) + txtfilename.Text += filter.SelectedItem.ToString(); + + switch (OpenerStyle) + { + case FileOpenerStyle.Open: + if(Utils.FileExists(currentFolder + "/" + txtfilename.Text)) + { + Callback?.Invoke(currentFolder + "/" + txtfilename.Text); + AppearanceManager.Close(this); + } + else + { + Infobox.Show("File not found", $"The file {currentFolder}/{txtfilename.Text} was not found."); + } + break; + case FileOpenerStyle.Save: + Callback?.Invoke(currentFolder + "/" + txtfilename.Text); + AppearanceManager.Close(this); + break; + } + } + else + { + Infobox.Show("File Skimmer", "Please provide a filename."); + } + } + + private void filter_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + Reset(); + } + } +} -- cgit v1.2.3