From 881d57a1781a93af1605009983747881fc027ff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=98=8E?= Date: Sat, 21 Nov 2026 11:04:40 -0500 Subject: [PATCH] =?UTF-8?q?fix(windows):=20stop=20filing=20every=20group?= =?UTF-8?q?=20as=20=E3=80=8C=E5=BE=AE=E4=BF=A1=E3=80=8D=20(#41)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WeChat 4.x titles its main window 「微信」 whatever chat is open, and newer builds no longer expose the mmui::MainWindow UIA tree. The UIA read came back empty, the window-title fallback returned 「微信」, and that was taken as the group name — so the OCR fallback never ran and every group landed in the same 「微信的聊天.md」. A title that is only the app name (微信 / Weixin / WeChat) now reads as no name in both the window-title and UIA readers, so the chain falls through to OCR of the chat header. If that also fails, the note is named after the participants instead of a shared 「微信」. Fixes #41 Co-Authored-By: Claude Opus 5.5 --- .../Identity/WeChatWindowTitleReader.cs | 24 +++++++++++++------ .../Services/WeChatUiTitleReader.cs | 6 ++++- .../SceneDecisionTests.cs | 24 +++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/windows/src/WeChatBridge.Windows.Core/Identity/WeChatWindowTitleReader.cs b/windows/src/WeChatBridge.Windows.Core/Identity/WeChatWindowTitleReader.cs index 39cf522..470cde8 100644 --- a/windows/src/WeChatBridge.Windows.Core/Identity/WeChatWindowTitleReader.cs +++ b/windows/src/WeChatBridge.Windows.Core/Identity/WeChatWindowTitleReader.cs @@ -19,20 +19,30 @@ public sealed class WeChatTitleReaderException(WeChatTitleReaderErrorKind kind, } /// -/// Windows port of WeChatTitleReader. On macOS the group name comes from -/// Accessibility labels or a Screen-Recording screenshot of the title bar; -/// Windows needs neither — the WeChat main window's own title bar text already -/// contains the current chat name, and GetWindowText reads it without -/// capturing any pixels. The privacy boundary is identical to the macOS -/// Accessibility path: window titles only, no screen capture, no injection. +/// Windows port of WeChatTitleReader: reads the chat name from WeChat's +/// top-level window titles with GetWindowText, without capturing any +/// pixels. The privacy boundary is identical to the macOS Accessibility path: +/// window titles only, no screen capture, no injection. +/// +/// WeChat 4.x (Weixin) titles its main window 「微信」 whatever chat is open, so +/// a title that is only the app name is not a chat name. It is dropped here, so +/// the caller falls through to the OCR read instead of filing every group as +/// 「微信」 (issue #41). /// public sealed class WeChatWindowTitleReader { /// Weixin.exe is WeChat 4.x; WeChat.exe is the 3.x line. private static readonly string[] WeChatProcessNames = ["weixin", "wechat"]; + /// What WeChat calls its own windows; never a chat name. + private static readonly HashSet AppNames = + new(StringComparer.OrdinalIgnoreCase) { "微信", "Weixin", "WeChat" }; + private readonly Func> _windowTitles; + /// True for a title that names the app rather than a chat. + public static bool IsAppName(string title) => AppNames.Contains(title.Trim()); + /// Injectable so tests parse titles without a /// running WeChat. Null reads the real windows via user32. public WeChatWindowTitleReader(Func>? windowTitles = null) => @@ -49,7 +59,7 @@ public GroupTitleParser.Title Read() if (titles.Count == 0) throw new WeChatTitleReaderException( WeChatTitleReaderErrorKind.NoWindow, "没有找到可见的微信聊天窗口。"); - return GroupTitleParser.Parse(titles) + return GroupTitleParser.Parse(titles.Where(title => !IsAppName(title))) ?? throw new WeChatTitleReaderException( WeChatTitleReaderErrorKind.EmptyTitle, "没有识别出微信群名。"); } diff --git a/windows/src/WeChatBridge.Windows/Services/WeChatUiTitleReader.cs b/windows/src/WeChatBridge.Windows/Services/WeChatUiTitleReader.cs index 1fe0155..f879a51 100644 --- a/windows/src/WeChatBridge.Windows/Services/WeChatUiTitleReader.cs +++ b/windows/src/WeChatBridge.Windows/Services/WeChatUiTitleReader.cs @@ -52,7 +52,11 @@ public static class WeChatUiTitleReader /// public static GroupTitleParser.Title? Read() { - var candidates = ChatTitleCandidates(); + // A bare 「微信」 is the app, not the chat: dropping it lets the caller + // fall through to the next reader (issue #41). + var candidates = ChatTitleCandidates() + .Where(candidate => !WeChatWindowTitleReader.IsAppName(candidate)) + .ToList(); if (candidates.Count == 0) return null; foreach (var candidate in candidates) diff --git a/windows/tests/WeChatBridge.Windows.Tests/SceneDecisionTests.cs b/windows/tests/WeChatBridge.Windows.Tests/SceneDecisionTests.cs index 87db7fd..805e295 100644 --- a/windows/tests/WeChatBridge.Windows.Tests/SceneDecisionTests.cs +++ b/windows/tests/WeChatBridge.Windows.Tests/SceneDecisionTests.cs @@ -620,6 +620,30 @@ public void WindowTitleReaderParsesInjectedTitles() Assert.Null(empty.TryRead()); } + /// + /// Issue #41: WeChat 4.x titles its main window 「微信」 whatever chat is + /// open. That title must read as "no name" so the OCR fallback runs, + /// instead of every group being filed under 「微信」. + /// + [Fact] + public void WindowTitleReaderIgnoresTheAppName() + { + foreach (var appName in new[] { "微信", "Weixin", "WeChat", " weixin " }) + { + var reader = new WeChatWindowTitleReader(() => [appName]); + var error = Assert.Throws(() => reader.Read()); + Assert.Equal(WeChatTitleReaderErrorKind.EmptyTitle, error.Kind); + Assert.Null(reader.TryRead()); + } + + var mixed = new WeChatWindowTitleReader(() => ["微信", "华东客户群 (128)"]); + Assert.Equal("华东客户群", mixed.Read().Name); + + // The longest-line fallback must not pick the app name over a real chat. + var direct = new WeChatWindowTitleReader(() => ["微信", "张三"]); + Assert.Equal("张三", direct.Read().Name); + } + [Fact] public void DefaultSceneRequiresExplicitSelection() {