From ca697e2e59dddb5183fd10e7ca7d52d9fd153e2c Mon Sep 17 00:00:00 2001 From: diluculo Date: Fri, 27 Jul 2018 12:28:36 +0900 Subject: [PATCH] Add TryOpenDocumentByPath to ShellViewModel --- src/Gemini/Framework/Services/IShell.cs | 8 +++-- .../Shell/Commands/OpenFileCommandHandler.cs | 15 ++++---- .../Shell/ViewModels/ShellViewModel.cs | 36 ++++++++++++++----- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/Gemini/Framework/Services/IShell.cs b/src/Gemini/Framework/Services/IShell.cs index 50943ae5..9dc6495e 100644 --- a/src/Gemini/Framework/Services/IShell.cs +++ b/src/Gemini/Framework/Services/IShell.cs @@ -1,4 +1,4 @@ -using System; +using System; using Caliburn.Micro; using Gemini.Modules.MainMenu; using Gemini.Modules.StatusBar; @@ -32,9 +32,11 @@ public interface IShell : IGuardClose, IDeactivate void ShowTool(ITool model); bool TryActivateDocumentByPath(string path); - void OpenDocument(IDocument model); + void TryOpenDocumentByPath(string path); + + void OpenDocument(IDocument model); void CloseDocument(IDocument document); void Close(); } -} \ No newline at end of file +} diff --git a/src/Gemini/Modules/Shell/Commands/OpenFileCommandHandler.cs b/src/Gemini/Modules/Shell/Commands/OpenFileCommandHandler.cs index 90213197..b0456533 100644 --- a/src/Gemini/Modules/Shell/Commands/OpenFileCommandHandler.cs +++ b/src/Gemini/Modules/Shell/Commands/OpenFileCommandHandler.cs @@ -1,4 +1,4 @@ -using System.ComponentModel.Composition; +using System.ComponentModel.Composition; using System.Linq; using System.Threading.Tasks; using System.Windows; @@ -8,6 +8,7 @@ using Gemini.Framework.Services; using Microsoft.Win32; using System.IO; +using Gemini.Framework.Threading; namespace Gemini.Modules.Shell.Commands { @@ -24,7 +25,7 @@ public OpenFileCommandHandler(IShell shell, [ImportMany] IEditorProvider[] edito _editorProviders = editorProviders; } - public override async Task Run(Command command) + public override Task Run(Command command) { var dialog = new OpenFileDialog(); @@ -39,12 +40,10 @@ public override async Task Run(Command command) { var fullPath = Path.GetFullPath(dialog.FileName); - if (!_shell.TryActivateDocumentByPath(fullPath)) - _shell.OpenDocument(await GetEditor(fullPath)); - - // Add the file to the recent documents list - _shell.RecentFiles.Update(fullPath); + _shell.TryOpenDocumentByPath(fullPath); } + + return TaskUtility.Completed; } internal static Task GetEditor(string path) @@ -74,4 +73,4 @@ internal static Task GetEditor(string path) return Task.FromResult(editor); } } -} \ No newline at end of file +} diff --git a/src/Gemini/Modules/Shell/ViewModels/ShellViewModel.cs b/src/Gemini/Modules/Shell/ViewModels/ShellViewModel.cs index 48a3da50..c56571a9 100644 --- a/src/Gemini/Modules/Shell/ViewModels/ShellViewModel.cs +++ b/src/Gemini/Modules/Shell/ViewModels/ShellViewModel.cs @@ -1,19 +1,20 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.Composition; -using System.IO; -using System.Linq; -using System.Windows; using Caliburn.Micro; using Gemini.Framework; using Gemini.Framework.Services; using Gemini.Framework.Themes; using Gemini.Modules.MainMenu; +using Gemini.Modules.RecentFiles; +using Gemini.Modules.Shell.Commands; using Gemini.Modules.Shell.Services; using Gemini.Modules.Shell.Views; using Gemini.Modules.StatusBar; using Gemini.Modules.ToolBars; -using Gemini.Modules.RecentFiles; +using System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.IO; +using System.Linq; +using System.Windows; namespace Gemini.Modules.Shell.ViewModels { @@ -202,7 +203,24 @@ public bool TryActivateDocumentByPath(string path) return false; } - public void OpenDocument(IDocument model) + public async void TryOpenDocumentByPath(string path) + { + if (!File.Exists(path)) + return; + + if (!TryActivateDocumentByPath(path)) + { + var editor = await OpenFileCommandHandler.GetEditor(path); + if (editor != null) + { + OpenDocument(editor); + // Add the file to the recent documents list + RecentFiles.Update(path); + } + } + } + + public void OpenDocument(IDocument model) { ActivateItem(model); } @@ -307,4 +325,4 @@ public void Close() Application.Current.MainWindow.Close(); } } -} \ No newline at end of file +}