| 1 | |
| 2 | use crate::ActionId; |
| 3 | |
| 4 | use gpui::*; |
| 5 | use std::{ |
| 6 | cell::RefCell, |
| 7 | fmt::Write as _, |
| 8 | fs, |
| 9 | path::{Path, PathBuf}, |
| 10 | sync::LazyLock, |
| 11 | time::{Duration, Instant}, |
| 12 | }; |
| 13 | |
| 14 | const WRITE_INTERVAL: Duration = Duration::from_millis(250); |
| 15 | |
| 16 | struct Target { |
| 17 | id: String, |
| 18 | label: String, |
| 19 | bounds: Bounds<Pixels>, |
| 20 | enabled: bool, |
| 21 | } |
| 22 | |
| 23 | #[derive(Default)] |
| 24 | struct State { |
| 25 | targets: Vec<Target>, |
| 26 | last_write: Option<Instant>, |
| 27 | } |
| 28 | |
| 29 | thread_local! { |
| 30 | static STATE: RefCell<State> = RefCell::new(State::default()); |
| 31 | } |
| 32 | |
| 33 | fn output_path() -> Option<&'static Path> { |
| 34 | static PATH: LazyLock<Option<PathBuf>> = |
| 35 | LazyLock::new(|| std::env::var_os("EDITH_HITMAP").map(PathBuf::from)); |
| 36 | PATH.as_deref() |
| 37 | } |
| 38 | pub(crate) fn enabled() -> bool { |
| 39 | output_path().is_some() |
| 40 | } |
| 41 | |
| 42 | |
| 43 | |
| 44 | pub(crate) fn frame() -> Option<impl IntoElement> { |
| 45 | output_path()?; |
| 46 | Some( |
| 47 | canvas( |
| 48 | |_, _, _| STATE.with(|state| state.borrow_mut().targets.clear()), |
| 49 | |_, _, _, _| flush(), |
| 50 | ) |
| 51 | .absolute() |
| 52 | .size_full(), |
| 53 | ) |
| 54 | } |
| 55 | |
| 56 | |
| 57 | pub(crate) fn control( |
| 58 | id: &'static str, |
| 59 | label: &'static str, |
| 60 | enabled: bool, |
| 61 | ) -> Option<impl IntoElement> { |
| 62 | dynamic(|| (id.into(), label.into()), enabled) |
| 63 | } |
| 64 | |
| 65 | |
| 66 | pub(crate) fn dynamic( |
| 67 | target: impl FnOnce() -> (String, String), |
| 68 | enabled: bool, |
| 69 | ) -> Option<impl IntoElement> { |
| 70 | output_path()?; |
| 71 | let (id, label) = target(); |
| 72 | Some( |
| 73 | canvas( |
| 74 | move |bounds, _, _| { |
| 75 | STATE.with(|state| { |
| 76 | state.borrow_mut().targets.push(Target { |
| 77 | id, |
| 78 | label, |
| 79 | bounds, |
| 80 | enabled, |
| 81 | }); |
| 82 | }); |
| 83 | }, |
| 84 | |_, _, _, _| (), |
| 85 | ) |
| 86 | .absolute() |
| 87 | .size_full(), |
| 88 | ) |
| 89 | } |
| 90 | |
| 91 | fn flush() { |
| 92 | let Some(path) = output_path() else { |
| 93 | return; |
| 94 | }; |
| 95 | let now = Instant::now(); |
| 96 | let body = STATE.with(|state| { |
| 97 | let mut state = state.borrow_mut(); |
| 98 | if state |
| 99 | .last_write |
| 100 | .is_some_and(|last| now.duration_since(last) < WRITE_INTERVAL) |
| 101 | { |
| 102 | return None; |
| 103 | } |
| 104 | state.last_write = Some(now); |
| 105 | let mut json = String::from("["); |
| 106 | for (i, target) in state.targets.iter().enumerate() { |
| 107 | if i != 0 { |
| 108 | json.push(','); |
| 109 | } |
| 110 | json.push_str("{\"id\":"); |
| 111 | json_string(&mut json, &target.id); |
| 112 | json.push_str(",\"label\":"); |
| 113 | json_string(&mut json, &target.label); |
| 114 | let bounds = target.bounds; |
| 115 | let _ = write!( |
| 116 | json, |
| 117 | ",\"x\":{},\"y\":{},\"w\":{},\"h\":{},\"enabled\":{}", |
| 118 | f32::from(bounds.origin.x), |
| 119 | f32::from(bounds.origin.y), |
| 120 | f32::from(bounds.size.width), |
| 121 | f32::from(bounds.size.height), |
| 122 | target.enabled, |
| 123 | ); |
| 124 | json.push('}'); |
| 125 | } |
| 126 | json.push(']'); |
| 127 | Some(json) |
| 128 | }); |
| 129 | let Some(body) = body else { |
| 130 | return; |
| 131 | }; |
| 132 | let temp = path.with_extension(format!("hitmap-{}.tmp", std::process::id())); |
| 133 | if fs::write(&temp, body).is_ok() { |
| 134 | let _ = fs::rename(temp, path); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | fn json_string(json: &mut String, value: &str) { |
| 139 | json.push('"'); |
| 140 | for c in value.chars() { |
| 141 | match c { |
| 142 | '"' => json.push_str("\\\""), |
| 143 | '\\' => json.push_str("\\\\"), |
| 144 | '\n' => json.push_str("\\n"), |
| 145 | '\r' => json.push_str("\\r"), |
| 146 | '\t' => json.push_str("\\t"), |
| 147 | c if c <= '\u{1f}' => { |
| 148 | let _ = write!(json, "\\u{:04x}", c as u32); |
| 149 | } |
| 150 | c => json.push(c), |
| 151 | } |
| 152 | } |
| 153 | json.push('"'); |
| 154 | } |
| 155 | |
| 156 | pub(crate) fn action_id(action: ActionId) -> String { |
| 157 | format!("action.{action:?}") |
| 158 | } |
| 159 | |
| 160 | |
| 161 | pub(crate) fn action(action: ActionId, enabled: bool) -> Option<impl IntoElement> { |
| 162 | dynamic(move || (action_id(action), action.label().into()), enabled) |
| 163 | } |