optimize<FolderDialog>: simplify FolderDialog interface

This commit is contained in:
leo 2020-11-23 14:02:13 +08:00
parent 5a9c4c32b0
commit 9d9e741aa5
6 changed files with 16 additions and 43 deletions

View file

@ -45,8 +45,9 @@ namespace SourceGit.UI {
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void SelectParentFolder(object sender, RoutedEventArgs e) { private void SelectParentFolder(object sender, RoutedEventArgs e) {
var dialog = new FolderDailog("Select folder to store repository", null); FolderDailog.Open("Select folder to store repository", path => {
dialog.Open(path => txtParentFolder.Text = path); txtParentFolder.Text = path;
});
} }
/// <summary> /// <summary>

View file

@ -297,8 +297,7 @@ namespace SourceGit.UI {
MenuItem saveAs = new MenuItem(); MenuItem saveAs = new MenuItem();
saveAs.Header = "Save As ..."; saveAs.Header = "Save As ...";
saveAs.Click += (obj, ev) => { saveAs.Click += (obj, ev) => {
var dialog = new FolderDailog("Save To ...", null); FolderDailog.Open("Save file to ...", saveTo => {
dialog.Open(saveTo => {
var savePath = Path.Combine(saveTo, Path.GetFileName(path)); var savePath = Path.Combine(saveTo, Path.GetFileName(path));
commit.SaveFileTo(repo, path, savePath); commit.SaveFileTo(repo, path, savePath);
}); });
@ -503,8 +502,7 @@ namespace SourceGit.UI {
saveAs.Header = "Save As ..."; saveAs.Header = "Save As ...";
saveAs.IsEnabled = node.CommitObject == null || node.CommitObject.Kind == Git.Commit.Object.Type.Blob; saveAs.IsEnabled = node.CommitObject == null || node.CommitObject.Kind == Git.Commit.Object.Type.Blob;
saveAs.Click += (obj, ev) => { saveAs.Click += (obj, ev) => {
var dialog = new FolderDailog("Save To ...", null); FolderDailog.Open("Save file to ...", saveTo => {
dialog.Open(saveTo => {
var path = Path.Combine(saveTo, node.Name); var path = Path.Combine(saveTo, node.Name);
commit.SaveFileTo(repo, node.FilePath, path); commit.SaveFileTo(repo, node.FilePath, path);
}); });

View file

@ -92,8 +92,8 @@ namespace SourceGit.UI {
/// Constructor /// Constructor
/// </summary> /// </summary>
/// <param name="title"></param> /// <param name="title"></param>
/// <param name="initPath"></param> /// <param name="cb"></param>
public FolderDailog(string title, string initPath) { public FolderDailog(string title, Action<string> onOK) {
InitializeComponent(); InitializeComponent();
// Move to center. // Move to center.
@ -104,46 +104,23 @@ namespace SourceGit.UI {
var drives = DriveInfo.GetDrives(); var drives = DriveInfo.GetDrives();
foreach (var drive in drives) { foreach (var drive in drives) {
var node = new Node(drive.Name, drive.Name); var node = new Node(drive.Name, drive.Name);
node.CollectChildren(); node.CollectChildren();
if (initPath != null && initPath.StartsWith(drive.Name)) InitializePath(node, initPath);
root.Children.Add(node); root.Children.Add(node);
} }
cb = onOK;
btnSure.IsEnabled = false;
txtTitle.Content = title.ToUpper(); txtTitle.Content = title.ToUpper();
treePath.ItemsSource = root.Children; treePath.ItemsSource = root.Children;
if (selected != null) {
Helpers.TreeViewHelper.SelectOneByContext(treePath, selected);
} else {
btnSure.IsEnabled = false;
}
} }
/// <summary> /// <summary>
/// Set callbacks on click OK. /// Open dialog.
/// </summary> /// </summary>
/// <param name="title"></param>
/// <param name="onOK"></param> /// <param name="onOK"></param>
public void Open(Action<string> onOK) { public static void Open(string title, Action<string> onOK) {
cb = onOK; new FolderDailog(title, onOK).Show();
Show();
}
/// <summary>
/// Initialize given path.
/// </summary>
/// <param name="parent"></param>
/// <param name="initPath"></param>
private void InitializePath(Node parent, string initPath) {
foreach (var child in parent.Children) {
child.CollectChildren();
if (child.Path == initPath) {
selected = child;
} else if (initPath.StartsWith(child.Path)) {
InitializePath(child, initPath);
}
}
} }
#region EVENTS #region EVENTS

View file

@ -586,8 +586,7 @@ namespace SourceGit.UI {
var patch = new MenuItem(); var patch = new MenuItem();
patch.Header = "Save As Patch"; patch.Header = "Save As Patch";
patch.Click += (o, e) => { patch.Click += (o, e) => {
var dialog = new FolderDailog("Save To ...", null); FolderDailog.Open("Save patch to ...", saveTo => {
dialog.Open(saveTo => {
Repo.RunCommand($"format-patch {commit.SHA} -1 -o \"{saveTo}\"", null); Repo.RunCommand($"format-patch {commit.SHA} -1 -o \"{saveTo}\"", null);
}); });
}; };

View file

@ -44,8 +44,7 @@ namespace SourceGit.UI {
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void OpenOrAddRepo(object sender, RoutedEventArgs e) { private void OpenOrAddRepo(object sender, RoutedEventArgs e) {
var dialog = new FolderDailog("Open or init local repository", null); FolderDailog.Open("Open or init local repository", path => {
dialog.Open(path => {
CheckAndOpenRepo(path); CheckAndOpenRepo(path);
}); });
} }

View file

@ -116,8 +116,7 @@ namespace SourceGit.UI {
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
private void SelectDefaultClonePath(object sender, RoutedEventArgs e) { private void SelectDefaultClonePath(object sender, RoutedEventArgs e) {
var dialog = new FolderDailog("Select Folder To Clone Repository Into As Default", null); FolderDailog.Open("Select default clone path", path => {
dialog.Open(path => {
txtGitCloneDir.Text = path; txtGitCloneDir.Text = path;
App.Preference.GitDefaultCloneDir = path; App.Preference.GitDefaultCloneDir = path;
}); });