| 1 | use std::cell::Cell; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | use gpui_kit::*; |
| 5 | |
| 6 | #[cfg(windows)] |
| 7 | mod native_windows; |
| 8 | #[cfg(windows)] |
| 9 | use native_windows as native; |
| 10 | #[cfg(not(windows))] |
| 11 | mod native_stub; |
| 12 | #[cfg(not(windows))] |
| 13 | use native_stub as native; |
| 14 | |
| 15 | use native::Native; |
| 16 | pub use native::available; |
| 17 | #[cfg(windows)] |
| 18 | pub use native::configure; |
| 19 | |
| 20 | const PERSONAL_SITE: &str = "/personal/"; |
| 21 | const TEAM_SITES: [&str; 2] = ["/sites/", "/teams/"]; |
| 22 | const PLAYER_OPTIONS: &str = "%7B%22ust%22%3Atrue%2C%22hv%22%3A%22CopyEmbedCode%22%7D"; |
| 23 | |
| 24 | /// SharePoint's embeddable player for a video stored in OneDrive or a team site. |
| 25 | pub fn player_url(content_url: &str) -> Option<String> { |
| 26 | let rest = content_url.strip_prefix("https://")?; |
| 27 | let (host, path) = rest.split_once('/')?; |
| 28 | if !host.ends_with(".sharepoint.com") { |
| 29 | return None; |
| 30 | } |
| 31 | let path = format!("/{}", path.split(['?', '#']).next()?); |
| 32 | let site_prefix = std::iter::once(PERSONAL_SITE) |
| 33 | .chain(TEAM_SITES) |
| 34 | .find(|prefix| path.starts_with(prefix))?; |
| 35 | let site_name = path[site_prefix.len()..].split('/').next()?; |
| 36 | if site_name.is_empty() || path.len() <= site_prefix.len() + site_name.len() + 1 { |
| 37 | return None; |
| 38 | } |
| 39 | let site = format!("https://{host}{site_prefix}{site_name}"); |
| 40 | Some(format!( |
| 41 | "{site}/_layouts/15/embed.aspx?id={path}&embed={PLAYER_OPTIONS}" |
| 42 | )) |
| 43 | } |
| 44 | |
| 45 | struct Playing { |
| 46 | key: SharedString, |
| 47 | window: WindowId, |
| 48 | native: Native, |
| 49 | painted_in: Rc<Cell<u64>>, |
| 50 | } |
| 51 | |
| 52 | #[derive(Default)] |
| 53 | pub struct InlineVideo { |
| 54 | playing: Option<Playing>, |
| 55 | frame: Rc<Cell<u64>>, |
| 56 | } |
| 57 | |
| 58 | impl Global for InlineVideo {} |
| 59 | |
| 60 | pub fn play(key: SharedString, content_url: &str, window: &mut Window, cx: &mut App) -> bool { |
| 61 | let Some(url) = player_url(content_url).filter(|_| available()) else { |
| 62 | return false; |
| 63 | }; |
| 64 | stop(cx); |
| 65 | let Some((native, mut new_windows)) = Native::open(&url, window) else { |
| 66 | return false; |
| 67 | }; |
| 68 | cx.spawn(async move |cx| { |
| 69 | while let Some(target) = new_windows.recv().await { |
| 70 | cx.update(|cx| cx.open_url(&target)); |
| 71 | } |
| 72 | }) |
| 73 | .detach(); |
| 74 | let state = cx.default_global::<InlineVideo>(); |
| 75 | state.playing = Some(Playing { |
| 76 | key, |
| 77 | window: window.window_handle().window_id(), |
| 78 | native, |
| 79 | painted_in: Rc::new(Cell::new(u64::MAX)), |
| 80 | }); |
| 81 | window.refresh(); |
| 82 | true |
| 83 | } |
| 84 | |
| 85 | pub fn stop(cx: &mut App) { |
| 86 | if !cx.has_global::<InlineVideo>() { |
| 87 | return; |
| 88 | } |
| 89 | if let Some(playing) = cx.global_mut::<InlineVideo>().playing.take() { |
| 90 | playing.native.close(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /// Keeps the player on the slot while the slot is painted. |
| 95 | pub fn slot(key: SharedString) -> impl IntoElement { |
| 96 | canvas( |
| 97 | move |bounds, window, cx| { |
| 98 | let Some(state) = cx.try_global::<InlineVideo>() else { |
| 99 | return; |
| 100 | }; |
| 101 | let Some(playing) = state.playing.as_ref().filter(|playing| playing.key == key) else { |
| 102 | return; |
| 103 | }; |
| 104 | playing.painted_in.set(state.frame.get()); |
| 105 | let visible = window.content_mask().bounds.intersect(&bounds); |
| 106 | let hidden: Vec<Bounds<Pixels>> = clipped_parts(bounds, visible); |
| 107 | playing.native.place( |
| 108 | bounds, |
| 109 | &hidden, |
| 110 | window.scale_factor(), |
| 111 | visible.size.height > px(0.), |
| 112 | ); |
| 113 | }, |
| 114 | |_, _, _, _| {}, |
| 115 | ) |
| 116 | .absolute() |
| 117 | .inset_0() |
| 118 | } |
| 119 | |
| 120 | /// Paints after the timeline: closes a player whose slot was not painted this frame. |
| 121 | pub fn sentinel() -> impl IntoElement { |
| 122 | canvas( |
| 123 | |_, window, cx| { |
| 124 | let window_id = window.window_handle().window_id(); |
| 125 | let Some(state) = cx.try_global::<InlineVideo>() else { |
| 126 | return false; |
| 127 | }; |
| 128 | let frame = state.frame.get(); |
| 129 | state.frame.set(frame.wrapping_add(1)); |
| 130 | state.playing.as_ref().is_some_and(|playing| { |
| 131 | playing.window == window_id && playing.painted_in.get() != frame |
| 132 | }) |
| 133 | }, |
| 134 | |_, orphaned, _, cx| { |
| 135 | if orphaned { |
| 136 | stop(cx); |
| 137 | } |
| 138 | }, |
| 139 | ) |
| 140 | .absolute() |
| 141 | .size_0() |
| 142 | } |
| 143 | |
| 144 | fn clipped_parts(bounds: Bounds<Pixels>, visible: Bounds<Pixels>) -> Vec<Bounds<Pixels>> { |
| 145 | if visible.size.height <= px(0.) || visible.size.width <= px(0.) { |
| 146 | return Vec::new(); |
| 147 | } |
| 148 | let mut parts = Vec::new(); |
| 149 | if visible.top() > bounds.top() { |
| 150 | parts.push(Bounds::from_corners( |
| 151 | bounds.origin, |
| 152 | point(bounds.right(), visible.top()), |
| 153 | )); |
| 154 | } |
| 155 | if visible.bottom() < bounds.bottom() { |
| 156 | parts.push(Bounds::from_corners( |
| 157 | point(bounds.left(), visible.bottom()), |
| 158 | bounds.bottom_right(), |
| 159 | )); |
| 160 | } |
| 161 | parts |
| 162 | } |
| 163 | |
| 164 | #[cfg(test)] |
| 165 | mod tests { |
| 166 | use gpui_kit::{Bounds, point, px, size}; |
| 167 | |
| 168 | use super::{clipped_parts, player_url}; |
| 169 | |
| 170 | #[test] |
| 171 | fn a_onedrive_chat_video_plays_in_the_personal_site_player() { |
| 172 | let url = player_url( |
| 173 | "https://contoso-my.sharepoint.com/personal/ann_contoso_com/Documents/Chat%20Files/clip.mp4", |
| 174 | ); |
| 175 | let expected = "https://contoso-my.sharepoint.com/personal/ann_contoso_com/_layouts/15/embed.aspx?id=/personal/ann_contoso_com/Documents/Chat%20Files/clip.mp4&embed=%7B%22ust%22%3Atrue%2C%22hv%22%3A%22CopyEmbedCode%22%7D"; |
| 176 | assert_eq!(url.as_deref(), Some(expected)); |
| 177 | } |
| 178 | |
| 179 | #[test] |
| 180 | fn a_channel_video_plays_in_the_team_site_player() { |
| 181 | let url = player_url( |
| 182 | "https://contoso.sharepoint.com/sites/Dev/Shared%20Documents/General/demo.mp4?web=1", |
| 183 | ) |
| 184 | .unwrap(); |
| 185 | assert!(url.starts_with("https://contoso.sharepoint.com/sites/Dev/_layouts/15/embed.aspx?id=/sites/Dev/Shared%20Documents/General/demo.mp4&")); |
| 186 | } |
| 187 | |
| 188 | #[test] |
| 189 | fn other_hosts_have_no_inline_player() { |
| 190 | assert_eq!(player_url("https://example.com/personal/a/clip.mp4"), None); |
| 191 | assert_eq!(player_url("https://contoso.sharepoint.com/clip.mp4"), None); |
| 192 | assert_eq!( |
| 193 | player_url("https://contoso-my.sharepoint.com/personal/"), |
| 194 | None |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn only_the_parts_outside_the_clip_are_cut_away() { |
| 200 | let bounds = Bounds::new(point(px(0.), px(0.)), size(px(100.), px(100.))); |
| 201 | let visible = Bounds::new(point(px(0.), px(30.)), size(px(100.), px(50.))); |
| 202 | let parts = clipped_parts(bounds, visible); |
| 203 | assert_eq!(parts.len(), 2); |
| 204 | assert_eq!(parts[0].size.height, px(30.)); |
| 205 | assert_eq!(parts[1].origin.y, px(80.)); |
| 206 | assert!(clipped_parts(bounds, bounds).is_empty()); |
| 207 | } |
| 208 | } |