diff --git a/komorebi-recorder/src/viewer/app.rs b/komorebi-recorder/src/viewer/app.rs index 903b0671..c9e0d3dc 100644 --- a/komorebi-recorder/src/viewer/app.rs +++ b/komorebi-recorder/src/viewer/app.rs @@ -109,6 +109,8 @@ pub struct Open { pub help: bool, /// The form for marking or editing a bug region, while it is open. pub composer: Option, + /// The form for proposing a rule for a ghost tile, while it is open. + pub rule: Option, /// The trace of each event looked at, by sequence number, worked out once. pub traces: HashMap>, /// The OS trace around each event looked at, by sequence number, worked out once. @@ -258,6 +260,7 @@ impl ViewerApp { overlay: self.prefs.overlay, help: false, composer: None, + rule: None, traces: HashMap::new(), os_traces: HashMap::new(), state: None, @@ -615,6 +618,8 @@ impl Open { self.help = false; } else if self.composer.is_some() { self.composer = None; + } else if self.rule.is_some() && !self.is_sending() { + self.rule = None; } else { self.selection = None; self.playback.set_loop(None); @@ -772,6 +777,7 @@ impl Open { .show(ctx, |ui| self.video_ui(ui, ctx)); self.composer_window(ctx); + self.rule_window(ctx); if self.help { self.help_window(ctx); } @@ -990,6 +996,149 @@ mod tests { frame(&mut app, &ctx, vec![]); } + /// A Windows session in which komorebi tiles a window that Windows says is cloaked on every + /// read of it, with a bug marked from 1.5 s to 3.5 s and another from 20 s to 21 s. + fn ghost_session(name: &str) -> PathBuf { + use crate::analysis::flavor::Flavor; + use crate::sources::winprobe::WindowFacts; + + const GHOST: i64 = 131_072; + let window = || testkit::windows_window(GHOST, "DDM.exe", Some([0, 0, 960, 1040])); + let state = || testkit::windows_state(65_537, vec![vec![window()]]); + let event = json!({"type": "FocusChange", "content": ["SystemForeground", window()]}); + let cloaked = WindowFacts { + hwnd: GHOST as u32, + at_ns: 0, + exists: true, + visible: true, + cloaked: true, + minimized: false, + rect: Some([0, 0, 960, 1040]), + komorebi_says: None, + }; + let (session, manifest) = SessionBuilder::new(name) + .flavor(Flavor::Windows) + .event(2.0, event.clone(), Some(state())) + .probe(cloaked.clone()) + .event(3.0, event, Some(state())) + .probe(cloaked) + .build(); + + for (from, to, text) in [(1.5, 3.5, "an empty tile"), (20.0, 21.0, "something else")] { + annotations::add( + &session.dir, + annotations::New { + kind: Some(annotations::Kind::Bug), + start_mach_ns: manifest.mach_at_video_seconds(from), + end_mach_ns: Some(manifest.mach_at_video_seconds(to)), + text: text.to_string(), + ..annotations::New::default() + }, + ) + .expect("bug"); + } + session.dir + } + + #[test] + fn a_bug_marked_while_a_ghost_tile_was_there_is_offered_a_rule() { + let dir = ghost_session("viewer-ghost"); + let mut app = ViewerApp::new(prefs_dir("ghost")); + app.open_now(&dir).expect("open"); + let ctx = egui::Context::default(); + + assert_eq!(open(&mut app).loaded.ghosts.len(), 1); + let ghost = open(&mut app).loaded.ghosts[0].clone(); + assert_eq!((ghost.exe.as_str(), ghost.class.as_str()), ("DDM.exe", "Window")); + + assert_eq!( + open(&mut app).ghosts_during(1.5, 3.5), + std::slice::from_ref(&ghost) + ); + assert!(open(&mut app).ghosts_during(20.0, 21.0).is_empty()); + + // the Bugs tab draws the card, and its button opens the form + open(&mut app).tab = Tab::Bugs; + frame(&mut app, &ctx, vec![]); + open(&mut app).open_rule_form(ghost.hwnd); + let form = open(&mut app).rule.as_ref().expect("the form is open"); + assert_eq!(form.app, "DDM"); + let rule = form.rule().expect("a rule"); + assert_eq!(rule.identifiers.len(), 2, "the program and the class together"); + frame(&mut app, &ctx, vec![]); + + // a blank name has no rule to send, and sending then does nothing + open(&mut app).rule.as_mut().expect("form").app = " ".to_string(); + assert!(open(&mut app).rule.as_ref().expect("form").rule().is_err()); + open(&mut app).start_sending(|| unreachable!("there is nothing to send")); + assert!(!open(&mut app).is_sending()); + + // and Escape closes the form + frame(&mut app, &ctx, vec![key(egui::Key::Escape)]); + assert!(open(&mut app).rule.is_none()); + } + + #[test] + fn a_rule_is_sent_in_the_background_and_what_came_of_it_is_shown() { + use crate::asc::submit::Outcome; + use crate::asc::submit::Tools; + use crate::asc::submit::Upstream; + use crate::viewer::rules::Stage; + + let dir = ghost_session("viewer-send"); + let mut app = ViewerApp::new(prefs_dir("send")); + app.open_now(&dir).expect("open"); + let ctx = egui::Context::default(); + let hwnd = open(&mut app).loaded.ghosts[0].hwnd; + open(&mut app).open_rule_form(hwnd); + + // to a repository that is not there: nothing is sent anywhere + let nowhere = std::env::temp_dir().join(format!( + "komorebi-recorder-viewer-no-such-repository-{}", + std::process::id() + )); + let upstream = Upstream { + url: nowhere.to_string_lossy().into_owned(), + ..Upstream::komorebi_asc() + }; + open(&mut app).start_sending(move || { + ( + Tools { + git: "git".into(), + gh: None, + env: Vec::new(), + }, + upstream, + ) + }); + assert!(open(&mut app).is_sending()); + // pressing it again while it goes does nothing more + open(&mut app).start_sending(|| unreachable!("it is being sent")); + + let deadline = Instant::now() + Duration::from_secs(30); + while open(&mut app).is_sending() && Instant::now() < deadline { + frame(&mut app, &ctx, vec![]); + std::thread::sleep(Duration::from_millis(30)); + } + + let form = open(&mut app).rule.take().expect("the form is still open"); + let Stage::Done(Outcome::Failed(failure)) = form.stage else { + panic!("it did not fail as it should: done is {}", matches!(form.stage, Stage::Done(_))); + }; + assert_eq!(failure.step, "cloning the repository"); + assert!(failure.instructions.contains("DDMWindow") || failure.instructions.contains("\"Window\"")); + if let Some(kept) = &failure.dir { + let _ = std::fs::remove_dir_all(kept); + } + + // what the form says when it is over draws + open(&mut app).rule = Some(crate::viewer::rules::RuleForm { + stage: Stage::Done(Outcome::Failed(failure.clone())), + ..form + }); + frame(&mut app, &ctx, vec![]); + } + #[test] fn the_keys_drive_the_playhead() { let dir = session("viewer-keys", false); diff --git a/komorebi-recorder/src/viewer/inspector.rs b/komorebi-recorder/src/viewer/inspector.rs index 4dfe8a42..65bcd940 100644 --- a/komorebi-recorder/src/viewer/inspector.rs +++ b/komorebi-recorder/src/viewer/inspector.rs @@ -24,6 +24,9 @@ use super::app::Open; use super::app::Tab; use super::theme; use crate::analysis::classify::Class; +use crate::analysis::ghost::GhostTile; +use crate::asc::Platform; +use crate::asc::rule::Rule; use crate::analysis::os_trace::GroupKind; use crate::analysis::os_trace::OsTraceData; use crate::analysis::os_trace::Phase; @@ -48,6 +51,7 @@ struct Wants { loop_region: Option<(f64, f64)>, copy: Option, mark: bool, + propose_rule: Option, } fn chip(ui: &mut egui::Ui, text: &str, colour: Color32) { @@ -59,6 +63,60 @@ fn chip(ui: &mut egui::Ui, text: &str, colour: Color32) { ); } +/// What the Bugs tab says under a bug region that was marked while a ghost tile was there: that +/// it looks like one, and the button that proposes a rule for it. +fn ghost_name(ghost: &GhostTile) -> String { + match ghost.platform { + Platform::Windows => format!("{} ({})", ghost.exe, ghost.class), + Platform::Macos if ghost.title.is_empty() => ghost.exe.clone(), + Platform::Macos => format!("{} ({:?})", ghost.exe, ghost.title), + } +} + +fn ghost_card(ui: &mut egui::Ui, ghost: &GhostTile, wants: &mut Wants) { + egui::Frame::new() + .fill(Color32::from_rgb(40, 38, 24)) + .corner_radius(4.0) + .inner_margin(egui::Margin::same(8)) + .show(ui, |ui| { + ui.horizontal_wrapped(|ui| { + chip(ui, "ghost tile", Color32::from_rgb(235, 185, 60)); + ui.label(RichText::new(ghost_name(ghost)).strong()); + }); + ui.label( + RichText::new(format!( + "komorebi tiled it at {}, and on {} reads from {} it was {}, and never on \ + screen: an empty tile, where a person expects a window.", + ghost.place, + ghost.reads, + timecode::format(ghost.first_video_s), + ghost.hidden_how, + )) + .small() + .color(theme::TEXT_DIM), + ); + let rule = Rule::for_ghost(ghost); + if rule.is_ok() { + if ui + .button("Propose a rule to komorebi-application-specific-configuration…") + .on_hover_text("shows the rule first; nothing is sent until you say so") + .clicked() + { + wants.propose_rule = Some(ghost.hwnd); + } + } else { + let why = rule.err().map_or_else(String::new, |error| error.to_string()); + ui.label( + RichText::new(format!( + "No rule can be written that catches it and nothing else: {why}." + )) + .small() + .color(theme::TEXT_DIM), + ); + } + }); +} + fn class_name(class: Class) -> String { format!("{class:?}").to_lowercase() } @@ -183,6 +241,9 @@ impl Open { if wants.mark { self.open_composer(); } + if let Some(hwnd) = wants.propose_rule { + self.open_rule_form(hwnd); + } } /// What the OS trace recorded around an event, worked out once. @@ -636,6 +697,11 @@ impl Open { } }); }); + if kind == Kind::Bug { + for ghost in self.ghosts_during(annotation.start_video_s, annotation.end_video_s) { + ghost_card(ui, &ghost, wants); + } + } ui.add_space(4.0); } } diff --git a/komorebi-recorder/src/viewer/loaded.rs b/komorebi-recorder/src/viewer/loaded.rs index b7c46043..b962f0a8 100644 --- a/komorebi-recorder/src/viewer/loaded.rs +++ b/komorebi-recorder/src/viewer/loaded.rs @@ -12,6 +12,7 @@ use crate::analysis::Analysis; use crate::analysis::catalog::Skew; +use crate::analysis::ghost::GhostTile; use crate::analysis::logparse::Level; use crate::analysis::overview::Finding; use crate::analysis::overview::IncidentView; @@ -90,6 +91,9 @@ pub struct Loaded { pub warnings: Vec, /// How the komorebi source this build indexed compares with the komorebi that was recorded. pub skew: Skew, + /// The ghost tiles the recording shows: tiles that held a window a person could not see. + /// Empty for a recording made without `--probe-windows`, and for one of komorebi for Mac. + pub ghosts: Vec, } impl Loaded { @@ -179,6 +183,10 @@ impl Loaded { let mut warnings = analysis.warnings(); warnings.extend(loaded_annotations.warnings.clone()); let skew = analysis.skew(); + let ghosts = analysis.ghost_tiles().unwrap_or_else(|error| { + warnings.push(format!("could not look for ghost tiles: {error:#}")); + Vec::new() + }); Ok(Self { incidents: analysis.incidents(), @@ -198,6 +206,7 @@ impl Loaded { displays, warnings, skew, + ghosts, }) } diff --git a/komorebi-recorder/src/viewer/mod.rs b/komorebi-recorder/src/viewer/mod.rs index fc332832..8a94b033 100644 --- a/komorebi-recorder/src/viewer/mod.rs +++ b/komorebi-recorder/src/viewer/mod.rs @@ -79,6 +79,7 @@ pub mod keymap; mod lanes; pub mod loaded; pub mod prefs; +mod rules; pub mod theme; pub mod timeline; mod video; diff --git a/komorebi-recorder/src/viewer/rules.rs b/komorebi-recorder/src/viewer/rules.rs new file mode 100644 index 00000000..f5121079 --- /dev/null +++ b/komorebi-recorder/src/viewer/rules.rs @@ -0,0 +1,414 @@ +//! Proposing a rule for a ghost tile: the form that shows what would be sent, and the sending. +//! +//! When a bug region the user marked was made while a ghost tile was there +//! ([`crate::analysis::ghost`]), the Bugs tab offers to propose a rule that ignores that window, +//! to komorebi-application-specific-configuration ([`crate::asc`]). The button opens a +//! [`RuleForm`]. It shows what was seen and the rule that would be proposed, with the application's +//! name to change, and says plainly what sending does: nothing leaves the machine until the user +//! presses the button. +//! +//! Pressing it sends in the background ([`Open::start_sending`]): cloning, committing and pushing +//! take seconds, and a window that stops answering for them is not one anyone trusts with their +//! credentials. The form lists the steps as they happen, and ends with what [`submit`] came to: the +//! link to the pull request, a link to open one, or, if something failed, where the clone is kept +//! and what to do to finish by hand. +//! +//! This needs an `egui::Context` to draw, so there are no examples; the tests in `viewer::app` open +//! the form, send to a local repository and read what came back. + +use super::app::Open; +use super::theme; +use crate::analysis::ghost::GhostTile; +use crate::asc::Platform; +use crate::asc::rule::Rule; +use crate::asc::rule::app_name; +use crate::asc::submit; +use crate::asc::submit::Outcome; +use crate::asc::submit::Proposal; +use crate::asc::submit::Tools; +use crate::asc::submit::Upstream; +use crate::timecode; +use eframe::egui; +use eframe::egui::RichText; +use std::sync::mpsc::Receiver; +use std::sync::mpsc::channel; + +/// What the background thread tells the form. +pub enum Update { + /// A step has begun. + Said(String), + /// It is over, and this is how it went. + Finished(Outcome), +} + +/// Where the form is. +pub enum Stage { + /// Showing what would be sent, waiting for the user. + Review, + /// Sending: the steps so far, and the channel the rest arrives on. + Sending { + /// The steps that have begun, in order. + said: Vec, + /// Where the thread reports. + updates: Receiver, + }, + /// Over. + Done(Outcome), +} + +/// The form for proposing the rule for one ghost tile. +pub struct RuleForm { + /// The ghost tile the rule is for. + pub ghost: GhostTile, + /// The name of the application's entry in the rule file, as typed. + pub app: String, + /// Where it is. + pub stage: Stage, +} + +impl RuleForm { + /// A form for `ghost`, with the application named for its program. + #[must_use] + pub fn new(ghost: GhostTile) -> Self { + Self { + app: app_name(&ghost.exe), + ghost, + stage: Stage::Review, + } + } + + /// The rule the form would send: the narrowest [`Rule::for_ghost`] can write, as the + /// application the form names. + /// + /// # Errors + /// + /// Returns what is missing when the name is blank, or when there is not enough to write a rule + /// that catches the ghost and nothing else. + pub fn rule(&self) -> Result { + let app = self.app.trim(); + if app.is_empty() { + return Err("name the application".to_string()); + } + let mut rule = Rule::for_ghost(&self.ghost).map_err(|error| error.to_string())?; + rule.app = app.to_string(); + Ok(rule) + } +} + +impl Open { + /// The ghost tiles that were there at some time between `start_s` and `end_s` of the video, + /// which is what a bug region marked then is about. + #[must_use] + pub fn ghosts_during(&self, start_s: f64, end_s: f64) -> Vec { + self.loaded + .ghosts + .iter() + .filter(|ghost| ghost.overlaps(start_s, end_s, self.loaded.duration_s)) + .cloned() + .collect() + } + + /// Opens the form for the ghost tile with window handle `hwnd`, if the recording has one. + pub fn open_rule_form(&mut self, hwnd: u64) { + if let Some(ghost) = self.loaded.ghosts.iter().find(|ghost| ghost.hwnd == hwnd) { + self.rule = Some(RuleForm::new(ghost.clone())); + } + } + + /// Sends the rule the form shows, in the background, with the tools and the repository `make` + /// gives (it runs on the thread: finding `gh` runs it). + /// + /// Does nothing unless the form is open, at [`Stage::Review`], and has a rule to send. The + /// window sends to the real repository with [`Open::send_rule`]; this is its seam for the tests. + pub fn start_sending(&mut self, make: impl FnOnce() -> (Tools, Upstream) + Send + 'static) { + let Some(form) = self.rule.as_mut() else { + return; + }; + let (Stage::Review, Ok(rule)) = (&form.stage, form.rule()) else { + return; + }; + + let proposal = Proposal::ghost_tile(&form.ghost, rule, env!("CARGO_PKG_VERSION")); + let (sender, updates) = channel(); + std::thread::spawn(move || { + let (tools, upstream) = make(); + let progress = sender.clone(); + let outcome = submit::submit(&proposal, &tools, &upstream, &mut |step| { + let _ = progress.send(Update::Said(step.to_string())); + }); + let _ = sender.send(Update::Finished(outcome)); + }); + form.stage = Stage::Sending { + said: Vec::new(), + updates, + }; + } + + /// Sends the rule to komorebi-application-specific-configuration on GitHub. + pub fn send_rule(&mut self) { + self.start_sending(|| (Tools::detect(), Upstream::komorebi_asc())); + } + + /// Takes in what the sending thread has reported. + pub fn poll_rule(&mut self) { + let Some(form) = self.rule.as_mut() else { + return; + }; + let mut finished = None; + if let Stage::Sending { said, updates } = &mut form.stage { + while let Ok(update) = updates.try_recv() { + match update { + Update::Said(step) => said.push(step), + Update::Finished(outcome) => finished = Some(outcome), + } + } + } + if let Some(outcome) = finished { + form.stage = Stage::Done(outcome); + } + } + + /// Whether a sending is going on, so that the window asks to be drawn again to see it end. + #[must_use] + pub fn is_sending(&self) -> bool { + self.rule + .as_ref() + .is_some_and(|form| matches!(form.stage, Stage::Sending { .. })) + } + + /// Draws the form, if it is open. Needs an `egui::Context`, so a window is needed. + pub fn rule_window(&mut self, ctx: &egui::Context) { + self.poll_rule(); + let Some(form) = self.rule.as_mut() else { + return; + }; + + let mut open = true; + let mut send = false; + let mut copy: Option = None; + let mut reveal: Option = None; + let sending = matches!(form.stage, Stage::Sending { .. }); + + egui::Window::new("Propose a rule for this ghost tile") + .open(&mut open) + .collapsible(false) + .resizable(true) + .default_width(540.0) + .anchor(egui::Align2::CENTER_CENTER, [0.0, 0.0]) + .show(ctx, |ui| { + let ghost = form.ghost.clone(); + ui.label(RichText::new(what_was_seen(&ghost)).color(theme::TEXT_DIM)); + ui.add_space(8.0); + + match &form.stage { + Stage::Review => { + ui.label(RichText::new("The rule").strong()); + ui.label( + RichText::new(format!( + "for {} in komorebi-application-specific-configuration: ignore the \ + windows that look exactly like this one, and no others. {}", + ghost.platform.file(), + leaves_alone(&ghost), + )) + .small() + .color(theme::TEXT_DIM), + ); + ui.horizontal(|ui| { + ui.label("application"); + ui.add( + egui::TextEdit::singleline(&mut form.app).desired_width(f32::INFINITY), + ); + }); + let rule = form.rule(); + let preview = rule.as_ref().map_or_else(|_| String::new(), Rule::preview); + if let Err(why) = &rule { + ui.label( + RichText::new(format!("No rule can be written: {why}")) + .color(theme::BUG), + ); + } + ui.add( + egui::TextEdit::multiline(&mut preview.as_str()) + .font(egui::TextStyle::Monospace) + .desired_rows(12) + .desired_width(f32::INFINITY), + ); + + ui.add_space(8.0); + ui.label( + RichText::new( + "Sending clones the repository into a temporary folder, commits the \ + rule on a new branch and opens a pull request on GitHub under your \ + account, with the credentials you already have set up for git (and \ + the GitHub CLI, if you have it). Nothing is sent until you press \ + the button.", + ) + .small() + .color(theme::TEXT_DIM), + ); + ui.add_space(6.0); + ui.horizontal(|ui| { + let rule = form.rule().ok(); + if ui + .add_enabled(rule.is_some(), egui::Button::new("Send as a pull request")) + .clicked() + { + send = true; + } + if ui + .add_enabled(rule.is_some(), egui::Button::new("Copy the rule")) + .clicked() + { + copy = rule.map(|rule| rule.preview()); + } + }); + } + Stage::Sending { said, .. } => { + for step in said { + ui.label(RichText::new(format!("• {step}")).monospace().size(12.0)); + } + ui.horizontal(|ui| { + ui.spinner(); + ui.label( + RichText::new("this can take a minute").color(theme::TEXT_DIM), + ); + }); + } + Stage::Done(outcome) => finished_ui(ui, outcome, &mut copy, &mut reveal), + } + }); + + if let Some(path) = reveal { + crate::platform::shell::reveal(&path); + } + if send { + self.send_rule(); + } + if let Some(text) = copy { + ctx.copy_text(text); + self.say("copied"); + } + // a form that is being sent is not closed under the thread that is sending it + if !open && !sending { + self.rule = None; + } + } +} + +/// What was seen of the ghost, in a sentence for the form. +fn what_was_seen(ghost: &GhostTile) -> String { + let (from, to) = ( + timecode::format(ghost.first_video_s), + timecode::format(ghost.last_video_s), + ); + match ghost.platform { + Platform::Windows => format!( + "komorebi gave `{}` (class `{}`) a tile, and on {} reads between {from} and {to} Windows \ + said the window was {}. It was never on screen, so the tile is empty for as long as the \ + window exists.", + ghost.exe, ghost.class, ghost.reads, ghost.hidden_how, + ), + Platform::Macos => format!( + "komorebi gave `{}` ({}) a tile, and on {} reads between {from} and {to} the frame it \ + had for the window was {}, never one a person could see. The tile is empty for as long \ + as the window exists.", + ghost.exe, + if ghost.title.is_empty() { + "no title".to_string() + } else { + format!("{:?}", ghost.title) + }, + ghost.reads, + ghost.hidden_how, + ), + } +} + +/// What the rule leaves alone: the program's other windows that the recording saw. +fn leaves_alone(ghost: &GhostTile) -> String { + match ghost.siblings.len() { + 0 => format!( + "The recording saw no other window of {}, but there may be some, so the rule is \ + narrow all the same.", + ghost.exe + ), + 1 => format!( + "{} has another window in the recording, which stays tiled.", + ghost.exe + ), + many => format!( + "{} has {many} other windows in the recording, which stay tiled.", + ghost.exe + ), + } +} + +/// What the form says when it is over. +fn finished_ui( + ui: &mut egui::Ui, + outcome: &Outcome, + copy: &mut Option, + reveal: &mut Option, +) { + match outcome { + Outcome::AlreadyThere => { + ui.label( + RichText::new("The repository has this rule already, so there is nothing to send.") + .color(theme::OK), + ); + } + Outcome::Opened { url, branch } => { + ui.label(RichText::new("The pull request is open.").color(theme::OK)); + ui.hyperlink(url); + ui.label( + RichText::new(format!("from the branch {branch}")) + .small() + .color(theme::TEXT_DIM), + ); + } + Outcome::Pushed { + branch, + compare_url, + } => { + ui.label( + RichText::new(format!( + "The branch {branch} is pushed. Open the pull request from it:" + )) + .color(theme::OK), + ); + ui.hyperlink(compare_url); + } + Outcome::Failed(failure) => { + ui.label( + RichText::new(format!("Could not finish: {}", failure.step)).color(theme::BUG), + ); + ui.label(RichText::new(&failure.error).monospace().size(11.0)); + ui.add_space(6.0); + if let Some(dir) = &failure.dir { + ui.horizontal(|ui| { + ui.label("The work is kept in"); + ui.monospace(dir.display().to_string()); + }); + ui.horizontal(|ui| { + if ui.button("Open the folder").clicked() { + *reveal = Some(dir.clone()); + } + if ui.button("Copy the path").clicked() { + *copy = Some(dir.display().to_string()); + } + }); + } + ui.add_space(6.0); + ui.label(RichText::new("To finish by hand").strong()); + ui.add( + egui::TextEdit::multiline(&mut failure.instructions.as_str()) + .font(egui::TextStyle::Monospace) + .desired_rows(12) + .desired_width(f32::INFINITY), + ); + if ui.button("Copy the instructions").clicked() { + *copy = Some(failure.instructions.clone()); + } + } + } +}