Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,30 @@ public sealed class WeChatTitleReaderException(WeChatTitleReaderErrorKind kind,
}

/// <summary>
/// Windows port of <c>WeChatTitleReader</c>. 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 <c>GetWindowText</c> 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 <c>WeChatTitleReader</c>: reads the chat name from WeChat's
/// top-level window titles with <c>GetWindowText</c>, 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).
/// </summary>
public sealed class WeChatWindowTitleReader
{
/// <summary>Weixin.exe is WeChat 4.x; WeChat.exe is the 3.x line.</summary>
private static readonly string[] WeChatProcessNames = ["weixin", "wechat"];

/// <summary>What WeChat calls its own windows; never a chat name.</summary>
private static readonly HashSet<string> AppNames =
new(StringComparer.OrdinalIgnoreCase) { "微信", "Weixin", "WeChat" };

private readonly Func<IReadOnlyList<string>> _windowTitles;

/// <summary>True for a title that names the app rather than a chat.</summary>
public static bool IsAppName(string title) => AppNames.Contains(title.Trim());

/// <param name="windowTitles">Injectable so tests parse titles without a
/// running WeChat. Null reads the real windows via user32.</param>
public WeChatWindowTitleReader(Func<IReadOnlyList<string>>? windowTitles = null) =>
Expand All @@ -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, "没有识别出微信群名。");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public static class WeChatUiTitleReader
/// </summary>
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)
Expand Down
24 changes: 24 additions & 0 deletions windows/tests/WeChatBridge.Windows.Tests/SceneDecisionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,30 @@ public void WindowTitleReaderParsesInjectedTitles()
Assert.Null(empty.TryRead());
}

/// <summary>
/// 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 「微信」.
/// </summary>
[Fact]
public void WindowTitleReaderIgnoresTheAppName()
{
foreach (var appName in new[] { "微信", "Weixin", "WeChat", " weixin " })
{
var reader = new WeChatWindowTitleReader(() => [appName]);
var error = Assert.Throws<WeChatTitleReaderException>(() => 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()
{
Expand Down
Loading