xbp 10.38.0

XBP is a zero-config build pack that can also interact with proxies, kafka, sockets, synthetic monitors.
Documentation
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
//! System tray UI for worktree-watch (Windows native tray + Linux StatusNotifier / GNOME).
//!
//! Uses the same `tray-icon` + `muda` stack as `xbp mcp --tray` so both desktop
//! environments share one implementation path:
//! - Windows: NotifyIcon
//! - Linux/GNOME: StatusNotifierItem (AppIndicator extension on classic GNOME Shell)

use crate::commands::worktree_watch::{
    collect_worktree_watch_tray_snapshot, run_worktree_watch_start, run_worktree_watch_stop,
    WorktreeWatchStartOptions, WorktreeWatchStopOptions, WorktreeWatchTargetOptions,
    WorktreeWatchTraySnapshot,
};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

const TRAY_ICON_BYTES: &[u8] =
    include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/xylex-app-icon.png"));
const STATUS_POLL_MS: u64 = 2_500;
const MENU_POLL_MS: u64 = 120;

#[derive(Debug, Clone)]
pub struct WorktreeWatchTrayOptions {
    pub target: WorktreeWatchTargetOptions,
    pub sync_interval_seconds: u64,
}

/// Run the blocking worktree-watch tray event loop until Quit is chosen.
pub fn run_worktree_watch_tray(options: WorktreeWatchTrayOptions) -> Result<(), String> {
    run_tray_loop(options)
}

fn run_tray_loop(options: WorktreeWatchTrayOptions) -> Result<(), String> {
    use muda::{CheckMenuItem, Menu, MenuEvent, MenuItem, PredefinedMenuItem, Submenu};
    use tray_icon::{Icon, TrayIconBuilder};

    let icon = Icon::from_rgba(load_rgba_icon()?, 320, 320)
        .map_err(|error| format!("Failed to create tray icon: {error}"))?;

    let target = Arc::new(options.target.clone());
    let sync_interval_seconds = options.sync_interval_seconds;
    let quit_flag = Arc::new(AtomicBool::new(false));

    let start_item = MenuItem::new("Start watching", true, None);
    let stop_item = MenuItem::new("Stop watching", true, None);
    let refresh_item = MenuItem::new("Refresh status", true, None);
    let stats_item = MenuItem::new("Show stats (console)", true, None);
    let status_item = MenuItem::new("Status: loading…", false, None);
    let target_item = MenuItem::new(&format!("Target: {}", describe_target(&target)), false, None);
    let quit_item = MenuItem::new("Quit tray", true, None);

    // Auto-refresh checkbox — keeps tooltip/status updated while open.
    let auto_refresh = CheckMenuItem::new("Auto-refresh status", true, true, None);

    let repos_submenu = Submenu::new("Repositories", true);

    let menu = Menu::new();
    menu.append(&status_item)
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&target_item)
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&PredefinedMenuItem::separator())
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&start_item)
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&stop_item)
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&PredefinedMenuItem::separator())
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&refresh_item)
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&auto_refresh)
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&stats_item)
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&repos_submenu)
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&PredefinedMenuItem::separator())
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;
    menu.append(&quit_item)
        .map_err(|error| format!("Failed to build tray menu: {error}"))?;

    let tray = TrayIconBuilder::new()
        .with_tooltip("XBP worktree-watch")
        .with_icon(icon)
        .with_menu(Box::new(menu))
        .build()
        .map_err(|error| format!("Failed to create tray icon: {error}"))?;

    println!(
        "XBP worktree-watch tray ready ({}) — right-click the tray icon for Start/Stop/Stats.",
        platform_tray_hint()
    );
    println!("Target: {}", describe_target(&target));
    if cfg!(target_os = "linux") {
        println!(
            "GNOME Shell note: StatusNotifier/AppIndicator support is required \
(e.g. AppIndicator extension on classic GNOME)."
        );
    }

    let last_snapshot: Arc<Mutex<Option<WorktreeWatchTraySnapshot>>> = Arc::new(Mutex::new(None));

    // Initial status.
    if let Ok(snapshot) = collect_worktree_watch_tray_snapshot(&target) {
        apply_snapshot_to_tray(
            &tray,
            &status_item,
            &start_item,
            &stop_item,
            &repos_submenu,
            &snapshot,
        );
        if let Ok(mut guard) = last_snapshot.lock() {
            *guard = Some(snapshot);
        }
    }

    let menu_channel = MenuEvent::receiver();
    let mut last_poll = std::time::Instant::now()
        .checked_sub(Duration::from_millis(STATUS_POLL_MS))
        .unwrap_or_else(std::time::Instant::now);

    while !quit_flag.load(Ordering::SeqCst) {
        while let Ok(event) = menu_channel.try_recv() {
            if event.id == quit_item.id() {
                quit_flag.store(true, Ordering::SeqCst);
                break;
            }
            if event.id == refresh_item.id() || event.id == auto_refresh.id() {
                // fall through to poll below
                last_poll = std::time::Instant::now()
                    .checked_sub(Duration::from_millis(STATUS_POLL_MS))
                    .unwrap_or_else(std::time::Instant::now);
            }
            if event.id == start_item.id() {
                let target = target.clone();
                match start_watchers((*target).clone(), sync_interval_seconds) {
                    Ok(message) => {
                        println!("{message}");
                        last_poll = std::time::Instant::now()
                            .checked_sub(Duration::from_millis(STATUS_POLL_MS))
                            .unwrap_or_else(std::time::Instant::now);
                    }
                    Err(error) => eprintln!("Start failed: {error}"),
                }
            }
            if event.id == stop_item.id() {
                let target = target.clone();
                match stop_watchers((*target).clone()) {
                    Ok(message) => {
                        println!("{message}");
                        last_poll = std::time::Instant::now()
                            .checked_sub(Duration::from_millis(STATUS_POLL_MS))
                            .unwrap_or_else(std::time::Instant::now);
                    }
                    Err(error) => eprintln!("Stop failed: {error}"),
                }
            }
            if event.id == stats_item.id() {
                match collect_worktree_watch_tray_snapshot(&target) {
                    Ok(snapshot) => print_stats_report(&snapshot),
                    Err(error) => eprintln!("Stats failed: {error}"),
                }
            }
        }

        let should_poll = auto_refresh.is_checked()
            && last_poll.elapsed() >= Duration::from_millis(STATUS_POLL_MS);
        if should_poll {
            last_poll = std::time::Instant::now();
            match collect_worktree_watch_tray_snapshot(&target) {
                Ok(snapshot) => {
                    apply_snapshot_to_tray(
                        &tray,
                        &status_item,
                        &start_item,
                        &stop_item,
                        &repos_submenu,
                        &snapshot,
                    );
                    if let Ok(mut guard) = last_snapshot.lock() {
                        *guard = Some(snapshot);
                    }
                }
                Err(error) => {
                    let _ = tray.set_tooltip(Some(format!("XBP watch error: {error}")));
                    let _ = status_item.set_text(format!("Status: error ({error})"));
                }
            }
        }

        thread::sleep(Duration::from_millis(MENU_POLL_MS));
    }

    println!("Worktree-watch tray closed.");
    Ok(())
}

