@deepseek-harness-tui/dsh-tui 0.19.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -57
- package/cordis.patch.yml +5 -3
- package/cordis.yml +3 -2
- package/guide/dsh-tui-guide/configuration.en.md +1 -1
- package/guide/dsh-tui-guide/configuration.md +1 -1
- package/guide/dsh-tui-guide/getting-started.en.md +2 -2
- package/guide/dsh-tui-guide/getting-started.md +2 -2
- package/guide/dsh-tui-guide/user-guide.en.md +7 -2
- package/guide/dsh-tui-guide/user-guide.md +7 -2
- package/lib/settings.json +24 -8
- package/lib/types/adapter/ports/channel-view.d.ts +39 -3
- package/lib/types/adapter/ports/channel-view.d.ts.map +1 -1
- package/lib/types/agent/events.d.ts +3 -1
- package/lib/types/agent/events.d.ts.map +1 -1
- package/lib/types/backends/claude/tools.d.ts.map +1 -1
- package/lib/types/backends/claude/tools.js +8 -3
- package/lib/types/backends/codex/translate/presentation.d.ts.map +1 -1
- package/lib/types/backends/codex/translate/presentation.js +5 -2
- package/lib/types/channel/interaction.d.ts.map +1 -1
- package/lib/types/channel/interaction.js +1 -0
- package/lib/types/channel/permissions.d.ts +15 -0
- package/lib/types/channel/permissions.d.ts.map +1 -1
- package/lib/types/channel/permissions.js +28 -0
- package/lib/types/channel/projection.d.ts.map +1 -1
- package/lib/types/channel/projection.js +11 -1
- package/lib/types/channel/questions.d.ts +26 -0
- package/lib/types/channel/questions.d.ts.map +1 -1
- package/lib/types/channel/questions.js +43 -0
- package/lib/types/commands.js +1 -1
- package/lib/types/components/MessageList.d.ts.map +1 -1
- package/lib/types/components/MessageList.js +3 -3
- package/lib/types/components/SplitDiffView.d.ts.map +1 -1
- package/lib/types/components/SplitDiffView.js +3 -2
- package/lib/types/components/StarPrompt.d.ts +5 -3
- package/lib/types/components/StarPrompt.d.ts.map +1 -1
- package/lib/types/components/StarPrompt.js +56 -6
- package/lib/types/components/SubagentCard.d.ts.map +1 -1
- package/lib/types/components/SubagentCard.js +1 -1
- package/lib/types/components/SubagentDashboard.d.ts.map +1 -1
- package/lib/types/components/SubagentDashboard.js +3 -2
- package/lib/types/components/SubagentDetailScene.d.ts.map +1 -1
- package/lib/types/components/SubagentDetailScene.js +1 -1
- package/lib/types/components/messages/AssistantToolUseMessage.d.ts.map +1 -1
- package/lib/types/components/messages/AssistantToolUseMessage.js +121 -23
- package/lib/types/components/teamRoster.d.ts +6 -3
- package/lib/types/components/teamRoster.d.ts.map +1 -1
- package/lib/types/dsh-adapter/approvals.d.ts +19 -0
- package/lib/types/dsh-adapter/approvals.d.ts.map +1 -1
- package/lib/types/dsh-adapter/approvals.js +17 -1
- package/lib/types/dsh-adapter/backend/translate.d.ts.map +1 -1
- package/lib/types/dsh-adapter/backend/translate.js +15 -3
- package/lib/types/dsh-adapter/channel/extensions.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/extensions.js +7 -4
- package/lib/types/dsh-adapter/channel/local-actions.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/local-actions.js +7 -3
- package/lib/types/dsh-adapter/channel/projection-helpers.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/projection-helpers.js +4 -2
- package/lib/types/dsh-adapter/channel/settings-host.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/settings-host.js +179 -33
- package/lib/types/dsh-adapter/channel/subagent-projection.d.ts +55 -4
- package/lib/types/dsh-adapter/channel/subagent-projection.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/subagent-projection.js +80 -16
- package/lib/types/dsh-adapter/channel/subagent-transcript.d.ts +1 -1
- package/lib/types/dsh-adapter/channel/subagent-transcript.d.ts.map +1 -1
- package/lib/types/dsh-adapter/channel/subagent-transcript.js +6 -5
- package/lib/types/dsh-adapter/channel/task-completion.js +2 -10
- package/lib/types/dsh-adapter/compat/compaction-previews.d.ts +11 -0
- package/lib/types/dsh-adapter/compat/compaction-previews.d.ts.map +1 -0
- package/lib/types/dsh-adapter/compat/compaction-previews.js +38 -0
- package/lib/types/dsh-adapter/compat/messages.d.ts +1 -1
- package/lib/types/dsh-adapter/compat/messages.d.ts.map +1 -1
- package/lib/types/dsh-adapter/compat/messages.js +2 -2
- package/lib/types/dsh-adapter/contract.d.ts +7 -4
- package/lib/types/dsh-adapter/contract.d.ts.map +1 -1
- package/lib/types/dsh-adapter/contract.js +8 -4
- package/lib/types/dsh-adapter/experimental-bundles.d.ts +5 -0
- package/lib/types/dsh-adapter/experimental-bundles.d.ts.map +1 -1
- package/lib/types/dsh-adapter/experimental-bundles.js +0 -2
- package/lib/types/dsh-adapter/host-contract.d.ts +1 -1
- package/lib/types/dsh-adapter/host-contract.js +2 -2
- package/lib/types/dsh-adapter/host-dsh.d.ts +2 -0
- package/lib/types/dsh-adapter/host-dsh.d.ts.map +1 -1
- package/lib/types/dsh-adapter/host-dsh.js +3 -0
- package/lib/types/dsh-adapter/index.d.ts +9 -1
- package/lib/types/dsh-adapter/index.d.ts.map +1 -1
- package/lib/types/dsh-adapter/index.js +5 -0
- package/lib/types/dsh-adapter/oauth/account-notifications.d.ts +73 -0
- package/lib/types/dsh-adapter/oauth/account-notifications.d.ts.map +1 -0
- package/lib/types/dsh-adapter/oauth/account-notifications.js +250 -0
- package/lib/types/dsh-adapter/oauth/bonus.d.ts +34 -9
- package/lib/types/dsh-adapter/oauth/bonus.d.ts.map +1 -1
- package/lib/types/dsh-adapter/oauth/bonus.js +47 -5
- package/lib/types/dsh-adapter/oauth/deepseek.d.ts +7 -2
- package/lib/types/dsh-adapter/oauth/deepseek.d.ts.map +1 -1
- package/lib/types/dsh-adapter/oauth/deepseek.js +6 -1
- package/lib/types/dsh-adapter/oauth/index.d.ts.map +1 -1
- package/lib/types/dsh-adapter/oauth/index.js +44 -0
- package/lib/types/dsh-adapter/oauth/service.d.ts.map +1 -1
- package/lib/types/dsh-adapter/oauth/service.js +5 -6
- package/lib/types/dsh-adapter/plugin-manage.d.ts +6 -4
- package/lib/types/dsh-adapter/plugin-manage.d.ts.map +1 -1
- package/lib/types/dsh-adapter/plugin-manage.js +3 -2
- package/lib/types/dsh-adapter/plugin.d.ts +237 -6
- package/lib/types/dsh-adapter/plugin.d.ts.map +1 -1
- package/lib/types/dsh-adapter/plugin.js +608 -23
- package/lib/types/dsh-adapter/session-projection-store.d.ts +1 -1
- package/lib/types/dsh-adapter/session-projection-store.d.ts.map +1 -1
- package/lib/types/dsh-adapter/session-projection-store.js +30 -23
- package/lib/types/dsh-adapter/sessionTree.d.ts.map +1 -1
- package/lib/types/dsh-adapter/sessionTree.js +4 -1
- package/lib/types/dsh-adapter/subagents.d.ts +1 -0
- package/lib/types/dsh-adapter/subagents.d.ts.map +1 -1
- package/lib/types/dsh-adapter/subagents.js +10 -2
- package/lib/types/dsh-adapter/team-mode.d.ts +29 -17
- package/lib/types/dsh-adapter/team-mode.d.ts.map +1 -1
- package/lib/types/dsh-adapter/team-mode.js +138 -17
- package/lib/types/dsh-adapter/team-roster.d.ts +53 -18
- package/lib/types/dsh-adapter/team-roster.d.ts.map +1 -1
- package/lib/types/dsh-adapter/team-roster.js +206 -39
- package/lib/types/dsh-adapter/team-tools.d.ts +25 -12
- package/lib/types/dsh-adapter/team-tools.d.ts.map +1 -1
- package/lib/types/dsh-adapter/team-tools.js +81 -26
- package/lib/types/i18n.d.ts +65 -9
- package/lib/types/i18n.d.ts.map +1 -1
- package/lib/types/i18n.js +29 -8
- package/lib/types/ink/dom.d.ts.map +1 -1
- package/lib/types/ink/dom.js +13 -3
- package/lib/types/ink/flush-tick.d.ts +6 -0
- package/lib/types/ink/flush-tick.d.ts.map +1 -1
- package/lib/types/ink/flush-tick.js +31 -0
- package/lib/types/ink/hooks/use-terminal-title.d.ts +15 -0
- package/lib/types/ink/hooks/use-terminal-title.d.ts.map +1 -1
- package/lib/types/ink/hooks/use-terminal-title.js +23 -3
- package/lib/types/ink/ink.js +2 -2
- package/lib/types/ink/jumpback-state.d.ts +33 -0
- package/lib/types/ink/jumpback-state.d.ts.map +1 -0
- package/lib/types/ink/jumpback-state.js +51 -0
- package/lib/types/ink/jumpback.d.ts +78 -0
- package/lib/types/ink/jumpback.d.ts.map +1 -0
- package/lib/types/ink/jumpback.js +515 -0
- package/lib/types/ink/log-update.js +37 -14
- package/lib/types/ink/notify-helper/activate.cs +836 -0
- package/lib/types/ink/notify-helper/compile.d.ts +30 -0
- package/lib/types/ink/notify-helper/compile.d.ts.map +1 -0
- package/lib/types/ink/notify-helper/compile.js +235 -0
- package/lib/types/ink/terminal-notification-windows.d.ts +8 -4
- package/lib/types/ink/terminal-notification-windows.d.ts.map +1 -1
- package/lib/types/ink/terminal-notification-windows.js +14 -21
- package/lib/types/ink/terminal-notification.d.ts +31 -2
- package/lib/types/ink/terminal-notification.d.ts.map +1 -1
- package/lib/types/ink/terminal-notification.js +51 -0
- package/lib/types/ink/terminal.d.ts +2 -1
- package/lib/types/ink/terminal.d.ts.map +1 -1
- package/lib/types/ink/terminal.js +9 -4
- package/lib/types/screens/AgentTranscriptScene.d.ts.map +1 -1
- package/lib/types/screens/AgentTranscriptScene.js +1 -1
- package/lib/types/screens/Chat.d.ts +4 -3
- package/lib/types/screens/Chat.d.ts.map +1 -1
- package/lib/types/screens/Chat.js +38 -9
- package/lib/types/settings/definitions.d.ts +12 -0
- package/lib/types/settings/definitions.d.ts.map +1 -1
- package/lib/types/settings/definitions.js +15 -7
- package/package.json +112 -112
- package/presets/liangshen/agent.cordis.yml +4 -0
- package/tui-profile/feed/README.md +1 -1
- package/tui-profile/feed/first-party.json +1 -1
|
@@ -0,0 +1,836 @@
|
|
|
1
|
+
// activate.cs - the helper Windows starts when a dsh-tui task-completion toast is clicked.
|
|
2
|
+
//
|
|
3
|
+
// Two modes; the TypeScript side calls only the first:
|
|
4
|
+
// activate.exe --probe <marker> [-DataDir <dir>]
|
|
5
|
+
// activate.exe --activate <marker> [-DataDir <dir>]
|
|
6
|
+
//
|
|
7
|
+
// `--probe` answers one question without touching the desktop: is the marker carried by
|
|
8
|
+
// exactly one pane (or, when that pane is not realized, exactly one tab title)? It runs
|
|
9
|
+
// before a toast carries an activation URI, so a toast never promises an unverifiable jump.
|
|
10
|
+
//
|
|
11
|
+
// `--activate` is the click path: resolve the marker to one pane, restore and foreground
|
|
12
|
+
// its window, select the tab when the pane is not realized yet, and call SetFocus on the
|
|
13
|
+
// SAME element that was resolved. It then re-reads the desktop and exits 0 only when all
|
|
14
|
+
// five checks hold: that pane is the focused element (runtime id), it has keyboard focus,
|
|
15
|
+
// its HelpText still carries the marker, its window is foreground and not minimized.
|
|
16
|
+
//
|
|
17
|
+
// The marker is opaque here: this file never formats one, it only matches the string it
|
|
18
|
+
// was handed as a substring of a title. Windows Terminal exposes each pane's title as
|
|
19
|
+
// TermControl.HelpText and each tab's as TabItem.Name (the focused pane's). Looking at
|
|
20
|
+
// per-pane HelpText first is what makes the target the pane that produced the notification
|
|
21
|
+
// instead of "some TermControl in the window".
|
|
22
|
+
//
|
|
23
|
+
// Argument forms: the registered protocol command passes the whole launch URI
|
|
24
|
+
// (`dsh-tui:activate?m=<marker>`), parsed down to the marker; a bare marker is accepted
|
|
25
|
+
// too, so the helper can be driven by hand while diagnosing.
|
|
26
|
+
//
|
|
27
|
+
// Exit codes: 0 = the jump was performed and verified, 2 = invalid invocation (including a
|
|
28
|
+
// marker outside [A-Za-z0-9._-]{1,64}), 3 = the target was unreachable or a step/check
|
|
29
|
+
// failed. Nothing else is ever returned.
|
|
30
|
+
//
|
|
31
|
+
// Log: <DataDir>/notify/jumpback.log (default %USERPROFILE%\.dsh-tui). Every run leaves its
|
|
32
|
+
// stages and, on failure, the reason there. The shell starts this process detached, so the
|
|
33
|
+
// log is the only trace a click leaves; the data directory travels as an explicit argument
|
|
34
|
+
// rather than an inherited environment variable, because the shell starts us with none.
|
|
35
|
+
//
|
|
36
|
+
// Restrictions (do not "modernize" this file): it has to compile with the in-box csc.exe
|
|
37
|
+
// (.NET Framework 4.x) against the framework's UIAutomationClient/UIAutomationTypes, so it
|
|
38
|
+
// stays at C# 5 - no string interpolation, no null-conditional or nameof operators, no
|
|
39
|
+
// inline out declarations, no static using directives - and it takes no NuGet package and
|
|
40
|
+
// no runtime besides the framework. It never executes anything taken from a notification:
|
|
41
|
+
// the marker is only ever compared, never run.
|
|
42
|
+
using System;
|
|
43
|
+
using System.Collections.Generic;
|
|
44
|
+
using System.Diagnostics;
|
|
45
|
+
using System.IO;
|
|
46
|
+
using System.Runtime.InteropServices;
|
|
47
|
+
using System.Text;
|
|
48
|
+
using System.Threading;
|
|
49
|
+
using System.Windows.Automation;
|
|
50
|
+
|
|
51
|
+
internal static class JumpBack
|
|
52
|
+
{
|
|
53
|
+
private const string TerminalWindowClass = "CASCADIA_HOSTING_WINDOW_CLASS";
|
|
54
|
+
private const string TermControlClass = "TermControl";
|
|
55
|
+
private const int SW_RESTORE = 9;
|
|
56
|
+
|
|
57
|
+
// Every wait below is a ceiling on a state, not a duration: the helper polls and moves
|
|
58
|
+
// on the moment the state holds, so a successful jump costs what it measures.
|
|
59
|
+
private const int PollIntervalMs = 25;
|
|
60
|
+
private const int RestoreCapMs = 1000;
|
|
61
|
+
private const int ForegroundAttempts = 3;
|
|
62
|
+
private const int ForegroundCapMs = 250;
|
|
63
|
+
private const int SelectAttempts = 2;
|
|
64
|
+
private const int SelectCapMs = 1000;
|
|
65
|
+
private const int RealizeCapMs = 1000;
|
|
66
|
+
private const int FocusAttempts = 3;
|
|
67
|
+
private const int FocusCapMs = 400;
|
|
68
|
+
|
|
69
|
+
// The log lives in the user's profile and every probe appends to it; keep it bounded.
|
|
70
|
+
private const long MaxLogBytes = 256 * 1024;
|
|
71
|
+
|
|
72
|
+
private const int ExitOk = 0;
|
|
73
|
+
private const int ExitUsage = 2;
|
|
74
|
+
private const int ExitUnreachable = 3;
|
|
75
|
+
|
|
76
|
+
[DllImport("user32.dll")]
|
|
77
|
+
private static extern bool SetForegroundWindow(IntPtr hwnd);
|
|
78
|
+
[DllImport("user32.dll")]
|
|
79
|
+
private static extern IntPtr GetForegroundWindow();
|
|
80
|
+
[DllImport("user32.dll")]
|
|
81
|
+
private static extern bool IsIconic(IntPtr hwnd);
|
|
82
|
+
[DllImport("user32.dll")]
|
|
83
|
+
private static extern bool ShowWindow(IntPtr hwnd, int command);
|
|
84
|
+
[DllImport("user32.dll")]
|
|
85
|
+
private static extern bool IsWindowVisible(IntPtr hwnd);
|
|
86
|
+
|
|
87
|
+
private static string logPath;
|
|
88
|
+
|
|
89
|
+
private sealed class PaneHit
|
|
90
|
+
{
|
|
91
|
+
public IntPtr Hwnd;
|
|
92
|
+
public AutomationElement Window;
|
|
93
|
+
public AutomationElement Tab;
|
|
94
|
+
public AutomationElement Pane;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private sealed class TabHit
|
|
98
|
+
{
|
|
99
|
+
public AutomationElement Window;
|
|
100
|
+
public IntPtr Hwnd;
|
|
101
|
+
public AutomationElement Tab;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private sealed class Checks
|
|
105
|
+
{
|
|
106
|
+
public bool SameElement;
|
|
107
|
+
public bool KeyboardFocus;
|
|
108
|
+
public bool HelpText;
|
|
109
|
+
public bool Foreground;
|
|
110
|
+
public bool NotMinimized;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private sealed class Options
|
|
114
|
+
{
|
|
115
|
+
public string Mode;
|
|
116
|
+
public string Marker;
|
|
117
|
+
public string DataDir;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private static int Main(string[] args)
|
|
121
|
+
{
|
|
122
|
+
// A pure decision table for the candidate association: no UIA, no desktop,
|
|
123
|
+
// so the Windows regression can prove the ambiguity rule without a window.
|
|
124
|
+
if (args.Length == 1 && args[0] == "--selftest-candidates") return RunCandidateSelfTest();
|
|
125
|
+
|
|
126
|
+
Options options;
|
|
127
|
+
string argumentError;
|
|
128
|
+
if (!TryParseArgs(args, out options, out argumentError))
|
|
129
|
+
{
|
|
130
|
+
logPath = ResolveLogPath(options.DataDir);
|
|
131
|
+
Log("stage=args ok=false reason=\"" + Escape(argumentError) + "\"");
|
|
132
|
+
Usage();
|
|
133
|
+
return ExitUsage;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
logPath = ResolveLogPath(options.DataDir);
|
|
137
|
+
Log("stage=start mode=" + options.Mode + " marker=" + options.Marker
|
|
138
|
+
+ " log=" + (logPath == null ? "<none>" : logPath));
|
|
139
|
+
|
|
140
|
+
try
|
|
141
|
+
{
|
|
142
|
+
if (options.Mode == "--probe") return Probe(options.Marker);
|
|
143
|
+
return Activate(options.Marker);
|
|
144
|
+
}
|
|
145
|
+
catch (Exception failure)
|
|
146
|
+
{
|
|
147
|
+
Log("stage=exception ok=false reason=\"" + Escape(failure.Message) + "\"");
|
|
148
|
+
return ExitUnreachable;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// The command line is fixed by the registered protocol command: anything extra is
|
|
153
|
+
// rejected, not guessed at. Marker validation happens here, before any UIA call.
|
|
154
|
+
private static bool TryParseArgs(string[] args, out Options options, out string error)
|
|
155
|
+
{
|
|
156
|
+
options = new Options();
|
|
157
|
+
error = null;
|
|
158
|
+
for (int i = 0; i < args.Length; i++)
|
|
159
|
+
{
|
|
160
|
+
string argument = args[i];
|
|
161
|
+
if (argument == "-DataDir")
|
|
162
|
+
{
|
|
163
|
+
if (i + 1 >= args.Length) { error = "missing value after -DataDir"; return false; }
|
|
164
|
+
if (options.DataDir != null) { error = "duplicate -DataDir"; return false; }
|
|
165
|
+
options.DataDir = args[++i];
|
|
166
|
+
if (options.DataDir.Length == 0) { error = "empty -DataDir"; return false; }
|
|
167
|
+
}
|
|
168
|
+
else if (argument == "--probe" || argument == "--activate")
|
|
169
|
+
{
|
|
170
|
+
if (options.Mode != null) { error = "duplicate mode"; return false; }
|
|
171
|
+
options.Mode = argument;
|
|
172
|
+
}
|
|
173
|
+
else if (argument.Length > 0 && argument[0] == '-')
|
|
174
|
+
{
|
|
175
|
+
error = "unknown option " + Quote(argument);
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
else if (options.Marker == null)
|
|
179
|
+
{
|
|
180
|
+
options.Marker = NormalizeMarker(argument);
|
|
181
|
+
}
|
|
182
|
+
else
|
|
183
|
+
{
|
|
184
|
+
error = "unexpected extra argument " + Quote(argument);
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (options.Mode == null) { error = "missing mode (--probe or --activate)"; return false; }
|
|
189
|
+
if (options.Marker == null) { error = "missing marker"; return false; }
|
|
190
|
+
if (!ValidMarker(options.Marker)) { error = "invalid marker (expected [A-Za-z0-9._-]{1,64})"; return false; }
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// The shell starts us as: activate.exe --activate "dsh-tui:activate?m=<marker>" -DataDir <dir>
|
|
195
|
+
// The marker is URL-safe by contract (see ValidMarker), so no percent-decoding is done:
|
|
196
|
+
// a value that would need it was not produced by this process.
|
|
197
|
+
private static string NormalizeMarker(string raw)
|
|
198
|
+
{
|
|
199
|
+
const string prefix = "dsh-tui:activate?m=";
|
|
200
|
+
if (raw.Length >= prefix.Length &&
|
|
201
|
+
string.Compare(raw, 0, prefix, 0, prefix.Length, StringComparison.OrdinalIgnoreCase) == 0)
|
|
202
|
+
{
|
|
203
|
+
return raw.Substring(prefix.Length);
|
|
204
|
+
}
|
|
205
|
+
return raw;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// The marker arrives as an argument, so only a conservative ASCII token alphabet is
|
|
209
|
+
// accepted; explicit ranges (not char.IsLetter) keep non-ASCII out of a window title.
|
|
210
|
+
private static bool ValidMarker(string marker)
|
|
211
|
+
{
|
|
212
|
+
if (marker == null || marker.Length == 0 || marker.Length > 64) return false;
|
|
213
|
+
for (int i = 0; i < marker.Length; i++)
|
|
214
|
+
{
|
|
215
|
+
char c = marker[i];
|
|
216
|
+
bool accepted = (c >= 'A' && c <= 'Z')
|
|
217
|
+
|| (c >= 'a' && c <= 'z')
|
|
218
|
+
|| (c >= '0' && c <= '9')
|
|
219
|
+
|| c == '-' || c == '_' || c == '.';
|
|
220
|
+
if (!accepted) return false;
|
|
221
|
+
}
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
private static void Usage()
|
|
226
|
+
{
|
|
227
|
+
try
|
|
228
|
+
{
|
|
229
|
+
Console.Error.WriteLine("usage: activate.exe --probe <marker> [-DataDir <dir>]");
|
|
230
|
+
Console.Error.WriteLine(" activate.exe --activate <marker> [-DataDir <dir>]");
|
|
231
|
+
Console.Error.WriteLine(" <marker> is [A-Za-z0-9._-]{1,64}, or dsh-tui:activate?m=<marker>");
|
|
232
|
+
}
|
|
233
|
+
catch (Exception)
|
|
234
|
+
{
|
|
235
|
+
// An unusable console must not replace the exit code this mode owes the caller.
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Non-invasive: count matching panes/tabs; no tab is selected and no focus is moved.
|
|
240
|
+
private static int Probe(string marker)
|
|
241
|
+
{
|
|
242
|
+
Stopwatch clock = Stopwatch.StartNew();
|
|
243
|
+
List<PaneHit> panes;
|
|
244
|
+
List<TabHit> tabs;
|
|
245
|
+
FindHits(marker, out panes, out tabs);
|
|
246
|
+
bool capable = IsCapable(panes, tabs);
|
|
247
|
+
bool associated = panes.Count == 0 || panes[0].Tab != null;
|
|
248
|
+
bool tabsAgree = true;
|
|
249
|
+
if (panes.Count == 1)
|
|
250
|
+
{
|
|
251
|
+
for (int i = 0; i < tabs.Count; i++)
|
|
252
|
+
{
|
|
253
|
+
if (!SameElement(tabs[i].Tab, panes[0].Tab)) tabsAgree = false;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
string line = "stage=probe ok=" + Bool(capable) + " capable=" + Bool(capable)
|
|
257
|
+
+ " paneHits=" + panes.Count + " tabHits=" + tabs.Count
|
|
258
|
+
+ " associated=" + Bool(associated) + " tabsAgree=" + Bool(tabsAgree)
|
|
259
|
+
+ " lookupMs=" + clock.ElapsedMilliseconds;
|
|
260
|
+
Log(line);
|
|
261
|
+
// The caller reads a failed run's stderr for the stage that failed; the
|
|
262
|
+
// first stdout line is the start banner, not the reason.
|
|
263
|
+
if (!capable) Console.Error.WriteLine(line);
|
|
264
|
+
return capable ? ExitOk : ExitUnreachable;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// One realized pane (at most one tab title as an entry point), or no realized pane
|
|
268
|
+
// but exactly one tab title. Anything else is ambiguous, and ambiguity is never resolved
|
|
269
|
+
// by picking one candidate - a jump to the wrong session is worse than no jump. A pane
|
|
270
|
+
// hit and a tab hit only form one candidate when the tab is the pane's own tab; two
|
|
271
|
+
// hits from different windows/tabs are two sessions even when each count is one.
|
|
272
|
+
private static bool IsCapable(List<PaneHit> panes, List<TabHit> tabs)
|
|
273
|
+
{
|
|
274
|
+
bool associated = panes.Count == 0 || panes[0].Tab != null;
|
|
275
|
+
bool tabsAgree = true;
|
|
276
|
+
if (panes.Count == 1)
|
|
277
|
+
{
|
|
278
|
+
for (int i = 0; i < tabs.Count; i++)
|
|
279
|
+
{
|
|
280
|
+
if (!SameElement(tabs[i].Tab, panes[0].Tab)) tabsAgree = false;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return CandidateVerdict(panes.Count, tabs.Count, associated, tabsAgree);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// The decision without any UIA object, so `--selftest-candidates` can table-test it.
|
|
287
|
+
private static bool CandidateVerdict(int paneCount, int tabCount, bool paneAssociated, bool tabsAgree)
|
|
288
|
+
{
|
|
289
|
+
if (paneCount > 1) return false;
|
|
290
|
+
if (paneCount == 0) return tabCount == 1;
|
|
291
|
+
if (!paneAssociated) return false;
|
|
292
|
+
return tabsAgree;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Cases: one complete candidate is accepted; unrelated pane/tab hits are rejected before
|
|
296
|
+
// any window is touched. Exit 0 only when every row agrees.
|
|
297
|
+
private static int RunCandidateSelfTest()
|
|
298
|
+
{
|
|
299
|
+
object[][] cases = new object[][]
|
|
300
|
+
{
|
|
301
|
+
new object[] { "one associated pane", 1, 0, true, true, true },
|
|
302
|
+
new object[] { "pane plus its own tab", 1, 1, true, true, true },
|
|
303
|
+
new object[] { "pane plus another tab", 1, 1, true, false, false },
|
|
304
|
+
new object[] { "pane without a tab", 1, 1, false, true, false },
|
|
305
|
+
new object[] { "two panes", 2, 0, true, true, false },
|
|
306
|
+
new object[] { "one tab only", 0, 1, true, true, true },
|
|
307
|
+
new object[] { "two tabs", 0, 2, true, true, false },
|
|
308
|
+
new object[] { "no hits", 0, 0, true, true, false },
|
|
309
|
+
};
|
|
310
|
+
bool ok = true;
|
|
311
|
+
for (int i = 0; i < cases.Length; i++)
|
|
312
|
+
{
|
|
313
|
+
object[] row = cases[i];
|
|
314
|
+
bool actual = CandidateVerdict((int)row[1], (int)row[2], (bool)row[3], (bool)row[4]);
|
|
315
|
+
bool expected = (bool)row[5];
|
|
316
|
+
if (actual != expected)
|
|
317
|
+
{
|
|
318
|
+
Console.Error.WriteLine("selftest failed: " + row[0] + " expected " + Bool(expected) + " got " + Bool(actual));
|
|
319
|
+
ok = false;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
Console.Out.WriteLine(ok ? "candidate selftest passed (" + cases.Length + " cases)" : "candidate selftest failed");
|
|
323
|
+
return ok ? ExitOk : ExitUnreachable;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// The click path (D6). Every step is verified, and when a step fails the helper
|
|
327
|
+
// stops there: it never reports success for a jump it could not prove.
|
|
328
|
+
private static int Activate(string marker)
|
|
329
|
+
{
|
|
330
|
+
Stopwatch clock = Stopwatch.StartNew();
|
|
331
|
+
List<PaneHit> panes;
|
|
332
|
+
List<TabHit> tabs;
|
|
333
|
+
FindHits(marker, out panes, out tabs);
|
|
334
|
+
|
|
335
|
+
// The same predicate the probe used decides here as well: a jump is only attempted for
|
|
336
|
+
// the identity the probe called capable, and ambiguity is rejected before any
|
|
337
|
+
// window is touched so a marker carried by two panes cannot pull one of them forward.
|
|
338
|
+
if (!IsCapable(panes, tabs))
|
|
339
|
+
{
|
|
340
|
+
string reason = panes.Count == 0 && tabs.Count == 0 ? "not-found" : "ambiguous";
|
|
341
|
+
Log("stage=discover ok=false reason=" + reason
|
|
342
|
+
+ " paneHits=" + panes.Count + " tabHits=" + tabs.Count);
|
|
343
|
+
return ExitUnreachable;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
bool viaTab = panes.Count == 0;
|
|
347
|
+
IntPtr hwnd = viaTab ? tabs[0].Hwnd : panes[0].Hwnd;
|
|
348
|
+
Log("stage=discover ok=true discovery=" + (viaTab ? "tab" : "pane")
|
|
349
|
+
+ " hwnd=" + hwnd.ToInt64() + " paneHits=" + panes.Count + " tabHits=" + tabs.Count);
|
|
350
|
+
|
|
351
|
+
if (IsIconic(hwnd))
|
|
352
|
+
{
|
|
353
|
+
ShowWindow(hwnd, SW_RESTORE);
|
|
354
|
+
if (!WaitUntil(() => !IsIconic(hwnd), RestoreCapMs))
|
|
355
|
+
{
|
|
356
|
+
Log("stage=restore ok=false reason=\"the window is still minimized after SW_RESTORE\" hwnd=" + hwnd.ToInt64());
|
|
357
|
+
return ExitUnreachable;
|
|
358
|
+
}
|
|
359
|
+
Log("stage=restore ok=true hwnd=" + hwnd.ToInt64());
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (!BringToForeground(hwnd))
|
|
363
|
+
{
|
|
364
|
+
Log("stage=foreground ok=false reason=\"the window never became the foreground window\" hwnd=" + hwnd.ToInt64());
|
|
365
|
+
return ExitUnreachable;
|
|
366
|
+
}
|
|
367
|
+
Log("stage=foreground ok=true hwnd=" + hwnd.ToInt64());
|
|
368
|
+
|
|
369
|
+
AutomationElement pane;
|
|
370
|
+
if (viaTab)
|
|
371
|
+
{
|
|
372
|
+
// The tab is only an entry point: select, verify, then resolve the pane again by
|
|
373
|
+
// HelpText; the unselected element does not exist yet and no runtime id survives.
|
|
374
|
+
if (!SelectTab(tabs[0].Tab))
|
|
375
|
+
{
|
|
376
|
+
Log("stage=select ok=false reason=\"the tab did not report itself selected\"");
|
|
377
|
+
return ExitUnreachable;
|
|
378
|
+
}
|
|
379
|
+
Log("stage=select ok=true");
|
|
380
|
+
pane = WaitForPane(tabs[0].Window, marker, RealizeCapMs);
|
|
381
|
+
if (pane == null)
|
|
382
|
+
{
|
|
383
|
+
Log("stage=realize ok=false reason=\"the selected tab never exposed exactly one pane with the marker\"");
|
|
384
|
+
return ExitUnreachable;
|
|
385
|
+
}
|
|
386
|
+
Log("stage=realize ok=true");
|
|
387
|
+
}
|
|
388
|
+
else
|
|
389
|
+
{
|
|
390
|
+
pane = panes[0].Pane;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
string paneRtid = RuntimeIdOf(pane);
|
|
394
|
+
if (paneRtid == null)
|
|
395
|
+
{
|
|
396
|
+
Log("stage=focus ok=false reason=\"the target pane has no runtime id\"");
|
|
397
|
+
return ExitUnreachable;
|
|
398
|
+
}
|
|
399
|
+
if (!FocusPane(pane, paneRtid, marker))
|
|
400
|
+
{
|
|
401
|
+
Log("stage=focus ok=false reason=\"the keyboard focus did not land on the target pane\" rtid=" + paneRtid);
|
|
402
|
+
return ExitUnreachable;
|
|
403
|
+
}
|
|
404
|
+
Log("stage=focus ok=true rtid=" + paneRtid);
|
|
405
|
+
|
|
406
|
+
Checks checks = Verify(paneRtid, marker, hwnd);
|
|
407
|
+
bool ok = checks.SameElement && checks.KeyboardFocus && checks.HelpText
|
|
408
|
+
&& checks.Foreground && checks.NotMinimized;
|
|
409
|
+
Log("stage=verify ok=" + Bool(ok)
|
|
410
|
+
+ " sameElement=" + Bool(checks.SameElement)
|
|
411
|
+
+ " hasKeyboardFocus=" + Bool(checks.KeyboardFocus)
|
|
412
|
+
+ " helpText=" + Bool(checks.HelpText)
|
|
413
|
+
+ " foreground=" + Bool(checks.Foreground)
|
|
414
|
+
+ " notMinimized=" + Bool(checks.NotMinimized)
|
|
415
|
+
+ " hwnd=" + hwnd.ToInt64() + " rtid=" + paneRtid
|
|
416
|
+
+ " totalMs=" + clock.ElapsedMilliseconds);
|
|
417
|
+
if (!ok)
|
|
418
|
+
{
|
|
419
|
+
Log("stage=activate ok=false reason=\"one or more of the five checks failed\"");
|
|
420
|
+
return ExitUnreachable;
|
|
421
|
+
}
|
|
422
|
+
Log("stage=activate ok=true totalMs=" + clock.ElapsedMilliseconds);
|
|
423
|
+
return ExitOk;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Top-level Windows Terminal windows, found by desktop class rather than process name.
|
|
427
|
+
// Minimized windows stay (still visible, which AC-6 needs); windows without a native
|
|
428
|
+
// handle cannot be restored or foregrounded and are skipped.
|
|
429
|
+
private static List<AutomationElement> TerminalWindows()
|
|
430
|
+
{
|
|
431
|
+
List<AutomationElement> windows = new List<AutomationElement>();
|
|
432
|
+
AutomationElementCollection found = AutomationElement.RootElement.FindAll(
|
|
433
|
+
TreeScope.Children,
|
|
434
|
+
new PropertyCondition(AutomationElement.ClassNameProperty, TerminalWindowClass));
|
|
435
|
+
for (int i = 0; i < found.Count; i++)
|
|
436
|
+
{
|
|
437
|
+
AutomationElement window = found[i];
|
|
438
|
+
IntPtr hwnd = HwndOf(window);
|
|
439
|
+
if (hwnd == IntPtr.Zero || !IsWindowVisible(hwnd)) continue;
|
|
440
|
+
windows.Add(window);
|
|
441
|
+
}
|
|
442
|
+
return windows;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
private static AutomationElementCollection PanesOf(AutomationElement window)
|
|
446
|
+
{
|
|
447
|
+
AndCondition condition = new AndCondition(
|
|
448
|
+
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text),
|
|
449
|
+
new PropertyCondition(AutomationElement.ClassNameProperty, TermControlClass));
|
|
450
|
+
return window.FindAll(TreeScope.Descendants, condition);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// A window that disappears or denies UIA access is logged and skipped: under-detecting a
|
|
454
|
+
// target degrades to "no jump", which is the contract, while guessing is not.
|
|
455
|
+
private static void FindHits(string marker, out List<PaneHit> panes, out List<TabHit> tabs)
|
|
456
|
+
{
|
|
457
|
+
panes = new List<PaneHit>();
|
|
458
|
+
tabs = new List<TabHit>();
|
|
459
|
+
List<AutomationElement> windows = TerminalWindows();
|
|
460
|
+
for (int i = 0; i < windows.Count; i++)
|
|
461
|
+
{
|
|
462
|
+
AutomationElement window = windows[i];
|
|
463
|
+
IntPtr hwnd = HwndOf(window);
|
|
464
|
+
try
|
|
465
|
+
{
|
|
466
|
+
AutomationElement selectedTab = SelectedTabOf(window);
|
|
467
|
+
List<AutomationElement> windowPanes = PanesCarryingMarker(window, marker);
|
|
468
|
+
for (int j = 0; j < windowPanes.Count; j++)
|
|
469
|
+
{
|
|
470
|
+
PaneHit hit = new PaneHit();
|
|
471
|
+
hit.Hwnd = hwnd;
|
|
472
|
+
hit.Window = window;
|
|
473
|
+
hit.Pane = windowPanes[j];
|
|
474
|
+
hit.Tab = selectedTab;
|
|
475
|
+
panes.Add(hit);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
AutomationElementCollection windowTabs = window.FindAll(
|
|
479
|
+
TreeScope.Descendants,
|
|
480
|
+
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem));
|
|
481
|
+
for (int j = 0; j < windowTabs.Count; j++)
|
|
482
|
+
{
|
|
483
|
+
AutomationElement tab = windowTabs[j];
|
|
484
|
+
if (TabNameOf(tab).IndexOf(marker, StringComparison.Ordinal) < 0) continue;
|
|
485
|
+
TabHit hit = new TabHit();
|
|
486
|
+
hit.Window = window;
|
|
487
|
+
hit.Hwnd = hwnd;
|
|
488
|
+
hit.Tab = tab;
|
|
489
|
+
tabs.Add(hit);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
catch (Exception failure)
|
|
493
|
+
{
|
|
494
|
+
Log("stage=discover window=" + hwnd.ToInt64() + " ok=false reason=\""
|
|
495
|
+
+ Escape(failure.Message) + "\"");
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// One identity rule for the whole file: a pane is a TermControl whose own HelpText
|
|
501
|
+
// carries the marker, shared by discovery and the post-switch lookup so it cannot drift.
|
|
502
|
+
private static List<AutomationElement> PanesCarryingMarker(AutomationElement window, string marker)
|
|
503
|
+
{
|
|
504
|
+
List<AutomationElement> hits = new List<AutomationElement>();
|
|
505
|
+
AutomationElementCollection panes = PanesOf(window);
|
|
506
|
+
for (int i = 0; i < panes.Count; i++)
|
|
507
|
+
{
|
|
508
|
+
AutomationElement pane = panes[i];
|
|
509
|
+
if (HelpTextOf(pane).IndexOf(marker, StringComparison.Ordinal) >= 0) hits.Add(pane);
|
|
510
|
+
}
|
|
511
|
+
return hits;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// The TabItem a pane belongs to. Only the selected tab's panes are realized in
|
|
515
|
+
// the UIA tree, so the window's selected tab is the pane's tab; a window without
|
|
516
|
+
// a selected tab cannot prove the association and stays ambiguous.
|
|
517
|
+
private static AutomationElement SelectedTabOf(AutomationElement window)
|
|
518
|
+
{
|
|
519
|
+
AutomationElementCollection tabs = window.FindAll(
|
|
520
|
+
TreeScope.Descendants,
|
|
521
|
+
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem));
|
|
522
|
+
for (int i = 0; i < tabs.Count; i++)
|
|
523
|
+
{
|
|
524
|
+
AutomationElement tab = tabs[i];
|
|
525
|
+
object pattern;
|
|
526
|
+
if (!tab.TryGetCurrentPattern(SelectionItemPattern.Pattern, out pattern)) continue;
|
|
527
|
+
if (((SelectionItemPattern)pattern).Current.IsSelected) return tab;
|
|
528
|
+
}
|
|
529
|
+
// A single-tab window has no ambiguity to resolve; when the pattern is
|
|
530
|
+
// unavailable in some WT build this is still one provable candidate.
|
|
531
|
+
if (tabs.Count == 1) return tabs[0];
|
|
532
|
+
return null;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
private static List<AutomationElement> FindPanesInWindow(AutomationElement window, string marker)
|
|
536
|
+
{
|
|
537
|
+
try
|
|
538
|
+
{
|
|
539
|
+
return PanesCarryingMarker(window, marker);
|
|
540
|
+
}
|
|
541
|
+
catch (Exception failure)
|
|
542
|
+
{
|
|
543
|
+
Log("stage=realize ok=false reason=\"pane lookup failed: " + Escape(failure.Message) + "\"");
|
|
544
|
+
return new List<AutomationElement>();
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// Selecting a tab does not realize its view immediately, so poll for the pane instead of
|
|
549
|
+
// sleeping a guessed delay. More than one hit inside the selected tab is still ambiguous.
|
|
550
|
+
private static AutomationElement WaitForPane(AutomationElement window, string marker, int capMs)
|
|
551
|
+
{
|
|
552
|
+
List<AutomationElement> hits = new List<AutomationElement>();
|
|
553
|
+
AutomationElement pane = null;
|
|
554
|
+
WaitUntil(() =>
|
|
555
|
+
{
|
|
556
|
+
hits = FindPanesInWindow(window, marker);
|
|
557
|
+
pane = hits.Count == 1 ? hits[0] : null;
|
|
558
|
+
return pane != null || hits.Count > 1;
|
|
559
|
+
}, capMs);
|
|
560
|
+
if (hits.Count > 1) Log("stage=realize ok=false reason=ambiguous paneHits=" + hits.Count);
|
|
561
|
+
return pane;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// SetForegroundWindow returns true without moving the window while the foreground lock
|
|
565
|
+
// holds, so each attempt asks the desktop instead of trusting the call. Failing here is
|
|
566
|
+
// a known real-machine risk; the stage is reported rather than a success being faked.
|
|
567
|
+
private static bool BringToForeground(IntPtr hwnd)
|
|
568
|
+
{
|
|
569
|
+
for (int attempt = 1; attempt <= ForegroundAttempts; attempt++)
|
|
570
|
+
{
|
|
571
|
+
SetForegroundWindow(hwnd);
|
|
572
|
+
if (WaitUntil(() => GetForegroundWindow() == hwnd, ForegroundCapMs))
|
|
573
|
+
{
|
|
574
|
+
Log("stage=foreground attempt=" + attempt + " ok=true");
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
Log("stage=foreground attempt=" + attempt + " ok=false");
|
|
578
|
+
}
|
|
579
|
+
return false;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
private static bool SelectTab(AutomationElement tab)
|
|
583
|
+
{
|
|
584
|
+
for (int attempt = 1; attempt <= SelectAttempts; attempt++)
|
|
585
|
+
{
|
|
586
|
+
try
|
|
587
|
+
{
|
|
588
|
+
SelectionItemPattern pattern = (SelectionItemPattern)tab.GetCurrentPattern(SelectionItemPattern.Pattern);
|
|
589
|
+
pattern.Select();
|
|
590
|
+
if (WaitUntil(() => IsSelected(pattern), SelectCapMs))
|
|
591
|
+
{
|
|
592
|
+
Log("stage=select attempt=" + attempt + " ok=true");
|
|
593
|
+
return true;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
catch (Exception failure)
|
|
597
|
+
{
|
|
598
|
+
Log("stage=select attempt=" + attempt + " ok=false reason=\"" + Escape(failure.Message) + "\"");
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
return false;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
private static bool IsSelected(SelectionItemPattern pattern)
|
|
605
|
+
{
|
|
606
|
+
try
|
|
607
|
+
{
|
|
608
|
+
return pattern.Current.IsSelected;
|
|
609
|
+
}
|
|
610
|
+
catch (Exception)
|
|
611
|
+
{
|
|
612
|
+
return false;
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// Focusing "the first focusable TermControl" proves only that some pane is focused; this
|
|
617
|
+
// focuses the element resolved through the marker and waits until the desktop agrees.
|
|
618
|
+
private static bool FocusPane(AutomationElement pane, string paneRtid, string marker)
|
|
619
|
+
{
|
|
620
|
+
for (int attempt = 1; attempt <= FocusAttempts; attempt++)
|
|
621
|
+
{
|
|
622
|
+
try
|
|
623
|
+
{
|
|
624
|
+
pane.SetFocus();
|
|
625
|
+
}
|
|
626
|
+
catch (Exception failure)
|
|
627
|
+
{
|
|
628
|
+
Log("stage=focus attempt=" + attempt + " ok=false reason=\"SetFocus threw: "
|
|
629
|
+
+ Escape(failure.Message) + "\"");
|
|
630
|
+
}
|
|
631
|
+
if (WaitUntil(() => IsFocused(paneRtid, marker), FocusCapMs))
|
|
632
|
+
{
|
|
633
|
+
Log("stage=focus attempt=" + attempt + " ok=true");
|
|
634
|
+
return true;
|
|
635
|
+
}
|
|
636
|
+
Log("stage=focus attempt=" + attempt + " ok=false reason=\"focus did not land on the target pane\"");
|
|
637
|
+
}
|
|
638
|
+
return false;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
private static bool IsFocused(string paneRtid, string marker)
|
|
642
|
+
{
|
|
643
|
+
try
|
|
644
|
+
{
|
|
645
|
+
AutomationElement focused = AutomationElement.FocusedElement;
|
|
646
|
+
if (focused == null) return false;
|
|
647
|
+
if (RuntimeIdOf(focused) != paneRtid) return false;
|
|
648
|
+
if (!focused.Current.HasKeyboardFocus) return false;
|
|
649
|
+
return HelpTextOf(focused).IndexOf(marker, StringComparison.Ordinal) >= 0;
|
|
650
|
+
}
|
|
651
|
+
catch (Exception)
|
|
652
|
+
{
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// The five checks the jump-back contract requires, each read from the live desktop
|
|
658
|
+
// after the focus call. A check that cannot be read is false: unproven is not verified.
|
|
659
|
+
private static Checks Verify(string paneRtid, string marker, IntPtr hwnd)
|
|
660
|
+
{
|
|
661
|
+
Checks checks = new Checks();
|
|
662
|
+
try
|
|
663
|
+
{
|
|
664
|
+
AutomationElement focused = AutomationElement.FocusedElement;
|
|
665
|
+
checks.SameElement = focused != null && RuntimeIdOf(focused) == paneRtid;
|
|
666
|
+
checks.KeyboardFocus = focused != null && focused.Current.HasKeyboardFocus;
|
|
667
|
+
checks.HelpText = focused != null
|
|
668
|
+
&& HelpTextOf(focused).IndexOf(marker, StringComparison.Ordinal) >= 0;
|
|
669
|
+
}
|
|
670
|
+
catch (Exception failure)
|
|
671
|
+
{
|
|
672
|
+
Log("stage=verify ok=false reason=\"reading the focused element failed: "
|
|
673
|
+
+ Escape(failure.Message) + "\"");
|
|
674
|
+
}
|
|
675
|
+
checks.Foreground = GetForegroundWindow() == hwnd;
|
|
676
|
+
checks.NotMinimized = !IsIconic(hwnd);
|
|
677
|
+
return checks;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// Runtime ids identify an element within the desktop session. They are read after any tab
|
|
681
|
+
// switch because the pane that was not realized before does not have one yet.
|
|
682
|
+
private static bool SameElement(AutomationElement left, AutomationElement right)
|
|
683
|
+
{
|
|
684
|
+
if (left == null || right == null) return false;
|
|
685
|
+
string leftId = RuntimeIdOf(left);
|
|
686
|
+
string rightId = RuntimeIdOf(right);
|
|
687
|
+
return leftId != null && leftId == rightId;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
private static string RuntimeIdOf(AutomationElement element)
|
|
691
|
+
{
|
|
692
|
+
try
|
|
693
|
+
{
|
|
694
|
+
int[] ids = element.GetRuntimeId();
|
|
695
|
+
if (ids == null || ids.Length == 0) return null;
|
|
696
|
+
StringBuilder text = new StringBuilder();
|
|
697
|
+
for (int i = 0; i < ids.Length; i++)
|
|
698
|
+
{
|
|
699
|
+
if (i > 0) text.Append('.');
|
|
700
|
+
text.Append(ids[i].ToString());
|
|
701
|
+
}
|
|
702
|
+
return text.ToString();
|
|
703
|
+
}
|
|
704
|
+
catch (Exception)
|
|
705
|
+
{
|
|
706
|
+
return null;
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
private static IntPtr HwndOf(AutomationElement element)
|
|
711
|
+
{
|
|
712
|
+
try
|
|
713
|
+
{
|
|
714
|
+
return new IntPtr(element.Current.NativeWindowHandle);
|
|
715
|
+
}
|
|
716
|
+
catch (Exception)
|
|
717
|
+
{
|
|
718
|
+
return IntPtr.Zero;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
private static string HelpTextOf(AutomationElement element)
|
|
723
|
+
{
|
|
724
|
+
try
|
|
725
|
+
{
|
|
726
|
+
return element.Current.HelpText == null ? "" : element.Current.HelpText;
|
|
727
|
+
}
|
|
728
|
+
catch (Exception)
|
|
729
|
+
{
|
|
730
|
+
return "";
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
private static string TabNameOf(AutomationElement element)
|
|
735
|
+
{
|
|
736
|
+
try
|
|
737
|
+
{
|
|
738
|
+
return element.Current.Name == null ? "" : element.Current.Name;
|
|
739
|
+
}
|
|
740
|
+
catch (Exception)
|
|
741
|
+
{
|
|
742
|
+
return "";
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
private static bool WaitUntil(Func<bool> state, int capMs)
|
|
747
|
+
{
|
|
748
|
+
int waited = 0;
|
|
749
|
+
while (true)
|
|
750
|
+
{
|
|
751
|
+
if (state()) return true;
|
|
752
|
+
if (waited >= capMs) return false;
|
|
753
|
+
int step = Math.Min(PollIntervalMs, capMs - waited);
|
|
754
|
+
Thread.Sleep(step);
|
|
755
|
+
waited += step;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
// The shell starts this outside the TUI's environment, so the data directory is an
|
|
760
|
+
// explicit argument; the fallback mirrors src/utils/paths.ts for a hand run.
|
|
761
|
+
private static string ResolveLogPath(string dataDir)
|
|
762
|
+
{
|
|
763
|
+
try
|
|
764
|
+
{
|
|
765
|
+
string dir = dataDir;
|
|
766
|
+
if (dir == null || dir.Length == 0)
|
|
767
|
+
{
|
|
768
|
+
string home = Environment.GetEnvironmentVariable("USERPROFILE");
|
|
769
|
+
if (home == null || home.Length == 0) home = Environment.GetEnvironmentVariable("HOME");
|
|
770
|
+
if (home == null || home.Length == 0) return null;
|
|
771
|
+
dir = Path.Combine(home, ".dsh-tui");
|
|
772
|
+
}
|
|
773
|
+
return Path.Combine(Path.Combine(dir, "notify"), "jumpback.log");
|
|
774
|
+
}
|
|
775
|
+
catch (Exception)
|
|
776
|
+
{
|
|
777
|
+
return null;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// The log is the only trace a click leaves, but an unwritable log must never cost the
|
|
782
|
+
// jump. The file copy comes first; the console copy may fail - exit codes are the
|
|
783
|
+
// contract, the console is not (a winexe may have none).
|
|
784
|
+
private static void Log(string message)
|
|
785
|
+
{
|
|
786
|
+
string line = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fff") + " " + message;
|
|
787
|
+
if (logPath != null)
|
|
788
|
+
{
|
|
789
|
+
try
|
|
790
|
+
{
|
|
791
|
+
// The helper creates its own log directory: on a first click neither
|
|
792
|
+
// <DataDir>/notify nor the log exists, and AppendAllText makes no parents.
|
|
793
|
+
string directory = Path.GetDirectoryName(logPath);
|
|
794
|
+
if (directory != null && directory.Length > 0) Directory.CreateDirectory(directory);
|
|
795
|
+
TruncateLogIfFull();
|
|
796
|
+
File.AppendAllText(logPath, line + "\r\n");
|
|
797
|
+
}
|
|
798
|
+
catch (Exception)
|
|
799
|
+
{
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
try
|
|
803
|
+
{
|
|
804
|
+
Console.WriteLine(line);
|
|
805
|
+
}
|
|
806
|
+
catch (Exception)
|
|
807
|
+
{
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
private static void TruncateLogIfFull()
|
|
812
|
+
{
|
|
813
|
+
FileInfo info = new FileInfo(logPath);
|
|
814
|
+
if (!info.Exists || info.Length <= MaxLogBytes) return;
|
|
815
|
+
File.WriteAllText(logPath, "");
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
private static string Escape(string text)
|
|
819
|
+
{
|
|
820
|
+
if (text == null) return "";
|
|
821
|
+
return text.Replace("\\", "\\\\")
|
|
822
|
+
.Replace("\"", "\\\"")
|
|
823
|
+
.Replace("\r", " ")
|
|
824
|
+
.Replace("\n", " ");
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
private static string Quote(string text)
|
|
828
|
+
{
|
|
829
|
+
return "\"" + Escape(text) + "\"";
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
private static string Bool(bool value)
|
|
833
|
+
{
|
|
834
|
+
return value ? "true" : "false";
|
|
835
|
+
}
|
|
836
|
+
}
|