From d2e66538e0a992f4048fef72a168a21337325280 Mon Sep 17 00:00:00 2001 From: Alexander Kislitsyn <4lex.kislitsyn@gmail.com> Date: Sat, 23 May 2020 13:00:55 +0300 Subject: [PATCH] Added support of window name from options in WindowEnumerationState class. --- .../LoggingBuilderExtensions.cs | 1 + Notepad.Extensions.Logging/NotepadLogger.cs | 5 +- .../NotepadLoggerProvider.cs | 4 ++ .../WindowEnumerationState.cs | 52 ++++++++++++++----- Notepad.Extensions.Logging/WindowFinder.cs | 3 ++ 5 files changed, 48 insertions(+), 17 deletions(-) diff --git a/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs b/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs index 54a242c..af2dc4b 100644 --- a/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs +++ b/Notepad.Extensions.Logging/LoggingBuilderExtensions.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging.Configuration; using Microsoft.Extensions.Options; +using Notepad.Extensions.Logging; using System; namespace Microsoft.Extensions.Logging diff --git a/Notepad.Extensions.Logging/NotepadLogger.cs b/Notepad.Extensions.Logging/NotepadLogger.cs index 3eab97f..94ccff6 100644 --- a/Notepad.Extensions.Logging/NotepadLogger.cs +++ b/Notepad.Extensions.Logging/NotepadLogger.cs @@ -94,14 +94,11 @@ namespace Notepad.Extensions.Logging switch (info.Kind) { case WindowKind.Notepad: - SendMessage(info.Handle, EM_REPLACESEL, (IntPtr)1, message); + SendMessage(info.Handle, EM_REPLACESEL, (IntPtr)1, message); break; - case WindowKind.NotepadPlusPlus: - { WriteToNotepadPlusPlus(info.Handle, message); break; - } } } diff --git a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs index c5cc50f..fa41322 100644 --- a/Notepad.Extensions.Logging/NotepadLoggerProvider.cs +++ b/Notepad.Extensions.Logging/NotepadLoggerProvider.cs @@ -42,6 +42,10 @@ namespace Notepad.Extensions.Logging private 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 2d13e75..9eddf2e 100644 --- a/Notepad.Extensions.Logging/WindowEnumerationState.cs +++ b/Notepad.Extensions.Logging/WindowEnumerationState.cs @@ -16,11 +16,13 @@ namespace Notepad.Extensions.Logging public IntPtr Handle { get; private set; } public WindowKind WindowKind { get; private set; } + public string WindowName { get; internal set; } public void Reset() { Handle = default; WindowKind = default; + WindowName = default; sb.Clear(); } @@ -47,26 +49,50 @@ namespace Notepad.Extensions.Logging return true; } - static Regex notepadPlusPlusRegex = new Regex(@"^new \d+ - Notepad\+\+$", RegexOptions.Compiled); + static Regex notepadPlusPlusRegex = new Regex(@" - Notepad\+\+$", RegexOptions.Compiled); bool IsKnownNotepadWindow(string titleText) { - switch (titleText) + if (!string.IsNullOrWhiteSpace(WindowName)) { - case "Untitled - Notepad": - WindowKind = WindowKind.Notepad; - Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "EDIT", null); - return true; + if (WindowName.Equals(titleText, StringComparison.Ordinal)) + { + WindowKind = notepadPlusPlusRegex.IsMatch(titleText) ? WindowKind.NotepadPlusPlus : WindowKind.Notepad; + } + } + else + { + switch (titleText) + { + case "Untitled - Notepad": + WindowKind = WindowKind.Notepad; + break; + default: + if (notepadPlusPlusRegex.IsMatch(titleText)) + { + WindowKind = WindowKind.NotepadPlusPlus; + } + break; + + } } - if (notepadPlusPlusRegex.IsMatch(titleText)) - { - WindowKind = WindowKind.NotepadPlusPlus; - Handle = NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "Scintilla", null); - return true; - } + Handle = FindInnerWindow(WindowKind); - return false; + return WindowKind != default; + } + + IntPtr FindInnerWindow(WindowKind windowKind) + { + switch (windowKind) + { + case WindowKind.Notepad: + return NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "EDIT", null); + case WindowKind.NotepadPlusPlus: + return NativeMethods.FindWindowEx(Handle, IntPtr.Zero, "Scintilla", null); + default: + return Handle; + } } } } diff --git a/Notepad.Extensions.Logging/WindowFinder.cs b/Notepad.Extensions.Logging/WindowFinder.cs index ce56966..c5d5a17 100644 --- a/Notepad.Extensions.Logging/WindowFinder.cs +++ b/Notepad.Extensions.Logging/WindowFinder.cs @@ -12,11 +12,14 @@ namespace Notepad.Extensions.Logging readonly ObjectPool statePool; + public string WindowName { get; internal set; } + public WindowInfo FindNotepadWindow() { var stateObject = statePool.Get(); try { + stateObject.WindowName = WindowName; NativeMethods.EnumWindows(enumWindowsDelegate, stateObject); return new WindowInfo(stateObject.WindowKind, stateObject.Handle); }