Moved window name to IWindowFinder method parameter from property.

This commit is contained in:
Alexander Kislitsyn 2020-05-23 14:54:13 +03:00
parent bf96d0622a
commit f1eafc15db
6 changed files with 17 additions and 29 deletions

View file

@ -2,6 +2,6 @@
{
interface IWindowFinder
{
WindowInfo FindNotepadWindow();
WindowInfo FindNotepadWindow(string windowName);
}
}

View file

@ -11,16 +11,18 @@ namespace Notepad.Extensions.Logging
{
class NotepadLogger : ILogger
{
public NotepadLogger(ObjectPool<StringBuilder> stringBuilderPool, IWindowFinder windowFinder, string categoryName)
public NotepadLogger(ObjectPool<StringBuilder> stringBuilderPool, IWindowFinder windowFinder, string categoryName, string windowName)
{
this.stringBuilderPool = stringBuilderPool ?? throw new ArgumentNullException(nameof(stringBuilderPool));
this.windowFinder = windowFinder ?? throw new ArgumentNullException(nameof(windowFinder));
this.categoryName = categoryName;
this.windowName = windowName;
}
readonly ObjectPool<StringBuilder> stringBuilderPool;
readonly IWindowFinder windowFinder;
readonly string categoryName;
readonly string windowName;
public IDisposable BeginScope<TState>(TState state) => NullDisposable.Instance;
@ -90,7 +92,7 @@ namespace Notepad.Extensions.Logging
void WriteToNotepad(string message)
{
var info = windowFinder.FindNotepadWindow();
var info = windowFinder.FindNotepadWindow(windowName);
switch (info.Kind)
{
case WindowKind.Notepad:

View file

@ -9,6 +9,6 @@ namespace Microsoft.Extensions.Logging
/// <summary>
/// Name of window to search.
/// </summary>
public string WindowName { get; set; } = "Untitled - Notepad";
public string WindowName { get; set; }
}
}

View file

@ -32,7 +32,7 @@ namespace Notepad.Extensions.Logging
readonly IDisposable optionsReloadToken;
NotepadLoggerOptions options;
public ILogger CreateLogger(string categoryName) => new NotepadLogger(stringBuilderPool, windowFinder, categoryName);
public ILogger CreateLogger(string categoryName) => new NotepadLogger(stringBuilderPool, windowFinder, categoryName, options.WindowName);
public void Dispose()
{
@ -42,10 +42,6 @@ namespace Notepad.Extensions.Logging
void ReloadLoggerOptions(NotepadLoggerOptions options)
{
this.options = options;
if (windowFinder is WindowFinder finder)
{
finder.WindowName = this.options.WindowName;
}
}
}
}

View file

@ -49,7 +49,7 @@ namespace Notepad.Extensions.Logging
return true;
}
static Regex notepadPlusPlusRegex = new Regex(@" - Notepad\+\+$", RegexOptions.Compiled);
static Regex notepadPlusPlusRegex = new Regex(@"^new \d+ - Notepad\+\+$", RegexOptions.Compiled);
bool IsKnownNotepadWindow(string titleText)
{
@ -57,24 +57,16 @@ namespace Notepad.Extensions.Logging
{
if (WindowName.Equals(titleText, StringComparison.Ordinal))
{
WindowKind = notepadPlusPlusRegex.IsMatch(titleText) ? WindowKind.NotepadPlusPlus : WindowKind.Notepad;
WindowKind = titleText.EndsWith(" - Notepad++") ? WindowKind.NotepadPlusPlus : WindowKind.Notepad;
}
}
else
else if (titleText.Equals("Untitled - Notepad", StringComparison.Ordinal))
{
switch (titleText)
{
case "Untitled - Notepad":
WindowKind = WindowKind.Notepad;
break;
default:
if (notepadPlusPlusRegex.IsMatch(titleText))
{
WindowKind = WindowKind.NotepadPlusPlus;
}
break;
}
WindowKind = WindowKind.Notepad;
}
else if (notepadPlusPlusRegex.IsMatch(titleText))
{
WindowKind = WindowKind.NotepadPlusPlus;
}
Handle = FindInnerWindow(WindowKind);

View file

@ -12,14 +12,12 @@ namespace Notepad.Extensions.Logging
readonly ObjectPool<WindowEnumerationState> statePool;
public string WindowName { get; internal set; }
public WindowInfo FindNotepadWindow()
public WindowInfo FindNotepadWindow(string windowName)
{
var stateObject = statePool.Get();
try
{
stateObject.WindowName = WindowName;
stateObject.WindowName = windowName;
NativeMethods.EnumWindows(enumWindowsDelegate, stateObject);
return new WindowInfo(stateObject.WindowKind, stateObject.Handle);
}