-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathmain.rs
More file actions
746 lines (698 loc) · 30.1 KB
/
Copy pathmain.rs
File metadata and controls
746 lines (698 loc) · 30.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
//! Cap desktop, rewritten in gpui.
//!
//! Milestone 1 is the main recording window (compact + expanded) with real
//! device enumeration. No tauri, no webview: the whole UI is gpui.
#![cfg_attr(all(windows, not(debug_assertions)), windows_subsystem = "windows")]
mod app_windows;
mod assets;
mod auth;
#[cfg(target_os = "macos")]
mod camera_bench;
#[cfg(target_os = "macos")]
mod camera_blur;
#[cfg(any(not(target_os = "macos"), test))]
mod camera_blur_portable;
mod camera_window;
mod controls_window;
mod deeplink;
mod dev_restore;
mod devices;
mod diagnostics;
mod editor_audio;
mod editor_canvas;
mod editor_clips;
mod editor_color;
mod editor_crop;
mod editor_edits;
mod editor_export;
#[cfg(target_os = "linux")]
mod editor_modal;
mod editor_panels;
mod editor_sidebar;
mod editor_tabs;
mod editor_timeline;
mod editor_window;
mod feeds;
mod hotkeys;
mod import;
mod library;
mod main_window;
mod menus;
mod mode_select_window;
mod onboarding_audio;
mod onboarding_window;
mod permissions;
mod permissions_ui;
#[cfg(test)]
mod picker_benchmark;
#[cfg(debug_assertions)]
mod picker_ui_benchmark;
mod platform;
mod presets;
mod recording;
mod screenshot;
mod screenshot_annotations;
mod screenshot_crop;
mod screenshot_editor;
mod screenshot_export;
mod session;
mod settings_pages;
mod settings_window;
mod single_instance;
mod store;
mod target_overlay;
mod target_thumbnails;
mod teleprompter_window;
mod theme;
mod transcription;
mod tray;
mod ui;
mod updates;
mod upload;
use gpui::{App, AppContext as _, Bounds, WindowBounds, WindowOptions, px, size};
use crate::{assets::Assets, main_window::MainWindow, session::RecordingSession};
const MAIN_WINDOW_WIDTH: f32 = 330.;
const MAIN_WINDOW_HEIGHT: f32 = 432.;
/// The corner radius the native material is clipped to. `radius = 16` for
/// material `"panel"` on both visual systems in
/// `apps/desktop/src/utils/macos-window-material.ts`, and the same 16 the
/// shell paints with (`rounded-[16px]`).
#[cfg(target_os = "macos")]
const MAIN_WINDOW_MATERIAL_RADIUS: f64 = 16.;
fn parse_auto_record(spec: &str) -> Option<(main_window::Mode, u64)> {
if spec == "screenshot" {
// A capture has no duration; the harness arms the target, captures,
// and the stop half of the driver is a no-op on an idle session.
return Some((main_window::Mode::Screenshot, 0));
}
let (mode, secs) = spec.split_once(':')?;
let mode = match mode {
"studio" => main_window::Mode::Studio,
"instant" => main_window::Mode::Instant,
_ => return None,
};
Some((mode, secs.parse().ok()?))
}
/// `CAP_GPUI_AUTO_EDITOR`: a `.cap` path, or `1` for the newest studio
/// recording the library scan finds. The library is the same one Recents
/// reads, so `=1` opens exactly the card that would be first in the carousel
/// (skipping instant recordings and screenshots, which the editor rejects).
fn resolve_auto_editor(target: &str) -> Option<std::path::PathBuf> {
if target != "1" {
let path = std::path::PathBuf::from(target);
return path.is_dir().then_some(path);
}
library::recent_media()
.into_iter()
.find(|item| item.kind == library::MediaKind::Studio)
.map(|item| item.bundle)
}
/// `CAP_GPUI_AUTO_SCREENSHOT_EDITOR`: a screenshot `.cap` path, or `1` for
/// the newest screenshot in the library -- [`resolve_auto_editor`]'s shape
/// for the screenshot editor.
fn resolve_auto_screenshot_editor(target: &str) -> Option<std::path::PathBuf> {
if target != "1" {
let path = std::path::PathBuf::from(target);
return path.is_dir().then_some(path);
}
library::recent_media()
.into_iter()
.find(|item| item.kind == library::MediaKind::Screenshot)
.map(|item| item.bundle)
}
/// stdout, plus a rolling daily file the Feedback page can upload.
///
/// Mirrors `src-tauri/src/main.rs`: `tracing_appender::rolling::daily` into
/// `~/Library/Logs/so.cap.desktop` on macOS (the directory the Tauri app
/// already writes into) under a **different** filename prefix, so the two apps
/// never interleave lines into one file. Returns the non-blocking writer's
/// guard, which has to outlive `main` or the last lines never reach the disk.
fn init_logging() -> Option<tracing_appender::non_blocking::WorkerGuard> {
use tracing_subscriber::{Layer as _, layer::SubscriberExt as _, util::SubscriberInitExt as _};
// The binary is `cap-gpui`, so the crate these spans are recorded under is
// `cap_gpui` -- not `cap_desktop_gpui`, which is the package name and
// matches nothing.
let filter = || {
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| "cap_gpui=info".into())
};
let file = create_log_appender(&diagnostics::logs_dir(), diagnostics::LOG_FILE_PREFIX).map(
|appender| {
let (writer, guard) = tracing_appender::non_blocking(
cap_utils::diagnostic_writer::DiagnosticWriter::new(
appender,
&diagnostics::logs_dir(),
diagnostics::LOG_FILE_PREFIX,
),
);
let queue_errors = writer.error_counter();
cap_utils::operation_diagnostics::install_queue_loss_counter(move || {
queue_errors.dropped_lines()
});
let diagnostic_writer = writer.clone();
cap_utils::operation_diagnostics::install_sink(
cap_utils::operation_diagnostics::AppInfo {
flavor: "gpui",
version: env!("CARGO_PKG_VERSION"),
source_revision: option_env!("CAP_BUILD_REVISION"),
debug_build: cfg!(debug_assertions),
source_dirty: option_env!("CAP_BUILD_DIRTY").map(|value| value == "true"),
},
move |bytes| {
use std::io::Write;
let _ = diagnostic_writer.clone().write_all(bytes);
},
);
(
tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_writer(writer)
.with_filter(filter()),
guard,
)
},
);
let (file_layer, guard) = match file {
Some((layer, guard)) => (Some(layer), Some(guard)),
None => (None, None),
};
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_filter(filter()))
.with(file_layer)
.init();
guard
}
fn create_log_appender(
directory: &std::path::Path,
prefix: &str,
) -> Option<tracing_appender::rolling::RollingFileAppender> {
use std::io::Write;
match tracing_appender::rolling::RollingFileAppender::builder()
.rotation(tracing_appender::rolling::Rotation::DAILY)
.filename_prefix(prefix)
.build(directory)
{
Ok(appender) => Some(appender),
Err(error) => {
let _ = writeln!(
std::io::stderr(),
"Could not open {prefix} in {}: {error}; console logging remains enabled",
directory.display()
);
None
}
}
}
fn main() {
#[cfg(target_os = "linux")]
if let Some(threads) = cap_utils::linux_runtime::llvmpipe_thread_count() {
// Mesa counts host CPUs inside containers; apply the process limit before logging starts threads.
unsafe {
std::env::set_var("LP_NUM_THREADS", threads.to_string());
}
}
#[cfg(target_os = "linux")]
if let Some(config) = cap_utils::linux_package::appimage_alsa_config_path() {
// Logging starts a worker thread, so configure the process environment first.
unsafe {
std::env::set_var("ALSA_CONFIG_PATH", config);
}
}
let _log_guard = init_logging();
single_instance::acquire();
store::mark_handoff_session();
platform::install_url_scheme_handler();
for argument in std::env::args().skip(1) {
if argument.contains("token") || argument.contains("api_key") {
crate::auth::submit_deep_link(&argument);
}
}
let app = gpui_platform::application().with_assets(Assets);
// `RunEvent::Reopen`: the dock icon clicked while the app runs. Must be
// registered on the builder -- gpui exposes it nowhere else -- with the
// handler guarding against firing before the window registry exists.
app.on_reopen(crate::app_windows::handle_dock_reopen);
#[cfg(target_os = "macos")]
app.on_open_urls(|urls| {
for url in urls {
crate::deeplink::submit_deep_link(&url);
}
});
app.run(|cx: &mut App| {
gpui_tokio::init(cx);
gpui_tokio::Tokio::spawn(cx, cap_utils::operation_diagnostics::run_checkpoints()).detach();
// The dock icon: an unbundled dev binary shows the generic terminal
// document without it. The bytes are the shipping app's icon.png.
platform::set_dock_icon(include_bytes!("../assets/dock-icon.png"));
if let Err(error) = Assets.load_fonts(cx) {
tracing::error!("failed to load embedded fonts: {error:#}");
}
// 330x395 is what `CapWindowId::Main::min_size` uses in the Tauri app,
// and the window is fixed at that size there too.
// The text-input key map. Every editing chord in the app -- Backspace,
// the arrows, Cmd-A/C/X/V/Z -- is an action scoped to the `TextInput`
// key context, which is what stops a focused field's Backspace from
// reaching the editor's delete-the-selection handler: a matched
// binding consumes the keystroke before any `on_key_down` listener on
// the path runs (`gpui/src/window.rs:5280-5296`).
ui::bind_text_input_keys(cx);
crate::theme::init(cx);
let session = RecordingSession::init(cx);
crate::feeds::Feeds::init(cx);
crate::target_overlay::TargetSelect::init(cx);
let dev_restore = dev_restore::load();
let main_size = size(px(MAIN_WINDOW_WIDTH), px(MAIN_WINDOW_HEIGHT));
let bounds = dev_restore
.as_ref()
.and_then(|restore| restore.main_window_bounds(main_size, cx))
.unwrap_or_else(|| Bounds::centered(None, main_size, cx));
let window_handle = cx
.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(bounds)),
// `None`, not a transparent titlebar with repositioned traffic
// lights: the Tauri main window returns `None` from
// `traffic_lights_position`, which routes it to
// `.decorations(false)`. There is no titlebar and there are no
// traffic lights -- the whole shell is custom-drawn. In gpui a
// `None` titlebar drops NSClosable/NSMiniaturizable/NSResizable
// from the style mask, which is the equivalent.
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
titlebar: None,
#[cfg(target_os = "windows")]
titlebar: Some(gpui::TitlebarOptions {
title: Some("Cap".into()),
appears_transparent: true,
..Default::default()
}),
#[cfg(target_os = "linux")]
titlebar: Some(gpui::TitlebarOptions {
title: Some("Cap".into()),
..Default::default()
}),
#[cfg(target_os = "linux")]
app_id: Some("Cap".into()),
#[cfg(target_os = "linux")]
window_min_size: Some(size(px(MAIN_WINDOW_WIDTH), px(MAIN_WINDOW_HEIGHT))),
#[cfg(target_os = "linux")]
window_decorations: Some(gpui::WindowDecorations::Client),
#[cfg(target_os = "macos")]
kind: gpui::WindowKind::Floating,
#[cfg(not(target_os = "macos"))]
kind: gpui::WindowKind::Normal,
// The header is dragged by the app via `start_window_move`
// rather than by AppKit, so mark the content view as app-owned
// titlebar content.
app_owns_titlebar_drag: true,
window_background: if cfg!(target_os = "windows") {
gpui::WindowBackgroundAppearance::Opaque
} else {
gpui::WindowBackgroundAppearance::Transparent
},
is_resizable: false,
is_minimizable: false,
..Default::default()
},
{
let session = session.clone();
move |window, cx| cx.new(|cx| MainWindow::new(session, window, cx))
},
)
.inspect_err(|error| tracing::error!("failed to open the main window: {error:#}"));
let Ok(window_handle) = window_handle else {
cx.quit();
return;
};
cx.on_app_quit(|_| async {
crate::store::clear_handoff_marker();
})
.detach();
app_windows::init(window_handle, session, cx);
#[cfg(target_os = "macos")]
match platform::install_native_quit_handler() {
Ok(requests) => {
cx.spawn(async move |cx| {
while requests.recv_async().await.is_ok() {
cx.update(menus::quit);
}
})
.detach();
}
Err(error) => {
tracing::error!(%error, "Could not install safe native Quit handling");
menus::quit(cx);
return;
}
}
#[cfg(target_os = "linux")]
single_instance::init_linux_reopen(cx);
updates::schedule_startup_check(cx);
upload::queue::init(cx);
// The app menu (and with it ⌘W/⌘M/⌘Q) and the status-bar item. Both
// reach into the window registry, so they come after it -- and the menu
// before the tray, because the tray's Quit item is the menu's Quit.
if !std::env::var("CAP_GPUI_NO_MENUS").is_ok_and(|v| v == "1") {
menus::init(cx);
}
if !std::env::var("CAP_GPUI_NO_TRAY").is_ok_and(|v| v == "1") {
tray::init(cx);
}
// The store's global shortcuts, after the registry they dispatch into.
hotkeys::init(cx);
// The gate's TCC queries (screen recording preflight, AXIsProcessTrusted)
// are ~30ms of synchronous XPC on this Mac -- the whole startup delta
// between the 08-17 and 08-18 builds. Both are thread-safe reads, so
// the gate runs off-main and onboarding opens a beat after the main
// window instead of holding it back.
cx.spawn(async move |cx| {
let show_onboarding = cx
.background_executor()
.spawn(async { crate::store::should_show_onboarding() })
.await;
if show_onboarding {
cx.update(app_windows::open_onboarding);
}
})
.detach();
// `CAP_GPUI_AUTO_TRAY` / `CAP_GPUI_TRAY_DUMP`: the tray's harness path.
tray::drive_from_env(cx);
// `CAP_GPUI_AUTO_CLOSE=settings|main:<secs>`: run the ⌘W body against
// that window after a delay -- exercises the exact deferred close path
// without synthetic keypresses (which race whatever the user has
// focused).
if let Ok(spec) = std::env::var("CAP_GPUI_AUTO_CLOSE")
&& let Some((which, secs)) = spec.split_once(':')
&& let Ok(secs) = secs.parse::<u64>()
{
let which = which.to_string();
cx.spawn(async move |cx| {
cx.background_executor()
.timer(std::time::Duration::from_secs(secs))
.await;
cx.update(|cx| {
let windows = cx.global::<app_windows::AppWindows>();
let handle: Option<gpui::AnyWindowHandle> = match which.as_str() {
"settings" => windows.settings.map(Into::into),
"main" => Some(windows.main.into()),
_ => None,
};
if let Some(handle) = handle {
menus::close_window_by_handle(handle, cx);
}
});
})
.detach();
}
// The Tauri app syncs the dock the moment a dock-activating window is
// shown; the main window is up by here.
menus::sync_dock_visibility(cx);
// `CAP_GPUI_DEV_RESTORE=<state file>`: `dev.sh`'s relaunch loop.
// Reopens the previous process's windows in place and keeps the
// state file current for the next swap.
if let Some(dev_restore) = dev_restore {
dev_restore.init(cx);
}
// Enumeration is started here rather than in `MainWindow::new`, which
// runs before the window is fully built -- see `start_enumeration`.
// The panel behavior (`MAIN_PANEL_LEVEL`, all Spaces -- what the Tauri
// app does via tauri_nspanel) is applied here for the same reason: the
// NSWindow does not exist yet inside the builder closure.
let native_main = window_handle
.update(cx, |view, window, cx| {
platform::apply_panel_behavior(
window,
platform::PanelBehavior {
#[cfg(target_os = "macos")]
level: objc2_app_kit::NSFloatingWindowLevel,
#[cfg(not(target_os = "macos"))]
level: platform::MAIN_WINDOW_LEVEL,
join_all_spaces: true,
shadow: true,
},
);
#[cfg(target_os = "linux")]
if let Err(error) = platform::remove_x11_window_decorations(window) {
tracing::warn!(%error, "could not remove X11 main window decorations");
}
tracing::info!(
number = platform::window_number(window),
"main window opened"
);
view.start_enumeration(window, cx);
view.start_recovery_check(window, cx);
view.auto_expand(window, cx);
view.auto_open_recent(window, cx);
// The AppKit work below must not run inside this update:
// inserting a subview and mutating the content view's layer
// synchronously re-enters gpui's own window callbacks, which
// re-borrow the App. Grab the retained NSWindow here, act on
// it from a task (the `place_overlay_panel` rule).
platform::native_window(window)
})
.expect("failed to start device enumeration");
// The native window material: `NSGlassEffectView` on macOS 26+,
// `NSVisualEffectView` vibrancy below that -- what
// `applyMacOSWindowMaterial("panel")` does in the Tauri app. Nothing
// paints it; the shell paints a translucent tint *over* it, so the
// window has to be told which one landed.
#[cfg(target_os = "macos")]
cx.spawn(async move |cx| {
let Some(native) = native_main else {
tracing::error!("no NSWindow behind the main window; material not installed");
return;
};
let kind = platform::install_window_material(&native, MAIN_WINDOW_MATERIAL_RADIUS);
match kind {
Some(kind) => tracing::info!(?kind, "installed main window material"),
None => tracing::info!("no native window material available"),
}
cx.update(|cx| {
cx.set_global(platform::WindowMaterial(kind));
// The palette is resolved in `render`, which has already run
// by now -- nudge it so the tint replaces the opaque shell.
window_handle.update(cx, |_, _, cx| cx.notify()).ok();
});
})
.detach();
#[cfg(not(target_os = "macos"))]
let _ = native_main;
// `CAP_GPUI_DEBUG_LIGHTS=1`: poll the main window's style mask and
// standard-button set, logging on change -- pins down *when* AppKit
// materializes titlebar buttons on the buttonless main window.
if std::env::var("CAP_GPUI_DEBUG_LIGHTS").is_ok_and(|v| v == "1") {
cx.spawn(async move |cx| {
let mut last = None::<String>;
loop {
cx.background_executor()
.timer(std::time::Duration::from_millis(250))
.await;
let state = cx.update(|cx| {
window_handle
.update(cx, |_, window, _| platform::debug_titlebar_state(window))
.ok()
.flatten()
});
if state != last {
tracing::info!(?state, "titlebar state changed");
last = state;
}
}
})
.detach();
}
// `CAP_GPUI_AUTO_SETTINGS=1` (or a page slug, e.g. `hotkeys`): open
// the settings window the way the header gear does. Same reason as
// every other `CAP_GPUI_AUTO_*` hook -- unprivileged synthetic clicks
// are dropped, so the screenshot harness needs a way in.
if let Ok(page) = std::env::var("CAP_GPUI_AUTO_SETTINGS")
&& !page.is_empty()
{
let page =
settings_window::Page::from_slug(&page).unwrap_or(settings_window::Page::General);
app_windows::open_settings(page, cx);
}
permissions_ui::auto_open_from_env(cx); // `CAP_GPUI_AUTO_PERMISSIONS=1`: the permissions surface.
// `CAP_GPUI_AUTO_MODE_SELECT=1`: open the 580x340 mode picker the way
// the mode dot does, main window hidden included. A mode name instead
// of `1` also clicks that card.
if let Ok(mode) = std::env::var("CAP_GPUI_AUTO_MODE_SELECT")
&& !mode.is_empty()
{
app_windows::open_mode_select(cx);
if let Some(mode) = match mode.as_str() {
"instant" => Some(main_window::Mode::Instant),
"studio" => Some(main_window::Mode::Studio),
"screenshot" => Some(main_window::Mode::Screenshot),
_ => None,
} {
app_windows::choose_mode_in_mode_select(mode, cx);
}
}
// `CAP_GPUI_AUTO_CAMERA_BENCH=1`: open the camera bubble and pump
// synthetic frames through the production delivery path, reporting
// gpui draw timings for that window. See `camera_bench`.
#[cfg(target_os = "macos")]
if std::env::var("CAP_GPUI_AUTO_CAMERA_BENCH").is_ok_and(|v| v == "1") {
camera_bench::run(cx);
}
// `CAP_GPUI_AUTO_TELEPROMPTER=1`: open the teleprompter the way the
// header's scan-text button does. Same reason as the other hooks:
// unprivileged synthetic clicks are dropped. Any other value is typed
// into the script through the same path a keystroke takes, which is how
// the persistence round trip is checked.
if let Ok(script) = std::env::var("CAP_GPUI_AUTO_TELEPROMPTER")
&& !script.is_empty()
{
app_windows::open_teleprompter(cx);
if script != "1" {
app_windows::type_into_teleprompter(script, cx);
}
// `CAP_GPUI_AUTO_PLAY=1`: press play once the window has painted --
// the scrollable height, which is what decides whether playback can
// start at all, does not exist before the first frame.
if std::env::var("CAP_GPUI_AUTO_PLAY").is_ok_and(|value| value == "1") {
cx.spawn(async move |cx| {
cx.background_executor()
.timer(std::time::Duration::from_millis(1200))
.await;
cx.update(app_windows::play_teleprompter);
})
.detach();
}
}
// `CAP_GPUI_AUTO_EDITOR=<path-to-.cap>`: open the editor on that
// bundle the way a Recents card does. `=1` picks the newest studio
// recording in the library. Same reason as the other `CAP_GPUI_AUTO_*`
// hooks -- unprivileged synthetic clicks are dropped, so the
// screenshot harness needs a way in.
if let Ok(target) = std::env::var("CAP_GPUI_AUTO_EDITOR")
&& !target.is_empty()
{
match resolve_auto_editor(&target) {
Some(path) => app_windows::open_editor(path, cx),
None => tracing::error!(target, "CAP_GPUI_AUTO_EDITOR: no studio .cap to open"),
}
}
// `CAP_GPUI_AUTO_HOTKEY=<store key>`: run a hotkey action's dispatch
// arm (e.g. `screenshotDisplay`) as if the OS shortcut fired.
if let Ok(action) = std::env::var("CAP_GPUI_AUTO_HOTKEY")
&& !action.is_empty()
{
let action = action.clone();
cx.spawn(async move |cx| {
// Give enumeration and the first paint the same beat the
// other harness hooks give them.
cx.background_executor()
.timer(std::time::Duration::from_secs(2))
.await;
cx.update(|cx| hotkeys::dispatch_for_harness(&action, cx));
})
.detach();
}
// `CAP_GPUI_AUTO_SCREENSHOT_EDITOR=<path-to-.cap>` (or `1` for the
// newest screenshot): open the screenshot editor the way a Recents
// card does.
if let Ok(target) = std::env::var("CAP_GPUI_AUTO_SCREENSHOT_EDITOR")
&& !target.is_empty()
{
match resolve_auto_screenshot_editor(&target) {
Some(path) => app_windows::open_screenshot_editor(path, cx),
None => tracing::error!(
target,
"CAP_GPUI_AUTO_SCREENSHOT_EDITOR: no screenshot .cap to open"
),
}
}
// `CAP_GPUI_AUTO_RECORD=studio:5` / `instant:4` / `screenshot`: arm
// the primary display and record for N seconds (or capture once). The
// end-to-end check drives the recorder this way because unprivileged
// synthetic clicks are dropped.
#[cfg(debug_assertions)]
picker_ui_benchmark::run(window_handle, cx);
if let Ok(auto) = std::env::var("CAP_GPUI_AUTO_RECORD")
&& let Some((mode, secs)) = parse_auto_record(&auto)
{
window_handle
.update(cx, |view, window, cx| {
view.auto_record(mode, secs, window, cx)
})
.expect("failed to arm auto-record");
} else if let Some(kind) = main_window::auto_overlay_kind() {
// `CAP_GPUI_AUTO_OVERLAY` without a recording: put the overlays up
// and leave them there, which is how they get screenshotted.
window_handle
.update(cx, |view, window, cx| {
view.auto_open_overlay(kind, window, cx)
})
.expect("failed to arm the target overlay");
}
// Under the dev loop the app relaunches on every save; activating
// would yank focus from the code editor each time. The restored
// windows come forward with `orderFrontRegardless` instead, and the
// main window floats at panel level 100 regardless of activation.
if !dev_restore::enabled() {
cx.activate(true);
}
});
}
#[cfg(test)]
mod logging_tests {
use super::create_log_appender;
use std::{io::Write, path::PathBuf};
struct LogDirectory(PathBuf);
impl LogDirectory {
fn new() -> Self {
let nonce = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let directory = std::env::temp_dir()
.join(format!("cap-gpui-logging-{}-{nonce}", std::process::id()));
std::fs::create_dir_all(&directory).unwrap();
Self(directory)
}
}
impl Drop for LogDirectory {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[test]
fn healthy_log_destination_preserves_existing_records() {
let directory = LogDirectory::new();
let destination = directory.0.join("nested");
for record in ["first\n", "second\n"] {
let mut appender = create_log_appender(&destination, "cap.log").unwrap();
appender.write_all(record.as_bytes()).unwrap();
appender.flush().unwrap();
}
let records: String = std::fs::read_dir(destination)
.unwrap()
.map(|entry| std::fs::read_to_string(entry.unwrap().path()).unwrap())
.collect();
assert!(records.contains("first\n"));
assert!(records.contains("second\n"));
}
#[test]
fn unavailable_log_directory_disables_only_file_logging() {
let directory = LogDirectory::new();
let destination = directory.0.join("blocked");
std::fs::write(&destination, "existing file").unwrap();
assert!(create_log_appender(&destination, "cap.log").is_none());
assert_eq!(
std::fs::read_to_string(destination).unwrap(),
"existing file"
);
}
#[test]
fn unavailable_daily_log_file_disables_only_file_logging() {
let directory = LogDirectory::new();
let today = chrono::Utc::now().date_naive();
for days in [-1, 0, 1] {
let date = today + chrono::Duration::days(days);
std::fs::create_dir(directory.0.join(format!("cap.log.{date}"))).unwrap();
}
assert!(create_log_appender(&directory.0, "cap.log").is_none());
assert!(create_log_appender(&directory.0, "other.log").is_some());
}
}