From 9ef4d183e6eb69a55b8f9ed046901ed0d196f400 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Sat, 9 Jan 2021 22:36:50 -0500 Subject: [PATCH] improve FileSystemEditor navigation and add a path entry text box --- .../Editors/FileSystem/FileSystemEditor.glade | 179 ++++++++++------- .../EditorDocumentExplorer.cs | 34 ++++ .../Editors/FileSystem/FileSystemEditor.cs | 180 ++++++++++++++++-- 3 files changed, 303 insertions(+), 90 deletions(-) diff --git a/Content/UniversalEditor.Content.PlatformIndependent/Editors/FileSystem/FileSystemEditor.glade b/Content/UniversalEditor.Content.PlatformIndependent/Editors/FileSystem/FileSystemEditor.glade index a771c35c..10526e60 100644 --- a/Content/UniversalEditor.Content.PlatformIndependent/Editors/FileSystem/FileSystemEditor.glade +++ b/Content/UniversalEditor.Content.PlatformIndependent/Editors/FileSystem/FileSystemEditor.glade @@ -2,6 +2,111 @@ + + False + + + + + + True + False + vertical + + + True + True + + + False + True + 0 + + + + + True + True + in + + + True + True + tm + 0 + + + multiple + + + + + True + Name + True + True + + + + 0 + + + + + + + True + Size + True + True + + + + 1 + + + + + + + True + Type + True + True + + + + 2 + + + + + + + True + Date modified + True + True + + + + 3 + + + + + + + + + True + True + 1 + + + + + @@ -14,78 +119,4 @@ - - False - - - - - - True - True - tm - - - multiple - - - - - True - Name - True - True - - - - 0 - - - - - - - True - Size - True - True - - - - 1 - - - - - - - True - Type - True - True - - - - 2 - - - - - - - True - Date modified - True - True - - - - 3 - - - - - - - diff --git a/Libraries/UniversalEditor.UserInterface/EditorDocumentExplorer.cs b/Libraries/UniversalEditor.UserInterface/EditorDocumentExplorer.cs index f518641d..efd05dcd 100644 --- a/Libraries/UniversalEditor.UserInterface/EditorDocumentExplorer.cs +++ b/Libraries/UniversalEditor.UserInterface/EditorDocumentExplorer.cs @@ -20,6 +20,7 @@ // along with this program. If not, see . using System; using MBS.Framework; +using MBS.Framework.UserInterface; using MBS.Framework.UserInterface.Controls; using MBS.Framework.UserInterface.Controls.ListView; @@ -46,6 +47,39 @@ namespace UniversalEditor.UserInterface } return null; } + set + { + TreeModelRow row = FindRow(value); + if (row != null) + { + ListViewControl lv = ((MainWindow)(Application.Instance as IHostApplication).CurrentWindow).DocumentExplorerPanel.ListView; + lv.SelectedRows.Clear(); + lv.SelectedRows.Add(row); + + Parent.OnDocumentExplorerSelectionChanged(new EditorDocumentExplorerSelectionChangedEventArgs(value)); + } + } + } + + private TreeModelRow FindRow(EditorDocumentExplorerNode node, TreeModelRow parent = null) + { + ListViewControl lv = ((MainWindow)(Application.Instance as IHostApplication).CurrentWindow).DocumentExplorerPanel.ListView; + + TreeModelRow.TreeModelRowCollection coll = lv.Model.Rows; + if (parent != null) + { + coll = parent.Rows; + } + foreach (TreeModelRow row in coll) + { + if (row.GetExtraData("node") == node) + return row; + + TreeModelRow row2 = FindRow(node, row); + if (row2 != null) + return row2; + } + return null; } public event EditorDocumentExplorerBeforeContextMenuEventHandler BeforeContextMenu; diff --git a/Libraries/UniversalEditor.UserInterface/Editors/FileSystem/FileSystemEditor.cs b/Libraries/UniversalEditor.UserInterface/Editors/FileSystem/FileSystemEditor.cs index 5412d5ab..041fdd40 100644 --- a/Libraries/UniversalEditor.UserInterface/Editors/FileSystem/FileSystemEditor.cs +++ b/Libraries/UniversalEditor.UserInterface/Editors/FileSystem/FileSystemEditor.cs @@ -35,6 +35,9 @@ using MBS.Framework.UserInterface.Input.Mouse; using System.Collections.Specialized; using UniversalEditor.ObjectModels.FileSystem.FileSources; using MBS.Framework; +using MBS.Framework.UserInterface.Controls; +using System.Text; +using System.Diagnostics.Contracts; namespace UniversalEditor.Editors.FileSystem { @@ -43,6 +46,97 @@ namespace UniversalEditor.Editors.FileSystem { private ListViewControl tv = null; private DefaultTreeModel tm = null; + private TextBox txtPath; + + [EventHandler(nameof(txtPath), "KeyDown")] + private void txtPath_KeyDown(object sender, KeyEventArgs e) + { + if (e.Key == KeyboardKey.Enter) + { + IFileSystemObject fso = (ObjectModel as FileSystemObjectModel).FindObject(txtPath.Text); + if (fso != null) + { + NavigateToObject(fso); + } + else + { + if (txtPath.Text == "/") + { + CurrentFolder = null; + return; + } + MessageDialog.ShowDialog(String.Format("Could not find the path {0}.", txtPath.Text), "File System Editor", MessageDialogButtons.OK, MessageDialogIcon.Error); + } + } + } + + private void NavigateToObject(IFileSystemObject[] fsos) + { + if (fsos.Length == 1) + { + NavigateToObject(fsos[0]); + } + else + { + for (int i = 0; i < fsos.Length; i++) + { + if (fsos[i] is Folder) + { + // nautilus does the equivalent of 'CurrentFolder = ...' except opens in multiple tabs + // which... we don't really have the ability to do multiple tabs for the same document at the moment + tv.SelectedRows[i].Expanded = true; + } + else + { + NavigateToObject(fsos[i]); + } + } + } + } + + /// + /// Navigates to the specified . + /// + /// Fso. + private void NavigateToObject(IFileSystemObject fso) + { + Contract.Requires(fso != null); + + if (fso is File) + { + File f = (fso as File); + + EmbeddedFileAccessor ma = new EmbeddedFileAccessor(f); + Document doc = new Document(ma); + doc.Saved += doc_Saved; + (Application.Instance as IHostApplication).CurrentWindow.OpenFile(doc); + } + else if (fso is Folder) + { + CurrentFolder = (fso as Folder); + } + } + + private EditorDocumentExplorerNode FindDocumentExplorerNode(Folder folder, EditorDocumentExplorerNode parent = null) + { + EditorDocumentExplorerNode.EditorDocumentExplorerNodeCollection coll = DocumentExplorer.Nodes; + if (parent != null) + { + coll = parent.Nodes; + } + foreach (EditorDocumentExplorerNode node in coll) + { + if (node.GetExtraData("item") == folder) + { + return node; + } + + EditorDocumentExplorerNode node2 = FindDocumentExplorerNode(folder, node); + if (node2 != null) + return node2; + } + return null; + } internal void ClearSelectionContent(FileSystemSelection sel) { @@ -56,6 +150,7 @@ namespace UniversalEditor.Editors.FileSystem } } + [EventHandler(nameof(tv), nameof(ListViewControl.RowActivated))] private void tv_RowActivated(object sender, ListViewRowActivatedEventArgs e) { FileSystemContextMenu_Open_Click(sender, e); @@ -66,8 +161,6 @@ namespace UniversalEditor.Editors.FileSystem base.OnCreated(e); tv.SelectionMode = SelectionMode.Multiple; - tv.BeforeContextMenu += tv_BeforeContextMenu; - tv.RowActivated += tv_RowActivated; tv.SortContainerRowsFirst = true; Context.AttachCommandEventHandler("FileSystemContextMenu_Open", FileSystemContextMenu_Open_Click); @@ -91,6 +184,8 @@ namespace UniversalEditor.Editors.FileSystem this.tv.DragDropDataRequest += tv_DragDropDataRequest; OnObjectModelChanged(EventArgs.Empty); + + txtPath.Text = GetPath(CurrentFolder); } private void tv_DragDropDataRequest(object sender, DragDropDataRequestEventArgs e) @@ -230,19 +325,23 @@ namespace UniversalEditor.Editors.FileSystem if (tv.SelectedRows.Count < 1) return; + if (tv.SelectedRows.Count == 1) + { + IFileSystemObject fso = (tv.SelectedRows[0].GetExtraData("item")); + if (fso is Folder) + { + CurrentFolder = (fso as Folder); + return; + } + } + + List list = new List(); for (int i = 0; i < tv.SelectedRows.Count; i++) { IFileSystemObject fso = tv.SelectedRows[i].GetExtraData("item"); - if (fso is File) - { - File f = (fso as File); - - EmbeddedFileAccessor ma = new EmbeddedFileAccessor(f); - Document doc = new Document(ma); - doc.Saved += doc_Saved; - (Application.Instance as IHostApplication).CurrentWindow.OpenFile(doc); - } + list.Add(fso); } + NavigateToObject(list.ToArray()); } private void doc_Saved(object sender, EventArgs e) @@ -588,17 +687,17 @@ namespace UniversalEditor.Editors.FileSystem return _er; } - private Folder _CurrentFolder = null; - public Folder CurrentFolder + private IFileSystemContainer _CurrentFolder = null; + public IFileSystemContainer CurrentFolder { get { return _CurrentFolder; } set { bool changed = (_CurrentFolder != value); - _CurrentFolder = value; - if (!changed) return; + _CurrentFolder = value; + txtPath.Text = GetPath(_CurrentFolder); UpdateList(); } } @@ -675,6 +774,38 @@ namespace UniversalEditor.Editors.FileSystem Folder item = e.Node.GetExtraData("item"); CurrentFolder = item; + + if (txtPath != null) + { + txtPath.Text = GetPath(item); + } + } + + /// + /// Returns the fully-qualified path, separated by a forward slash (/) + /// + /// The path. + /// Item. + private string GetPath(IFileSystemObject item) + { + List list = new List(); + if (item != null) + { + IFileSystemContainer parent = item.Parent; + list.Add(item.Name); + while (parent != null) + { + list.Add(parent.Name); + parent = parent.Parent; + } + + list.Reverse(); + } + + if (list.Count == 0) + return "/"; + + return String.Join("/", list); } private void RecursiveAddFolder(Folder f, TreeModelRow parent = null) @@ -850,7 +981,24 @@ namespace UniversalEditor.Editors.FileSystem } } - void tv_BeforeContextMenu(object sender, EventArgs e) + [EventHandler(nameof(tv), nameof(ListViewControl.KeyDown))] + private void tv_KeyDown(object sender, KeyEventArgs e) + { + if (e.Key == KeyboardKey.Back) + { + if (CurrentFolder == null) + { + (Application.Instance as UIApplication).PlaySystemSound(SystemSound.Beep); + return; + } + + Folder parent = (CurrentFolder.Parent as Folder); + CurrentFolder = parent; + } + } + + [EventHandler(nameof(tv), nameof(ListViewControl.BeforeContextMenu))] + private void tv_BeforeContextMenu(object sender, EventArgs e) { TreeModelRow row = null; if (e is MouseEventArgs)