fn start_watchers(
    target: WorktreeWatchTargetOptions,
    sync_interval_seconds: u64,
) -> Result<String, String> {
    // Multi-repo without parent: start each repo detached.
    if !target.repos.is_empty() && target.parent.is_none() {
        let mut started = 0usize;
        for repo in &target.repos {
            let rt = tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .map_err(|error| format!("Failed to start async runtime: {error}"))?;
            rt.block_on(run_worktree_watch_start(WorktreeWatchStartOptions {
                target: WorktreeWatchTargetOptions {
                    repo: Some(repo.clone()),
                    parent: None,
                    repos: Vec::new(),
                },
                detach: true,
                sync_interval_seconds,
                once: false,
            }))?;
            started += 1;
        }
        return Ok(format!("Started {started} detached worktree watcher(s)."));
    }

    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .map_err(|error| format!("Failed to start async runtime: {error}"))?;
    rt.block_on(run_worktree_watch_start(WorktreeWatchStartOptions {
        target,
        detach: true,
        sync_interval_seconds,
        once: false,
    }))?;
    Ok("Started detached worktree watcher.".to_string())
}

fn stop_watchers(target: WorktreeWatchTargetOptions) -> Result<String, String> {
    if !target.repos.is_empty() && target.parent.is_none() {
        let mut stopped_msgs = Vec::new();
        for repo in &target.repos {
            run_worktree_watch_stop(WorktreeWatchStopOptions {
                target: WorktreeWatchTargetOptions {
                    repo: Some(repo.clone()),
                    parent: None,
                    repos: Vec::new(),
                },
                force: false,
            })?;
            stopped_msgs.push(repo.display().to_string());
        }
        return Ok(format!(
            "Stop requested for {} repo(s).",
            stopped_msgs.len()
        ));
    }

    run_worktree_watch_stop(WorktreeWatchStopOptions {
        target,
        force: false,
    })?;
    Ok("Stop requested.".to_string())
}

