BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / embedded_web.rs

embedded_web.rs

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