Repository navigation
Expand file tree
/
Copy pathscrawl.rs
More file actions
1429 lines (1355 loc) · 49.1 KB
/
Copy pathscrawl.rs
File metadata and controls
1429 lines (1355 loc) · 49.1 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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! The Annotate pane (macOS, Linux, and Windows): Scrawl's captures inside
//! Hex. The opt-in with Screen Recording while it's missing and the capture
//! shortcut, collapsed How it works, what happens after an annotation (how
//! long it's kept and its folder), optional Agents setup, and an Advanced fold
//! for the transcriber, demos, and the replay Lab. Scrawl's own app keeps the
//! shortcut while it runs until "Use Hex for annotations" asks it to quit.
#[cfg(not(target_os = "macos"))]
pub(super) mod folder;
pub(super) mod routing_rules;
use rune::{El, Tx, col, row, text};
use scrawl_capture::retention::{Policy, Preset};
use super::controls::{
ButtonKind, CardRow, Choice, METRICS, Paint, button, card, folder_label, folding, icon_row,
icon_toggle_row, nested, note, page, section, section_with, segmented, segmented_tx, select,
setting_row, step_row,
};
use super::general::{advanced_open, advanced_toggle, disclosure_link, keys_button};
use super::icons::{CLOCK, FOLDER, KEYBOARD, WAVE};
use super::lists::permission_control;
use super::platform::{PlatformRequest, ScrawlRequest, ScrawlView};
use super::settings::cancel_capture;
use super::sidebar::SCRAWL_ICON;
use super::{Menu, Pane, ShortcutTarget, Ui};
use crate::agent_install::{self, Agent, AgentStatus, SkillState};
use crate::app_settings::{AppSettings, CaptureTranscriber};
use crate::desktop::i18n::{tr, tr_fill};
use crate::desktop::onboarding::{PermissionAction, PermissionKind};
/// The switch's second line: the whole gesture with the bound shortcut while
/// on and a shortcut is bound, else what Annotate does.
fn description(ui: &Ui, enabled: bool) -> String {
if enabled && ui.records_shortcuts() {
let separator = if cfg!(target_os = "macos") { "" } else { "+" };
tr_fill(
"Tap {}, talk and draw; tap again and it's copied",
&ui.settings.capture.hotkey.keycaps().join(separator),
)
} else {
tr("Record your screen, draw on it, and talk over it").into()
}
}
/// Turns Annotate on or off; turning it on fails while its shortcut
/// collides with another, and leaves the shortcuts as they are.
fn set_enabled(settings: &mut AppSettings, enabled: bool) -> Result<(), &'static str> {
if enabled {
// Judged as it will be, not as it is: a shortcut the capture
// shortcuts only reserve while Annotate is on (the Screenshots
// record shortcut among them) must still count.
let mut candidate = settings.clone();
candidate.capture.enabled = true;
if super::settings::conflicts(
&candidate,
ShortcutTarget::Capture,
&candidate.capture.hotkey,
) {
return Err(tr(
"The capture shortcut is already in use. Change it below before turning captures on.",
));
}
}
settings.capture.enabled = enabled;
Ok(())
}
pub(super) fn toggle(ui: &mut Ui) {
if crate::policy::Policy::current().locks(crate::policy::Lock::Annotate) {
return;
}
cancel_capture(ui);
let enabled = !ui.settings.capture.enabled;
let mut candidate = ui.drafted();
let error = match set_enabled(&mut candidate, enabled) {
Ok(()) if ui.commit(candidate) => None,
Ok(()) => ui.error.clone(),
Err(error) => Some(error.to_string()),
};
if let Some(view) = ui.platform.scrawl.as_mut() {
view.enable_error = error;
}
}
fn transcriber_description(transcriber: CaptureTranscriber) -> &'static str {
match transcriber {
CaptureTranscriber::Parakeet => tr("Uses Hex's dictation model"),
CaptureTranscriber::Apple => tr("Apple's speech recognizer"),
}
}
/// What the Keep menu calls `policy`: one of Scrawl's presets, or the days
/// imported from Scrawl.
fn retention_label(policy: Policy) -> String {
match Preset::ALL
.into_iter()
.find(|preset| preset.policy() == policy)
{
Some(Preset::Week) => tr("Video for 7 days").into(),
Some(Preset::Month) => tr("Video for 30 days").into(),
Some(Preset::Forever) => tr("Forever").into(),
None => match policy.media_days {
Some(days) => tr_fill("Video for {} days", &days.to_string()),
None => tr("Forever").into(),
},
}
}
/// The non-obvious half of retention: the dropdown already names video days,
/// so the caption only notes how long the annotation itself lasts.
fn retention_description(policy: Policy) -> Option<String> {
match (policy.media_days, policy.bundle_days) {
(_, Some(days)) => Some(tr_fill("Annotations for {} days", &days.to_string())),
(Some(_), None) => Some(tr("Annotations stay until you remove them").into()),
(None, None) => None,
}
}
/// The steps in order, each with its keys where it has any: one key taps
/// to start and stop and is held to draw; the capsule's pen draws without
/// it; Esc stops drawing.
fn how_it_works_steps(ui: &Ui, p: Paint) -> Vec<El> {
let shortcut = ui.settings.capture.hotkey.keycaps();
let keys = |key: &str, parts: &[String]| Some(super::controls::keycaps(p, key, parts));
// Wayland tells Hex nothing of Shift outside its own windows.
let draw_how = if cfg!(not(target_os = "linux")) {
tr(
"While it records, hold the shortcut and move the pointer; no click needed. Shift draws too",
)
} else {
tr("While it records, hold the shortcut and move the pointer; no click needed")
};
vec![
step_row(
p,
"how-start",
1,
tr("Start"),
keys("how-start-keys", &shortcut),
tr("Tap the shortcut to start recording; talk as you go"),
),
step_row(
p,
"how-draw",
2,
tr("Draw"),
keys("how-draw-keys", &shortcut),
draw_how,
),
step_row(
p,
"how-pen",
3,
tr("Pen"),
keys("how-pen-keys", &["Esc".to_string()]),
tr(
"Or click the pen on the bar, then drag to draw; click it again or press Esc to stop",
),
),
step_row(
p,
"how-stop",
4,
tr("Stop"),
keys("how-stop-keys", &shortcut),
tr("Tap the shortcut again: the annotation is copied, ready to paste"),
),
step_row(
p,
"how-send",
5,
tr("Send"),
None,
tr("Paste it anywhere, or click Send to… on the capsule to pick a session"),
),
]
}
/// A fold's link, and under it, while open, its card.
fn fold(p: Paint, key: &str, link: El, rows: Option<Vec<El>>) -> El {
col()
.key(key)
.gap(METRICS.label_gap)
.child(link)
.children(rows.map(|rows| card(p, "card", rows)))
}
/// How it works, closed until its link is pressed.
fn how_it_works(ui: &Ui, p: Paint, view: &ScrawlView) -> El {
let link = disclosure_link(
p,
"how-toggle",
tr("How it works"),
view.how_it_works,
|ui: &mut Ui| {
if let Some(view) = ui.platform.scrawl.as_mut() {
view.how_it_works = !view.how_it_works;
}
},
);
fold(
p,
"how",
link,
view.how_it_works.then(|| how_it_works_steps(ui, p)),
)
}
fn scrawl_request(ui: &mut Ui, request: ScrawlRequest) {
ui.ask(PlatformRequest::Scrawl(request));
}
/// macOS's host opens the chooser and saves the pick; elsewhere the window
/// does.
fn choose_folder(ui: &mut Ui) {
#[cfg(target_os = "macos")]
scrawl_request(ui, ScrawlRequest::ChooseFolder);
#[cfg(not(target_os = "macos"))]
folder::choose(ui);
}
/// While Scrawl's own app runs it keeps the shortcut; only this card's
/// button asks it to quit.
fn scrawl_running(p: Paint, view: &ScrawlView) -> El {
let description = if view.quitting_scrawl {
tr("Waiting for Scrawl to quit…")
} else {
tr("Hex leaves the Annotate shortcut to Scrawl until you switch")
};
card(
p,
"scrawl-running",
vec![setting_row(
p,
"scrawl-running-row",
tr("Scrawl is running"),
Some(description),
button(
p,
"use-hex-for-captures",
tr("Use Hex for annotations"),
ButtonKind::Primary,
|ui: &mut Ui, _| scrawl_request(ui, ScrawlRequest::QuitScrawl),
),
)],
)
}
/// The switch, Screen Recording while Annotate can't record yet, and the
/// shortcut (or desktop command on Linux without a shortcuts portal).
fn top_rows(ui: &Ui, p: Paint, view: &ScrawlView) -> Vec<CardRow> {
let enabled = ui.settings.capture.enabled;
let switch = if crate::policy::Policy::current().locks(crate::policy::Lock::Annotate) {
super::controls::managed_row(p, "capture-enabled", Some(&SCRAWL_ICON), tr("Annotate"))
} else {
icon_toggle_row(
p,
"capture-enabled",
&SCRAWL_ICON,
tr("Annotate"),
description(ui, enabled).as_str(),
enabled,
toggle,
)
};
let mut rows = vec![
switch.into(),
folding(
"capture-error-fold",
view.enable_error.as_ref().map(|error| {
note(
"capture-error",
&format!("{} {error}", tr("Could not update Annotate:")),
p.danger(),
)
}),
),
folding("screen-recording-fold", screen_recording_row(ui, p, view)),
];
// Without a shortcuts portal nothing registers the key: the row says
// which command to bind in the desktop's own keyboard settings.
if !ui.records_shortcuts() {
if enabled {
rows.push(
note(
"no-portal",
tr(
"Your desktop has no shortcuts portal. Bind this command in its keyboard settings.",
),
p.muted(),
)
.into(),
);
rows.push(
super::settings::command_row(
p,
"capture-command",
tr("Annotate"),
tr("Start and stop an annotation"),
&["hex annotate toggle"],
)
.into(),
);
}
return rows;
}
rows.push(
icon_row(
p,
"capture-shortcut",
&KEYBOARD,
tr("Shortcut"),
enabled.then(|| tr("Hold while recording to draw")),
keys_button(ui, p, ShortcutTarget::Capture),
)
.into(),
);
rows
}
/// Screen Recording, only while Annotate is on and can't record the screen.
/// Linux asks through the desktop's screen-cast portal when a capture
/// starts, so it has none.
fn screen_recording_row(ui: &Ui, p: Paint, view: &ScrawlView) -> Option<El> {
if !ui.settings.capture.enabled || !cfg!(target_os = "macos") {
return None;
}
if view.screen_needs_restart {
// macOS lets a running app use the grant only after it relaunches.
return Some(nested(setting_row(
p,
"screen-recording",
tr("Screen Recording"),
Some(tr("Turns on when Hex restarts")),
button(
p,
"screen-recording-restart",
tr("Restart Hex"),
ButtonKind::Secondary,
|ui: &mut Ui, _| ui.ask(PlatformRequest::Permission(PermissionAction::Restart)),
),
)));
}
if view.screen_recording {
return None;
}
let stale = ui.platform.stale(PermissionKind::ScreenRecording);
Some(nested(setting_row(
p,
"screen-recording",
tr("Screen Recording"),
Some(if stale {
tr("An older Hex entry can keep this off")
} else {
tr("Hex can't see your screen yet")
}),
permission_control(
p,
"screen-recording-allow",
ui.platform
.control_for(PermissionKind::ScreenRecording, false),
ButtonKind::Secondary,
move |ui: &mut Ui| {
let action = if stale {
PermissionAction::ResetAndPrompt(PermissionKind::ScreenRecording)
} else {
PermissionAction::ShowGuide(PermissionKind::ScreenRecording)
};
ui.ask(PlatformRequest::Permission(action))
},
),
)))
}
/// Keep, then the folder.
fn after_rows(ui: &Ui, p: Paint, view: &ScrawlView, layer: &mut Vec<El>) -> Vec<El> {
let policy = ui.settings.capture.retention();
let mut choices: Vec<Choice> = Preset::ALL
.into_iter()
.map(|preset| Choice {
label: retention_label(preset.policy()),
detail: None,
selected: preset.policy() == policy,
choose: Box::new(move |ui: &mut Ui| {
ui.open = None;
ui.update(|settings| settings.capture.set_retention(preset.policy()));
}),
})
.collect();
if !choices.iter().any(|choice| choice.selected) {
choices.push(Choice {
label: retention_label(policy),
detail: None,
selected: true,
choose: Box::new(|ui: &mut Ui| ui.open = None),
});
}
let kept = retention_description(policy);
let folder = folder_label(&ui.settings.capture.captures_dir());
let mut rows = vec![
routing_rules::settings_row(ui, p),
icon_row(
p,
"capture-retention",
&CLOCK,
tr("Keep"),
kept.as_deref(),
select(
p,
"capture-retention-menu",
tr("Keep"),
&retention_label(policy),
ui.open == Some(Menu::CaptureRetention),
choices,
|ui: &mut Ui| {
ui.open =
(ui.open != Some(Menu::CaptureRetention)).then_some(Menu::CaptureRetention);
},
layer,
),
),
icon_row(
p,
"capture-folder",
&FOLDER,
tr("Folder"),
Some(folder.as_str()),
row()
.key("controls")
.gap(8.0)
.center_y()
.child(button(
p,
"reveal-capture-folder",
tr("Reveal"),
ButtonKind::Secondary,
|ui: &mut Ui, _| scrawl_request(ui, ScrawlRequest::RevealFolder),
))
.child(button(
p,
"choose-capture-folder",
tr("Change…"),
ButtonKind::Secondary,
|ui: &mut Ui, _| choose_folder(ui),
)),
),
];
if let Some(error) = &view.folder_error {
rows.push(note("capture-folder-error", error, p.danger()));
}
rows
}
/// The transcriber, showing Hex in screen recordings for demos, and the
/// replay Lab: settings most people never change. macOS only.
fn more_rows(ui: &Ui, p: Paint) -> Vec<El> {
#[cfg_attr(not(target_os = "macos"), allow(unused_mut))]
let mut rows = vec![transcriber_row(ui, p), recordings_row(ui, p)];
#[cfg(target_os = "macos")]
if crate::labs::Feature::AnnotateReplay.on_this_os()
&& crate::labs::Feature::AnnotateReplay.on(&ui.settings)
{
let mut control = row()
.key("capture-replay-shortcut-control")
.gap(4.0)
.center_y()
.child(keys_button(ui, p, ShortcutTarget::CaptureReplay));
if ui.settings.capture.replay_hotkey.is_some() {
control = control.child(super::controls::icon_button(
p,
"clear-capture-replay-shortcut",
&rune_theme_opencode::icons::TRASH,
tr("Clear shortcut"),
|ui: &mut Ui| {
ui.update(|settings| settings.capture.replay_hotkey = None);
},
));
}
use super::lists::{Tone, tone_tag};
rows.push(super::controls::icon_row_tagged(
p,
"capture-replay-shortcut",
&super::icons::REFRESH,
tr("Replay last 15 s"),
Some(tone_tag(p, "lab", tr("Lab"), Tone::Accent)),
Some(tr("Press to save; hold to narrate")),
control,
));
}
rows
}
/// Lets screen recordings, captures included, show Hex's overlays.
fn recordings_row(ui: &Ui, p: Paint) -> El {
icon_toggle_row(
p,
"capture-show-in-recordings",
&rune_kit::glyphs::EYE,
tr("Show Hex in screen recordings"),
tr("Include the capsule, ink, and HUD"),
ui.settings.capture.show_overlays_in_recordings,
|ui: &mut Ui| {
ui.update(|settings| {
settings.capture.show_overlays_in_recordings =
!settings.capture.show_overlays_in_recordings;
});
},
)
}
fn transcriber_row(ui: &Ui, p: Paint) -> El {
let transcriber = ui.settings.capture.transcriber;
let selected = CaptureTranscriber::ALL
.iter()
.position(|choice| *choice == transcriber)
.unwrap_or(0);
icon_row(
p,
"capture-transcriber",
&WAVE,
tr("Transcriber"),
Some(transcriber_description(transcriber)),
segmented(
p,
"capture-transcriber-choice",
&["Parakeet", "Apple"],
selected,
|ui: &mut Ui, index| {
let transcriber = CaptureTranscriber::ALL[index];
ui.update(|settings| settings.capture.transcriber = transcriber);
},
),
)
}
/// The command the copy row hands to people who manage skills themselves.
const SKILLS_COMMAND: &str = "npx skills add anomalyco/hex --skill hex-annotate";
/// The Annotate pane's Agents section (Labs › Install agent skill from
/// Annotate): each coding agent's skill folder and, on macOS, the hex link.
/// The folders are read when the pane opens and after an install, never
/// while drawing.
#[derive(Default)]
pub(super) struct Agents {
paths: Option<agent_install::Paths>,
statuses: Vec<AgentStatus>,
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
cli: Option<agent_install::cli::Status>,
/// One per `statuses` row: checked means Install writes there.
selected: Vec<bool>,
error: Option<String>,
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
cli_error: Option<String>,
}
impl Agents {
/// Reads every agent's folder and the hex link, and checks each found
/// agent that cannot read this Hex's skill.
pub(super) fn refresh(&mut self) {
self.paths = agent_install::Paths::current();
self.read();
}
fn read(&mut self) {
if let Some(paths) = &self.paths {
self.statuses = agent_install::status(paths);
}
#[cfg(target_os = "macos")]
{
self.cli = Some(agent_install::cli::status(
&agent_install::cli::Environment::current(),
));
}
self.selected = self
.statuses
.iter()
.map(|status| status.found && !status.linked && status.skill != SkillState::Current)
.collect();
}
/// Whether Install has anything to write.
fn any_selected(&self) -> bool {
self.selected.iter().any(|selected| *selected)
}
fn selected_for(&self, agent: Agent) -> bool {
self.statuses
.iter()
.position(|status| status.agent == agent)
.and_then(|index| self.selected.get(index).copied())
.unwrap_or(false)
}
fn toggle(&mut self, agent: Agent) {
let Some(index) = self
.statuses
.iter()
.position(|status| status.agent == agent && status.found && !status.linked)
else {
return;
};
if let Some(selected) = self.selected.get_mut(index) {
*selected = !*selected;
}
}
/// Writes the skill into every checked agent's folder, then reads them
/// again so each row says Installed.
fn install(&mut self) {
let Some(paths) = self.paths.clone() else {
return;
};
let mut failure = None;
for (index, status) in self.statuses.iter().enumerate() {
if !self.selected.get(index).copied().unwrap_or(false) || status.linked {
continue;
}
if let Err(error) = agent_install::install(&paths, status.agent) {
failure.get_or_insert(error);
}
}
self.read();
self.error = failure;
}
#[cfg(target_os = "macos")]
fn install_cli(&mut self) {
use agent_install::cli::LinkError;
self.cli_error = None;
match agent_install::cli::install(&agent_install::cli::Environment::current()) {
Ok(status) => self.cli = Some(status),
Err(LinkError::Translocation) => {
self.cli_error = Some(tr("Move Hex to Applications first").to_string());
}
Err(LinkError::Io(error)) => self.cli_error = Some(error),
}
}
/// A scene's rows: one ready to install, one outdated, one current, one
/// not found, and the link already installed.
pub(super) fn fixture() -> Self {
let home = std::path::PathBuf::from("/Users/you");
Self {
statuses: vec![
AgentStatus {
agent: Agent::ClaudeCode,
found: true,
skill: SkillState::Missing,
path: home.join(".claude/skills/hex-annotate/SKILL.md"),
linked: false,
},
AgentStatus {
agent: Agent::Codex,
found: true,
skill: SkillState::Outdated,
path: home.join(".agents/skills/hex-annotate/SKILL.md"),
linked: false,
},
AgentStatus {
agent: Agent::OpenCode,
found: true,
skill: SkillState::Current,
path: home.join(".config/opencode/skills/hex-annotate/SKILL.md"),
linked: false,
},
AgentStatus {
agent: Agent::Pi,
found: false,
skill: SkillState::Missing,
path: home.join(".pi/agent/skills/hex-annotate/SKILL.md"),
linked: false,
},
],
selected: vec![true, true, false, false],
cli: Some(agent_install::cli::Status {
state: agent_install::cli::State::Installed,
path: std::path::PathBuf::from("/opt/homebrew/bin/hex"),
off_path: false,
}),
..Self::default()
}
}
}
/// One agent's row: its name, whether it is found and has this Hex's skill,
/// and a switch that checks it for Install. A not-found agent, and a skill
/// someone else manages through a symlink, has no switch: it is read by
/// content and never written here.
fn agent_row(ui: &Ui, p: Paint, status: &AgentStatus) -> El {
// A symlinked skill or folder is someone else's to update; the row says
// so rather than offering a state Hex can't change.
let label = if status.linked {
tr("Managed elsewhere")
} else {
match (status.found, status.skill) {
(false, _) => tr("Not found"),
(true, SkillState::Missing) => tr("Not installed"),
(true, SkillState::Current) => tr("Installed"),
(true, SkillState::Outdated) => tr("Update available"),
}
};
let key = format!("agent-{}", status.agent.key());
if !status.found || status.linked {
return setting_row(p, &key, status.agent.name(), Some(label), row());
}
let checked = ui.agents.selected_for(status.agent);
let row = setting_row(
p,
&key,
status.agent.name(),
Some(label),
super::controls::row_switch(p, checked),
);
let agent = status.agent;
super::controls::pressable_row(row, checked)
.on_press(move |ui: &mut Ui| ui.agents.toggle(agent))
}
/// The command people who manage skills themselves run, with its copy.
fn agent_command_row(p: Paint) -> El {
let command = row()
.key("command")
.gap(8.0)
.center_y()
.child(
text(SKILLS_COMMAND)
.key("line")
.font(p.k.ty.mono)
.color(p.muted())
.a11y_hidden(),
)
.child(button(
p,
"agents-skill-copy",
tr("Copy"),
ButtonKind::Secondary,
|_, tx| tx.copy(SKILLS_COMMAND.to_owned()),
));
setting_row(
p,
"agents-skill-command",
tr("Install with the skills CLI"),
Some(tr("For skills you manage yourself")),
command,
)
}
/// The macOS Command-line tool row, with its Install, and where the link is
/// when it landed in `~/.local/bin`, which may not be on PATH.
#[cfg(target_os = "macos")]
fn cli_rows(ui: &Ui, p: Paint) -> Vec<El> {
use agent_install::cli::{State, Status};
let status: Option<&Status> = ui.agents.cli.as_ref();
let (label, installable) = match status {
Some(Status {
state: State::Installed,
path,
..
}) => (tr_fill("Installed at {}", &folder_label(path)), false),
Some(Status {
state: State::OwnedByAnother,
..
}) => (tr("Owned by another program").to_string(), false),
Some(_) | None => (tr("Not installed").to_string(), true),
};
let control = if installable {
button(
p,
"agents-cli-install",
tr("Install"),
ButtonKind::Secondary,
|ui: &mut Ui, _| ui.agents.install_cli(),
)
} else {
row()
};
let mut rows = vec![setting_row(
p,
"agents-cli",
tr("Command-line tool"),
Some(label.as_str()),
control,
)];
if let Some(Status {
path,
off_path: true,
..
}) = status
{
let folder = path.parent().unwrap_or(path);
rows.push(note(
"agents-cli-path",
&tr_fill("{} isn't on your PATH", &folder_label(folder)),
p.muted(),
));
}
if let Some(error) = &ui.agents.cli_error {
rows.push(note(
"agents-cli-error",
&tr_fill("Couldn't link the command-line tool: {}", error),
p.danger(),
));
}
rows
}
/// The Agents section: the intro, each agent's row, the copyable skills
/// command, and on macOS the Command-line tool.
fn agents_section(ui: &Ui, p: Paint) -> El {
let mut rows = vec![note(
"agents-intro",
tr("Let coding agents read your captures with the hex-annotate skill"),
p.muted(),
)];
for status in &ui.agents.statuses {
rows.push(agent_row(ui, p, status));
}
if let Some(error) = &ui.agents.error {
rows.push(note(
"agents-error",
&tr_fill("Couldn't install the skill: {}", error),
p.danger(),
));
}
rows.push(agent_command_row(p));
#[cfg(target_os = "macos")]
rows.extend(cli_rows(ui, p));
if !ui.settings.labs.annotate_agent_tools {
rows.push(note(
"agents-tools-off",
tr("The skill's zoom, inspect, and frames commands need Labs › Agent tools for captures on"),
p.muted(),
));
}
section_with(
p,
"agents",
tr("Agents"),
super::controls::button_enabled(
p,
"agents-install",
tr("Install"),
ButtonKind::Secondary,
ui.agents.any_selected(),
|ui: &mut Ui, _| ui.agents.install(),
),
rows,
)
}
/// What the Annotate pane shows, picked in its header.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(super) enum View {
#[default]
Annotations,
Settings,
}
/// Whether the Annotations/Captures tab is reachable beside Settings.
pub(super) fn has_list(ui: &Ui) -> bool {
ui.settings.capture.enabled || ui.settings.labs.screenshots
}
/// Polls the Annotations/Captures list only while its tab is visible.
pub(super) fn poll_list(ui: &mut Ui) -> bool {
let visible = ui.pane == Pane::Annotate
&& ui.captures.view == View::Annotations
&& has_list(ui)
&& !ui.labs_open;
#[cfg(not(target_os = "macos"))]
let chosen = folder::poll(ui);
#[cfg(target_os = "macos")]
let chosen = false;
ui.captures.poll(visible) | chosen
}
/// The header's `Annotations | Settings` switch (or `Captures | Settings`
/// while Labs › Screenshots is on), beside the title as in Meetings; hidden
/// while Annotate and Screenshots are both off.
pub(super) fn view_switch(ui: &Ui, p: Paint) -> Option<El> {
if !has_list(ui) {
return None;
}
let list_label = if ui.settings.labs.screenshots {
tr("Captures")
} else {
tr("Annotations")
};
let views = [list_label, tr("Settings")];
let selected = match ui.captures.view {
View::Annotations => 0,
View::Settings => 1,
};
Some(segmented_tx(
p,
"annotate-view",
&views,
selected,
|ui: &mut Ui, index: usize, tx: &mut Tx| {
let view = if index == 0 {
View::Annotations
} else {
View::Settings
};
if ui.captures.view != view {
tx.settle();
ui.flush_drafts();
ui.open = None;
ui.captures.view = view;
ui.captures.confirm_trash = None;
}
},
))
}
pub(super) fn pane(ui: &Ui, p: Paint, layer: &mut Vec<El>) -> El {
if has_list(ui) && ui.captures.view == View::Annotations {
return super::captures::pane(ui, p);
}
let view = ui.platform.scrawl.clone().unwrap_or_default();
let enabled = ui.settings.capture.enabled;
let mut content = page("content").child(card(p, "enable", top_rows(ui, p, &view)));
if !enabled {
return super::scroll_body(p.k, "scroll", content);
}
if view.scrawl_running {
content = content.child(scrawl_running(p, &view));
}
content = content.child(how_it_works(ui, p, &view)).child(section(
p,
"after",
tr("After an annotation"),
after_rows(ui, p, &view, layer),
));
if ui.settings.labs.annotate_agent_install {
content = content.child(agents_section(ui, p));
}
// Apple's speech recognizer, keeping Hex out of recordings, and replay
// are macOS's; Linux and Windows transcribe with Hex's model.
if cfg!(target_os = "macos") {
let link = advanced_toggle(ui, p, Pane::Annotate);
let open = advanced_open(ui, Pane::Annotate);
content = content.child(fold(p, "advanced", link, open.then(|| more_rows(ui, p))));
}
super::scroll_body(p.k, "scroll", content)
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(target_os = "macos")]
#[test]
fn captures_default_on_and_refuse_a_shortcut_in_use() {
let settings = AppSettings::default();
assert!(settings.capture.enabled);
let mut taken = AppSettings::default();
taken.capture.hotkey = taken.dictation_hotkey.clone();
let before = serde_json::to_value(&taken).unwrap();
assert!(
set_enabled(&mut taken, true)
.unwrap_err()
.contains("already in use")
);
assert_eq!(serde_json::to_value(&taken).unwrap(), before);
// Turning captures off always works.
set_enabled(&mut taken, false).unwrap();
}
/// A record shortcut saved while Annotate was off holds its key: giving
/// Annotate the same chord and turning it on is refused, since the
/// Screenshots record shortcut would otherwise go dead and Annotate's
/// whole-screen recording would take the chord.
#[cfg(target_os = "macos")]
#[test]
fn annotate_will_not_take_a_saved_record_shortcut() {
let record = crate::app_settings::HotkeyBinding {
modifiers: crate::app_settings::HotkeyModifiers {
command: Some(crate::app_settings::ModifierSide::Either),
shift: Some(crate::app_settings::ModifierSide::Either),
..Default::default()
},
key: Some(crate::app_settings::HotkeyKey::named("r")),
};
let mut settings = AppSettings::default();
settings.capture.enabled = false;
settings.labs.screenshots = true;
settings.screenshots.record_hotkey = Some(record.clone());
settings.capture.hotkey = record;
let before = serde_json::to_value(&settings).unwrap();
assert!(
set_enabled(&mut settings, true)
.unwrap_err()
.contains("already in use")
);
assert_eq!(serde_json::to_value(&settings).unwrap(), before);
// With the Lab off, every Mac's saved record shortcut doesn't hold
// the key, and turning the Lab on lets the record shortcut go.