fn apply_snapshot_to_tray(
    tray: &tray_icon::TrayIcon,
    status_item: &muda::MenuItem,
    start_item: &muda::MenuItem,
    stop_item: &muda::MenuItem,
    repos_submenu: &muda::Submenu,
    snapshot: &WorktreeWatchTraySnapshot,
) {
    let state = if snapshot.any_running { "ON" } else { "OFF" };
    let tooltip = format!(
        "XBP worktree-watch: {state}\n{}\n{}",
        snapshot.target_label,
        snapshot
            .stats_line
            .clone()
            .unwrap_or_else(|| "no stats".to_string())
    );
    let _ = tray.set_tooltip(Some(tooltip));

    let status_text = if snapshot.parent_watcher_running {
        format!(
            "Status: ON (parent pid {}) · {} unsynced",
            snapshot.parent_watcher_pid.unwrap_or(0),
            snapshot.total_unsynced_records
        )
    } else if snapshot.any_running {
        format!(
            "Status: ON ({}/{} watching) · {} unsynced",
            snapshot.running_watchers, snapshot.repository_count, snapshot.total_unsynced_records
        )
    } else {
        format!(
            "Status: OFF · {} repo(s) · {} unsynced",
            snapshot.repository_count, snapshot.total_unsynced_records
        )
    };
    let _ = status_item.set_text(status_text);

    // Enable start when nothing is running; stop when something is.
    let _ = start_item.set_enabled(!snapshot.any_running);
    let _ = stop_item.set_enabled(snapshot.any_running);

    // Rebuild repo submenu labels (items are static-ish; we clear by recreating text-only rows).
    // muda Submenu doesn't support easy clear — append status lines as disabled items once.
    // For simplicity, only set the submenu text summary.
    let _ = repos_submenu.set_text(format!(
        "Repositories ({}/{} watching)",
        snapshot.running_watchers, snapshot.repository_count
    ));
}

fn print_stats_report(snapshot: &WorktreeWatchTraySnapshot) {
    println!("── XBP worktree-watch stats ──");
    println!("Target:  {}", snapshot.target_label);
    println!("Mode:    {}", snapshot.mode);
    println!(
        "Running: {} (parent={}, repos={}/{})",
        snapshot.any_running,
        snapshot.parent_watcher_running,
        snapshot.running_watchers,
        snapshot.repository_count
    );
    println!(
        "Backlog: {} records / {} files unsynced",
        snapshot.total_unsynced_records, snapshot.total_unsynced_files
    );
    if let Some(line) = &snapshot.stats_line {
        println!("Summary: {line}");
    }
    for repo in &snapshot.repositories {
        let flag = if repo.running { "ON " } else { "off" };
        println!(
            "  [{flag}] {}/{} ({})  unsynced={}  {}",
            repo.owner,
            repo.name,
            repo.branch,
            repo.unsynced_records,
            repo.root
        );
    }
    println!("──────────────────────────────");
}

fn describe_target(target: &WorktreeWatchTargetOptions) -> String {
    if let Some(parent) = target.parent.as_ref() {
        return format!("parent {}", parent.display());
    }
    if !target.repos.is_empty() {
        return format!(
            "{} repo(s): {}",
            target.repos.len(),
            target
                .repos
                .iter()
                .map(|path| path.display().to_string())
                .collect::<Vec<_>>()
                .join(", ")
        );
    }
    if let Some(repo) = target.repo.as_ref() {
        return format!("repo {}", repo.display());
    }
    "current repository".to_string()
}

fn platform_tray_hint() -> &'static str {
    if cfg!(windows) {
        "Windows system tray"
    } else if cfg!(target_os = "linux") {
        "Linux StatusNotifier / GNOME tray"
    } else if cfg!(target_os = "macos") {
        "macOS menu bar"
    } else {
        "system tray"
    }
}

fn load_rgba_icon() -> Result<Vec<u8>, String> {
    let image = image::load_from_memory(TRAY_ICON_BYTES)
        .map_err(|error| format!("Failed to decode tray icon: {error}"))?
        .into_rgba8();
    Ok(image.into_raw())
}

/// Parse tray options without depending on clap types (used from handler).
pub fn tray_options_from_parts(
    repo: Option<PathBuf>,
    parent: Option<PathBuf>,
    repos: Vec<PathBuf>,
    sync_interval_seconds: u64,
) -> Result<WorktreeWatchTrayOptions, String> {
    if parent.is_some() && repo.is_some() {
        return Err("Pass either `--repo` or `--parent`, not both.".to_string());
    }
    if parent.is_some() && !repos.is_empty() {
        return Err("Pass either `--repos` or `--parent`, not both.".to_string());
    }
    if repo.is_some() && !repos.is_empty() {
        return Err("Pass either `--repo` or `--repos`, not both.".to_string());
    }
    Ok(WorktreeWatchTrayOptions {
        target: WorktreeWatchTargetOptions {
            repo,
            parent,
            repos,
        },
        sync_interval_seconds,
    })
}