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 transform card: where a clip sits on the canvas and how it is turned. |
| 2 | //! Position, scale and rotation are the main rows; the crop rows and their |
| 3 | //! diagram sit under `More …`. |
| 4 | |
| 5 | use super::common::*; |
| 6 | use crate::player::cards::{PIP_SCALE, TRANSFORM_MAIN_BANDS, is_position_band}; |
| 7 | use crate::ui::icons::{Icon, IconSize}; |
| 8 | use crate::ui::kit::{self, ButtonSize, ButtonStyle, Segment}; |
| 9 | use crate::ui::type_scale::{self, Typeset}; |
| 10 | use crate::ui::widgets::*; |
| 11 | use crate::transform_ui::{SCALE_BAND, TRANSFORM_BANDS, transform_snap}; |
| 12 | use crate::*; |
| 13 | use engine::project::KeyKind; |
| 14 | use engine::stabilise::{Crop, Stabilise, Status}; |
| 15 | |
| 16 | /// Each row's slider id, in [`TRANSFORM_BANDS`] order. |
| 17 | const TRANSFORM_IDS: [&str; 8] = [ |
| 18 | "transform-pos-x", |
| 19 | "transform-pos-y", |
| 20 | "transform-scale", |
| 21 | "transform-rotate", |
| 22 | "transform-crop-l", |
| 23 | "transform-crop-r", |
| 24 | "transform-crop-t", |
| 25 | "transform-crop-b", |
| 26 | ]; |
| 27 | |
| 28 | /// The rotation chips, in the order the card lists them, with the degrees each |
| 29 | /// one sets. `−90` goes through [`transform_snap`], which stores it as 270. |
| 30 | const ROTATE_CHIPS: [(&str, f32); 4] = [("−90°", -90.), ("0°", 0.), ("+90°", 90.), ("180°", 180.)]; |
| 31 | |
| 32 | /// The Place row's choices, in the order the row reads. |
| 33 | const PLACE_CORNERS: [(&str, engine::transform::Corner); 5] = [ |
| 34 | ("Centre", engine::transform::Corner::Centre), |
| 35 | ("Top left", engine::transform::Corner::TopLeft), |
| 36 | ("Top right", engine::transform::Corner::TopRight), |
| 37 | ("Bottom left", engine::transform::Corner::BottomLeft), |
| 38 | ("Bottom right", engine::transform::Corner::BottomRight), |
| 39 | ]; |
| 40 | |
| 41 | /// The crop diagram's box. Its own size, not the canvas's: the kept rectangle's |
| 42 | /// shape is what it shows. |
| 43 | const CROP_DIAGRAM_W: f32 = 120.; |
| 44 | const CROP_DIAGRAM_H: f32 = 68.; |
| 45 | |
| 46 | /// One band's value in a copy of `params`. |
| 47 | fn band_value(params: TransformParams, band: usize) -> f32 { |
| 48 | let mut read = params; |
| 49 | *transform_band_mut(&mut read, band) |
| 50 | } |
| 51 | |
| 52 | /// What a row reads on the card. Position is whole pixels of the canvas (the |
| 53 | /// engine keeps a fraction of it), scale is a percent of the clip's own size, |
| 54 | /// rotation reads 270 back as the −90 it was set from, and crop is a percent of |
| 55 | /// the frame. |
| 56 | pub(crate) fn band_readout(band: usize, value: f32, canvas: (u32, u32)) -> String { |
| 57 | if is_position_band(band) { |
| 58 | let span = (if band == 0 { canvas.0 } else { canvas.1 }) as f32; |
| 59 | // `+ 0.0` turns a rounded -0.0 into 0, so a centred row reads "0 px". |
| 60 | let px = (value * span).round() + 0.0; |
| 61 | format!("{px:.0} px") |
| 62 | } else if band == SCALE_BAND { |
| 63 | format!("{:.0}%", value * 100.) |
| 64 | } else if band == ROTATE_BAND && value == 270. { |
| 65 | "−90°".to_owned() |
| 66 | } else { |
| 67 | transform_row_value(band, value) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// The crop diagram: the frame, with the rectangle the crop keeps drawn inside |
| 72 | /// it. The kept share is what is left after the four crops, so it is never |
| 73 | /// empty (each crop is at most 45%). |
| 74 | fn crop_diagram(params: TransformParams) -> impl IntoElement { |
| 75 | let kept_w = 1. - params.crop_l - params.crop_r; |
| 76 | let kept_h = 1. - params.crop_t - params.crop_b; |
| 77 | div() |
| 78 | .flex_none() |
| 79 | .w(px(CROP_DIAGRAM_W)) |
| 80 | .h(px(CROP_DIAGRAM_H)) |
| 81 | .relative() |
| 82 | .rounded(px(2.)) |
| 83 | .border_1() |
| 84 | .border_color(rgba(DARK_SEAM())) |
| 85 | .bg(rgb(DARK_PANEL())) |
| 86 | .child( |
| 87 | div() |
| 88 | .absolute() |
| 89 | .left(relative(params.crop_l)) |
| 90 | .top(relative(params.crop_t)) |
| 91 | .w(relative(kept_w)) |
| 92 | .h(relative(kept_h)) |
| 93 | .border_1() |
| 94 | .border_color(rgb(INK3())) |
| 95 | .bg(rgb(DARK_RAISED())), |
| 96 | ) |
| 97 | } |
| 98 | |
| 99 | /// The strength chips, with the strength each one sets. |
| 100 | /// The deinterlace choices the Properties row offers, in the order they read. |
| 101 | const DEINTERLACE_CHIPS: [(&str, engine::project::Deinterlace, &str); 5] = [ |
| 102 | ("Frame", engine::project::Deinterlace::Frame, "Smooth: blends the two fields"), |
| 103 | ("Field", engine::project::Deinterlace::Field, "Sharp: doubles the motion"), |
| 104 | ("Off", engine::project::Deinterlace::Off, "Leave the lines as they are"), |
| 105 | ("Film (auto)", engine::project::Deinterlace::IvtcAuto, "Back to 24 film frames; finds the pattern itself"), |
| 106 | ("Film", engine::project::Deinterlace::Ivtc, "Back to 24 film frames from 30 fps TV"), |
| 107 | ]; |
| 108 | const STRENGTH_CHIPS: [(&str, u8); 3] = [("Light", 25), ("Medium", 50), ("Strong", 80)]; |
| 109 | |
| 110 | /// The edge chips: what the corrected frame does at its edges. |
| 111 | const EDGE_CHIPS: [(&str, Crop); 2] = [("Zoom in", Crop::Zoom), ("Full frame", Crop::Full)]; |
| 112 | |
| 113 | /// What the analysis line says. |
| 114 | pub(crate) fn stabilise_words(status: &Status, zoom: Option<f64>) -> String { |
| 115 | match status { |
| 116 | Status::Running(f) => format!("Analysing {:.0}%", f * 100.), |
| 117 | Status::Ready => match zoom { |
| 118 | Some(z) if z > 1.0005 => format!("Steadied · enlarged {:.0}% to hide the edges", (z - 1.) * 100.), |
| 119 | _ => "Steadied".to_owned(), |
| 120 | }, |
| 121 | Status::Failed(e) if e.contains("cancelled") => "Stopped — plays as shot until measured".to_owned(), |
| 122 | Status::Failed(e) => format!("Could not measure the shake: {e}"), |
| 123 | Status::Missing => "Not measured yet".to_owned(), |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// Reset all: the whole placement back to identity. |
| 128 | fn reset_transform(this: &mut Player, cx: &mut Context<Player>) { |
| 129 | this.set_transform(TransformParams::default(), cx); |
| 130 | } |
| 131 | |
| 132 | impl Player { |
| 133 | /// The Stabilise section: an on/off row, then (when on) how strong and what |
| 134 | /// the edges do, and where the shake measurement stands. `None` for a clip |
| 135 | /// with no camera to steady (a still, a song). |
| 136 | fn transform_stabilise(&self, lane: Lane, idx: usize, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 137 | let session = self.session.as_ref()?; |
| 138 | let (path, lo, hi) = session.stabilise_range(lane, idx)?; |
| 139 | let st = session.stabilise_of(lane, idx); |
| 140 | let on = st.is_some(); |
| 141 | let me = cx.entity(); |
| 142 | let toggle = kit::switch_row( |
| 143 | "transform-stabilise", |
| 144 | "Stabilise", |
| 145 | Some("Take the camera shake out of this clip".into()), |
| 146 | on, |
| 147 | None, |
| 148 | move |_, app| { |
| 149 | me.update(app, |this, cx| { |
| 150 | let next = if on { None } else { Some(Stabilise::default()) }; |
| 151 | this.write_stabilise(lane, idx, next, cx); |
| 152 | }); |
| 153 | }, |
| 154 | ); |
| 155 | let mut section = div().id("transform-stabilise-section").w_full().flex().flex_col().min_w(px(0.)).overflow_hidden().gap(px(4.)).px(px(6.)).child(toggle); |
| 156 | let Some(st) = st else { |
| 157 | return Some(section.into_any_element()); |
| 158 | }; |
| 159 | let label = |text: &'static str| div().type_style(type_scale::body()).text_color(rgb(INK2())).child(text); |
| 160 | let me = cx.entity(); |
| 161 | let strength = kit::segmented_equal( |
| 162 | "transform-stabilise-strength", |
| 163 | &STRENGTH_CHIPS.map(|(text, _)| Segment::text(text)), |
| 164 | STRENGTH_CHIPS.iter().position(|&(_, v)| v == st.strength()).unwrap_or(usize::MAX), |
| 165 | true, |
| 166 | None, |
| 167 | move |pick, _, app| { |
| 168 | me.update(app, |this, cx| { |
| 169 | this.write_stabilise(lane, idx, Some(Stabilise::with_percent(STRENGTH_CHIPS[pick].1, st.crop())), cx); |
| 170 | }); |
| 171 | }, |
| 172 | ); |
| 173 | let me = cx.entity(); |
| 174 | let edges = kit::segmented_equal( |
| 175 | "transform-stabilise-edges", |
| 176 | &EDGE_CHIPS.map(|(text, _)| Segment::text(text)), |
| 177 | EDGE_CHIPS.iter().position(|&(_, c)| c == st.crop()).unwrap_or(usize::MAX), |
| 178 | true, |
| 179 | None, |
| 180 | move |pick, _, app| { |
| 181 | me.update(app, |this, cx| { |
| 182 | this.write_stabilise(lane, idx, Some(Stabilise::with_percent(st.strength(), EDGE_CHIPS[pick].1)), cx); |
| 183 | }); |
| 184 | }, |
| 185 | ); |
| 186 | let status = engine::stabilise::status(&path, lo, hi); |
| 187 | let zoom = matches!(status, Status::Ready) |
| 188 | .then(|| engine::stabilise::correction(&path, lo, hi, st, session.meta().frame_rate).map(|c| c.zoom)) |
| 189 | .flatten(); |
| 190 | let action: Option<AnyElement> = match status { |
| 191 | Status::Running(_) => Some( |
| 192 | dark_ghost_button( |
| 193 | "transform-stabilise-cancel", |
| 194 | "Stop", |
| 195 | "", |
| 196 | false, |
| 197 | cx.listener(move |this, _: &ClickEvent, _, cx| this.cancel_stabilise(lane, idx, cx)), |
| 198 | ) |
| 199 | .into_any_element(), |
| 200 | ), |
| 201 | Status::Failed(_) | Status::Missing => Some( |
| 202 | dark_ghost_button( |
| 203 | "transform-stabilise-retry", |
| 204 | "Measure", |
| 205 | "", |
| 206 | false, |
| 207 | cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 208 | this.wait_stabilise(lane, idx); |
| 209 | cx.notify(); |
| 210 | }), |
| 211 | ) |
| 212 | .into_any_element(), |
| 213 | ), |
| 214 | Status::Ready => None, |
| 215 | }; |
| 216 | section = section |
| 217 | .child(label("Strength")) |
| 218 | .child(strength) |
| 219 | .child(label("Edges")) |
| 220 | .child(edges) |
| 221 | .child( |
| 222 | div() |
| 223 | .flex() |
| 224 | .justify_between() |
| 225 | .items_center() |
| 226 | .gap(px(8.)) |
| 227 | // The Stop / Measure button comes and goes with the analysis; |
| 228 | // the row keeps its height either way. |
| 229 | .min_h(px(CONTROL_H)) |
| 230 | .child(dark_help(stabilise_words(&status, zoom)).flex_1().w_auto()) |
| 231 | .children(action), |
| 232 | ); |
| 233 | Some(section.into_any_element()) |
| 234 | } |
| 235 | |
| 236 | /// The field order (`tff` / `bff`) of the clip's source, `None` for a progressive one, one |
| 237 | /// not probed yet, and anything that is not footage. |
| 238 | pub(crate) fn clip_interlaced(&self, lane: Lane, idx: usize) -> Option<&'static str> { |
| 239 | let (path, _, _) = self.session.as_ref()?.stabilise_range(lane, idx)?; |
| 240 | self.facts.get(&path)?.as_ref()?.video.as_ref()?.interlaced |
| 241 | } |
| 242 | |
| 243 | /// The Deinterlace row: Frame / Field / Off, only for a clip whose source is interlaced. |
| 244 | fn transform_deinterlace(&self, lane: Lane, idx: usize, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 245 | let order = self.clip_interlaced(lane, idx)?; |
| 246 | let order_why = if order == "tff" { |
| 247 | "This video's lines come top field first" |
| 248 | } else { |
| 249 | "This video's lines come bottom field first" |
| 250 | }; |
| 251 | let current = self.session.as_ref()?.deinterlace_of(lane, idx); |
| 252 | let at = DEINTERLACE_CHIPS.iter().position(|&(_, m, _)| m == current).unwrap_or(0); |
| 253 | let open = self.deinterlace_open; |
| 254 | let field = kit::dropdown_field("transform-deinterlace", DEINTERLACE_CHIPS[at].0, true) |
| 255 | .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 256 | this.deinterlace_open = !this.deinterlace_open; |
| 257 | cx.notify(); |
| 258 | })); |
| 259 | let list = open.then(|| { |
| 260 | let t = crate::ui::theme::tokens(); |
| 261 | let backdrop = div().absolute().top(px(-4000.)).left(px(-4000.)).size(px(8000.)).on_mouse_down( |
| 262 | MouseButton::Left, |
| 263 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 264 | this.deinterlace_open = false; |
| 265 | cx.stop_propagation(); |
| 266 | cx.notify(); |
| 267 | }), |
| 268 | ); |
| 269 | let mut plate = kit::plate(6.).id("transform-deinterlace-list").w(px(280.)).p(px(crate::layout::MENU_PAD)).flex().flex_col().occlude(); |
| 270 | for (i, &(name, mode, why)) in DEINTERLACE_CHIPS.iter().enumerate() { |
| 271 | plate = plate.child( |
| 272 | div() |
| 273 | .id(("transform-deinterlace-row", i)) |
| 274 | .flex_none() |
| 275 | .px(px(8.)) |
| 276 | .py(px(4.)) |
| 277 | .flex() |
| 278 | .items_start() |
| 279 | .gap(px(8.)) |
| 280 | .rounded(px(4.)) |
| 281 | .cursor_pointer() |
| 282 | .hover(|s| s.bg(rgba(crate::ui::theme::WASH_HOVER))) |
| 283 | .child(div().flex_none().size(px(16.)).children((i == at).then(|| Icon::Check.el(IconSize::Inline, t.accent)))) |
| 284 | .child( |
| 285 | div() |
| 286 | .flex_1() |
| 287 | .min_w(px(0.)) |
| 288 | .flex() |
| 289 | .flex_col() |
| 290 | .child(div().type_style(type_scale::body_strong()).text_color(rgb(t.ink1)).child(name)) |
| 291 | .child(dark_help(why)), |
| 292 | ) |
| 293 | .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { |
| 294 | this.deinterlace_open = false; |
| 295 | this.write_deinterlace(lane, idx, mode, cx); |
| 296 | })), |
| 297 | ); |
| 298 | } |
| 299 | gpui::deferred( |
| 300 | div() |
| 301 | .absolute() |
| 302 | .top(px(0.)) |
| 303 | .right(px(0.)) |
| 304 | .child(backdrop) |
| 305 | .child(div().absolute().top(px(crate::layout::redesign::CONTROL_M + 2.)).right(px(0.)).child(plate)), |
| 306 | ) |
| 307 | }); |
| 308 | Some( |
| 309 | div() |
| 310 | .id("transform-deinterlace-section") |
| 311 | .w_full() |
| 312 | .flex() |
| 313 | .flex_col() |
| 314 | .min_w(px(0.)) |
| 315 | .gap(px(4.)) |
| 316 | .pt(px(4.)) |
| 317 | .px(px(6.)) |
| 318 | .child( |
| 319 | div() |
| 320 | .flex() |
| 321 | .items_center() |
| 322 | .justify_between() |
| 323 | .gap(px(8.)) |
| 324 | .child(div().type_style(type_scale::body()).text_color(rgb(INK2())).id("transform-deinterlace-label").tooltip(crate::ui::widgets::overlay_tip_hover("Deinterlace", "", Some(order_why))).child("Deinterlace")) |
| 325 | .child(div().relative().flex_none().w(px(150.)).child(field).children(list)), |
| 326 | ) |
| 327 | // Always two lines tall, so the longest line never moves what is below. |
| 328 | .child(dark_help(DEINTERLACE_CHIPS[at].2).min_h(px(30.))) |
| 329 | .into_any_element(), |
| 330 | ) |
| 331 | } |
| 332 | |
| 333 | /// The Reframe row: one click follows the subject and keys the placement so the |
| 334 | /// clip fills the project ([`engine::reframe`]). It is an action, not a switch: while |
| 335 | /// it measures the button reads Stop and the caption shows the percent; once it has |
| 336 | /// landed on this clip the caption says how many keys it wrote. `None` for a clip with no footage. |
| 337 | fn transform_reframe(&self, lane: Lane, idx: usize, cx: &mut Context<Self>) -> Option<AnyElement> { |
| 338 | let session = self.session.as_ref()?; |
| 339 | session.stabilise_range(lane, idx)?; |
| 340 | let running = self.reframe_run.is_some(); |
| 341 | let caption = match (self.reframe_words(), self.reframed) { |
| 342 | (Some(words), _) => words, |
| 343 | (None, Some((l, i, n))) if l == lane && i == idx => { |
| 344 | format!("Reframed with {n} key{}", if n == 1 { "" } else { "s" }) |
| 345 | } |
| 346 | (None, _) => "Follows the subject so this clip fills the project".to_string(), |
| 347 | }; |
| 348 | let me = cx.entity(); |
| 349 | let button = kit::Button::new("transform-reframe") |
| 350 | .label(if running { "Stop reframing" } else { "Reframe to fill the frame" }) |
| 351 | .icon(Icon::Scan) |
| 352 | .style(ButtonStyle::Secondary) |
| 353 | .size(ButtonSize::Small) |
| 354 | .build() |
| 355 | .w_full() |
| 356 | .justify_center() |
| 357 | .on_click(move |_, _, app| { |
| 358 | me.update(app, |this, cx| { |
| 359 | if this.reframe_run.is_some() { |
| 360 | this.cancel_reframe(cx); |
| 361 | } else { |
| 362 | this.start_reframe(lane, idx, cx); |
| 363 | } |
| 364 | }); |
| 365 | }); |
| 366 | Some( |
| 367 | div() |
| 368 | .w_full() |
| 369 | .px(px(6.)) |
| 370 | .flex() |
| 371 | .flex_col() |
| 372 | .gap(px(4.)) |
| 373 | .child(button) |
| 374 | .child(div().type_style(type_scale::caption()).text_color(rgb(INK3())).child(caption)) |
| 375 | .into_any_element(), |
| 376 | ) |
| 377 | } |
| 378 | |
| 379 | /// The transform card: [`color_card`](Self::color_card)'s own shape, the |
| 380 | /// placement's rows and their rotation chips, and the crop under a More |
| 381 | /// fold. There is no histogram -- nothing here a graded frame would tilt. |
| 382 | pub(crate) fn transform_card( |
| 383 | &self, |
| 384 | viewport: Size<Pixels>, |
| 385 | cx: &mut Context<Self>, |
| 386 | ) -> Option<impl IntoElement> { |
| 387 | let (lane, idx) = self.transform_open?; |
| 388 | let params = self.transform_params(); |
| 389 | let canvas = self.transform_canvas(); |
| 390 | let crop_open = self.transform_crop_open(); |
| 391 | // Rotation has its own row of quarter turns below, not a bar. |
| 392 | let mut rows: Vec<AnyElement> = (0..TRANSFORM_MAIN_BANDS) |
| 393 | .filter(|&band| band != ROTATE_BAND) |
| 394 | .map(|band| self.transform_slider(band, params, canvas, cx)) |
| 395 | .collect(); |
| 396 | rows.push(self.transform_rotation(params, canvas, cx)); |
| 397 | rows.push(self.transform_place(params, cx)); |
| 398 | rows.push(self.opacity_row(params.opacity, cx)); |
| 399 | rows.push(self.key_row(KeyKind::Placement, cx)); |
| 400 | rows.push(self.key_row(KeyKind::Opacity, cx)); |
| 401 | if let Some(row) = self.transform_stabilise(lane, idx, cx) { |
| 402 | rows.push(row); |
| 403 | } |
| 404 | if let Some(row) = self.transform_deinterlace(lane, idx, cx) { |
| 405 | rows.push(row); |
| 406 | } |
| 407 | if let Some(row) = self.transform_reframe(lane, idx, cx) { |
| 408 | rows.push(row); |
| 409 | } |
| 410 | rows.push( |
| 411 | disclosure_row( |
| 412 | "transform-more", |
| 413 | "Crop", |
| 414 | crop_open, |
| 415 | |this: &mut Player, cx: &mut Context<Player>| this.toggle_transform_crop(cx), |
| 416 | cx, |
| 417 | ) |
| 418 | .into_any_element(), |
| 419 | ); |
| 420 | if crop_open { |
| 421 | rows.push(self.transform_crop(params, canvas, cx)); |
| 422 | } |
| 423 | let help_text = "drag a bar, or ↑↓ picks one and ←→ moves it (rotation steps by 90°, \ |
| 424 | position by 10 px), r resets — a click away or esc closes"; |
| 425 | // The head's Reset all shows only once the placement is off identity. |
| 426 | let reset_all: Option<fn(&mut Player, &mut Context<Player>)> = if params.is_identity() { |
| 427 | None |
| 428 | } else { |
| 429 | Some(reset_transform as fn(&mut Player, &mut Context<Player>)) |
| 430 | }; |
| 431 | Some( |
| 432 | self.card_outer(viewport, cx) |
| 433 | .child( |
| 434 | div() |
| 435 | .w_full() |
| 436 | .max_w(px(card_max_w( |
| 437 | TRANSFORM_W, |
| 438 | self.card_maximized, |
| 439 | f32::from(viewport.width), |
| 440 | ))) |
| 441 | .on_mouse_down(MouseButton::Left, swallow) |
| 442 | .flex() |
| 443 | .flex_col() |
| 444 | .gap(px(CARD_GAP)) |
| 445 | .map(|d| self.card_surface(d)) |
| 446 | .child( |
| 447 | div() |
| 448 | .id("transform-reset") |
| 449 | .w_full() |
| 450 | .flex() |
| 451 | .justify_end() |
| 452 | .child(dark_card_head_reset("", None, Some(help_text.into()), None, reset_all, cx)), |
| 453 | ) |
| 454 | .children(rows), |
| 455 | ), |
| 456 | ) |
| 457 | } |
| 458 | |
| 459 | /// One slider row: the shared [`slider_row`] for one band, under a tooltip |
| 460 | /// that names it. |
| 461 | fn transform_slider( |
| 462 | &self, |
| 463 | band: usize, |
| 464 | params: TransformParams, |
| 465 | canvas: (u32, u32), |
| 466 | cx: &mut Context<Self>, |
| 467 | ) -> AnyElement { |
| 468 | let (label, _, _) = TRANSFORM_BANDS[band]; |
| 469 | let value = band_value(params, band); |
| 470 | let default = band_value(TransformParams::default(), band); |
| 471 | let on_press: SliderPress<Player> = Box::new( |
| 472 | move |this: &mut Player, x: Pixels, cx: &mut Context<Player>| { |
| 473 | this.transform_band = band; |
| 474 | this.opacity_drag = false; |
| 475 | this.transform_dragging = true; |
| 476 | this.drag_transform(x, true, cx); |
| 477 | }, |
| 478 | ); |
| 479 | let on_reset: SliderReset<Player> = |
| 480 | Rc::new(move |this: &mut Player, cx: &mut Context<Player>| { |
| 481 | let old = this.transform_params(); |
| 482 | let mut params = old; |
| 483 | *transform_band_mut(&mut params, band) = default; |
| 484 | if band == SCALE_BAND { |
| 485 | params = this.scaled_in_place(old, params); |
| 486 | } |
| 487 | this.set_transform(params, cx); |
| 488 | }); |
| 489 | // Typed in the unit the row reads: pixels for position, percent for scale and crop. |
| 490 | let typed = (band != ROTATE_BAND).then(|| { |
| 491 | let (_, low, high) = TRANSFORM_BANDS[band]; |
| 492 | let scale = if is_position_band(band) { |
| 493 | (if band == 0 { canvas.0 } else { canvas.1 }).max(1) as f32 |
| 494 | } else { |
| 495 | 100. |
| 496 | }; |
| 497 | SliderTyped { |
| 498 | lo: f64::from(low * scale), |
| 499 | hi: f64::from(high * scale), |
| 500 | set: Rc::new(move |this: &mut Player, v: f64, cx: &mut Context<Player>| { |
| 501 | let old = this.transform_params(); |
| 502 | let mut params = old; |
| 503 | *transform_band_mut(&mut params, band) = (v / f64::from(scale)) as f32; |
| 504 | if band == SCALE_BAND { |
| 505 | params = this.scaled_in_place(old, params); |
| 506 | } |
| 507 | this.set_transform(params, cx); |
| 508 | }), |
| 509 | } |
| 510 | }); |
| 511 | let row = SliderRow { |
| 512 | id: TRANSFORM_IDS[band], |
| 513 | label: label.into(), |
| 514 | value: band_readout(band, value, canvas).into(), |
| 515 | fraction: knob_fraction(band, value), |
| 516 | changed: value != default, |
| 517 | bar: self.transform_bars[band].clone(), |
| 518 | on_press, |
| 519 | on_reset: Some(on_reset), |
| 520 | typed, |
| 521 | }; |
| 522 | // The ids are the ones the keys menu's reach points at: a row to pick, |
| 523 | // and a bar the arrows move. |
| 524 | div() |
| 525 | .id(("transform-row", band)) |
| 526 | .w_full() |
| 527 | .flex() |
| 528 | .flex_col() |
| 529 | .px(px(6.)) |
| 530 | .rounded(px(3.)) |
| 531 | .tooltip(overlay_tip_hover(label, "", None)) |
| 532 | .child( |
| 533 | div() |
| 534 | .id(("transform-bar", band)) |
| 535 | .w_full() |
| 536 | .flex() |
| 537 | .flex_col() |
| 538 | .child(slider_row(row, cx)), |
| 539 | ) |
| 540 | .into_any_element() |
| 541 | } |
| 542 | |
| 543 | /// Rotation: a read-out and four chips (−90, 0, +90, 180). Rotation has no |
| 544 | /// slider, because the engine only ever renders a quarter turn. |
| 545 | fn transform_rotation( |
| 546 | &self, |
| 547 | params: TransformParams, |
| 548 | canvas: (u32, u32), |
| 549 | cx: &mut Context<Self>, |
| 550 | ) -> AnyElement { |
| 551 | let value = band_value(params, ROTATE_BAND); |
| 552 | let me = cx.entity(); |
| 553 | let chips = kit::segmented_equal( |
| 554 | "transform-rotate-chip", |
| 555 | &ROTATE_CHIPS.map(|(text, _)| Segment::text(text)), |
| 556 | ROTATE_CHIPS.iter().position(|&(_, d)| value == transform_snap(ROTATE_BAND, d)).unwrap_or(usize::MAX), |
| 557 | true, |
| 558 | None, |
| 559 | move |pick, _, app| { |
| 560 | me.update(app, |this, cx| { |
| 561 | this.transform_band = ROTATE_BAND; |
| 562 | let mut params = this.transform_params(); |
| 563 | params.rotate = transform_snap(ROTATE_BAND, ROTATE_CHIPS[pick].1); |
| 564 | this.set_transform(params, cx); |
| 565 | cx.notify(); |
| 566 | }); |
| 567 | }, |
| 568 | ); |
| 569 | let reset = cx.entity(); |
| 570 | div() |
| 571 | .id("transform-rotation") |
| 572 | .w_full() |
| 573 | .flex() |
| 574 | .flex_col() |
| 575 | .gap(px(4.)) |
| 576 | .px(px(6.)) |
| 577 | .rounded(px(3.)) |
| 578 | .child( |
| 579 | div() |
| 580 | .flex() |
| 581 | .justify_between() |
| 582 | .items_center() |
| 583 | .child(div().type_style(type_scale::body()).text_color(rgb(INK2())).child("Rotation")) |
| 584 | // The value box sits where a slider row's does: its reset slot is 16px and |
| 585 | // 8px off the box (value_pill's is 24px and 2px off), so pull the box 2px right. |
| 586 | .child(div().mr(px(-2.)).child(typed_value_pill( |
| 587 | "transform-rotate-pill", |
| 588 | &band_readout(ROTATE_BAND, value, canvas), |
| 589 | None, |
| 590 | 56., |
| 591 | value != 0., |
| 592 | // Typed in degrees. The engine only turns a quarter at a time, so the |
| 593 | // number is snapped to the nearest quarter turn, as the chips are. |
| 594 | Some(SliderTyped { |
| 595 | // From -90, which reads the same turn as 270. |
| 596 | lo: -90., |
| 597 | hi: f64::from(TRANSFORM_BANDS[ROTATE_BAND].2), |
| 598 | set: Rc::new(|this: &mut Player, v: f64, cx: &mut Context<Player>| { |
| 599 | this.transform_band = ROTATE_BAND; |
| 600 | let mut params = this.transform_params(); |
| 601 | params.rotate = transform_snap(ROTATE_BAND, v as f32); |
| 602 | this.set_transform(params, cx); |
| 603 | cx.notify(); |
| 604 | }), |
| 605 | }), |
| 606 | move |_, app| { |
| 607 | reset.update(app, |this, cx| { |
| 608 | let mut params = this.transform_params(); |
| 609 | params.rotate = 0.; |
| 610 | this.set_transform(params, cx); |
| 611 | }); |
| 612 | }, |
| 613 | cx, |
| 614 | ))), |
| 615 | ) |
| 616 | .child(chips) |
| 617 | .into_any_element() |
| 618 | } |
| 619 | |
| 620 | /// The Place row: where the clip sits, as one of five corners. A dropdown |
| 621 | /// shows the corner the position matches (Custom when the position is |
| 622 | /// none of them) and lists the five; a Picture in picture button beside the label |
| 623 | /// sets bottom right at 30%. Disabled, with the reason as its tooltip, when |
| 624 | /// the clip has no picture size to place by. |
| 625 | fn transform_place(&self, params: TransformParams, cx: &mut Context<Self>) -> AnyElement { |
| 626 | let Some((lane, idx)) = self.transform_open else { |
| 627 | return div().into_any_element(); |
| 628 | }; |
| 629 | let size = self.clip_picture_size(lane, idx); |
| 630 | let canvas = self.transform_canvas(); |
| 631 | let current = size.and_then(|size| engine::transform::corner_of(¶ms, size, canvas, engine::transform::CORNER_MARGIN_PX)); |
| 632 | let picked = current.and_then(|c| PLACE_CORNERS.iter().position(|&(_, k)| k == c)).unwrap_or(usize::MAX); |
| 633 | let why = size.is_none().then_some("This clip has no picture size to place by"); |
| 634 | let enabled = why.is_none(); |
| 635 | // A position that is no named corner (after Auto reframe, or dragged) reads Custom. |
| 636 | let value = PLACE_CORNERS.get(picked).map_or("Custom", |&(name, _)| name); |
| 637 | let field = kit::dropdown_field("transform-place", value, enabled).when(enabled, |f| { |
| 638 | f.on_click(cx.listener(|this, _: &ClickEvent, _, cx| { |
| 639 | this.place_open = !this.place_open; |
| 640 | cx.notify(); |
| 641 | })) |
| 642 | }); |
| 643 | let list = (self.place_open && enabled).then(|| { |
| 644 | let backdrop = div().absolute().top(px(-4000.)).left(px(-4000.)).size(px(8000.)).on_mouse_down( |
| 645 | MouseButton::Left, |
| 646 | cx.listener(|this, _: &MouseDownEvent, _, cx| { |
| 647 | this.place_open = false; |
| 648 | cx.stop_propagation(); |
| 649 | cx.notify(); |
| 650 | }), |
| 651 | ); |
| 652 | let names: Vec<SharedString> = PLACE_CORNERS.iter().map(|&(name, _)| name.into()).collect(); |
| 653 | // The list opens above the field when the window has no room below it. |
| 654 | let above = kit::list_opens_above("transform-place", names.len()); |
| 655 | let me = cx.entity(); |
| 656 | let choices = kit::choice_list("transform-place-list", 200., &names, picked, move |pick, _, app| { |
| 657 | me.update(app, |this, cx| { |
| 658 | this.place_open = false; |
| 659 | this.place_in_corner(PLACE_CORNERS[pick].1, None, cx); |
| 660 | }); |
| 661 | }); |
| 662 | let gap = px(crate::layout::redesign::CONTROL_M + 2.); |
| 663 | let at = div().absolute().right(px(0.)).child(choices); |
| 664 | let at = if above { at.bottom(gap) } else { at.top(gap) }; |
| 665 | gpui::deferred(div().absolute().top(px(0.)).right(px(0.)).child(backdrop).child(at)) |
| 666 | }); |
| 667 | let mut pip = kit::states( |
| 668 | div() |
| 669 | .id("transform-pip") |
| 670 | .flex_none() |
| 671 | .h(px(CONTROL_H)) |
| 672 | .px(px(8.)) |
| 673 | .flex() |
| 674 | .items_center() |
| 675 | .rounded(px(3.)) |
| 676 | .type_style(type_scale::body()) |
| 677 | .text_color(rgb(if enabled { tokens().ink1 } else { tokens().ink4 })) |
| 678 | .child("Picture in picture"), |
| 679 | kit::Fills::ghost(), |
| 680 | enabled, |
| 681 | None, |
| 682 | ) |
| 683 | .tooltip(overlay_tip_hover( |
| 684 | "Picture in picture", |
| 685 | "", |
| 686 | Some(why.unwrap_or("Bottom right at 30%, with a 32 px margin")), |
| 687 | )); |
| 688 | if enabled { |
| 689 | let me = cx.entity(); |
| 690 | pip = pip.on_click(move |_, _, app| { |
| 691 | me.update(app, |this, cx| this.place_in_corner(engine::transform::Corner::BottomRight, Some(PIP_SCALE), cx)); |
| 692 | }); |
| 693 | } |
| 694 | div() |
| 695 | .id("transform-place") |
| 696 | .w_full() |
| 697 | .flex() |
| 698 | .flex_col() |
| 699 | .gap(px(4.)) |
| 700 | .px(px(6.)) |
| 701 | .rounded(px(3.)) |
| 702 | .child( |
| 703 | div() |
| 704 | .flex() |
| 705 | .justify_between() |
| 706 | .items_center() |
| 707 | .child(div().type_style(type_scale::body()).text_color(rgb(INK2())).child("Place")) |
| 708 | .child(pip), |
| 709 | ) |
| 710 | .child(div().relative().w_full().child(field).children(list)) |
| 711 | .into_any_element() |
| 712 | } |
| 713 | |
| 714 | /// The crop rows under the More fold: the diagram, then one slider per side. |
| 715 | fn transform_crop( |
| 716 | &self, |
| 717 | params: TransformParams, |
| 718 | canvas: (u32, u32), |
| 719 | cx: &mut Context<Self>, |
| 720 | ) -> AnyElement { |
| 721 | let rows: Vec<AnyElement> = (TRANSFORM_MAIN_BANDS..TRANSFORM_BANDS.len()) |
| 722 | .map(|band| self.transform_slider(band, params, canvas, cx)) |
| 723 | .collect(); |
| 724 | div() |
| 725 | .w_full() |
| 726 | .flex() |
| 727 | .flex_col() |
| 728 | .gap(px(4.)) |
| 729 | .pt(px(4.)) |
| 730 | .child( |
| 731 | div() |
| 732 | .w_full() |
| 733 | .flex() |
| 734 | .items_center() |
| 735 | .gap(px(12.)) |
| 736 | .px(px(6.)) |
| 737 | .child(crop_diagram(params)) |
| 738 | .child(dark_help("The lighter part stays in the picture; the rest is cut away.").flex_1().w_auto()), |
| 739 | ) |
| 740 | .children(rows) |
| 741 | .into_any_element() |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | #[cfg(test)] |
| 746 | mod tests { |
| 747 | use super::*; |
| 748 | use crate::player::cards::snap_position_px; |
| 749 | |
| 750 | const HD: (u32, u32) = (1920, 1080); |
| 751 | |
| 752 | #[test] |
| 753 | fn the_stabilise_line_reads_in_plain_words() { |
| 754 | assert_eq!(stabilise_words(&Status::Running(0.4), None), "Analysing 40%"); |
| 755 | assert_eq!(stabilise_words(&Status::Ready, Some(1.07)), "Steadied · enlarged 7% to hide the edges"); |
| 756 | assert_eq!(stabilise_words(&Status::Ready, Some(1.0)), "Steadied"); |
| 757 | assert!(stabilise_words(&Status::Failed("stabilise analysis cancelled".into()), None).starts_with("Stopped")); |
| 758 | } |
| 759 | |
| 760 | #[test] |
| 761 | fn position_reads_in_whole_canvas_pixels_with_no_minus_on_zero() { |
| 762 | assert_eq!(band_readout(0, 0.5, HD), "960 px"); |
| 763 | assert_eq!(band_readout(1, -0.25, HD), "-270 px"); |
| 764 | assert_eq!(band_readout(0, -0.0, HD), "0 px"); |
| 765 | } |
| 766 | |
| 767 | #[test] |
| 768 | fn scale_and_crop_read_as_percent_and_rotation_reads_minus_ninety() { |
| 769 | assert_eq!(band_readout(SCALE_BAND, 1.0, HD), "100%"); |
| 770 | assert_eq!(band_readout(SCALE_BAND, 0.1, HD), "10%"); |
| 771 | assert_eq!(band_readout(4, 0.25, HD), "25%"); |
| 772 | assert_eq!(band_readout(ROTATE_BAND, 270.0, HD), "−90°"); |
| 773 | assert_eq!(band_readout(ROTATE_BAND, 90.0, HD), "90°"); |
| 774 | } |
| 775 | |
| 776 | #[test] |
| 777 | fn a_position_drag_lands_on_a_whole_pixel_inside_its_range() { |
| 778 | for raw in [0.1234_f32, 0.5004, -0.3333, 0.99999] { |
| 779 | let snapped = snap_position_px(0, raw, HD); |
| 780 | let px = snapped * 1920.; |
| 781 | assert!((px - px.round()).abs() < 1e-3, "{raw} gave {px} px"); |
| 782 | assert!( |
| 783 | (snapped - raw).abs() <= 0.5 / 1920. + 1e-6, |
| 784 | "{raw} moved to {snapped}" |
| 785 | ); |
| 786 | } |
| 787 | assert_eq!(snap_position_px(0, 5., HD), 1.); |
| 788 | assert_eq!(snap_position_px(1, -5., HD), -1.); |
| 789 | } |
| 790 | } |