From f1eafc15dbb17cdff788d54b5fcde9f16e703e4e Mon Sep 17 00:00:00 2001 From: Alexander Kislitsyn <4lex.kislitsyn@gmail.com> Date: Sat, 23 May 2020 14:54:13 +0300 Subject: [PATCH] Moved window name to IWindowFinder method parameter from property. --- Notepad.Extensions.Logging/IWindowFinder.cs | 2 +- Notepad.Extensions.Logging/NotepadLogger.cs | 6 +++-- .../NotepadLoggerOptions.cs | 2 +- .../NotepadLoggerProvider.cs | 6 +---- .../WindowEnumerationState.cs | 24 +++++++------------ Notepad.Extensions.Logging/WindowFinder.cs | 6 ++--- 6 files changed, 17 insertions(+), 29 deletions(-) diff --git a/Notepad.Extensions.Logging/IWindowFinder.cs b/Notepad.Extensions.Logging/IWindowFinder.cs index b8d446a..76dd275 100644 --- a/Notepad.Extensions.Logging/IWindowFinder.cs +++ b/Notepad.Extensions.Logging/IWindowFinder.cs @@ -2,6 +2,6 @@ { interface IWindowFinder { - WindowInfo FindNotepadWindow(); + WindowInfo FindNotepadWindow(string windowName); } } \ No newline at end of file diff --git a/Notepad.Extensions.Logging/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index 94ccff6..a896938 100644 --- a/Notepad.Extensions.Logging/NotepadLogger.cs +++ b/Notepad.Extensions.Logging/NotepadLogger.cs @@ -11,16 +11,18 @@ namespace Notepad.Extensions.Logging { class NotepadLogger : ILogger { - public NotepadLogger(ObjectPool stringBuilderPool, IWindowFinder windowFinder, string categoryName) + public NotepadLogger(ObjectPool 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 stringBuilderPool; readonly IWindowFinder windowFinder; readonly string categoryName; + readonly string windowName; public IDisposable BeginScope(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: diff --git a/Notepad.Extensions.Logging/NotepadLoggerOptions.cs b/Notepad.Extensions.Logging/NotepadLoggerOptions.cs index 8894945..b5eb08f 100644 --- a/Notepad.Extensions.Logging/NotepadLoggerOptions.cs +++ b/Notepad.Extensions.Logging/NotepadLoggerOptions.cs @@ -9,6 +9,6 @@ namespace Microsoft.Extensions.Logging /// /// Name of window to search. /// - public string WindowName { get; set; } = "Untitled - Notepad"; + public string WindowName { get; set; } } } diff --git a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs index 99425a5..7407e5b 100644 --- a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs +++ b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs @@ -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; - } } } } diff --git a/Notepad.Extensions.Logging/WindowEnumerationState.cs b/Notepad.Extensions.Logging/WindowEnumerationState.cs index 9eddf2e..bd1a5ad 100644 --- a/Notepad.Extensions.Logging/WindowEnumerationState.cs +++ b/Notepad.Extensions.Logging/WindowEnumerationState.cs @@ -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); diff --git a/Notepad.Extensions.Logging/WindowFinder.cs b/Notepad.Extensions.Logging/WindowFinder.cs index c5d5a17..54d0ff8 100644 --- a/Notepad.Extensions.Logging/WindowFinder.cs +++ b/Notepad.Extensions.Logging/WindowFinder.cs @@ -12,14 +12,12 @@ namespace Notepad.Extensions.Logging readonly ObjectPool 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); }