public void MenuAddDirectory_Click(object sender, RoutedEventArgs e) { var selected = TreeFolderBrowser.SelectedItem as PathItem; if (selected == null || selected.Parent == null) { // No files/folders selected = new PathItem() { IsFolder = true, FullPath = Sidebar.FolderPath, Parent = Sidebar.ActivePathItem.Parent, Files = Sidebar.ActivePathItem.Files }; Sidebar.ActivePathItem = selected; } string path; if (!selected.IsFolder) { path = Path.Combine(Path.GetDirectoryName(selected.FullPath), "NewFolder"); } else { var treeItem = Sidebar.GetNestedTreeviewItem(selected); if (treeItem != null) { treeItem.IsExpanded = true; } path = Path.Combine(selected.FullPath, "NewFolder"); } var item = new PathItem { FullPath = path, IsFolder = true, IsEditing = true, IsSelected = true }; item.SetIcon(); if (!selected.IsFolder) { item.Parent = selected.Parent; } else { item.Parent = selected; } item.Parent.Files.Insert(0, item); Sidebar.SetTreeViewSelectionByItem(item); }
public void MenuAddFile_Click(object sender, RoutedEventArgs e) { var selected = TreeFolderBrowser.SelectedItem as PathItem; if (selected == null) { // No files/folders selected = new PathItem() { IsFolder = true, FullPath = Sidebar.FolderPath }; Sidebar.ActivePathItem = selected; } string path; TreeViewItem parentTreeViewItem = Sidebar.GetNestedTreeviewItem(selected); if (selected.FullPath == "..") { path = Path.Combine(Sidebar.FolderPath, "README.md"); } else if (!selected.IsFolder) { path = Path.Combine(Path.GetDirectoryName(selected.FullPath), "README.md"); } else { if (parentTreeViewItem != null) { parentTreeViewItem.IsExpanded = true; } path = Path.Combine(selected.FullPath, "README.md"); } if (File.Exists(path)) { path = Path.Combine(Path.GetDirectoryName(path), "NewFile.md"); if (File.Exists(path)) { for (int i = 1; i < 30; i++) { path = Path.Combine(Path.GetDirectoryName(path), $"NewFile{i}.md"); if (!File.Exists(path)) { break; } } } } var item = new PathItem { FullPath = path, IsFolder = false, IsFile = true, IsEditing = true, IsSelected = true }; item.EditName = item.DisplayName; item.SetIcon(); if (selected.FullPath == "..") { item.Parent = Sidebar.ActivePathItem; // current path } else if (!selected.IsFolder) { item.Parent = selected.Parent; } else { item.Parent = selected; } item.Parent.Files.Insert(0, item); Model.Window.Dispatcher.InvokeAsync( () => Sidebar.SetTreeViewSelectionByItem(item, parentTreeViewItem), DispatcherPriority.ApplicationIdle); }
public void MenuDeleteFile_Click(object sender, RoutedEventArgs e) { var selected = TreeFolderBrowser.SelectedItem as PathItem; if (selected == null) { return; } if (MessageBox.Show( "Delete:\r\n" + selected.FullPath + "\r\n\r\n" + "Are you sure?", "Delete File", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes) { return; } try { //Directory.Delete(selected.FullPath, true); //File.Delete(selected.FullPath); // Recyle Bin Code can handle both files and directories if (!mmFileUtils.MoveToRecycleBin(selected.FullPath)) { return; } var parent = selected.Parent; var index = -1; var file = parent?.Files?.FirstOrDefault(fl => fl.FullPath == selected.FullPath); if (file != null) { var tab = Model.Window.GetTabFromFilename(file.FullPath); if (tab != null) { Model.Window.CloseTab(tab, dontPromptForSave: true); } if (parent != null) { index = parent.Files.IndexOf(selected); parent.Files.Remove(file); if (index > 0) { Sidebar.SetTreeViewSelectionByItem(parent.Files[index]); } } } // Delay required to overcome editor focus after MsgBox Model.Window.Dispatcher.Delay(700, s => TreeFolderBrowser.Focus()); } catch (Exception ex) { Model.Window.ShowStatusError("Delete operation failed: " + ex.Message); } }