| 1 | use gpui_kit::component::{ |
| 2 | button::{Button, ButtonVariants as _}, |
| 3 | h_flex, v_flex, |
| 4 | }; |
| 5 | use gpui_kit::*; |
| 6 | use tokio::sync::mpsc::UnboundedReceiver; |
| 7 | |
| 8 | use crate::theme; |
| 9 | |
| 10 | #[cfg(windows)] |
| 11 | mod native_windows; |
| 12 | #[cfg(windows)] |
| 13 | use native_windows as native; |
| 14 | #[cfg(not(windows))] |
| 15 | mod native_stub; |
| 16 | #[cfg(not(windows))] |
| 17 | use native_stub as native; |
| 18 | |
| 19 | #[cfg(windows)] |
| 20 | pub use native::configure; |
| 21 | pub use native::{Native, NativeEvent, available}; |
| 22 | |
| 23 | const BAR_HEIGHT: f32 = 28.; |
| 24 | const BAR_TEXT_SIZE: f32 = 11.5; |
| 25 | const OPEN_LABEL: &str = "Open in browser \u{2197}"; |
| 26 | |
| 27 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 28 | pub struct Overlays { |
| 29 | pub panels: bool, |
| 30 | } |
| 31 | |
| 32 | impl Global for Overlays {} |
| 33 | |
| 34 | impl Overlays { |
| 35 | pub fn read(cx: &App) -> Self { |
| 36 | cx.try_global::<Overlays>().copied().unwrap_or_default() |
| 37 | } |
| 38 | |
| 39 | pub fn update(cx: &mut App, change: impl FnOnce(&mut Overlays)) { |
| 40 | let mut next = Overlays::read(cx); |
| 41 | change(&mut next); |
| 42 | if next != Overlays::read(cx) { |
| 43 | cx.set_global(next); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | pub struct RootFocus(pub FocusHandle); |
| 49 | |
| 50 | impl Global for RootFocus {} |
| 51 | |
| 52 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 53 | pub enum FocusSpot { |
| 54 | Nowhere, |
| 55 | Page, |
| 56 | Elsewhere, |
| 57 | } |
| 58 | |
| 59 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 60 | pub struct Visibility { |
| 61 | pub overlays: Overlays, |
| 62 | pub has_area: bool, |
| 63 | pub focus: FocusSpot, |
| 64 | } |
| 65 | |
| 66 | pub fn embed_visible(visibility: Visibility) -> bool { |
| 67 | visibility.has_area && !visibility.overlays.panels && visibility.focus != FocusSpot::Elsewhere |
| 68 | } |
| 69 | |
| 70 | pub fn focus_spot<T: PartialEq>(focused: Option<T>, inside_pane: bool, page: &[T]) -> FocusSpot { |
| 71 | match focused { |
| 72 | None => FocusSpot::Nowhere, |
| 73 | Some(_) if inside_pane => FocusSpot::Page, |
| 74 | Some(handle) if page.contains(&handle) => FocusSpot::Page, |
| 75 | Some(_) => FocusSpot::Elsewhere, |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | pub fn url_bar_text(url: &str) -> String { |
| 80 | let shown = url.strip_prefix("https://").unwrap_or(url); |
| 81 | shown.strip_suffix('/').unwrap_or(shown).to_owned() |
| 82 | } |
| 83 | |
| 84 | pub enum EmbeddedWebEvent { |
| 85 | Closed { start_url: String }, |
| 86 | } |
| 87 | |
| 88 | pub struct EmbeddedWeb { |
| 89 | tab_id: String, |
| 90 | start_url: String, |
| 91 | url: String, |
| 92 | native: Native, |
| 93 | focus_handle: FocusHandle, |
| 94 | container_focus: FocusHandle, |
| 95 | _events: Task<()>, |
| 96 | } |
| 97 | |
| 98 | impl EventEmitter<EmbeddedWebEvent> for EmbeddedWeb {} |
| 99 | |
| 100 | impl EmbeddedWeb { |
| 101 | pub fn new( |
| 102 | tab_id: String, |
| 103 | start_url: String, |
| 104 | native: Native, |
| 105 | mut events: UnboundedReceiver<NativeEvent>, |
| 106 | container_focus: FocusHandle, |
| 107 | cx: &mut Context<Self>, |
| 108 | ) -> Self { |
| 109 | let task = cx.spawn(async move |this, cx| { |
| 110 | while let Some(event) = events.recv().await { |
| 111 | let handled = this.update(cx, |this, cx| this.on_event(event, cx)); |
| 112 | if handled.is_err() { |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | }); |
| 117 | EmbeddedWeb { |
| 118 | tab_id, |
| 119 | url: start_url.clone(), |
| 120 | start_url, |
| 121 | native, |
| 122 | focus_handle: cx.focus_handle(), |
| 123 | container_focus, |
| 124 | _events: task, |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | pub fn tab_id(&self) -> &str { |
| 129 | &self.tab_id |
| 130 | } |
| 131 | |
| 132 | fn on_event(&mut self, event: NativeEvent, cx: &mut Context<Self>) { |
| 133 | match event { |
| 134 | NativeEvent::Navigated(url) => { |
| 135 | self.url = url; |
| 136 | cx.notify(); |
| 137 | } |
| 138 | NativeEvent::NewWindow(url) => cx.open_url(&url), |
| 139 | NativeEvent::Loaded => {} |
| 140 | NativeEvent::Closed => cx.emit(EmbeddedWebEvent::Closed { |
| 141 | start_url: self.start_url.clone(), |
| 142 | }), |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | impl Drop for EmbeddedWeb { |
| 148 | fn drop(&mut self) { |
| 149 | self.native.close(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | impl Render for EmbeddedWeb { |
| 154 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 155 | let native = self.native.clone(); |
| 156 | let pane_focus = self.focus_handle.clone(); |
| 157 | let container_focus = self.container_focus.clone(); |
| 158 | let open_url = self.url.clone(); |
| 159 | v_flex() |
| 160 | .size_full() |
| 161 | .track_focus(&self.focus_handle) |
| 162 | .child( |
| 163 | h_flex() |
| 164 | .h(px(BAR_HEIGHT)) |
| 165 | .flex_none() |
| 166 | .w_full() |
| 167 | .px(px(12.)) |
| 168 | .gap(px(8.)) |
| 169 | .items_center() |
| 170 | .border_b_1() |
| 171 | .border_color(theme::border()) |
| 172 | .child( |
| 173 | div() |
| 174 | .flex_1() |
| 175 | .min_w_0() |
| 176 | .overflow_hidden() |
| 177 | .text_ellipsis() |
| 178 | .whitespace_nowrap() |
| 179 | .text_size(px(BAR_TEXT_SIZE)) |
| 180 | .text_color(theme::text_muted()) |
| 181 | .child(url_bar_text(&self.url)), |
| 182 | ) |
| 183 | .child( |
| 184 | Button::new("embedded-web-open") |
| 185 | .ghost() |
| 186 | .compact() |
| 187 | .label(OPEN_LABEL) |
| 188 | .on_click(move |_, _, cx| cx.open_url(&open_url)), |
| 189 | ), |
| 190 | ) |
| 191 | .child( |
| 192 | div() |
| 193 | .flex_1() |
| 194 | .min_h_0() |
| 195 | .w_full() |
| 196 | .bg(theme::background()) |
| 197 | .child( |
| 198 | canvas( |
| 199 | move |bounds, window, cx| { |
| 200 | let mut page = vec![container_focus.clone()]; |
| 201 | page.extend( |
| 202 | cx.try_global::<RootFocus>().map(|root| root.0.clone()), |
| 203 | ); |
| 204 | let visible = embed_visible(Visibility { |
| 205 | overlays: Overlays::read(cx), |
| 206 | has_area: bounds.size.width > px(0.) |
| 207 | && bounds.size.height > px(0.), |
| 208 | focus: focus_spot( |
| 209 | window.focused(cx), |
| 210 | pane_focus.contains_focused(window, cx), |
| 211 | &page, |
| 212 | ), |
| 213 | }); |
| 214 | native.place(bounds, window.scale_factor(), visible); |
| 215 | }, |
| 216 | |_, _, _, _| {}, |
| 217 | ) |
| 218 | .size_full(), |
| 219 | ), |
| 220 | ) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | #[cfg(test)] |
| 225 | mod tests { |
| 226 | use super::{FocusSpot, Overlays, Visibility, embed_visible, focus_spot, url_bar_text}; |
| 227 | |
| 228 | fn visibility(panels: bool, focus: FocusSpot, has_area: bool) -> Visibility { |
| 229 | Visibility { |
| 230 | overlays: Overlays { panels }, |
| 231 | has_area, |
| 232 | focus, |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn the_page_shows_only_with_room_and_no_panel() { |
| 238 | assert!(embed_visible(visibility(false, FocusSpot::Page, true))); |
| 239 | assert!(!embed_visible(visibility(true, FocusSpot::Page, true))); |
| 240 | assert!(!embed_visible(visibility(false, FocusSpot::Page, false))); |
| 241 | } |
| 242 | |
| 243 | #[test] |
| 244 | fn focus_outside_the_page_hides_it() { |
| 245 | assert!(!embed_visible(visibility( |
| 246 | false, |
| 247 | FocusSpot::Elsewhere, |
| 248 | true |
| 249 | ))); |
| 250 | assert!(embed_visible(visibility(false, FocusSpot::Nowhere, true))); |
| 251 | } |
| 252 | |
| 253 | #[test] |
| 254 | fn focus_is_classified_against_the_page_handles() { |
| 255 | assert_eq!(focus_spot(None, false, &[1, 2]), FocusSpot::Nowhere); |
| 256 | assert_eq!(focus_spot(Some(2), false, &[1, 2]), FocusSpot::Page); |
| 257 | assert_eq!(focus_spot(Some(9), true, &[1, 2]), FocusSpot::Page); |
| 258 | assert_eq!(focus_spot(Some(9), false, &[1, 2]), FocusSpot::Elsewhere); |
| 259 | assert_eq!(focus_spot(Some(9), false, &[]), FocusSpot::Elsewhere); |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn the_url_bar_drops_the_scheme_and_a_trailing_slash() { |
| 264 | assert_eq!(url_bar_text("https://docs.example/"), "docs.example"); |
| 265 | assert_eq!( |
| 266 | url_bar_text("https://docs.example/a/b?c=1"), |
| 267 | "docs.example/a/b?c=1" |
| 268 | ); |
| 269 | assert_eq!(url_bar_text("http://plain.example"), "http://plain.example"); |
| 270 | } |
| 271 | } |