Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
heku committed Nov 7, 2021
1 parent 9e9bd2e commit 1087311
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 25 deletions.
12 changes: 2 additions & 10 deletions Kool.EditProject.Shared/Models/CustomEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using static Kool.EditProject.EditProjectPackage;

namespace Kool.EditProject.Models
{
Expand All @@ -16,17 +17,8 @@ internal sealed class CustomEditor : IFileEditor
[DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr hWnd);

private readonly string _exe;
private readonly string _arg;

private static readonly Dictionary<string, int> EditingFiles = new(StringComparer.OrdinalIgnoreCase);

public CustomEditor(string exe, string arg)
{
_exe = exe;
_arg = arg;
}

public void OpenFile(string file)
{
if (EditingFiles.TryGetValue(file, out var id))
Expand All @@ -49,7 +41,7 @@ public void OpenFile(string file)
var process = new Process
{
EnableRaisingEvents = true,
StartInfo = new ProcessStartInfo(_exe, _arg.Replace("$FILE", file))
StartInfo = new ProcessStartInfo(Options.EditorExe, Options.EditorArg.Replace("$FILE", file))
};
process.Exited += (_, _) => EditingFiles.Remove(file);
process.Start();
Expand Down
14 changes: 1 addition & 13 deletions Kool.EditProject.Shared/Models/FileEditorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,7 @@ internal static class FileEditorFactory
{
private static IFileEditor CachedEditor;

public static IFileEditor GetEditor()
{
return CachedEditor ??= CreateEditor();

#pragma warning disable IDE1006 // Naming Styles
static IFileEditor CreateEditor()
#pragma warning restore IDE1006 // Naming Styles
{
return Options.UseCustomEditor
? new CustomEditor(Options.EditorExe, Options.EditorArg)
: new VsEditor();
}
}
public static IFileEditor GetEditor() => CachedEditor ??= Options.UseCustomEditor ? new CustomEditor() : new VsEditor();

public static void ClearCache() => CachedEditor = null;
}
Expand Down
3 changes: 1 addition & 2 deletions Kool.EditProject.Shared/Pages/OptionsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public OptionsPage(Options options)

private void OnBrowseButtonClicked(object sender, RoutedEventArgs e)
{
// TODO: How to localize it?
var dialog = new OpenFileDialog();
if (!string.IsNullOrWhiteSpace(_options.EditorExe))
{
Expand All @@ -41,7 +40,7 @@ private void OnTestButtonClicked(object sender, RoutedEventArgs e)
{
var file = Path.GetTempFileName();
File.WriteAllText(file, "Hello World");
new CustomEditor(_options.EditorExe, _options.EditorArg).OpenFile(file);
new CustomEditor().OpenFile(file);
}
catch (Exception ex)
{
Expand Down
Binary file modified Screenshots/MultipleProjects.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/Options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/SingleProject.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Screenshots/Solution.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1087311

Please sign in to comment.