edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·ff99ad6·Tahinli·6 days ago·1055 commits
| 1 | //! The bars and the floating cards that are not inspector sections. |
| 2 | |
| 3 | use crate::ui::hitmap; |
| 4 | use crate::ui::popup::{popup_plate, popup_row, popup_row_icon, popup_rule, popup_w, shortcut}; |
| 5 | use crate::ui::stance::{menu_above, menu_floor, picture_bottom}; |
| 6 | use crate::ui::type_scale::{self, Typeset}; |
| 7 | use crate::ui::widgets::*; |
| 8 | use crate::*; |
| 9 | |
| 10 | impl Player { |
| 11 | /// The running import holds its own bar above the notice's, for the notice |
| 12 | /// bar's reason: the message is the point. Not a notice, though -- nothing |
| 13 | /// dismisses it, because it is about work still going on, and it leaves by |
| 14 | /// itself when the file lands. |
| 15 | /// |
| 16 | /// The bar under the words is a *sweep*, not a fill: neither read reports |
| 17 | /// where in the file it is, so a fill would have to invent the one number |
| 18 | /// this cannot know. What it does say truthfully is "something is still |
| 19 | /// running", which is exactly the question a frozen-looking window raises. |
| 20 | /// |
| 21 | /// ...and beside them the way out. A read that may be twenty seconds of a |
| 22 | /// cold 24 GB film needs one, and the only honest place for it is the line |
| 23 | /// that says the read is happening ([`Player::cancel_import`]). |
| 24 | pub(crate) fn import_bar(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { |
| 25 | let import = self.importing.as_ref()?; |
| 26 | let elapsed = import.started.elapsed().as_secs_f32(); |
| 27 | let line = import_line( |
| 28 | &file_name(&import.path), |
| 29 | import.seen, |
| 30 | elapsed, |
| 31 | import.since.elapsed().as_secs_f32(), |
| 32 | self.imports.len(), |
| 33 | arrival(self.opening.as_deref(), &import.path) != Landing::Import, |
| 34 | ); |
| 35 | // A quarter of the bar, crossing it every three seconds and wrapping. |
| 36 | // Tied to the elapsed clock, so it moves for as long as the read does |
| 37 | // and stops dead the instant the file lands. |
| 38 | const SWEEP: f32 = 0.25; |
| 39 | let at = (elapsed / 3.).fract() * (1. + SWEEP) - SWEEP; |
| 40 | Some( |
| 41 | div() |
| 42 | .flex_none() |
| 43 | .flex() |
| 44 | .flex_col() |
| 45 | .gap(px(4.)) |
| 46 | .px(px(12.)) |
| 47 | .py(px(6.)) |
| 48 | .bg(rgb(BG_RAISED())) |
| 49 | .child( |
| 50 | div() |
| 51 | .flex() |
| 52 | .items_center() |
| 53 | .gap(px(8.)) |
| 54 | .child(div().flex_1().min_w(px(0.)).child(line)) |
| 55 | .child(control( |
| 56 | "cancel-import", |
| 57 | // Hugs its label: the bar's own line is what wants |
| 58 | // the width, and this button never relabels. |
| 59 | 0., |
| 60 | // The ordinary plane -- the accent is Export's, and |
| 61 | // a way out of a read is not the primary action. |
| 62 | BG_RAISED(), |
| 63 | None, |
| 64 | "Cancel", |
| 65 | "stops this file and anything queued behind it, and the read with \ |
| 66 | them where the container lets it stop" |
| 67 | .to_string(), |
| 68 | true, |
| 69 | cx.listener(|this, _: &ClickEvent, _, cx| this.cancel_import(cx)), |
| 70 | )), |
| 71 | ) |
| 72 | .child( |
| 73 | div() |
| 74 | .relative() |
| 75 | .w_full() |
| 76 | .h(px(2.)) |
| 77 | .bg(rgb(BG_HOVER())) |
| 78 | .child( |
| 79 | div() |
| 80 | .absolute() |
| 81 | .top_0() |
| 82 | .left(relative(at.max(0.))) |
| 83 | // Clipped at both ends by hand: the segment |
| 84 | // enters from the left edge and leaves past the |
| 85 | // right one, and a width that overhung would |
| 86 | // paint outside the track. |
| 87 | .w(relative((at + SWEEP).min(1.) - at.max(0.))) |
| 88 | .h(px(2.)) |
| 89 | .bg(rgb(ACCENT_PRIMARY())), |
| 90 | ), |
| 91 | ), |
| 92 | ) |
| 93 | } |
| 94 | |
| 95 | /// Whether the picture under the playhead comes from a file that is not on the disk. |
| 96 | pub(crate) fn picture_is_missing(&self) -> bool { |
| 97 | let Some(session) = self.session.as_ref() else { |
| 98 | return false; |
| 99 | }; |
| 100 | let offline = session.offline_sources(); |
| 101 | !offline.is_empty() |
| 102 | && self.under_playhead().into_iter().next().is_some_and(|(lane, idx)| { |
| 103 | session.lane_clips(lane).get(idx).is_some_and(|c| offline.iter().any(|(i, _)| *i == c.source)) |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | /// The line a long-standing seek shows, in the import bar's place and by the |
| 108 | /// import bar's rules: nothing dismisses it, and it leaves by itself the |
| 109 | /// moment the frame lands. Once the picture is plainly not coming it says why. No sweep under it -- an open reports nothing about |
| 110 | /// where it has got to, and the clock is the honest half of that bar anyway. |
| 111 | pub(crate) fn seek_bar(&self) -> Option<impl IntoElement> { |
| 112 | // A missing file is the banner's to say (`missing_clip_note`), once, at the top of the |
| 113 | // picture: a second note here would repeat it. This line is for a read that failed. |
| 114 | if self.picture_is_missing() { |
| 115 | return None; |
| 116 | } |
| 117 | let line = seek_line(self.seek_since.map(|t| t.elapsed()))?; |
| 118 | // The same plate as the top banner, centred, so the two read as one family. |
| 119 | let t = crate::ui::theme::tokens(); |
| 120 | Some( |
| 121 | div().flex_none().flex().justify_center().px(px(12.)).py(px(6.)).child( |
| 122 | div() |
| 123 | .flex() |
| 124 | .items_center() |
| 125 | .px(px(10.)) |
| 126 | .py(px(4.)) |
| 127 | .rounded(px(6.)) |
| 128 | .bg(rgba(crate::ui::theme::with_alpha(t.e0, 0xD9))) |
| 129 | .border_1() |
| 130 | .border_color(rgba(crate::ui::theme::with_alpha(t.warn, 0x80))) |
| 131 | .type_style(type_scale::body()) |
| 132 | .text_color(rgb(t.ink1)) |
| 133 | .child(div().whitespace_nowrap().child(line)), |
| 134 | ), |
| 135 | ) |
| 136 | } |
| 137 | |
| 138 | /// The menu a right-click on a clip opens: what that clip can be given, |
| 139 | /// each item beside the stroke that does the very same thing, and a |
| 140 | /// turn-over side that says what the clip *is*. An item that would do |
| 141 | /// nothing where the playhead is standing is dimmed and takes no click |
| 142 | /// rather than disappearing, so the menu reads the same every time. |
| 143 | /// |
| 144 | /// Every item goes through [`Player::act`], the table the key handler uses: |
| 145 | /// an item is its stroke, asked for with the mouse. Plain divs like the |
| 146 | /// rest of this window, so the root keeps the keyboard and escape still |
| 147 | /// reaches the handler that closes this. |
| 148 | pub(crate) fn context_card( |
| 149 | &self, |
| 150 | viewport: Size<Pixels>, |
| 151 | cx: &mut Context<Self>, |
| 152 | ) -> Option<impl IntoElement> { |
| 153 | let menu = self.context_menu?; |
| 154 | let session = self.session.as_ref()?; |
| 155 | // Both `None` when the menu was opened on a caption: a subtitle lane |
| 156 | // holds no `Clip` and a caption plays no source file. The rows are the |
| 157 | // oracle's either way ([`Ctx::caption`]), so what the card *has* is |
| 158 | // asked for where it is used rather than at the door -- refusing here |
| 159 | // would be a right-click that opens nothing. |
| 160 | let idx = match menu.on { |
| 161 | MenuOn::Clip(idx) => Some(idx), |
| 162 | MenuOn::Gap(..) | MenuOn::Bench | MenuOn::Empty | MenuOn::Head | MenuOn::Dock | MenuOn::Viewer | MenuOn::SeqTab | MenuOn::Bin | MenuOn::Marker => None, |
| 163 | }; |
| 164 | let clip = idx.and_then(|idx| session.lane_clips(menu.lane).get(idx).cloned()); |
| 165 | let source = clip.as_ref().and_then(|clip| session.sources().get(clip.source).cloned()); |
| 166 | let secs = |frames: u32| timecode(f64::from(frames) / self.fps, self.fps); |
| 167 | // Every row is the shared popup row (`ui::popup`); `measure` collects |
| 168 | // the text the plate's width is fitted to. |
| 169 | let mut measure: Vec<(String, String)> = Vec::new(); |
| 170 | let mut sub_rows: Vec<AnyElement> = Vec::new(); |
| 171 | let mut sub_measure: Vec<(String, String)> = Vec::new(); |
| 172 | let mut adjust_at: Option<usize> = None; |
| 173 | // Where the Clip label and Fit to frame rows sit, each opening its own submenu. |
| 174 | let mut label_at: Option<usize> = None; |
| 175 | let mut fit_at: Option<usize> = None; |
| 176 | let mut comp_rows: Vec<AnyElement> = Vec::new(); |
| 177 | let mut comp_measure: Vec<(String, String)> = Vec::new(); |
| 178 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 179 | // Whether the rule line before the destructive rows has been drawn. |
| 180 | let mut ruled = false; |
| 181 | if let (true, Some((clip, source))) = (menu.details, clip.as_ref().zip(source.clone())) { |
| 182 | // Read-only, so no ids and no hover: this side is a card, not a |
| 183 | // list of things to click. Each value is one truncated line, which |
| 184 | // is what keeps the height below the one the clamp was given. |
| 185 | // The file first, as the library's plate has it, then this clip's |
| 186 | // own block: where it comes from and how it plays. |
| 187 | let streams = self |
| 188 | .streams |
| 189 | .get(&source.path) |
| 190 | .map_or(&[][..], Vec::as_slice); |
| 191 | let mut lines = file_properties( |
| 192 | &source.path, |
| 193 | self.facts.get(&source.path).and_then(Option::as_ref), |
| 194 | streams, |
| 195 | self.bitrates.get(&source.path).copied().flatten(), |
| 196 | ); |
| 197 | lines.extend([ |
| 198 | ( |
| 199 | "Source range".to_string(), |
| 200 | format!("{} – {}", secs(clip.in_frame), secs(clip.out_frame)), |
| 201 | ), |
| 202 | // How long it is *where it sits*, which its rate decides -- and |
| 203 | // the rate itself beside it, because a clip half its source's |
| 204 | // length could be either a trim or a 2x. |
| 205 | ("This clip".to_string(), secs(clip.frames())), |
| 206 | ( |
| 207 | "Speed".to_string(), |
| 208 | match speed_chip_text(clip).filter(|_| !clip.speed_keys.is_empty()) { |
| 209 | Some(ramp) => format!("{ramp} (ramp)"), |
| 210 | None => format!("{} (tape)", clip.speed), |
| 211 | }, |
| 212 | ), |
| 213 | ( |
| 214 | "Source duration".to_string(), |
| 215 | secs(session.file_frames(&source.path)), |
| 216 | ), |
| 217 | ]); |
| 218 | for (label, value) in lines { |
| 219 | measure.push((label.clone(), value.clone())); |
| 220 | rows.push( |
| 221 | popup_row(("menu", rows.len()), label, value, None, None) |
| 222 | .cursor_default() |
| 223 | .into_any_element(), |
| 224 | ); |
| 225 | } |
| 226 | rows.push(copy_path_button(&source.path, cx)); |
| 227 | } else if let MenuOn::Gap(start, frames) = menu.on { |
| 228 | // Gap rows are ripples, not clip actions. The first is the exact |
| 229 | // hole under the pointer and wears the chord that closes a selected |
| 230 | // gap (DESIGN §9: every command wears its chord); the second |
| 231 | // appears only when the same track has another bounded hole too, |
| 232 | // and has no chord of its own -- it is the menu's only door to it. |
| 233 | let lane = menu.lane; |
| 234 | let del = shortcut(&self.keymap, ActionId::Delete); |
| 235 | measure.push(("Close gap".into(), del.clone())); |
| 236 | rows.push( |
| 237 | popup_row_icon( |
| 238 | ("menu", rows.len()), |
| 239 | "Close gap", |
| 240 | del, |
| 241 | Some(false), |
| 242 | Some(crate::ui::icons::Icon::ArrowLeftToLine), |
| 243 | self.close_gap_refusal(lane), |
| 244 | ) |
| 245 | .when(self.close_gap_refusal(lane).is_none(), |d| { |
| 246 | d.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 247 | this.context_menu = None; |
| 248 | this.close_gap(lane, start, frames, cx); |
| 249 | })) |
| 250 | }) |
| 251 | .into_any_element(), |
| 252 | ); |
| 253 | if session.gap_count(lane) > 1 { |
| 254 | measure.push(("Close all gaps on this track".into(), String::new())); |
| 255 | rows.push( |
| 256 | popup_row_icon( |
| 257 | ("menu", rows.len()), |
| 258 | "Close all gaps on this track", |
| 259 | "", |
| 260 | Some(false), |
| 261 | Some(crate::ui::icons::Icon::ArrowLeftToLine), |
| 262 | None, |
| 263 | ) |
| 264 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 265 | this.context_menu = None; |
| 266 | this.close_all_gaps_on_lane(lane, cx); |
| 267 | })) |
| 268 | .into_any_element(), |
| 269 | ); |
| 270 | } |
| 271 | } else { |
| 272 | // A grade on a waveform, an equalizer on a picture, a silence scan |
| 273 | // on a still: things that do not exist for what was right-clicked, |
| 274 | // so the menu is the list of what this clip can do rather than the |
| 275 | // registry with most of it struck through. One filter, in |
| 276 | // `menu_items`, so there is no second answer to keep in step. The |
| 277 | // state refusals below stay, dimmed and saying why -- the next |
| 278 | // click of the playhead lights them. |
| 279 | // The clip's own state where a clip was clicked; the editor's |
| 280 | // otherwise -- the bench and the lane head both ask about the |
| 281 | // selection ([`Player::ctx`] falls back to it), which is exactly |
| 282 | // what the cut machinery's rows act on. |
| 283 | let ctx = self.ctx(idx.map(|idx| (menu.lane, idx))); |
| 284 | // Three lists, one render: what the thing under the pointer can be |
| 285 | // told (DESIGN §9). Nothing here is a second version of an action |
| 286 | // -- every row is an [`ActionId`] the keyboard already reaches, so |
| 287 | // the label and the chord still come out of the registry. |
| 288 | let actions = match menu.on { |
| 289 | MenuOn::Bench => BENCH_ITEMS.to_vec(), |
| 290 | MenuOn::Empty => crate::menus::empty_items(menu.lane), |
| 291 | MenuOn::Dock => DOCK_ITEMS.to_vec(), |
| 292 | MenuOn::Viewer => VIEWER_ITEMS.to_vec(), |
| 293 | MenuOn::SeqTab => SEQ_TAB_ITEMS.to_vec(), |
| 294 | MenuOn::Marker => MARKER_ITEMS.to_vec(), |
| 295 | MenuOn::Bin => BIN_ITEMS.to_vec(), |
| 296 | MenuOn::Head => lane_items(menu.lane), |
| 297 | _ => menu_items(ctx.clone()), |
| 298 | }; |
| 299 | let fold = matches!(menu.on, MenuOn::Clip(_)); |
| 300 | let (sub_list, rest): (Vec<ActionId>, Vec<ActionId>) = |
| 301 | actions.into_iter().partition(|a| fold && adjust_item(*a)); |
| 302 | let (comp_list, main_list): (Vec<ActionId>, Vec<ActionId>) = |
| 303 | rest.into_iter().partition(|a| fold && crate::menus::compound_item(*a)); |
| 304 | let has_comp = !comp_list.is_empty(); |
| 305 | // Whether a row of the main list opens a choice submenu (Clip label, Fit to frame). |
| 306 | let has_pick = fold |
| 307 | && idx.is_some() |
| 308 | && main_list.iter().any(|a| matches!(a, ActionId::ClipLabel | ActionId::Fit)); |
| 309 | // A ▸ row that opens the submenu `which` beside itself. Hovering it opens that |
| 310 | // submenu after ADJUST_HOVER, a click at once. Each row tracks its own hot state |
| 311 | // (`sub_hot`), so resting on another ▸ row after the first has opened switches it. |
| 312 | let sub_row = |rows: &mut Vec<AnyElement>, |
| 313 | measure: &mut Vec<(String, String)>, |
| 314 | which: Sub, |
| 315 | (name, icon): (&'static str, Option<crate::ui::icons::Icon>), |
| 316 | enabled: bool, |
| 317 | cx: &mut Context<Self>| { |
| 318 | let id: usize = match which { |
| 319 | Sub::Compound => 901, |
| 320 | Sub::Label => 902, |
| 321 | Sub::Fit => 903, |
| 322 | Sub::Adjust | Sub::Closed => 900, |
| 323 | }; |
| 324 | measure.push((name.into(), "▸".into())); |
| 325 | rows.push( |
| 326 | popup_row_icon(("menu", id), name, "▸", Some(false), icon, None) |
| 327 | .when(enabled, |d| { |
| 328 | d.on_hover(cx.listener(move |this, on: &bool, window, cx| { |
| 329 | if !*on { |
| 330 | if this.sub_hot == which { |
| 331 | this.sub_hot = Sub::Closed; |
| 332 | } |
| 333 | return; |
| 334 | } |
| 335 | this.sub_hot = which; |
| 336 | // Where the pointer came onto the row: the apex of |
| 337 | // the safe triangle toward the submenu. |
| 338 | this.adjust_aim = Some(window.mouse_position()); |
| 339 | if this.context_menu.is_some_and(|m| m.sub != which) { |
| 340 | // Hover intent: opens once the pointer has |
| 341 | // rested on the row for a moment, so a |
| 342 | // pointer crossing it does not flash the |
| 343 | // submenu. |
| 344 | cx.spawn(async move |this, cx| { |
| 345 | cx.background_executor().timer(ADJUST_HOVER).await; |
| 346 | let _ = this.update(cx, |this, cx| { |
| 347 | if this.sub_hot == which |
| 348 | && let Some(m) = &mut this.context_menu |
| 349 | && m.sub != which |
| 350 | { |
| 351 | m.sub = which; |
| 352 | cx.notify(); |
| 353 | } |
| 354 | }); |
| 355 | }) |
| 356 | .detach(); |
| 357 | } |
| 358 | })) |
| 359 | }) |
| 360 | // A click opens it at once and never shuts it: the |
| 361 | // hover may have opened it a moment before. |
| 362 | .when(enabled, |d| { |
| 363 | d.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 364 | if let Some(m) = &mut this.context_menu { |
| 365 | m.sub = which; |
| 366 | } |
| 367 | cx.notify(); |
| 368 | })) |
| 369 | }) |
| 370 | .into_any_element(), |
| 371 | ); |
| 372 | }; |
| 373 | let has_sub = !sub_list.is_empty() || has_comp; |
| 374 | // The two submenu rows, Adjust then Compound, wherever the first is due. |
| 375 | let sub_rows_at = |rows: &mut Vec<AnyElement>, |
| 376 | measure: &mut Vec<(String, String)>, |
| 377 | adjust_at: &mut Option<usize>, |
| 378 | cx: &mut Context<Self>| { |
| 379 | *adjust_at = Some(rows.len()); |
| 380 | sub_row(rows, measure, Sub::Adjust, ("Adjust", Some(crate::ui::icons::Icon::SlidersHorizontal)), true, cx); |
| 381 | if has_comp { |
| 382 | sub_row(rows, measure, Sub::Compound, ("Compound", Some(crate::ui::icons::Icon::Layers)), true, cx); |
| 383 | } |
| 384 | }; |
| 385 | // 0: the plate; 1: the Adjust submenu; 2: the Compound submenu. |
| 386 | for (is_sub, action) in main_list |
| 387 | .into_iter() |
| 388 | .map(|a| (0u8, a)) |
| 389 | .chain(sub_list.into_iter().map(|a| (1, a))) |
| 390 | .chain(comp_list.into_iter().map(|a| (2, a))) |
| 391 | { |
| 392 | if is_sub > 0 && adjust_at.is_none() { |
| 393 | // No destructive row came first. |
| 394 | sub_rows_at(&mut rows, &mut measure, &mut adjust_at, cx); |
| 395 | } |
| 396 | if destructive(action) && has_sub && adjust_at.is_none() { |
| 397 | sub_rows_at(&mut rows, &mut measure, &mut adjust_at, cx); |
| 398 | } |
| 399 | let (rows, measure) = match is_sub { |
| 400 | 1 => (&mut sub_rows, &mut sub_measure), |
| 401 | 2 => (&mut comp_rows, &mut comp_measure), |
| 402 | _ => (&mut rows, &mut measure), |
| 403 | }; |
| 404 | let level = is_sub; |
| 405 | let is_sub = is_sub > 0; |
| 406 | // DESIGN §9's rule line: the verbs that take something away sit |
| 407 | // under a seam, wherever they are listed. Drawn once, before |
| 408 | // the first of them, and never as the plate's own top edge. |
| 409 | if destructive(action) && !is_sub && !rows.is_empty() && !ruled { |
| 410 | ruled = true; |
| 411 | rows.push(popup_rule()); |
| 412 | } |
| 413 | // The registry's own answer, the same one the actions card |
| 414 | // dims a row with -- and a row that takes no click says *why* |
| 415 | // rather than printing a stroke that would do nothing. |
| 416 | let refusal = enable(action, ctx.clone()); |
| 417 | let enabled = refusal.yes(); |
| 418 | // Clip label and Fit to frame are ▸ rows like Adjust: the list they |
| 419 | // offer hangs beside the row, and the row itself runs nothing. |
| 420 | if let (true, Some(_), ActionId::ClipLabel | ActionId::Fit) = (fold, idx, action) { |
| 421 | let (which, name) = match action { |
| 422 | ActionId::Fit => { |
| 423 | fit_at = Some(rows.len()); |
| 424 | (Sub::Fit, "Fit to frame") |
| 425 | } |
| 426 | _ => { |
| 427 | label_at = Some(rows.len()); |
| 428 | (Sub::Label, "Clip label") |
| 429 | } |
| 430 | }; |
| 431 | sub_row(rows, measure, which, (name, crate::menus::icon(action)), enabled, cx); |
| 432 | continue; |
| 433 | } |
| 434 | // The one item that is not about this clip says so, and says it |
| 435 | // here rather than in the registry: the stroke is global too, |
| 436 | // but its row in the keys menu is not sitting on a clip. |
| 437 | // The registry's label is the verb itself now (cleanse round |
| 438 | // 2, `a_label_names_the_verb_in_four_words`): four words at |
| 439 | // most, so it fits a 260px plate beside its chord instead of |
| 440 | // being cut mid-word ("Ungroup the selection (clips anc"). |
| 441 | // Nothing is trimmed here any more -- what a parenthesis used |
| 442 | // to carry is the refusal string in the column beside it. |
| 443 | let full = action.label(); |
| 444 | let label = match action { |
| 445 | // A switch says which way it is set: a row reading "Snap on |
| 446 | // / off" beside a chord tells nobody whether snapping is on |
| 447 | // right now, which is the one thing the spine's badge did |
| 448 | // say before it went. |
| 449 | ActionId::ToggleSnap => { |
| 450 | format!("Snap — {}", if self.snap { "on" } else { "off" }) |
| 451 | } |
| 452 | // A ▸ row in this menu, not a dialog: no ellipsis on its label. |
| 453 | ActionId::Fit => "Fit to frame".to_string(), |
| 454 | // Plain words for the three that read as jargon; the action ids stay. |
| 455 | ActionId::LoopTrim => "Loop while trimming".to_string(), |
| 456 | ActionId::Dissolve => "Blend into next clip".to_string(), |
| 457 | ActionId::Regroup => "Group again".to_string(), |
| 458 | _ if matches!(menu.on, MenuOn::Head) |
| 459 | && crate::ui::bench_stance::TrackSize::of_action(action).is_some() => |
| 460 | { |
| 461 | format!("Height: {}", crate::ui::bench_stance::TrackSize::of_action(action).map_or("", |s| s.word())) |
| 462 | } |
| 463 | ActionId::AddMarker if matches!(menu.on, MenuOn::Bench) => "Add marker here".to_string(), |
| 464 | ActionId::NewSolid |
| 465 | | ActionId::NewGradient |
| 466 | | ActionId::NewCountdown |
| 467 | | ActionId::NewBars |
| 468 | | ActionId::NewTone |
| 469 | | ActionId::NewBarsAndTone |
| 470 | | ActionId::NewAdjustment |
| 471 | | ActionId::NewRectangle |
| 472 | | ActionId::NewEllipse |
| 473 | | ActionId::NewLine |
| 474 | | ActionId::NewArrow |
| 475 | | ActionId::NewTitle => format!("New {}", full.to_lowercase()), |
| 476 | _ => full.to_string(), |
| 477 | }; |
| 478 | let hint = shortcut(&self.keymap, action); |
| 479 | // A switch wears the menu's ✓ when on, the bar's own style. |
| 480 | let check = match (action, menu.on) { |
| 481 | (ActionId::ClipEnabled, _) => Some({ |
| 482 | !idx.and_then(|i| self.session.as_ref().map(|s| s.mark_of(menu.lane, i).off)) |
| 483 | .unwrap_or(false) |
| 484 | }), |
| 485 | // The head's three heights are radio rows: the one the lane is at is ticked. |
| 486 | (_, MenuOn::Head) => crate::ui::bench_stance::TrackSize::of_action(action) |
| 487 | .map(|size| crate::ui::bench_stance::track_size(self, menu.lane) == size), |
| 488 | _ => None, |
| 489 | }; |
| 490 | measure.push((label.clone(), hint.clone())); |
| 491 | rows.push( |
| 492 | popup_row_icon( |
| 493 | ("menu", rows.len() + 1000 * usize::from(level)), |
| 494 | label, |
| 495 | hint, |
| 496 | Some(check.unwrap_or(false)), |
| 497 | crate::menus::icon(action), |
| 498 | refusal.why(), |
| 499 | ) |
| 500 | .when(!is_sub && (has_sub || has_pick), |d| { |
| 501 | d.on_hover(cx.listener(|this, on: &bool, window, cx| { |
| 502 | // Another row under the pointer closes the |
| 503 | // submenu -- unless the pointer is crossing |
| 504 | // the safe triangle toward it. |
| 505 | let toward = match (this.adjust_aim, this.adjust_sub.get()) { |
| 506 | (Some(a), Some((x, top, bottom))) => in_aim_triangle( |
| 507 | window.mouse_position(), |
| 508 | a, |
| 509 | point(px(x), px(top)), |
| 510 | point(px(x), px(bottom)), |
| 511 | ), |
| 512 | _ => false, |
| 513 | }; |
| 514 | if let Some(m) = &mut this.context_menu |
| 515 | && *on |
| 516 | && m.sub != Sub::Closed |
| 517 | && !toward |
| 518 | { |
| 519 | m.sub = Sub::Closed; |
| 520 | cx.notify(); |
| 521 | } |
| 522 | })) |
| 523 | }) |
| 524 | // Hover line for the pointer; it stands aside while the |
| 525 | // menu itself is up (`tip_may_paint`). |
| 526 | .when(enabled, |d| { |
| 527 | d.tooltip(crate::ui::widgets::tip_hover(full, "", None)) |
| 528 | }) |
| 529 | .when(enabled, |d| { |
| 530 | d.cursor_pointer() |
| 531 | .hover(|s| s.bg(rgba(crate::ui::theme::WASH_HOVER))) |
| 532 | .on_click(cx.listener( |
| 533 | move |this, _: &ClickEvent, window, cx| { |
| 534 | // Closed first: the action moves the very |
| 535 | // indices this menu is holding. |
| 536 | this.context_menu = None; |
| 537 | if let (MenuOn::Head, Some(size)) = |
| 538 | (menu.on, crate::ui::bench_stance::TrackSize::of_action(action)) |
| 539 | { |
| 540 | // A header's own height: this track only, not every track. |
| 541 | this.set_lane_size(menu.lane, size, cx); |
| 542 | } else if matches!(menu.on, MenuOn::Head) |
| 543 | && matches!( |
| 544 | action, |
| 545 | ActionId::Mix |
| 546 | | ActionId::RenameLane |
| 547 | | ActionId::RemoveVideoLane |
| 548 | | ActionId::RemoveAudioLane |
| 549 | ) |
| 550 | { |
| 551 | // The head's own verbs, which left the |
| 552 | // head itself for this menu (cleanse |
| 553 | // round 2): they act on the lane the |
| 554 | // hand named, the way the head's |
| 555 | // buttons did, not on the last of the |
| 556 | // kind the bare stroke reaches. |
| 557 | this.act_lane(action, menu.lane, cx); |
| 558 | } else { |
| 559 | this.act(action, window, cx); |
| 560 | } |
| 561 | }, |
| 562 | )) |
| 563 | }) |
| 564 | .children(hitmap::dynamic( |
| 565 | move || (format!("menu.{action:?}"), full.to_string()), |
| 566 | enabled, |
| 567 | )) |
| 568 | .into_any_element(), |
| 569 | ); |
| 570 | } |
| 571 | if has_sub && adjust_at.is_none() { |
| 572 | sub_rows_at(&mut rows, &mut measure, &mut adjust_at, cx); |
| 573 | } |
| 574 | // What a caption has no other side to turn over to: its file, its |
| 575 | // source range and its bitrate are a clip's, and a row that opened |
| 576 | // an empty card would be the stub this menu does not draw. |
| 577 | rows.extend(clip.as_ref().map(|_| { |
| 578 | measure.push(("Properties…".into(), String::new())); |
| 579 | popup_row_icon(("menu", rows.len()), "Properties…", "", Some(false), Some(crate::ui::icons::Icon::Info), None) |
| 580 | .tooltip(crate::ui::widgets::tip_hover("Properties", "", None)) |
| 581 | .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 582 | if let Some(menu) = &mut this.context_menu { |
| 583 | menu.details = true; |
| 584 | } |
| 585 | cx.notify(); |
| 586 | })) |
| 587 | .children(hitmap::control("menu.properties", "Properties", true)) |
| 588 | .into_any_element() |
| 589 | })); |
| 590 | } |
| 591 | // The height the card is *placed* by and the height its list is drawn |
| 592 | // to are one number: placed by a taller one, the card would hang off the |
| 593 | // window's floor -- the very thing the clamp is for. |
| 594 | // §5/§11 check 6: the picture is never covered. A right-click high on |
| 595 | // the window would otherwise hang the menu at the pointer, straight |
| 596 | // over the screen -- clamped below it into the bench/ledger/dock |
| 597 | // footprint instead. The list is sized against that same |
| 598 | // footprint (`room`), not the whole window, so a menu taller than |
| 599 | // the footprint scrolls inside its own plate instead of walking its |
| 600 | // clamped top edge back up over the picture ([`menu_floor`]). |
| 601 | // ...and that floor is the picture's painted edge, so the black bars |
| 602 | // of a letterboxed film are room the menu may use (R4). |
| 603 | // A clip's menu is opened on the timeline, so the picture floor is |
| 604 | // already above it; its own room is the whole window, and it opens |
| 605 | // upward from the pointer when there is none below. |
| 606 | let (at, room, floor) = match menu.on { |
| 607 | // The viewer's menu is opened ON the picture: it hangs at the pointer. The dock |
| 608 | // is beside the picture, not under it, so its menu hangs where it was opened too. |
| 609 | MenuOn::Clip(_) | MenuOn::Viewer | MenuOn::Dock => (menu.at, viewport, 0.), |
| 610 | _ => menu_floor( |
| 611 | menu.at, |
| 612 | viewport, |
| 613 | self.split_px(Split::Bench, viewport), |
| 614 | picture_bottom(self), |
| 615 | ), |
| 616 | }; |
| 617 | let list_h = menu_rows_h(rows.len(), room); |
| 618 | let h = MENU_PAD * 2. + list_h; |
| 619 | let pw = popup_w(measure.iter().map(|(l, h)| (l.as_str(), h.as_str())), true); |
| 620 | let (x, y) = menu_at_w(menu_above(at, viewport, floor, h), viewport, pw, h); |
| 621 | // The submenu that is out, and the row it hangs from: Adjust's, Compound's, or a |
| 622 | // choice list (Clip label, Fit to frame) built from the picker's own rows. |
| 623 | let (sub_rows, sub_measure, sub_at) = match (menu.sub, idx) { |
| 624 | (Sub::Adjust, _) => (sub_rows, sub_measure, adjust_at), |
| 625 | (Sub::Compound, _) => (comp_rows, comp_measure, adjust_at.map(|a| a + 1)), |
| 626 | (Sub::Label, Some(idx)) => { |
| 627 | let (rows, measure) = self.choice_sub_rows(Pick::Label(menu.lane, idx), cx); |
| 628 | (rows, measure, label_at) |
| 629 | } |
| 630 | (Sub::Fit, Some(idx)) => { |
| 631 | let (rows, measure) = self.choice_sub_rows(Pick::Fit(menu.lane, idx), cx); |
| 632 | (rows, measure, fit_at) |
| 633 | } |
| 634 | _ => (Vec::new(), Vec::new(), None), |
| 635 | }; |
| 636 | // The submenu's near edge, for the safe triangle (see `adjust_sub`). |
| 637 | self.adjust_sub.set(match sub_rows.is_empty() { |
| 638 | true => None, |
| 639 | false => { |
| 640 | let sw = popup_w( |
| 641 | sub_measure.iter().map(|(l, h)| (l.as_str(), h.as_str())), |
| 642 | true, |
| 643 | ); |
| 644 | let sh = MENU_PAD * 2. + sub_rows.len() as f32 * MENU_ROW_H; |
| 645 | let top = y + MENU_PAD + sub_at.unwrap_or(0) as f32 * MENU_ROW_H; |
| 646 | let right = x + pw + sw + MENU_EDGE > f32::from(viewport.width); |
| 647 | let sx = if right { x - sw - 2. } else { x + pw + 2. }; |
| 648 | let (sx, sy) = menu_at_w(point(px(sx), px(top)), viewport, sw, sh); |
| 649 | Some((if right { sx + sw } else { sx }, sy, sy + sh)) |
| 650 | } |
| 651 | }); |
| 652 | let full: SharedString = source |
| 653 | .map(|source| source.path.display().to_string()) |
| 654 | .unwrap_or_default() |
| 655 | .into(); |
| 656 | Some( |
| 657 | scrim() |
| 658 | // Click away closes it, either button, and the press is |
| 659 | // swallowed so nothing under the menu also takes it. No tint, |
| 660 | // unlike the modal cards: the timeline this menu is about has |
| 661 | // to stay readable behind it. |
| 662 | .on_mouse_down( |
| 663 | MouseButton::Left, |
| 664 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 665 | this.context_menu = None; |
| 666 | cx.notify(); |
| 667 | cx.stop_propagation(); |
| 668 | }), |
| 669 | ) |
| 670 | .on_mouse_down( |
| 671 | MouseButton::Right, |
| 672 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 673 | this.context_menu = None; |
| 674 | cx.notify(); |
| 675 | cx.stop_propagation(); |
| 676 | }), |
| 677 | ) |
| 678 | .child( |
| 679 | // One plate shape for all three hanging menus |
| 680 | // ([`menu_plate`]) -- the id is still this menu's own, so |
| 681 | // the details side keeps its tooltip. |
| 682 | popup_plate("menu-card", x, y, pw) |
| 683 | // Painted after the scrim, so this listener runs first |
| 684 | // (gpui bubbles mouse events in reverse, window.rs:3705) |
| 685 | // and a press meant for an item never closes the menu |
| 686 | // out from under its own click. |
| 687 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 688 | cx.stop_propagation() |
| 689 | }) |
| 690 | .on_mouse_down(MouseButton::Right, |_: &MouseDownEvent, _, cx| { |
| 691 | cx.stop_propagation() |
| 692 | }) |
| 693 | // The one value the details side has to truncate. |
| 694 | .when(menu.details, |d| { |
| 695 | d.tooltip(move |_, cx| cx.new(|_| Tip(full.clone())).into()) |
| 696 | }) |
| 697 | // The list scrolls where the card would otherwise grow |
| 698 | // past the window's floor -- an item hanging off the |
| 699 | // bottom edge is an item nobody can click. |
| 700 | .child( |
| 701 | div() |
| 702 | .id("menu-rows") |
| 703 | .flex() |
| 704 | .flex_col() |
| 705 | .max_h(px(list_h)) |
| 706 | .overflow_y_scroll() |
| 707 | .children(rows), |
| 708 | ), |
| 709 | ) |
| 710 | // The open submenu, beside the plate at its row; on the left |
| 711 | // of it where the right edge has no room. |
| 712 | .children((!sub_rows.is_empty()).then(|| { |
| 713 | let sw = popup_w( |
| 714 | sub_measure.iter().map(|(l, h)| (l.as_str(), h.as_str())), |
| 715 | true, |
| 716 | ); |
| 717 | let sh = MENU_PAD * 2. + sub_rows.len() as f32 * MENU_ROW_H; |
| 718 | let top = y + MENU_PAD + sub_at.unwrap_or(0) as f32 * MENU_ROW_H; |
| 719 | let sx = match x + pw + sw + MENU_EDGE > f32::from(viewport.width) { |
| 720 | true => x - sw - 2., |
| 721 | false => x + pw + 2., |
| 722 | }; |
| 723 | let (sx, sy) = menu_at_w(point(px(sx), px(top)), viewport, sw, sh); |
| 724 | popup_plate("menu-sub", sx, sy, sw) |
| 725 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 726 | cx.stop_propagation() |
| 727 | }) |
| 728 | .on_mouse_down(MouseButton::Right, |_: &MouseDownEvent, _, cx| { |
| 729 | cx.stop_propagation() |
| 730 | }) |
| 731 | .children(sub_rows) |
| 732 | })), |
| 733 | ) |
| 734 | } |
| 735 | |
| 736 | /// The rows of a clip menu's choice submenu: the list the picker shows for `of` |
| 737 | /// ([`Player::choices`]), each row picking through [`Player::choose`] and closing the menu. |
| 738 | fn choice_sub_rows(&self, of: Pick, cx: &mut Context<Self>) -> (Vec<AnyElement>, Vec<(String, String)>) { |
| 739 | let mut rows: Vec<AnyElement> = Vec::new(); |
| 740 | let mut measure: Vec<(String, String)> = Vec::new(); |
| 741 | for (n, (choice, label, detail, picked)) in self.choices(of).into_iter().enumerate() { |
| 742 | measure.push((label.to_string(), detail.to_string())); |
| 743 | rows.push( |
| 744 | popup_row(("menu-choice", n), label, detail, Some(picked), None) |
| 745 | .cursor_pointer() |
| 746 | .hover(|s| s.bg(rgba(crate::ui::theme::WASH_HOVER))) |
| 747 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 748 | this.context_menu = None; |
| 749 | this.choose(choice, cx); |
| 750 | })) |
| 751 | .into_any_element(), |
| 752 | ); |
| 753 | } |
| 754 | (rows, measure) |
| 755 | } |
| 756 | |
| 757 | /// The open choice list: every value of one enumerated setting at once, the |
| 758 | /// one in force marked, and a click on any of them picks *that* one -- what |
| 759 | /// a button stepping one value on per click could never say. Built on the |
| 760 | /// clip menu's machinery down to the scrim, the placement and the scroll |
| 761 | /// cap, so it hangs and closes exactly as the menus do and fits the same |
| 762 | /// 640x360 floor. The stroke for the setting is untouched: this is the |
| 763 | /// pointer's door to it, not a second setting. |
| 764 | pub(crate) fn picker_card( |
| 765 | &self, |
| 766 | viewport: Size<Pixels>, |
| 767 | cx: &mut Context<Self>, |
| 768 | ) -> Option<impl IntoElement> { |
| 769 | let picker = self.picker?; |
| 770 | let theme_picker = picker.of == Pick::Theme; |
| 771 | let from_palette = picker.at == Point::default() && picker.above.is_none(); |
| 772 | let choices = self.choices(picker.of); |
| 773 | let pw = crate::ui::popup::popup_w( |
| 774 | choices.iter().map(|(_, label, detail, _)| (label.as_ref(), detail.as_ref())), |
| 775 | true, |
| 776 | ); |
| 777 | let rows: Vec<AnyElement> = choices |
| 778 | .into_iter() |
| 779 | .enumerate() |
| 780 | .map(|(n, (choice, label, detail, picked))| { |
| 781 | let hitmap_label = label.clone(); |
| 782 | let row_tip = label.clone(); |
| 783 | let hitmap_id = match theme_picker { |
| 784 | true => format!("theme.{n}.row"), |
| 785 | false => format!("picker.choice.{n}"), |
| 786 | }; |
| 787 | // The shared popup row; the keyboard's row wears the focus ring. |
| 788 | // A refused row (a format this timeline cannot be written in) is |
| 789 | // dimmed and says why on hover; it takes no click. |
| 790 | let why = self.export_choice_refusal(choice); |
| 791 | let missing = self.speech_model_missing(choice); |
| 792 | crate::ui::popup::popup_row(("picker-row", n), label, detail, Some(picked), why.as_deref()) |
| 793 | .when(missing, |d| d.opacity(0.45)) |
| 794 | .when(n == picker.sel, |d| d.bg(rgb(DARK_RAISED()))) |
| 795 | .when(why.is_none(), |d| { |
| 796 | d.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| this.choose(choice, cx))) |
| 797 | }) |
| 798 | .tooltip(crate::ui::widgets::overlay_tip_hover(&row_tip, "", why.as_deref())) |
| 799 | .children(hitmap::dynamic( |
| 800 | move || (hitmap_id, hitmap_label.to_string()), |
| 801 | true, |
| 802 | )) |
| 803 | .into_any_element() |
| 804 | }) |
| 805 | .collect(); |
| 806 | // Anchored to the row that opened it: inside the window, flipping up |
| 807 | // when the room below is short. Only a list opened over the picture |
| 808 | // keeps the timeline floor; Settings is a dialog and owns its place. |
| 809 | let (at, room, floor) = match self.settings_open || !picker.floored { |
| 810 | true => (picker.at, viewport, 0.), |
| 811 | false => menu_floor( |
| 812 | picker.at, |
| 813 | viewport, |
| 814 | self.split_px(Split::Bench, viewport), |
| 815 | picture_bottom(self), |
| 816 | ), |
| 817 | }; |
| 818 | let list_h = menu_rows_h(rows.len(), room); |
| 819 | let h = MENU_PAD * 2. + list_h; |
| 820 | // Settings: the list hangs from the foot of the row that was clicked |
| 821 | // and flips above that row, never over it, when the room is short. |
| 822 | let at = match self.settings_open || !picker.floored { |
| 823 | true => settings_anchor(at, viewport, h), |
| 824 | false => at, |
| 825 | }; |
| 826 | let placed = match picker.above { |
| 827 | Some(top) if f32::from(at.y) + h + crate::layout::MENU_EDGE > f32::from(viewport.height) => { |
| 828 | point(at.x, px((top - h).max(0.))) |
| 829 | } |
| 830 | Some(_) => at, |
| 831 | None => menu_above(at, viewport, floor, h), |
| 832 | }; |
| 833 | let (x, y) = match from_palette { |
| 834 | // Opened by name from the command palette (no control to hang from): |
| 835 | // the list takes the palette's own place, where the eye already is. |
| 836 | true => (((f32::from(viewport.width) - pw) / 2.).max(MENU_EDGE), (f32::from(viewport.height) / 6.).max(MENU_EDGE)), |
| 837 | false => crate::oracle::menu_at_w(placed, viewport, pw, h), |
| 838 | }; |
| 839 | // Deferred: a list opened from a dialog (Settings' Theme, Frame rate, |
| 840 | // Subtitle font) paints over that dialog and takes its clicks. Settings |
| 841 | // is painted after this layer in the root, so without the deferral the |
| 842 | // dialog's own press handler closed Settings and the list stayed behind. |
| 843 | Some(gpui::deferred( |
| 844 | scrim() |
| 845 | // Click away closes it, either button, swallowed so nothing |
| 846 | // under the list also takes the press -- the clip menu's rule. |
| 847 | .on_mouse_down( |
| 848 | MouseButton::Left, |
| 849 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 850 | this.picker = None; |
| 851 | cx.notify(); |
| 852 | cx.stop_propagation(); |
| 853 | }), |
| 854 | ) |
| 855 | .on_mouse_down( |
| 856 | MouseButton::Right, |
| 857 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 858 | this.picker = None; |
| 859 | cx.notify(); |
| 860 | cx.stop_propagation(); |
| 861 | }), |
| 862 | ) |
| 863 | .child( |
| 864 | crate::ui::popup::popup_plate("picker-card", x, y, pw) |
| 865 | // Painted after the scrim, so this listener runs first |
| 866 | // and a press meant for a row never closes the list out |
| 867 | // from under its own click (`context_card`). |
| 868 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 869 | cx.stop_propagation() |
| 870 | }) |
| 871 | .on_mouse_down(MouseButton::Right, |_: &MouseDownEvent, _, cx| { |
| 872 | cx.stop_propagation() |
| 873 | }) |
| 874 | .child( |
| 875 | div() |
| 876 | .id("picker-rows") |
| 877 | .flex() |
| 878 | .flex_col() |
| 879 | .max_h(px(list_h)) |
| 880 | .overflow_y_scroll() |
| 881 | .children(rows), |
| 882 | ), |
| 883 | ), |
| 884 | )) |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | /// The command palette's plate width (DESIGN §4.4): wide enough for a label, |
| 889 | /// its category and its key on one 32px row. |
| 890 | const PALETTE_W: f32 = 560.; |
| 891 | /// One palette row, the click target the palette is built from. |
| 892 | const PALETTE_ROW_H: f32 = 36.; |
| 893 | /// A category heading in the grouped (empty-query) list. |
| 894 | const PALETTE_HEAD_H: f32 = 24.; |
| 895 | /// How many rows show before the list scrolls. |
| 896 | /// How long the pointer rests on the Adjust row before its submenu opens. |
| 897 | const ADJUST_HOVER: Duration = Duration::from_millis(150); |
| 898 | |
| 899 | /// Whether `p` lies inside the triangle `a`, `b`, `c` -- the corridor from |
| 900 | /// where the pointer left a menu row to the two near corners of its submenu. |
| 901 | pub(crate) fn in_aim_triangle(p: Point<Pixels>, a: Point<Pixels>, b: Point<Pixels>, c: Point<Pixels>) -> bool { |
| 902 | let f = |p: Point<Pixels>| (f32::from(p.x), f32::from(p.y)); |
| 903 | let (p, a, b, c) = (f(p), f(a), f(b), f(c)); |
| 904 | let side = |u: (f32, f32), v: (f32, f32)| (p.0 - v.0) * (u.1 - v.1) - (u.0 - v.0) * (p.1 - v.1); |
| 905 | let (d1, d2, d3) = (side(a, b), side(b, c), side(c, a)); |
| 906 | let neg = d1 < 0. || d2 < 0. || d3 < 0.; |
| 907 | let pos = d1 > 0. || d2 > 0. || d3 > 0.; |
| 908 | !(neg && pos) |
| 909 | } |
| 910 | |
| 911 | const PALETTE_ROWS_SHOWN: f32 = 10.; |
| 912 | |
| 913 | impl Player { |
| 914 | /// The command palette (DESIGN §4.4, Ctrl+K): a centred plate near the top |
| 915 | /// of the window, over everything, with a field that has the keyboard and a |
| 916 | /// list of every action under it. Click away or escape closes it; the keys |
| 917 | /// are the root handler's (`ui::stance`). |
| 918 | pub(crate) fn command_palette( |
| 919 | &self, |
| 920 | viewport: Size<Pixels>, |
| 921 | cx: &mut Context<Self>, |
| 922 | ) -> Option<impl IntoElement> { |
| 923 | if !self.palette_open { |
| 924 | return None; |
| 925 | } |
| 926 | let rows = self.palette_list(); |
| 927 | let grouped = self.palette_query.trim().is_empty(); |
| 928 | let sel = self.palette_sel; |
| 929 | let mut list: Vec<AnyElement> = Vec::new(); |
| 930 | let mut last_category = None; |
| 931 | for (i, row) in rows.iter().enumerate() { |
| 932 | let category = row.action.category(); |
| 933 | if grouped && last_category != Some(category) { |
| 934 | list.push( |
| 935 | div() |
| 936 | .flex_none() |
| 937 | .h(px(PALETTE_HEAD_H)) |
| 938 | .px(px(10.)) |
| 939 | .flex() |
| 940 | .items_end() |
| 941 | .pb(px(2.)) |
| 942 | .type_style(type_scale::section()) |
| 943 | .text_color(rgb(crate::ui::theme::tokens().ink2)) |
| 944 | .child(category.label()) |
| 945 | .into_any_element(), |
| 946 | ); |
| 947 | last_category = Some(category); |
| 948 | } |
| 949 | list.push(self.palette_row(i, *row, i == sel, grouped, cx)); |
| 950 | } |
| 951 | if rows.is_empty() { |
| 952 | list.push( |
| 953 | div() |
| 954 | .flex_none() |
| 955 | .flex() |
| 956 | .flex_col() |
| 957 | .gap(px(2.)) |
| 958 | .px(px(10.)) |
| 959 | .py(px(8.)) |
| 960 | .child( |
| 961 | div() |
| 962 | .type_style(type_scale::label(type_scale::LABEL_ROW_PX, gpui::FontWeight::MEDIUM)) |
| 963 | .text_color(rgb(INK2())) |
| 964 | .child("No command matches"), |
| 965 | ) |
| 966 | .child( |
| 967 | div() |
| 968 | .type_style(type_scale::caption()) |
| 969 | .text_color(rgb(INK3())) |
| 970 | .child("Try a shorter word, or clear the search"), |
| 971 | ) |
| 972 | .into_any_element(), |
| 973 | ); |
| 974 | } |
| 975 | let query = self.palette_query.clone(); |
| 976 | let field = div() |
| 977 | .flex_none() |
| 978 | .h(px(44.)) |
| 979 | .border_b_1() |
| 980 | .border_color(rgba(crate::ui::theme::tokens().line)) |
| 981 | .flex() |
| 982 | .items_center() |
| 983 | .gap(px(8.)) |
| 984 | .px(px(10.)) |
| 985 | .child(crate::ui::icons::Icon::Search.el(crate::ui::icons::IconSize::Inline, crate::ui::theme::tokens().ink3)) |
| 986 | .child( |
| 987 | div() |
| 988 | .flex_1() |
| 989 | .min_w(px(0.)) |
| 990 | .truncate() |
| 991 | .type_style(type_scale::emph()) |
| 992 | .text_color(rgb(if query.is_empty() { crate::ui::theme::tokens().ink3 } else { crate::ui::theme::tokens().ink1 })) |
| 993 | .child(match query.is_empty() { |
| 994 | true => "Search commands".to_string(), |
| 995 | // The same caret every text field draws (a thin block after the text). |
| 996 | false => format!("{query}\u{258f}"), |
| 997 | }), |
| 998 | ); |
| 999 | let list_h = PALETTE_ROWS_SHOWN * PALETTE_ROW_H; |
| 1000 | let plate_x = ((f32::from(viewport.width) - PALETTE_W) / 2.).max(MENU_EDGE); |
| 1001 | let plate_y = (f32::from(viewport.height) / 6.).max(MENU_EDGE); |
| 1002 | Some( |
| 1003 | scrim() |
| 1004 | // Click away closes it, either button, and the press goes no |
| 1005 | // further -- the picker's rule. |
| 1006 | .on_mouse_down( |
| 1007 | MouseButton::Left, |
| 1008 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 1009 | this.palette_open = false; |
| 1010 | cx.notify(); |
| 1011 | cx.stop_propagation(); |
| 1012 | }), |
| 1013 | ) |
| 1014 | .on_mouse_down( |
| 1015 | MouseButton::Right, |
| 1016 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 1017 | this.palette_open = false; |
| 1018 | cx.notify(); |
| 1019 | cx.stop_propagation(); |
| 1020 | }), |
| 1021 | ) |
| 1022 | .child( |
| 1023 | crate::ui::kit::plate(8.) |
| 1024 | .id("command-palette") |
| 1025 | .absolute() |
| 1026 | .left(px(plate_x)) |
| 1027 | .top(px(plate_y)) |
| 1028 | .w(px(PALETTE_W)) |
| 1029 | .flex() |
| 1030 | .flex_col() |
| 1031 | .gap(px(4.)) |
| 1032 | .p(px(4.)) |
| 1033 | // Painted after the scrim, so a press on the plate is |
| 1034 | // its own and never closes the palette under the click. |
| 1035 | .on_mouse_down(MouseButton::Left, |_: &MouseDownEvent, _, cx| { |
| 1036 | cx.stop_propagation() |
| 1037 | }) |
| 1038 | .on_mouse_down(MouseButton::Right, |_: &MouseDownEvent, _, cx| { |
| 1039 | cx.stop_propagation() |
| 1040 | }) |
| 1041 | .child(field) |
| 1042 | .child( |
| 1043 | div() |
| 1044 | .id("palette-rows") |
| 1045 | .flex() |
| 1046 | .flex_col() |
| 1047 | .max_h(px(list_h)) |
| 1048 | .overflow_y_scroll() |
| 1049 | .track_scroll(&self.palette_scroll) |
| 1050 | .children(list), |
| 1051 | ), |
| 1052 | ), |
| 1053 | ) |
| 1054 | } |
| 1055 | |
| 1056 | /// One palette row: the label in ink1 (ink4 when refused), its category in |
| 1057 | /// ink3, and on the right its key in mono -- or, for a refused row, the |
| 1058 | /// reason it cannot run (DESIGN §4.8: the reason stays readable in ink3). |
| 1059 | fn palette_row( |
| 1060 | &self, |
| 1061 | i: usize, |
| 1062 | row: crate::menus::PaletteRow, |
| 1063 | picked: bool, |
| 1064 | grouped: bool, |
| 1065 | cx: &mut Context<Self>, |
| 1066 | ) -> AnyElement { |
| 1067 | let action = row.action; |
| 1068 | let refused = row.refused.is_some(); |
| 1069 | // The shortcut column reads as the menus' does: the strokes, nothing |
| 1070 | // for an action with none. A refused row is dimmed and says why on |
| 1071 | // hover, never as text in the row. |
| 1072 | let right = crate::ui::popup::shortcut(&self.keymap, action); |
| 1073 | let why = row.refused; |
| 1074 | div() |
| 1075 | .id(("palette-row", i)) |
| 1076 | .flex_none() |
| 1077 | .h(px(PALETTE_ROW_H)) |
| 1078 | .flex() |
| 1079 | .items_center() |
| 1080 | .justify_between() |
| 1081 | .gap(px(12.)) |
| 1082 | .px(px(10.)) |
| 1083 | .rounded(px(4.)) |
| 1084 | .cursor_pointer() |
| 1085 | .when(picked, |d| d.bg(rgba(crate::ui::theme::tokens().accent_wash))) |
| 1086 | .when(!picked, |d| d.hover(|s| s.bg(rgba(crate::ui::theme::WASH_HOVER)))) |
| 1087 | .when_some(why, |d, why| { |
| 1088 | d.tooltip(crate::ui::widgets::overlay_tip_hover(action.label(), "", Some(why))) |
| 1089 | }) |
| 1090 | .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { |
| 1091 | this.palette_sel = i; |
| 1092 | this.palette_run(window, cx); |
| 1093 | })) |
| 1094 | .child( |
| 1095 | div() |
| 1096 | .flex() |
| 1097 | .items_center() |
| 1098 | .gap(px(8.)) |
| 1099 | .min_w(px(0.)) |
| 1100 | .child( |
| 1101 | div().flex_none().w(px(16.)).h(px(16.)).children( |
| 1102 | crate::menus::icon(action).map(|i| { |
| 1103 | i.el( |
| 1104 | crate::ui::icons::IconSize::Inline, |
| 1105 | if refused { crate::ui::theme::tokens().ink4 } else { crate::ui::theme::tokens().ink2 }, |
| 1106 | ) |
| 1107 | }), |
| 1108 | ), |
| 1109 | ) |
| 1110 | .child( |
| 1111 | div() |
| 1112 | .min_w(px(0.)) |
| 1113 | .truncate() |
| 1114 | .type_style(type_scale::body_strong()) |
| 1115 | .text_color(rgb(if refused { crate::ui::theme::tokens().ink4 } else { crate::ui::theme::tokens().ink1 })) |
| 1116 | .child(action.label()), |
| 1117 | ) |
| 1118 | // Under a category heading the tag would only repeat it. |
| 1119 | .when(!grouped, |d| { |
| 1120 | d.child( |
| 1121 | div() |
| 1122 | .flex_none() |
| 1123 | .type_style(type_scale::caption()) |
| 1124 | .text_color(rgb(crate::ui::theme::tokens().ink3)) |
| 1125 | .child(action.category().label()), |
| 1126 | ) |
| 1127 | }), |
| 1128 | ) |
| 1129 | .child(match right.is_empty() { |
| 1130 | true => div().flex_none(), |
| 1131 | false => crate::ui::kit::keycaps(&right), |
| 1132 | }) |
| 1133 | .into_any_element() |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | /// Where a list opened from a Settings row hangs: just under that row (a click |
| 1138 | /// anywhere on it lands within half a row of its middle), or above the row |
| 1139 | /// when the room under it does not hold the whole list. |
| 1140 | pub(crate) fn settings_anchor(click: Point<Pixels>, viewport: Size<Pixels>, h: f32) -> Point<Pixels> { |
| 1141 | let below = f32::from(click.y) + crate::layout::CONTROL_H / 2.; |
| 1142 | match below + h + crate::layout::MENU_EDGE <= f32::from(viewport.height) { |
| 1143 | true => point(click.x, px(below)), |
| 1144 | // The list's own top, so it ends where the row begins. |
| 1145 | false => point(click.x, px((f32::from(click.y) - crate::layout::CONTROL_H / 2. - h).max(0.))), |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | #[cfg(test)] |
| 1150 | mod aim_tests { |
| 1151 | use super::*; |
| 1152 | |
| 1153 | #[test] |
| 1154 | fn a_diagonal_move_toward_the_submenu_stays_inside_the_safe_triangle() { |
| 1155 | // The pointer left the Adjust row at (100, 200); the submenu's near |
| 1156 | // edge is x = 300, from y 180 to 420. |
| 1157 | let (a, b, c) = ( |
| 1158 | point(px(100.), px(200.)), |
| 1159 | point(px(300.), px(180.)), |
| 1160 | point(px(300.), px(420.)), |
| 1161 | ); |
| 1162 | assert!(in_aim_triangle(point(px(200.), px(260.)), a, b, c)); |
| 1163 | assert!(in_aim_triangle(point(px(290.), px(400.)), a, b, c)); |
| 1164 | // Straight down the plate, or back up, leaves it. |
| 1165 | assert!(!in_aim_triangle(point(px(100.), px(300.)), a, b, c)); |
| 1166 | assert!(!in_aim_triangle(point(px(150.), px(120.)), a, b, c)); |
| 1167 | // Same answer whichever way the corners are wound. |
| 1168 | assert!(in_aim_triangle(point(px(200.), px(260.)), a, c, b)); |
| 1169 | } |
| 1170 | |
| 1171 | #[test] |
| 1172 | fn the_adjust_submenu_opens_after_about_150_ms() { |
| 1173 | assert_eq!(ADJUST_HOVER, Duration::from_millis(150)); |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | #[cfg(test)] |
| 1178 | mod settings_anchor_tests { |
| 1179 | use super::*; |
| 1180 | |
| 1181 | #[test] |
| 1182 | fn a_settings_list_hangs_under_its_row_or_above_it_and_stays_in_the_window() { |
| 1183 | let vp = size(px(1280.), px(720.)); |
| 1184 | let under = settings_anchor(point(px(600.), px(192.)), vp, 120.); |
| 1185 | assert_eq!(f32::from(under.y), 192. + crate::layout::CONTROL_H / 2.); |
| 1186 | let flipped = settings_anchor(point(px(600.), px(500.)), vp, 300.); |
| 1187 | let (_, y) = crate::oracle::menu_at_w(crate::ui::stance::menu_above(flipped, vp, 0., 300.), vp, 200., 300.); |
| 1188 | assert!(y + 300. <= 500. - crate::layout::CONTROL_H / 2., "the flipped list ends above the row, got y={y}"); |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | impl Player { |
| 1193 | /// Why a gap on `lane` cannot be closed right now, in words; `None` when it can. The |
| 1194 | /// Close gap row and button show this as their tooltip and ignore the click while it holds. |
| 1195 | pub(crate) fn close_gap_refusal(&self, lane: Lane) -> Option<&'static str> { |
| 1196 | if self.exporting().is_some() { |
| 1197 | return Some("Wait for the export to finish"); |
| 1198 | } |
| 1199 | if self.session.as_ref().is_some_and(|s| s.lane_locked(lane)) { |
| 1200 | return Some("This track is locked: unlock it to close its gaps"); |
| 1201 | } |
| 1202 | None |
| 1203 | } |
| 1204 | } |