diff --git a/crates/client/src/gui/camera_view.rs b/crates/client/src/gui/camera_view.rs index a10c916..992c093 100644 --- a/crates/client/src/gui/camera_view.rs +++ b/crates/client/src/gui/camera_view.rs @@ -324,6 +324,7 @@ impl Yumush { &self, entity: &Entity, palette: Palette, + cx: &gpui_kit::App, ) -> Option { if self.chat.stage != Stage::Grid || !self.in_stage_chat() @@ -340,36 +341,98 @@ impl Yumush { if sources.is_empty() { return None; } - let columns = ((sources.len() as f32).sqrt().ceil() as usize).max(1); - let rows = sources.len().div_ceil(columns); - let mut cells = sources - .into_iter() - .map(|source| grid_cell(entity, self, palette, source)); - - Some( - div().flex_1().min_h_0().flex().flex_col().child( + let aspects: Vec = sources + .iter() + .map(|source| { + self.chat + .tiles + .get(source) + .map_or(WIDE, |tile| tile.surface.read(cx).aspect()) + }) + .collect(); + let size = self.chat.grid_size; + let columns = grid_columns(&aspects, size); + let (cell_w, cell_h) = cell_size(sources.len(), columns, size); + let mut cells = sources.into_iter().zip(aspects).map(|(source, aspect)| { + let width = cell_w.min(cell_h * aspect); + grid_cell(entity, self, palette, source, (width, width / aspect)) + }); + let rows = (0..sources_rows(columns, cells.len())) + .map(|_| { div() - .flex_1() - .min_h_0() - .px(px(space::XL)) - .pt(px(space::XL)) .flex() - .flex_col() - .gap(px(space::LG)) - .children((0..rows).map(|_| { - div().flex_1().min_h_0().flex().gap(px(space::LG)).children( - (0..columns).map(|_| { - cells - .next() - .unwrap_or_else(|| div().flex_1().into_any_element()) - }), - ) - })), - ), + .justify_center() + .items_center() + .gap(px(GRID_GAP)) + .children(cells.by_ref().take(columns).collect::>()) + }) + .collect::>(); + let measure = entity.clone(); + + Some( + div() + .flex_1() + .min_h_0() + .relative() + .flex() + .flex_col() + .justify_center() + .gap(px(GRID_GAP)) + .child( + gpui_kit::canvas( + move |bounds, _, cx| { + let size = + (f32::from(bounds.size.width), f32::from(bounds.size.height)); + measure.update(cx, |yumush, cx| { + if yumush.chat.grid_size != size { + yumush.chat.grid_size = size; + cx.notify(); + } + }); + }, + |_, _, _, _| {}, + ) + .absolute() + .inset_0(), + ) + .children(rows), ) } } +const WIDE: f32 = 16. / 9.; +const GRID_GAP: f32 = super::style::stroke::LINE; + +fn sources_rows(columns: usize, count: usize) -> usize { + count.div_ceil(columns.max(1)) +} + +fn cell_size(count: usize, columns: usize, (width, height): (f32, f32)) -> (f32, f32) { + let rows = sources_rows(columns, count); + ( + (width - GRID_GAP * (columns - 1) as f32) / columns as f32, + (height - GRID_GAP * (rows - 1) as f32) / rows as f32, + ) +} + +fn grid_columns(aspects: &[f32], size: (f32, f32)) -> usize { + (1..=aspects.len().max(1)) + .max_by(|a, b| { + let area = |columns: usize| { + let (cell_w, cell_h) = cell_size(aspects.len(), columns, size); + aspects + .iter() + .map(|aspect| { + let width = cell_w.min(cell_h * aspect); + width * width / aspect + }) + .sum::() + }; + area(*a).total_cmp(&area(*b)) + }) + .unwrap_or(1) +} + pub(super) fn ticker(cx: &mut Context) -> Task<()> { cx.spawn(async move |this, cx| { let mut tick = IDLE_TICK; @@ -498,8 +561,8 @@ fn grid_cell( yumush: &Yumush, palette: Palette, (user_id, kind): (Id, Kind), + (width, height): (f32, f32), ) -> AnyElement { - let t = palette.tokens; let entity = entity.clone(); let surface = yumush .chat @@ -509,12 +572,11 @@ fn grid_cell( .map(|tile| tile.surface.clone()); div() .id(format!("grid_{user_id}_{kind:?}")) - .flex_1() - .min_w_0() - .min_h_0() - .flex() - .flex_col() - .gap(px(space::SM)) + .w(px(width)) + .h(px(height)) + .flex_none() + .relative() + .overflow_hidden() .cursor_pointer() .tip(w().camera_view.watch) .on_click(move |_, window, cx| { @@ -522,35 +584,24 @@ fn grid_cell( yumush.toggle_viewer(user_id, kind, window, cx) }); }) - .child(tile_name(yumush, (user_id, kind), false, t)) + .child(div().absolute().inset_0().children(surface)) .child( - div() - .flex_1() - .min_h_0() - .rounded(px(radius::MD)) - .overflow_hidden() - .bg(t.raise()) - .children(surface), + tile_label(yumush, palette, (user_id, kind)) + .absolute() + .left(px(space::MD)) + .top(px(space::MD)), ) .into_any_element() } -pub(super) fn tile_label(yumush: &Yumush, palette: Palette, (user_id, _): (Id, Kind)) -> Div { +pub(super) fn tile_label(yumush: &Yumush, palette: Palette, source: (Id, Kind)) -> Div { let t = palette.tokens; - let name = yumush - .chat - .author(user_id) - .map_or_else(|| user_id.to_string(), |user| user.username.clone()); - let speaking = yumush.chat.waves.speaking.contains(&user_id); + let user_id = source.0; let muted = yumush.chat.server_muted.contains(&user_id) || (yumush.is_me(user_id) && yumush.chat.voice_flags().is_some_and(|(muted, _)| muted)); scrim(t) - .child( - words(name, Text::Label, Ink::Paper, t) - .flex_none() - .when(speaking, |name| name.text_color(t.person(user_id))), - ) + .child(tile_name(yumush, source, true, t)) .children(muted.then(|| super::parts::small_icon(IconName::MicOff).text_color(t.bad()))) } @@ -585,3 +636,16 @@ fn tile_name( .when(audible, |name| name.text_color(t.person(user_id))), ) } + +#[cfg(test)] +mod tests { + use super::{WIDE, grid_columns}; + + #[test] + fn grid_columns_fill_the_shape() { + assert_eq!(grid_columns(&[WIDE], (1300., 900.)), 1); + assert_eq!(grid_columns(&[WIDE, WIDE], (1300., 900.)), 1); + assert_eq!(grid_columns(&[WIDE, WIDE], (2400., 600.)), 2); + assert_eq!(grid_columns(&[WIDE; 4], (1600., 900.)), 2); + } +} diff --git a/crates/client/src/gui/chat.rs b/crates/client/src/gui/chat.rs index e5edfd0..7020a51 100644 --- a/crates/client/src/gui/chat.rs +++ b/crates/client/src/gui/chat.rs @@ -280,6 +280,7 @@ pub struct Chat { pub(super) tiles: BTreeMap<(Id, Kind), Tile>, pub(super) camera_task: Option>, pub(super) stage: Stage, + pub(super) grid_size: (f32, f32), pub(super) typing: HashMap>, pub(super) typing_sent: HashMap, } @@ -576,6 +577,7 @@ impl Chat { tiles: BTreeMap::new(), camera_task: None, stage: Stage::Strip, + grid_size: (1280., 720.), composer: Composer::Compose { reply: None }, } } @@ -1425,7 +1427,7 @@ impl Yumush { .child(div().flex_1().min_w_0().flex().flex_col().map(|panel| { match self.inline_viewer(entity, themed) { Some(viewer) => panel.child(viewer), - None => match self.stage_grid(entity, themed) { + None => match self.stage_grid(entity, themed, cx) { Some(grid) => panel.child(grid), None => panel .child(self.message_area(entity, themed)) diff --git a/crates/client/src/gui/surface.rs b/crates/client/src/gui/surface.rs index 75a4c3e..81d3401 100644 --- a/crates/client/src/gui/surface.rs +++ b/crates/client/src/gui/surface.rs @@ -53,6 +53,10 @@ impl Surface { std::mem::replace(&mut self.frame, frame) } + pub(super) fn aspect(&self) -> f32 { + self.aspect + } + pub(super) fn set_cover(&mut self, cover: bool, cx: &mut Context) { if self.cover != cover { self.cover = cover;