Fillerless stage grid: picks the column count that gives the videos most area, cells sized to each stream's aspect, hairline gaps, name overlaid on the video corner
Browse files at this commit701aad39f81ebe8109a78bb097f506b78a2f8dceParent: dd93b6c- style
- fillerless stage grid: picks the column count that gives the videos most area, cells sized to each stream's aspect, hairline gaps, name overlaid on the video corner
3 changed files +121 -51
- crates/client/src/gui/camera_view.rs +114 -50
- crates/client/src/gui/chat.rs +3 -1
- crates/client/src/gui/surface.rs +4 -0
crates/client/src/gui/camera_view.rsmodified+114 -50
| @@ -324,6 +324,7 @@ impl Yumush { | |||
| 324 | &self, | 324 | &self, |
| 325 | entity: &Entity<Self>, | 325 | entity: &Entity<Self>, |
| 326 | palette: Palette, | 326 | palette: Palette, |
| 327 | + cx: &gpui_kit::App, | ||
| 327 | ) -> Option<impl IntoElement> { | 328 | ) -> Option<impl IntoElement> { |
| 328 | if self.chat.stage != Stage::Grid | 329 | if self.chat.stage != Stage::Grid |
| 329 | || !self.in_stage_chat() | 330 | || !self.in_stage_chat() |
| @@ -340,36 +341,98 @@ impl Yumush { | |||
| 340 | if sources.is_empty() { | 341 | if sources.is_empty() { |
| 341 | return None; | 342 | return None; |
| 342 | } | 343 | } |
| 343 | - let columns = ((sources.len() as f32).sqrt().ceil() as usize).max(1); | 344 | + let aspects: Vec<f32> = sources |
| 344 | - let rows = sources.len().div_ceil(columns); | 345 | + .iter() |
| 345 | - let mut cells = sources | 346 | + .map(|source| { |
| 346 | - .into_iter() | 347 | + self.chat |
| 347 | - .map(|source| grid_cell(entity, self, palette, source)); | 348 | + .tiles |
| 348 | - | 349 | + .get(source) |
| 349 | - Some( | 350 | + .map_or(WIDE, |tile| tile.surface.read(cx).aspect()) |
| 350 | - div().flex_1().min_h_0().flex().flex_col().child( | 351 | + }) |
| 352 | + .collect(); | ||
| 353 | + let size = self.chat.grid_size; | ||
| 354 | + let columns = grid_columns(&aspects, size); | ||
| 355 | + let (cell_w, cell_h) = cell_size(sources.len(), columns, size); | ||
| 356 | + let mut cells = sources.into_iter().zip(aspects).map(|(source, aspect)| { | ||
| 357 | + let width = cell_w.min(cell_h * aspect); | ||
| 358 | + grid_cell(entity, self, palette, source, (width, width / aspect)) | ||
| 359 | + }); | ||
| 360 | + let rows = (0..sources_rows(columns, cells.len())) | ||
| 361 | + .map(|_| { | ||
| 351 | div() | 362 | div() |
| 352 | - .flex_1() | ||
| 353 | - .min_h_0() | ||
| 354 | - .px(px(space::XL)) | ||
| 355 | - .pt(px(space::XL)) | ||
| 356 | .flex() | 363 | .flex() |
| 357 | - .flex_col() | 364 | + .justify_center() |
| 358 | - .gap(px(space::LG)) | 365 | + .items_center() |
| 359 | - .children((0..rows).map(|_| { | 366 | + .gap(px(GRID_GAP)) |
| 360 | - div().flex_1().min_h_0().flex().gap(px(space::LG)).children( | 367 | + .children(cells.by_ref().take(columns).collect::<Vec<_>>()) |
| 361 | - (0..columns).map(|_| { | 368 | + }) |
| 362 | - cells | 369 | + .collect::<Vec<_>>(); |
| 363 | - .next() | 370 | + let measure = entity.clone(); |
| 364 | - .unwrap_or_else(|| div().flex_1().into_any_element()) | 371 | + |
| 365 | - }), | 372 | + Some( |
| 366 | - ) | 373 | + div() |
| 367 | - })), | 374 | + .flex_1() |
| 368 | - ), | 375 | + .min_h_0() |
| 376 | + .relative() | ||
| 377 | + .flex() | ||
| 378 | + .flex_col() | ||
| 379 | + .justify_center() | ||
| 380 | + .gap(px(GRID_GAP)) | ||
| 381 | + .child( | ||
| 382 | + gpui_kit::canvas( | ||
| 383 | + move |bounds, _, cx| { | ||
| 384 | + let size = | ||
| 385 | + (f32::from(bounds.size.width), f32::from(bounds.size.height)); | ||
| 386 | + measure.update(cx, |yumush, cx| { | ||
| 387 | + if yumush.chat.grid_size != size { | ||
| 388 | + yumush.chat.grid_size = size; | ||
| 389 | + cx.notify(); | ||
| 390 | + } | ||
| 391 | + }); | ||
| 392 | + }, | ||
| 393 | + |_, _, _, _| {}, | ||
| 394 | + ) | ||
| 395 | + .absolute() | ||
| 396 | + .inset_0(), | ||
| 397 | + ) | ||
| 398 | + .children(rows), | ||
| 369 | ) | 399 | ) |
| 370 | } | 400 | } |
| 371 | } | 401 | } |
| 372 | 402 | ||
| 403 | +const WIDE: f32 = 16. / 9.; | ||
| 404 | +const GRID_GAP: f32 = super::style::stroke::LINE; | ||
| 405 | + | ||
| 406 | +fn sources_rows(columns: usize, count: usize) -> usize { | ||
| 407 | + count.div_ceil(columns.max(1)) | ||
| 408 | +} | ||
| 409 | + | ||
| 410 | +fn cell_size(count: usize, columns: usize, (width, height): (f32, f32)) -> (f32, f32) { | ||
| 411 | + let rows = sources_rows(columns, count); | ||
| 412 | + ( | ||
| 413 | + (width - GRID_GAP * (columns - 1) as f32) / columns as f32, | ||
| 414 | + (height - GRID_GAP * (rows - 1) as f32) / rows as f32, | ||
| 415 | + ) | ||
| 416 | +} | ||
| 417 | + | ||
| 418 | +fn grid_columns(aspects: &[f32], size: (f32, f32)) -> usize { | ||
| 419 | + (1..=aspects.len().max(1)) | ||
| 420 | + .max_by(|a, b| { | ||
| 421 | + let area = |columns: usize| { | ||
| 422 | + let (cell_w, cell_h) = cell_size(aspects.len(), columns, size); | ||
| 423 | + aspects | ||
| 424 | + .iter() | ||
| 425 | + .map(|aspect| { | ||
| 426 | + let width = cell_w.min(cell_h * aspect); | ||
| 427 | + width * width / aspect | ||
| 428 | + }) | ||
| 429 | + .sum::<f32>() | ||
| 430 | + }; | ||
| 431 | + area(*a).total_cmp(&area(*b)) | ||
| 432 | + }) | ||
| 433 | + .unwrap_or(1) | ||
| 434 | +} | ||
| 435 | + | ||
| 373 | pub(super) fn ticker(cx: &mut Context<Yumush>) -> Task<()> { | 436 | pub(super) fn ticker(cx: &mut Context<Yumush>) -> Task<()> { |
| 374 | cx.spawn(async move |this, cx| { | 437 | cx.spawn(async move |this, cx| { |
| 375 | let mut tick = IDLE_TICK; | 438 | let mut tick = IDLE_TICK; |
| @@ -498,8 +561,8 @@ fn grid_cell( | |||
| 498 | yumush: &Yumush, | 561 | yumush: &Yumush, |
| 499 | palette: Palette, | 562 | palette: Palette, |
| 500 | (user_id, kind): (Id, Kind), | 563 | (user_id, kind): (Id, Kind), |
| 564 | + (width, height): (f32, f32), | ||
| 501 | ) -> AnyElement { | 565 | ) -> AnyElement { |
| 502 | - let t = palette.tokens; | ||
| 503 | let entity = entity.clone(); | 566 | let entity = entity.clone(); |
| 504 | let surface = yumush | 567 | let surface = yumush |
| 505 | .chat | 568 | .chat |
| @@ -509,12 +572,11 @@ fn grid_cell( | |||
| 509 | .map(|tile| tile.surface.clone()); | 572 | .map(|tile| tile.surface.clone()); |
| 510 | div() | 573 | div() |
| 511 | .id(format!("grid_{user_id}_{kind:?}")) | 574 | .id(format!("grid_{user_id}_{kind:?}")) |
| 512 | - .flex_1() | 575 | + .w(px(width)) |
| 513 | - .min_w_0() | 576 | + .h(px(height)) |
| 514 | - .min_h_0() | 577 | + .flex_none() |
| 515 | - .flex() | 578 | + .relative() |
| 516 | - .flex_col() | 579 | + .overflow_hidden() |
| 517 | - .gap(px(space::SM)) | ||
| 518 | .cursor_pointer() | 580 | .cursor_pointer() |
| 519 | .tip(w().camera_view.watch) | 581 | .tip(w().camera_view.watch) |
| 520 | .on_click(move |_, window, cx| { | 582 | .on_click(move |_, window, cx| { |
| @@ -522,35 +584,24 @@ fn grid_cell( | |||
| 522 | yumush.toggle_viewer(user_id, kind, window, cx) | 584 | yumush.toggle_viewer(user_id, kind, window, cx) |
| 523 | }); | 585 | }); |
| 524 | }) | 586 | }) |
| 525 | - .child(tile_name(yumush, (user_id, kind), false, t)) | 587 | + .child(div().absolute().inset_0().children(surface)) |
| 526 | .child( | 588 | .child( |
| 527 | - div() | 589 | + tile_label(yumush, palette, (user_id, kind)) |
| 528 | - .flex_1() | 590 | + .absolute() |
| 529 | - .min_h_0() | 591 | + .left(px(space::MD)) |
| 530 | - .rounded(px(radius::MD)) | 592 | + .top(px(space::MD)), |
| 531 | - .overflow_hidden() | ||
| 532 | - .bg(t.raise()) | ||
| 533 | - .children(surface), | ||
| 534 | ) | 593 | ) |
| 535 | .into_any_element() | 594 | .into_any_element() |
| 536 | } | 595 | } |
| 537 | 596 | ||
| 538 | -pub(super) fn tile_label(yumush: &Yumush, palette: Palette, (user_id, _): (Id, Kind)) -> Div { | 597 | +pub(super) fn tile_label(yumush: &Yumush, palette: Palette, source: (Id, Kind)) -> Div { |
| 539 | let t = palette.tokens; | 598 | let t = palette.tokens; |
| 540 | - let name = yumush | 599 | + let user_id = source.0; |
| 541 | - .chat | ||
| 542 | - .author(user_id) | ||
| 543 | - .map_or_else(|| user_id.to_string(), |user| user.username.clone()); | ||
| 544 | - let speaking = yumush.chat.waves.speaking.contains(&user_id); | ||
| 545 | let muted = yumush.chat.server_muted.contains(&user_id) | 600 | let muted = yumush.chat.server_muted.contains(&user_id) |
| 546 | || (yumush.is_me(user_id) && yumush.chat.voice_flags().is_some_and(|(muted, _)| muted)); | 601 | || (yumush.is_me(user_id) && yumush.chat.voice_flags().is_some_and(|(muted, _)| muted)); |
| 547 | 602 | ||
| 548 | scrim(t) | 603 | scrim(t) |
| 549 | - .child( | 604 | + .child(tile_name(yumush, source, true, t)) |
| 550 | - words(name, Text::Label, Ink::Paper, t) | ||
| 551 | - .flex_none() | ||
| 552 | - .when(speaking, |name| name.text_color(t.person(user_id))), | ||
| 553 | - ) | ||
| 554 | .children(muted.then(|| super::parts::small_icon(IconName::MicOff).text_color(t.bad()))) | 605 | .children(muted.then(|| super::parts::small_icon(IconName::MicOff).text_color(t.bad()))) |
| 555 | } | 606 | } |
| 556 | 607 | ||
| @@ -585,3 +636,16 @@ fn tile_name( | |||
| 585 | .when(audible, |name| name.text_color(t.person(user_id))), | 636 | .when(audible, |name| name.text_color(t.person(user_id))), |
| 586 | ) | 637 | ) |
| 587 | } | 638 | } |
| 639 | + | ||
| 640 | +#[cfg(test)] | ||
| 641 | +mod tests { | ||
| 642 | + use super::{WIDE, grid_columns}; | ||
| 643 | + | ||
| 644 | + #[test] | ||
| 645 | + fn grid_columns_fill_the_shape() { | ||
| 646 | + assert_eq!(grid_columns(&[WIDE], (1300., 900.)), 1); | ||
| 647 | + assert_eq!(grid_columns(&[WIDE, WIDE], (1300., 900.)), 1); | ||
| 648 | + assert_eq!(grid_columns(&[WIDE, WIDE], (2400., 600.)), 2); | ||
| 649 | + assert_eq!(grid_columns(&[WIDE; 4], (1600., 900.)), 2); | ||
| 650 | + } | ||
| 651 | +} | ||
crates/client/src/gui/chat.rsmodified+3 -1
| @@ -280,6 +280,7 @@ pub struct Chat { | |||
| 280 | pub(super) tiles: BTreeMap<(Id, Kind), Tile>, | 280 | pub(super) tiles: BTreeMap<(Id, Kind), Tile>, |
| 281 | pub(super) camera_task: Option<Task<()>>, | 281 | pub(super) camera_task: Option<Task<()>>, |
| 282 | pub(super) stage: Stage, | 282 | pub(super) stage: Stage, |
| 283 | + pub(super) grid_size: (f32, f32), | ||
| 283 | pub(super) typing: HashMap<Id, HashMap<Id, Instant>>, | 284 | pub(super) typing: HashMap<Id, HashMap<Id, Instant>>, |
| 284 | pub(super) typing_sent: HashMap<Id, Instant>, | 285 | pub(super) typing_sent: HashMap<Id, Instant>, |
| 285 | } | 286 | } |
| @@ -576,6 +577,7 @@ impl Chat { | |||
| 576 | tiles: BTreeMap::new(), | 577 | tiles: BTreeMap::new(), |
| 577 | camera_task: None, | 578 | camera_task: None, |
| 578 | stage: Stage::Strip, | 579 | stage: Stage::Strip, |
| 580 | + grid_size: (1280., 720.), | ||
| 579 | composer: Composer::Compose { reply: None }, | 581 | composer: Composer::Compose { reply: None }, |
| 580 | } | 582 | } |
| 581 | } | 583 | } |
| @@ -1425,7 +1427,7 @@ impl Yumush { | |||
| 1425 | .child(div().flex_1().min_w_0().flex().flex_col().map(|panel| { | 1427 | .child(div().flex_1().min_w_0().flex().flex_col().map(|panel| { |
| 1426 | match self.inline_viewer(entity, themed) { | 1428 | match self.inline_viewer(entity, themed) { |
| 1427 | Some(viewer) => panel.child(viewer), | 1429 | Some(viewer) => panel.child(viewer), |
| 1428 | - None => match self.stage_grid(entity, themed) { | 1430 | + None => match self.stage_grid(entity, themed, cx) { |
| 1429 | Some(grid) => panel.child(grid), | 1431 | Some(grid) => panel.child(grid), |
| 1430 | None => panel | 1432 | None => panel |
| 1431 | .child(self.message_area(entity, themed)) | 1433 | .child(self.message_area(entity, themed)) |
crates/client/src/gui/surface.rsmodified+4 -0
| @@ -53,6 +53,10 @@ impl Surface { | |||
| 53 | std::mem::replace(&mut self.frame, frame) | 53 | std::mem::replace(&mut self.frame, frame) |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | + pub(super) fn aspect(&self) -> f32 { | ||
| 57 | + self.aspect | ||
| 58 | + } | ||
| 59 | + | ||
| 56 | pub(super) fn set_cover(&mut self, cover: bool, cx: &mut Context<Self>) { | 60 | pub(super) fn set_cover(&mut self, cover: bool, cx: &mut Context<Self>) { |
| 57 | if self.cover != cover { | 61 | if self.cover != cover { |
| 58 | self.cover = cover; | 62 | self.cover = cover; |