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 interface IWindowFinder
{ {
WindowInfo FindNotepadWindow(); WindowInfo FindNotepadWindow(string windowName);
} }
} }

View file

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

View file

@ -9,6 +9,6 @@ namespace Microsoft.Extensions.Logging
/// <summary> /// <summary>
/// Name of window to search. /// Name of window to search.
/// </summary> /// </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; readonly IDisposable optionsReloadToken;
NotepadLoggerOptions options; 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() public void Dispose()
{ {
@ -42,10 +42,6 @@ namespace Notepad.Extensions.Logging
void ReloadLoggerOptions(NotepadLoggerOptions options) void ReloadLoggerOptions(NotepadLoggerOptions options)
{ {
this.options = 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; return true;
} }
static Regex notepadPlusPlusRegex = new Regex(@" - Notepad\+\+$", RegexOptions.Compiled); static Regex notepadPlusPlusRegex = new Regex(@"^new \d+ - Notepad\+\+$", RegexOptions.Compiled);
bool IsKnownNotepadWindow(string titleText) bool IsKnownNotepadWindow(string titleText)
{ {
@ -57,24 +57,16 @@ namespace Notepad.Extensions.Logging
{ {
if (WindowName.Equals(titleText, StringComparison.Ordinal)) 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) WindowKind = WindowKind.Notepad;
{ }
case "Untitled - Notepad": else if (notepadPlusPlusRegex.IsMatch(titleText))
WindowKind = WindowKind.Notepad; {
break; WindowKind = WindowKind.NotepadPlusPlus;
default:
if (notepadPlusPlusRegex.IsMatch(titleText))
{
WindowKind = WindowKind.NotepadPlusPlus;
}
break;
}
} }
Handle = FindInnerWindow(WindowKind); Handle = FindInnerWindow(WindowKind);

View file

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