public void MenuCopyPathToClipboard_Click(object sender, RoutedEventArgs e)
        {
            var selected = FolderBrowser.GetSelectedItem();

            string clipText = null;

            if (selected == null)
            {
                clipText = FolderBrowser.FolderPath;
            }
            else if (selected.FullPath == "..")
            {
                clipText = Path.GetDirectoryName(FolderBrowser.FolderPath);
            }
            else
            {
                clipText = selected.FullPath;
            }

            if (!string.IsNullOrEmpty(clipText))
            {
                if (ClipboardHelper.SetText(clipText))
                {
                    Model.Window.ShowStatus($"Path '{clipText}' has been copied to the Clipboard.",
                                            mmApp.Configuration.StatusMessageTimeout);
                }
            }
        }
        public void MenuRenameFile_Click(object sender, RoutedEventArgs e)
        {
            var selected = FolderBrowser.GetSelectedItem();

            if (selected == null)
            {
                return;
            }

            if (selected.FullPath == "..")
            {
                return;
            }

            // Start Editing the file name
            selected.EditName           = selected.DisplayName;
            selected.OriginalRenamePath = selected.FullPath;
            selected.IsEditing          = true;

            var tvItem = FolderBrowser.GetNestedTreeviewItem(selected);

            if (tvItem != null)
            {
                var tb = WindowUtilities.FindVisualChild <TextBox>(tvItem);
                tb?.Focus();
            }
        }
        public void MenuOpenFolderBrowserHere_Click(object sender, RoutedEventArgs e)
        {
            var selected = FolderBrowser.GetSelectedItem();

            if (selected == null)
            {
                return;
            }

            if (selected.FullPath == "..")
            {
                FolderBrowser.FolderPath = Path.GetDirectoryName(FolderBrowser.FolderPath.TrimEnd('\\'));
            }
            else
            {
                if (Directory.Exists(FolderBrowser.FolderPath))
                {
                    FolderBrowser.FolderPath = selected.FullPath;
                }
                else
                {
                    FolderBrowser.FolderPath = Path.GetDirectoryName(FolderBrowser.FolderPath);
                }
            }
        }
        public void MenuShowImage_Click(object sender, RoutedEventArgs e)
        {
            var selected = FolderBrowser.GetSelectedItem();

            if (selected == null)
            {
                return;
            }

            mmFileUtils.OpenImageInImageViewer(selected.FullPath);
        }
        public void MenuOpenTerminal_Click(object sender, RoutedEventArgs e)
        {
            string folder = FolderBrowser.FolderPath;

            var selected = FolderBrowser.GetSelectedItem();

            if (selected != null)
            {
                folder = Path.GetDirectoryName(selected.FullPath);
            }

            if (string.IsNullOrEmpty(folder))
            {
                return;
            }

            mmFileUtils.OpenTerminal(folder);
        }
        private void MenuGitClient_Click(object sender, RoutedEventArgs e)
        {
            var selected = FolderBrowser.GetSelectedItem();

            if (selected == null)
            {
                return;
            }

            var model = mmApp.Model;

            var path = selected.FullPath;

            if (selected.IsFile)
            {
                path = Path.GetDirectoryName(path);
            }

            Model.Commands.Git.OpenGitClientCommand.Execute(path);
        }
        public void MenuCommitGit_Click(object sender, RoutedEventArgs e)
        {
            var selected = FolderBrowser.GetSelectedItem();

            if (selected == null)
            {
                return;
            }

            var model = mmApp.Model;

            string file = selected.FullPath;

            if (string.IsNullOrEmpty(file))
            {
                return;
            }

            bool pushToGit = mmApp.Configuration.Git.GitCommitBehavior == GitCommitBehaviors.CommitAndPush;

            model.Commands.Git.CommitToGitCommand.Execute(file);
        }
        public void MenuOpenInExplorer_Click(object sender, RoutedEventArgs e)
        {
            string folder = FolderBrowser.FolderPath;

            var selected = FolderBrowser.GetSelectedItem();

            if (selected != null)
            {
                folder = selected.FullPath;     // Path.GetDirectoryName(selected.FullPath);
            }
            if (string.IsNullOrEmpty(folder))
            {
                return;
            }

            if (selected == null || selected.IsFolder)
            {
                ShellUtils.GoUrl(folder);
            }
            else
            {
                ShellUtils.OpenFileInExplorer(folder);
            }
        }
        public void ShowContextMenu()
        {
            ClearMenu();

            var  selectedItems         = FolderBrowser.GetSelectedItems();
            bool multipleItemsSelected = selectedItems.Count > 1;

            var cm       = ContextMenu;
            var pathItem = FolderBrowser.GetSelectedItem();

            if (pathItem == null)
            {
                return;
            }

            cm.Items.Clear();

            var ci = new MenuItem();

            ci.Header           = "_New File";
            ci.InputGestureText = "Ctrl-N";
            ci.Click           += MenuAddFile_Click;
            cm.Items.Add(ci);

            ci                  = new MenuItem();
            ci.Header           = "New Folder";
            ci.InputGestureText = "F8";
            ci.Click           += MenuAddDirectory_Click;
            cm.Items.Add(ci);

            cm.Items.Add(new Separator());


            var ciOpen = new MenuItem()
            {
                Header = "Open"
            };

            cm.Items.Add(ciOpen);

            if (pathItem.IsImage)
            {
                cm.Items.Add(new Separator());

                ci        = new MenuItem();
                ci.Header = "Show Image";
                ci.Name   = "FBCM_ShowImage";
                ci.Click += MenuShowImage_Click;
                cm.Items.Add(ci);

                ci        = new MenuItem();
                ci.Header = "Edit Image";
                ci.Name   = "FBCM_EditImage";
                ci.Click += MenuEditImage_Click;
                cm.Items.Add(ci);

                ci        = new MenuItem();
                ci.Header = "Optimize Image";
                ci.Name   = "FBCM_OptimizeImage";
                ci.Click += MenuOptimizeImage_Click;
                cm.Items.Add(ci);

                cm.Items.Add(new Separator());
            }
            else
            {
                if (pathItem.IsFile)
                {
                    ci        = new MenuItem();
                    ci.Header = "Open in Editor";
                    ci.Name   = "FBCM_OpenInEditor";
                    ci.Click += MenuOpenInEditor_Click;
                    ciOpen.Items.Add(ci);
                }

                ci        = new MenuItem();
                ci.Header = "Open with Shell";
                ci.Name   = "FBCM_OpenWithShell";
                ci.Click += MenuOpenWithShell_Click;
                ciOpen.Items.Add(ci);
            }


            ci                  = new MenuItem();
            ci.Header           = "Open With...";
            ci.Name             = "FBCM_OpenWith";
            ci.IsEnabled        = !multipleItemsSelected;
            ci.Command          = Model.Commands.OpenWithCommand;
            ci.CommandParameter = pathItem.FullPath;
            ciOpen.Items.Add(ci);



            ci        = new MenuItem();
            ci.Header = "Show in Explorer";
            ci.Name   = "FBCM_ShowInExplorer";
            ci.Click += MenuOpenInExplorer_Click;
            cm.Items.Add(ci);

            ci        = new MenuItem();
            ci.Header = "Open in Terminal";
            ci.Name   = "FBCM_OpenInTerminal";
            ci.Click += MenuOpenTerminal_Click;
            cm.Items.Add(ci);

            if (pathItem.IsFolder)
            {
                ci        = new MenuItem();
                ci.Header = "Open Folder Browser here";
                ci.Name   = "FBCM_OpenFolderBrowser";
                ci.Click += MenuOpenFolderBrowserHere_Click;
                cm.Items.Add(ci);
            }


            cm.Items.Add(new Separator());


            ci = new MenuItem();

            if (pathItem.DisplayName != "..")
            {
                ci.Header           = "Delete";
                ci.Name             = "FBCM_Delete";
                ci.InputGestureText = "Del";
                ci.Click           += MenuDeleteFile_Click;
                cm.Items.Add(ci);

                ci                  = new MenuItem();
                ci.Header           = "Rename";
                ci.Name             = "FBCM_Rename";
                ci.InputGestureText = "F2";
                ci.Click           += MenuRenameFile_Click;
                cm.Items.Add(ci);

                cm.Items.Add(new Separator());

                ci = new MenuItem
                {
                    Header           = "Cut",
                    ToolTip          = "Cut currently selected file(s)",
                    InputGestureText = "Ctrl-X",
                };
                ci.Name   = "FBCM_CutFile";
                ci.Click += MenuCutFile_Click;
                cm.Items.Add(ci);

                ci = new MenuItem
                {
                    Header           = "Copy",
                    ToolTip          = "Copy currently selected file(s)",
                    InputGestureText = "Ctrl-C",
                };
                ci.Click += MenuCopyFile_Click;
                cm.Items.Add(ci);

                if (Clipboard.ContainsFileDropList())
                {
                    ci = new MenuItem
                    {
                        Header           = "Paste",
                        InputGestureText = "Ctrl-V",
                        ToolTip          = "Paste copied files from the Folder Browser or Explorer into the current folder."
                    };
                    ci.Name   = "FBCM_Paste";
                    ci.Click += MenuPasteFile_Click;
                    cm.Items.Add(ci);
                }
                else if (Clipboard.ContainsImage())
                {
                    ci                  = new MenuItem();
                    ci.Header           = "Paste Clipboard Image as File";
                    ci.Name             = "FBCM_PasteImageAsFile";
                    ci.Command          = Model.Commands.PasteImageToFileCommand;
                    ci.CommandParameter = pathItem.FullPath;
                    cm.Items.Add(ci);
                }

                cm.Items.Add(new Separator());
            }

            ci                  = new MenuItem();
            ci.Header           = "Find in Files";
            ci.Name             = "FBCM_FindInFiles";
            ci.InputGestureText = "Ctrl-F";
            ci.Click           += FolderBrowser.MenuFindFiles_Click;
            cm.Items.Add(ci);

            ci                  = new MenuItem();
            ci.Header           = "Add to Favorites";
            ci.Name             = "FBCM_AddToFavorites";
            ci.Command          = Model.Commands.AddFavoriteCommand;
            ci.CommandParameter = pathItem.FullPath;
            cm.Items.Add(ci);

            cm.Items.Add(new Separator());

            bool   isGit        = false;
            var    git          = new GitHelper();
            string gitRemoteUrl = null;

            using (var repo = git.OpenRepository(pathItem.FullPath))
            {
                isGit = repo != null;
                if (isGit)
                {
                    gitRemoteUrl = repo.Network?.Remotes.FirstOrDefault()?.Url;
                }
            }

            if (isGit)
            {
                var ciGit = new MenuItem()
                {
                    Header = "Git"
                };
                ci.Name = "FBCM_GitSubmenu";
                cm.Items.Add(ciGit);

                ci                  = new MenuItem();
                ci.Header           = "Commit to _Git...";
                ci.Name             = "FBCM_GIT_Commit";
                ci.InputGestureText = "Ctrl-G";
                ci.Click           += MenuCommitGit_Click;
                ciGit.Items.Add(ci);

                if (pathItem.FileStatus == LibGit2Sharp.FileStatus.ModifiedInIndex ||
                    pathItem.FileStatus == LibGit2Sharp.FileStatus.ModifiedInWorkdir)
                {
                    ci                  = new MenuItem();
                    ci.Name             = "FBCM_GIT_UndoChanges";
                    ci.Header           = "_Undo Changes in Git";
                    ci.InputGestureText = "Ctrl-Z";
                    ci.Click           += MenuUndoGit_Click;
                    ciGit.Items.Add(ci);
                }

                ci           = new MenuItem();
                ci.Header    = "Open Folder in Git Client";
                ci.Name      = "FBCM_GIT_OpenFolderInGitClient";
                ci.Click    += MenuGitClient_Click;
                ci.IsEnabled = Model.Configuration.Git.GitClientExecutable != null &&
                               File.Exists(Model.Configuration.Git.GitClientExecutable);
                ciGit.Items.Add(ci);

                if (pathItem.FileStatus != LibGit2Sharp.FileStatus.Nonexistent)
                {
                    if (gitRemoteUrl != null && gitRemoteUrl.Contains("github.com"))
                    {
                        ci                  = new MenuItem();
                        ci.Header           = "Open on GitHub";
                        ci.Name             = "FBCM_PasteImageAsFile";
                        ci.Command          = mmApp.Model.Commands.Git.OpenOnGithubCommand;
                        ci.CommandParameter = pathItem.FullPath;
                        ciGit.Items.Add(ci);
                    }
                }

                cm.Items.Add(new Separator());
            }


            ci        = new MenuItem();
            ci.Header = "Copy Path to Clipboard";
            ci.Name   = "FBCM_CopyPathToClipboard";
            ci.Click += MenuCopyPathToClipboard_Click;
            cm.Items.Add(ci);


            cm.IsOpen = true;

            Show();
        }
        public void FileBrowserPasteFile()
        {
            var selected = FolderBrowser.GetSelectedItem();

            if (selected == null)
            {
                selected = Model.Window.FolderBrowser.ActivePathItem;
            }
            if (selected == null)
            {
                return;
            }

            var data = Clipboard.GetDataObject();

            if (selected.IsEditing && !data.GetDataPresent(DataFormats.FileDrop))
            {
                return;
            }

            var dl = data.GetData(DataFormats.FileDrop) as string[];

            if (dl == null)
            {
                return;
            }

            var  ms    = data.GetData("Preferred DropEffect") as MemoryStream;
            bool isCut = false;

            if (ms != null)
            {
                var bytes = ms.ToArray();
                if (bytes.Length == 4 && bytes[0] == 2 && bytes[1] == 0)
                {
                    isCut = true;
                }
            }

            string path = selected.FullPath;

            if (selected.IsFile)
            {
                path = Path.GetDirectoryName(selected.FullPath);
            }

            string errors = string.Empty;

            foreach (var sourceFile in dl)
            {
                string filename   = Path.GetFileName(sourceFile);
                string targetFile = Path.Combine(path, filename);

                if (sourceFile == targetFile)
                {
                    if (File.Exists(targetFile))
                    {
                        var dir       = Path.GetDirectoryName(targetFile);
                        var justfname = Path.GetFileNameWithoutExtension(targetFile);
                        var ext       = Path.GetExtension(targetFile);
                        targetFile = Path.Combine(dir, $"{justfname} - Copy{ext}");
                    }
                    else if (Directory.Exists(targetFile))
                    {
                        var dir     = Path.GetDirectoryName(targetFile);
                        var dirName = Path.GetFileName(targetFile);
                        targetFile = Path.Combine(dir, $"{dirName} - Copy");
                    }
                    else
                    {
                        Model.Window.ShowStatusError("Can't copy item: Source and target are the same.");
                        return;
                    }
                }

                try
                {
                    if (File.Exists(sourceFile))
                    {
                        if (!isCut)
                        {
                            mmFileUtils.CopyFileOrFolder(sourceFile, targetFile, confirmation: true);
                        }
                        else
                        {
                            mmFileUtils.MoveFileOrFolder(sourceFile, targetFile, confirmation: true);
                        }
                    }
                    else if (Directory.Exists(sourceFile))
                    {
                        if (!isCut)
                        {
                            mmFileUtils.CopyFileOrFolder(sourceFile, targetFile, confirmation: true);
                        }
                        else
                        {
                            mmFileUtils.MoveFileOrFolder(sourceFile, targetFile, confirmation: true);
                        }

                        Dispatcher.CurrentDispatcher.InvokeAsync(() =>
                        {
                            selected.IsSelected = true;
                            selected.IsExpanded = true;
                        }, DispatcherPriority.ApplicationIdle);
                    }
                }
                catch (Exception ex)
                {
                    errors += $"{Path.GetFileName(targetFile)},";
                    Model.Window.ShowStatusError($"Can't copy to {targetFile}: {ex.Message}");
                }
            }

            path = Path.GetDirectoryName(path);

            string fileString = dl.Length == 1 ? "1 item has" : dl.Length + " items have";

            if (isCut)
            {
                Model.Window.ShowStatus($"{fileString} been cut and copied into {path}.");
            }
            else
            {
                Model.Window.ShowStatus($"{fileString} been copied into {path}.");
            }
        }