Skip to content

Commit

Permalink
Add TryOpenDocumentByPath to ShellViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
diluculo committed Jul 27, 2018
1 parent e467131 commit ca697e2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
8 changes: 5 additions & 3 deletions src/Gemini/Framework/Services/IShell.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Caliburn.Micro;
using Gemini.Modules.MainMenu;
using Gemini.Modules.StatusBar;
Expand Down Expand Up @@ -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();
}
}
}
15 changes: 7 additions & 8 deletions src/Gemini/Modules/Shell/Commands/OpenFileCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.ComponentModel.Composition;
using System.ComponentModel.Composition;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -8,6 +8,7 @@
using Gemini.Framework.Services;
using Microsoft.Win32;
using System.IO;
using Gemini.Framework.Threading;

namespace Gemini.Modules.Shell.Commands
{
Expand All @@ -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();

Expand All @@ -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<IDocument> GetEditor(string path)
Expand Down Expand Up @@ -74,4 +73,4 @@ internal static Task<IDocument> GetEditor(string path)
return Task.FromResult(editor);
}
}
}
}
36 changes: 27 additions & 9 deletions src/Gemini/Modules/Shell/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -307,4 +325,4 @@ public void Close()
Application.Current.MainWindow.Close();
}
}
}
}

0 comments on commit ca697e2

Please sign in to comment.