| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use common::{model::Id, share::Kind}; |
| 4 | use gpui_kit::{ |
| 5 | Context, IntoElement, ObjectFit, ParentElement, Render, RenderImage, Styled, StyledImage, |
| 6 | Window, canvas, div, img, |
| 7 | }; |
| 8 | |
| 9 | use crate::net::NetworkHandle; |
| 10 | |
| 11 | const WIDE: f32 = 16. / 9.; |
| 12 | const ASPECT_CHANGE: f32 = 0.02; |
| 13 | |
| 14 | pub(super) struct Surface { |
| 15 | frame: Option<Arc<RenderImage>>, |
| 16 | aspect: f32, |
| 17 | cover: bool, |
| 18 | network: NetworkHandle, |
| 19 | source: (Id, Kind), |
| 20 | id: u64, |
| 21 | } |
| 22 | |
| 23 | impl Surface { |
| 24 | pub(super) fn new( |
| 25 | cover: bool, |
| 26 | network: NetworkHandle, |
| 27 | source: (Id, Kind), |
| 28 | cx: &mut Context<Self>, |
| 29 | ) -> Self { |
| 30 | Self { |
| 31 | frame: None, |
| 32 | aspect: WIDE, |
| 33 | cover, |
| 34 | network, |
| 35 | source, |
| 36 | id: cx.entity_id().as_u64(), |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub(super) fn replace( |
| 41 | &mut self, |
| 42 | frame: Option<Arc<RenderImage>>, |
| 43 | cx: &mut Context<Self>, |
| 44 | ) -> Option<Arc<RenderImage>> { |
| 45 | if let Some(frame) = &frame { |
| 46 | let size = frame.size(0); |
| 47 | let aspect = size.width.0 as f32 / size.height.0.max(1) as f32; |
| 48 | if (aspect - self.aspect).abs() > self.aspect * ASPECT_CHANGE { |
| 49 | self.aspect = aspect; |
| 50 | } |
| 51 | } |
| 52 | cx.notify(); |
| 53 | std::mem::replace(&mut self.frame, frame) |
| 54 | } |
| 55 | |
| 56 | pub(super) fn aspect(&self) -> f32 { |
| 57 | self.aspect |
| 58 | } |
| 59 | |
| 60 | pub(super) fn set_cover(&mut self, cover: bool, cx: &mut Context<Self>) { |
| 61 | if self.cover != cover { |
| 62 | self.cover = cover; |
| 63 | cx.notify(); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | impl Drop for Surface { |
| 69 | fn drop(&mut self) { |
| 70 | let (user_id, kind) = self.source; |
| 71 | self.network.show_share(user_id, kind, self.id, None); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl Render for Surface { |
| 76 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 77 | let fit = if self.cover { |
| 78 | ObjectFit::Cover |
| 79 | } else { |
| 80 | ObjectFit::Contain |
| 81 | }; |
| 82 | let network = self.network.clone(); |
| 83 | let ((user_id, kind), id) = (self.source, self.id); |
| 84 | let aspect = self.aspect; |
| 85 | let cover = self.cover; |
| 86 | let measure = canvas( |
| 87 | move |bounds, window, _| { |
| 88 | let (width, height) = (f32::from(bounds.size.width), f32::from(bounds.size.height)); |
| 89 | let width = if cover { |
| 90 | width.max(height * aspect) |
| 91 | } else { |
| 92 | width.min(height * aspect) |
| 93 | }; |
| 94 | let width = (width * window.scale_factor()).round() as u32; |
| 95 | network.show_share(user_id, kind, id, Some(width)); |
| 96 | }, |
| 97 | |_, _, _, _| {}, |
| 98 | ) |
| 99 | .absolute() |
| 100 | .size_full(); |
| 101 | div().size_full().relative().child(measure).children( |
| 102 | self.frame |
| 103 | .clone() |
| 104 | .map(|frame| img(frame).absolute().size_full().object_fit(fit)), |
| 105 | ) |
| 106 | } |
| 107 | } |