-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathAgentPaneContent.cpp
More file actions
493 lines (445 loc) · 16.9 KB
/
Copy pathAgentPaneContent.cpp
File metadata and controls
493 lines (445 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "AgentPaneContent.h"
#include "AgentPaneContent.g.cpp"
#include <algorithm>
#include <cwctype>
#include <winrt/Windows.UI.Xaml.Media.h>
using namespace winrt::Windows::UI;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Controls;
using namespace winrt::Windows::UI::Xaml::Media;
using namespace winrt::Microsoft::Terminal::Control;
using namespace winrt::Microsoft::Terminal::Settings::Model;
namespace winrt::TerminalApp::implementation
{
namespace
{
enum class AgentLogoKind
{
Copilot,
Claude,
Gemini,
Codex,
OpenCode,
};
// Map the agent's display name (case-insensitive substring) to its
// XAML path. Unknown agents fall back to Copilot.
AgentLogoKind _logoForAgent(const winrt::hstring& name)
{
std::wstring lower{ name };
std::transform(lower.begin(), lower.end(), lower.begin(),
[](wchar_t c) { return static_cast<wchar_t>(std::towlower(c)); });
if (lower.find(L"claude") != std::wstring::npos) return AgentLogoKind::Claude;
if (lower.find(L"codex") != std::wstring::npos) return AgentLogoKind::Codex;
if (lower.find(L"openai") != std::wstring::npos) return AgentLogoKind::Codex;
if (lower.find(L"gpt") != std::wstring::npos) return AgentLogoKind::Codex;
if (lower.find(L"gemini") != std::wstring::npos) return AgentLogoKind::Gemini;
if (lower.find(L"opencode") != std::wstring::npos) return AgentLogoKind::OpenCode;
return AgentLogoKind::Copilot;
}
}
AgentPaneContent::AgentPaneContent(const winrt::TerminalApp::TerminalPaneContent& inner) :
_inner{ inner }
{
InitializeComponent();
// The wta TermControl is owned by the inner TerminalPaneContent.
// Its GetRoot() returns the TermControl itself; pin it into our row 1.
if (_inner)
{
InnerContent().Content(_inner.GetRoot());
}
_wireInnerEvents();
// Default label + logo until wta sends an agent_status event.
_refreshLabel();
_refreshLogo();
}
winrt::TerminalApp::TerminalPaneContent AgentPaneContent::GetTerminalContent()
{
return _inner;
}
winrt::Microsoft::Terminal::Control::TermControl AgentPaneContent::GetTermControl()
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
return impl->GetTermControl();
}
return nullptr;
}
void AgentPaneContent::UpdateAgentStatus(const winrt::hstring& name,
const winrt::hstring& version,
const winrt::hstring& model,
const winrt::hstring& state,
const winrt::hstring& backend)
{
const bool nameChanged = _agentName != name;
_agentName = name;
_agentVersion = version;
_agentModel = model;
_agentState = state;
_agentBackend = backend;
_refreshLabel();
if (nameChanged)
{
_refreshLogo();
}
// Match `SetSessionsView` and `ApplyAutofixState`: any bottom-bar-
// affecting state mutation on AgentPaneContent must raise
// `StateChanged` so subscribers (TerminalPage's bar-refresh
// handler) can pick up the change without polling. The bar does
// not currently render the agent name itself, but the cross-
// window-drag fix path in `TabManagement.cpp` relies on
// `_UpdateBottomBarState` running once after the wire-up to
// reflect any cached state the helper pushed before the wire
// was in place — without the raise here, that catch-up wouldn't
// observe agent_status arriving in the same race window. Also
// future-proofs the bar against ever displaying agent name.
StateChanged.raise(*this, nullptr);
}
// Swap the bar between two modes:
// * chat / connecting / etc. (active=false) — agent logo + "<name> <version>"
// * session management view (active=true) — no logo, "Agent sessions"
// Idempotent so callers don't need to dedupe.
void AgentPaneContent::SetSessionsView(bool active)
{
if (_isSessionsView == active)
{
return;
}
_isSessionsView = active;
_refreshLabel();
_refreshLogo();
StateChanged.raise(*this, nullptr);
}
void AgentPaneContent::ApplyAutofixState(AutofixState state,
const winrt::hstring& paneId,
const winrt::hstring& summary,
const winrt::hstring& fixPreview,
const winrt::hstring& hotkeyHint,
const winrt::hstring& suggestionTitle)
{
_autofixState = state;
if (state == AutofixState::Idle)
{
// Clear ALL cached fields on idle, including `_hotkeyHint`.
// The bottom bar reads these directly, so a leftover hint
// from a prior Detected/Pending transition would otherwise
// hang around after the bar should have gone quiet.
_lastErrorPaneId = {};
_fixPreview = {};
_suggestionTitle = {};
_detectedSummary = {};
_hotkeyHint = {};
}
else
{
if (!paneId.empty())
{
_lastErrorPaneId = paneId;
}
if (!summary.empty())
{
_detectedSummary = summary;
}
if (!fixPreview.empty())
{
_fixPreview = fixPreview;
}
if (!hotkeyHint.empty())
{
_hotkeyHint = hotkeyHint;
}
if (!suggestionTitle.empty())
{
_suggestionTitle = suggestionTitle;
}
}
StateChanged.raise(*this, nullptr);
}
void AgentPaneContent::SetAgentPanePosition(const winrt::hstring& position)
{
if (_agentPanePosition == position)
{
return;
}
_agentPanePosition = position;
StateChanged.raise(*this, nullptr);
}
// Apply the supplied colors to the agent-pane top bar (#348). The vector
// logo paths bind to the label's foreground, so both take the same tint.
// The 1px bottom hairline uses the foreground color at ~15% alpha, so it
// reads as a soft separator (like the original #26FFFFFF) — consistent
// with the text but not a hard full-white/black line.
void AgentPaneContent::ApplyThemeColors(const Media::Brush& background,
const Media::Brush& foreground)
{
if (const auto barRoot = AgentBarRoot())
{
barRoot.Background(background);
if (const auto fgSolid = foreground.try_as<Media::SolidColorBrush>())
{
auto c = fgSolid.Color();
c.A = 0x26;
barRoot.BorderBrush(Media::SolidColorBrush{ c });
}
else
{
barRoot.BorderBrush(foreground);
}
}
if (const auto label = AgentLabelText())
{
label.Foreground(foreground);
}
}
void AgentPaneContent::_refreshLabel()
{
// Session-management view takes over the bar — the wta TUI below no
// longer renders its own "Agent sessions" header, so this is where
// that title lives.
if (_isSessionsView)
{
const auto text = _agentName.empty() ?
std::wstring{ RS_(L"AgentPane_SessionsTitle") } :
RS_fmt(L"AgentPane_SessionsTitleFormat", std::wstring{ _agentName });
AgentLabelText().Text(winrt::hstring{ text });
return;
}
std::wstring text;
if (_agentName.empty())
{
text = L"";
}
else
{
text = std::wstring{ _agentName };
if (!_agentBackend.empty())
{
text += L" \u00B7 ";
text += _agentBackend;
}
if (!_agentVersion.empty())
{
text += L" ";
text += _agentVersion;
}
else if (!_agentModel.empty())
{
text += L" ";
text += _agentModel;
}
}
AgentLabelText().Text(winrt::hstring{ text });
}
void AgentPaneContent::_refreshLogo()
{
if (_isSessionsView)
{
AgentLogo().Visibility(Visibility::Collapsed);
return;
}
if (_agentName.empty())
{
AgentLogo().Visibility(Visibility::Collapsed);
return;
}
const auto logo = _logoForAgent(_agentName);
CopilotLogo().Visibility(logo == AgentLogoKind::Copilot ? Visibility::Visible : Visibility::Collapsed);
ClaudeLogo().Visibility(logo == AgentLogoKind::Claude ? Visibility::Visible : Visibility::Collapsed);
GeminiLogo().Visibility(logo == AgentLogoKind::Gemini ? Visibility::Visible : Visibility::Collapsed);
CodexLogo().Visibility(logo == AgentLogoKind::Codex ? Visibility::Visible : Visibility::Collapsed);
OpenCodeLogo().Visibility(logo == AgentLogoKind::OpenCode ? Visibility::Visible : Visibility::Collapsed);
AgentLogo().Visibility(Visibility::Visible);
}
#pragma region IPaneContent forwarding
winrt::Windows::UI::Xaml::FrameworkElement AgentPaneContent::GetRoot()
{
return *this;
}
void AgentPaneContent::UpdateSettings(const CascadiaSettings& settings)
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
impl->UpdateSettings(settings);
}
// Re-pick up the pane position in case settings changed it.
SetAgentPanePosition(settings.GlobalSettings().AgentPanePosition());
}
winrt::Windows::Foundation::Size AgentPaneContent::MinimumSize()
{
// Reserve 36px (top bar) on top of the inner control's minimum.
// The bottom bar is window-level chrome now, so it isn't part of
// this pane's minimum size.
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
const auto inner = impl->MinimumSize();
return { inner.Width, inner.Height + 36.0f };
}
return { 1, 36.0f };
}
void AgentPaneContent::Focus(winrt::Windows::UI::Xaml::FocusState reason)
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
impl->Focus(reason);
}
}
void AgentPaneContent::Close()
{
_unwireInnerEvents();
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
impl->Close();
}
}
INewContentArgs AgentPaneContent::GetNewTerminalArgs(BuildStartupKind kind) const
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
return impl->GetNewTerminalArgs(kind);
}
return nullptr;
}
winrt::hstring AgentPaneContent::Title()
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
return impl->Title();
}
return L"Agent";
}
uint64_t AgentPaneContent::TaskbarState()
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
return impl->TaskbarState();
}
return 0;
}
uint64_t AgentPaneContent::TaskbarProgress()
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
return impl->TaskbarProgress();
}
return 0;
}
bool AgentPaneContent::ReadOnly()
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
return impl->ReadOnly();
}
return false;
}
winrt::hstring AgentPaneContent::Icon() const
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
return impl->Icon();
}
return {};
}
Windows::Foundation::IReference<winrt::Windows::UI::Color> AgentPaneContent::TabColor() const noexcept
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
return impl->TabColor();
}
return nullptr;
}
winrt::Windows::UI::Xaml::Media::Brush AgentPaneContent::BackgroundBrush()
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
return impl->BackgroundBrush();
}
return nullptr;
}
#pragma endregion
#pragma region ISnappable
float AgentPaneContent::SnapDownToGrid(const TerminalApp::PaneSnapDirection direction, const float sizeToSnap)
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
// Snapping is computed against the terminal grid; account for the
// 36px we steal off the top before delegating, then add it back.
if (direction == TerminalApp::PaneSnapDirection::Height)
{
const auto adjusted = std::max(0.0f, sizeToSnap - 36.0f);
return impl->SnapDownToGrid(direction, adjusted) + 36.0f;
}
return impl->SnapDownToGrid(direction, sizeToSnap);
}
return sizeToSnap;
}
Windows::Foundation::Size AgentPaneContent::GridUnitSize()
{
if (const auto& impl = winrt::get_self<implementation::TerminalPaneContent>(_inner))
{
return impl->GridUnitSize();
}
return { 1, 1 };
}
#pragma endregion
#pragma region inner event forwarding
void AgentPaneContent::_wireInnerEvents()
{
if (!_inner)
{
return;
}
// Forward each inner IPaneContent event up to our own subscribers so
// Tab / TerminalPage can stay agnostic to the wrapper.
const auto self = get_strong();
_innerCloseRequested = _inner.CloseRequested(
[self](const winrt::TerminalApp::IPaneContent& /*sender*/, const auto& args) {
self->CloseRequested.raise(*self, args);
});
_innerConnectionStateChanged = _inner.ConnectionStateChanged(
[self](const auto& sender, const auto& args) {
self->ConnectionStateChanged.raise(sender, args);
});
_innerBellRequested = _inner.BellRequested(
[self](const winrt::TerminalApp::IPaneContent& /*sender*/, const winrt::TerminalApp::BellEventArgs& args) {
self->BellRequested.raise(*self, args);
});
_innerTitleChanged = _inner.TitleChanged(
[self](const winrt::TerminalApp::IPaneContent& /*sender*/, const auto& args) {
self->TitleChanged.raise(*self, args);
});
_innerTabColorChanged = _inner.TabColorChanged(
[self](const winrt::TerminalApp::IPaneContent& /*sender*/, const auto& args) {
self->TabColorChanged.raise(*self, args);
});
_innerTaskbarProgressChanged = _inner.TaskbarProgressChanged(
[self](const winrt::TerminalApp::IPaneContent& /*sender*/, const auto& args) {
self->TaskbarProgressChanged.raise(*self, args);
});
_innerReadOnlyChanged = _inner.ReadOnlyChanged(
[self](const winrt::TerminalApp::IPaneContent& /*sender*/, const auto& args) {
self->ReadOnlyChanged.raise(*self, args);
});
_innerFocusRequested = _inner.FocusRequested(
[self](const winrt::TerminalApp::IPaneContent& /*sender*/, const auto& args) {
self->FocusRequested.raise(*self, args);
});
}
void AgentPaneContent::_unwireInnerEvents()
{
if (!_inner)
{
return;
}
_inner.CloseRequested(_innerCloseRequested);
_inner.ConnectionStateChanged(_innerConnectionStateChanged);
_inner.BellRequested(_innerBellRequested);
_inner.TitleChanged(_innerTitleChanged);
_inner.TabColorChanged(_innerTabColorChanged);
_inner.TaskbarProgressChanged(_innerTaskbarProgressChanged);
_inner.ReadOnlyChanged(_innerReadOnlyChanged);
_inner.FocusRequested(_innerFocusRequested);
}
#pragma endregion
}