rustimal 0.8.0

A minimal, GPU-rendered terminal for Windows that runs Git Bash, with animated split panes and a glass look
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Web apps in a pane: T3 Code and VS Code, each a web view (WebView2) laid over the pane
//! it was asked for. The shell keeps running underneath, and a shortcut switches between
//! the two. What to show comes from [`crate::programs::t3`] and [`crate::programs::vscode`].
//!
//! A web view is a window of its own on top of rustimal's drawing, with two results. It is
//! hidden while rustimal has something to draw over the pane, such as the command palette.
//! And while it has the keyboard rustimal sees no keys, so a small script in the page
//! passes rustimal's shortcuts back.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

use winit::event::KeyEvent;
use winit::event_loop::ActiveEventLoop;
use winit::window::Window;
use wry::dpi::{PhysicalPosition, PhysicalSize};
use wry::{NewWindowResponse, WebContext, WebView, WebViewBuilder, WebViewBuilderExtWindows, WebViewExtWindows};

use super::{App, State};
use crate::config::keys::{Action, Chord, Keymap};
use crate::platform::win32;
use crate::programs::t3::Page;
use crate::programs::vscode::{self, Spot};
use crate::terminal::embeds::{Embed, WebKind};
use crate::terminal::events::UserEvent;
use crate::terminal::layout::{PaneId, Rect};
use crate::terminal::links::{self, Target};

mod bridge;
mod code;
mod t3;

use bridge::*;
pub(in crate::app) use bridge::{FORWARDED, forwarded_chords};
pub use code::CodeRequest;
use code::*;
use t3::*;

/// Space left around the view, in logical pixels, so the pane's rim and focus ring show.
const INSET: f32 = 6.0;

#[derive(Default)]
pub(super) struct Views {
    views: Vec<View>,
    /// The pane whose view was last given the keyboard.
    keys: Option<PaneId>,
    /// What is being looked up or started, for which pane.
    opening: Vec<(PaneId, WebKind)>,
    /// Where the views keep their sign-in and settings. Declared after them, so it is
    /// dropped after them.
    context: Option<WebContext>,
    /// The hint about accepting VS Code's license has been shown.
    told_about_license: bool,
    /// Programs of their own, in panes (see `host`).
    pub(super) hosts: super::host::Hosts,
}

struct View {
    pane: PaneId,
    kind: WebKind,
    web: WebView,
    /// The folder it was opened for.
    dir: PathBuf,
    /// Switched away from. At most one view of a pane isn't.
    hidden: bool,
    /// What the web view was last told, to only tell it changes.
    shown: bool,
    bounds: Option<[i32; 4]>,
    /// The page of this origin that was last asked for, to return to after pairing.
    origin: String,
    wanted: String,
    /// When it was last sent to be paired, whether that is still going on, and how often.
    paired_at: Option<Instant>,
    pairing: bool,
    pair_tries: u8,
}

impl Views {
    fn get(&self, pane: PaneId, kind: WebKind) -> Option<&View> {
        self.views.iter().find(|v| v.pane == pane && v.kind == kind)
    }

    fn get_mut(&mut self, pane: PaneId, kind: WebKind) -> Option<&mut View> {
        self.views.iter_mut().find(|v| v.pane == pane && v.kind == kind)
    }

    /// The view a pane is showing, as opposed to its terminal.
    fn active(&self, pane: PaneId) -> Option<&View> {
        self.views.iter().find(|v| v.pane == pane && !v.hidden)
    }

    /// Something else is going to be shown over the pane: a web view that has the
    /// keyboard gives it up.
    pub(super) fn release_keys(&mut self, pane: PaneId) {
        if self.keys == Some(pane)
            && let Some(view) = self.active(pane)
        {
            let _ = view.web.focus_parent();
        }
        self.keys = None;
    }

    /// Switch a pane's views away, back to what is drawn underneath.
    pub(super) fn hide(&mut self, pane: PaneId) {
        for view in self.views.iter_mut().filter(|v| v.pane == pane) {
            view.hidden = true;
        }
        self.hosts.hide(pane);
    }

    /// The window has moved. A view has to be told, or its menus, drop-downs and the
    /// popup for typing accents stay where the window used to be. wry only passes this on
    /// for a view that fills its window, and these are children over a pane.
    pub(super) fn window_moved(&self) {
        for view in &self.views {
            // SAFETY: the controller belongs to a live view, used on its own thread.
            if let Err(e) = unsafe { view.web.controller().NotifyParentWindowPositionChanged() } {
                log::debug!("could not tell a web view the window moved: {e}");
            }
        }
    }

    /// Drop a pane's views, taking the keyboard back from them first.
    pub(super) fn remove(&mut self, pane: PaneId) {
        if self.keys == Some(pane) {
            for view in self.views.iter().filter(|v| v.pane == pane && v.shown) {
                let _ = view.web.focus_parent();
            }
            self.keys = None;
        }
        self.views.retain(|v| v.pane != pane);
        self.opening.retain(|&(p, _)| p != pane);
        self.hosts.remove(pane);
    }
}

/// Put this view of a pane in front of everything else the pane has.
fn bring_forward(st: &mut State, pane: PaneId, kind: WebKind) {
    // Taking the keyboard from the view going away, so the new one can be given it.
    if st.web.keys == Some(pane)
        && let Some(view) = st.web.active(pane).filter(|v| v.kind != kind)
    {
        let _ = view.web.focus_parent();
    }
    st.web.hide(pane);
    if let Some(view) = st.web.get_mut(pane, kind) {
        view.hidden = false;
    }
    st.web.keys = None;
    if let Some(p) = st.panes.iter_mut().find(|p| p.id == pane) {
        p.embeds.shown(Embed::Web(kind));
        if let Some(chat) = p.chat.as_mut() {
            chat.hidden = true;
        }
        if let Some(review) = p.review.as_mut() {
            review.hidden = true;
        }
    }
    st.blink_epoch = Instant::now();
    st.window.request_redraw();
}

/// Is this window the one in front? A web view with the keyboard counts as the window.
fn in_foreground(window: &Window) -> bool {
    win32::handle_of(window).is_some_and(win32::is_in_front)
}

/// Is `url` a page of the server at `origin`?
fn same_origin(url: &str, origin: &str) -> bool {
    url.strip_prefix(origin).is_some_and(|rest| rest.is_empty() || rest.starts_with('/'))
}

/// Links that lead away from the app belong in the browser.
fn open_outside(url: &str) {
    if url.starts_with("https://") || url.starts_with("http://") {
        links::open(&Target::Url(url.to_string()), "");
    }
}

/// Where a pane's view goes, as `[x, y, width, height]`.
pub(super) fn view_bounds(pane: Rect, scale: f32) -> [i32; 4] {
    let inset = (INSET * scale).round();
    let size = |s: f32| (s - 2.0 * inset).max(1.0).round() as i32;
    [(pane.x + inset).round() as i32, (pane.y + inset).round() as i32, size(pane.w), size(pane.h)]
}

fn to_web_rect([x, y, w, h]: [i32; 4]) -> wry::Rect {
    wry::Rect {
        position: PhysicalPosition::new(x, y).into(),
        size: PhysicalSize::new(w.max(1) as u32, h.max(1) as u32).into(),
    }
}

impl App {
    /// Whether the pane is still waiting for this: it may have been closed meanwhile.
    fn stop_waiting(&mut self, pane: PaneId, kind: WebKind) -> bool {
        let Some(st) = self.state.as_mut() else { return false };
        let waiting = st.web.opening.contains(&(pane, kind));
        st.web.opening.retain(|&o| o != (pane, kind));
        waiting
    }

    /// Show `url` in the pane's view of this kind, making the view if there is none.
    fn show_page(
        &mut self,
        pane: PaneId,
        kind: WebKind,
        dir: PathBuf,
        origin: &str,
        url: &str,
        load: bool,
    ) -> Result<(), String> {
        let Some(st) = self.state.as_mut() else { return Ok(()) };
        // A view is tied to its server's address.
        if let Some(i) = st.web.views.iter().position(|v| v.pane == pane && v.kind == kind && v.origin != origin) {
            let old = st.web.views.remove(i);
            let _ = old.web.focus_parent();
        }
        match st.web.get_mut(pane, kind) {
            Some(view) => {
                view.dir = dir;
                view.wanted = url.to_string();
                // Reloading the page it is already on would only lose the place in it.
                if load && !view.web.url().is_ok_and(|at| at == url) {
                    view.web.load_url(url).map_err(|e| e.to_string())?;
                }
            }
            None => build_view(st, &self.proxy, &self.keymap, pane, kind, dir, origin, url)?,
        }
        bring_forward(st, pane, kind);
        Ok(())
    }

    fn report(&mut self, kind: WebKind, result: Result<(), String>) {
        if let Err(e) = result {
            let name = match kind {
                WebKind::T3 => "T3 Code",
                WebKind::Code => "VS Code",
            };
            log::warn!("{name}: {e}");
            // A toast is one line.
            self.toast(format!("{name}: {}", e.lines().next().unwrap_or_default()));
        }
    }

    /// Close the app a pane is showing, leaving the pane and its shell. Returns false if
    /// the pane shows none.
    pub(super) fn close_web(&mut self, pane: PaneId) -> bool {
        let Some(st) = self.state.as_mut() else { return false };
        let Some(i) = st.web.views.iter().position(|v| v.pane == pane && !v.hidden && v.shown) else { return false };
        let view = st.web.views.remove(i);
        let _ = view.web.focus_parent();
        if let Some(p) = st.panes.iter_mut().find(|p| p.id == pane) {
            p.embeds.closed(Embed::Web(view.kind));
        }
        if st.web.keys == Some(pane) {
            st.web.keys = None;
        }
        let code_left = st.web.views.iter().any(|v| v.kind == WebKind::Code)
            || st.web.opening.iter().any(|&(_, kind)| kind == WebKind::Code);
        if view.kind == WebKind::Code && !code_left {
            vscode::release();
        }
        st.blink_epoch = Instant::now();
        st.window.request_redraw();
        // The keyboard goes back to the window, for the terminal.
        st.window.focus_window();
        true
    }

    /// Ctrl+Shift+T: switch the focused pane between its terminal and whatever is open
    /// over it, as it was left. With nothing open yet, T3 Code for the pane's folder.
    pub(super) fn toggle_embed(&mut self) {
        let Some(st) = self.state.as_mut() else { return };
        let id = st.focused;
        let in_view = st.web.active(id).is_some() || st.web.hosts.is_active(id);
        let Some(p) = st.panes.iter_mut().find(|p| p.id == id) else { return };
        let in_chat = p.chat.as_ref().is_some_and(|c| !c.hidden);
        let in_review = p.review.as_ref().is_some_and(|v| !v.hidden);
        if in_view || in_chat || in_review {
            if let Some(chat) = p.chat.as_mut() {
                chat.hidden = true;
            }
            if let Some(review) = p.review.as_mut() {
                review.hidden = true;
                review.blur();
            }
            st.web.hide(id);
            st.blink_epoch = Instant::now();
            st.window.request_redraw();
            return;
        }
        match p.embeds.latest() {
            Some(Embed::Web(kind)) => bring_forward(st, id, kind),
            Some(Embed::Host) => super::host::show_host(st, id),
            Some(Embed::Chat) => self.toggle_chat(),
            Some(Embed::Review) => self.show_review(id),
            None => {
                // Something may be on its way already.
                if st.web.opening.iter().any(|&(pane, _)| pane == id) {
                    return;
                }
                let cwd = p.cwd.clone().or_else(|| self.start_dir.clone()).or_else(dirs::home_dir);
                if let Some(cwd) = cwd {
                    self.open_t3(id, cwd, 1);
                }
            }
        }
    }

    /// Something a view's page sent.
    pub(super) fn web_message(&mut self, event_loop: &ActiveEventLoop, pane: PaneId, kind: WebKind, message: &str) {
        if kind == WebKind::T3 && (message == "pair" || message == "paired") {
            // Pairing goes on whether or not the view is in front.
            return self.t3_pairing(pane, message == "pair");
        }
        let Some(st) = self.state.as_mut() else { return };
        // A message can arrive after its view has been switched away from.
        if st.web.active(pane).is_none_or(|v| v.kind != kind || !v.shown) {
            return;
        }
        if st.focused != pane {
            let prev = st.focused;
            st.focused = pane;
            // The click that did this already gave the view the keyboard.
            st.web.keys = Some(pane);
            super::notify_focus(st, prev);
        }
        st.window.request_redraw();
        if let Some(action) = forwarded_action(message) {
            self.run_action(event_loop, action, false);
        }
    }

    /// A key that reached rustimal although the focused pane shows a web app (after a click
    /// on the title bar, say). Shortcuts work; nothing reaches the hidden shell.
    pub(super) fn web_key(&mut self, event_loop: &ActiveEventLoop, event: &KeyEvent) -> bool {
        let Some(st) = self.state.as_mut() else { return false };
        if st.web.active(st.focused).is_none_or(|v| !v.shown) && !st.web.hosts.shown(st.focused) {
            return false;
        }
        st.web.keys = None;
        st.window.request_redraw();
        let action = Chord::from_event(event, st.mods).and_then(|c| self.keymap.get(&c));
        let global = |a: Action| matches!(a, Action::Palette | Action::Settings);
        if action.is_some_and(|a| FORWARDED.contains(&a) || global(a)) {
            self.shortcut(event_loop, event);
        }
        true
    }

    /// winit reports the window losing focus when a view takes the keyboard.
    pub(super) fn web_focus_changed(&mut self, focused: bool) -> bool {
        let Some(st) = self.state.as_mut() else { return focused };
        if st.web.views.is_empty() && st.web.hosts.is_empty() {
            return focused;
        }
        if focused {
            // The window itself has the keyboard again; hand it back to the view.
            st.web.keys = None;
        }
        focused || in_foreground(&st.window)
    }

    /// While a view has the keyboard, winit doesn't hear that another app came to the
    /// front, so check before relying on it.
    pub(super) fn web_window_focus(&mut self) {
        if let Some(st) = self.state.as_mut()
            && !(st.web.views.is_empty() && st.web.hosts.is_empty())
        {
            st.window_focused = in_foreground(&st.window);
        }
    }

    /// Put each view over its pane, hide the ones that must not show, and move the
    /// keyboard to the focused one. Called with every frame.
    pub(super) fn sync_web(&mut self) {
        let Some(st) = self.state.as_ref() else { return };
        if st.web.views.is_empty() {
            return;
        }
        let covered = st.overlay.is_some();
        let Some(st) = self.state.as_mut() else { return };
        let State { web: apps, panes, picker, focused, scale, window_focused, .. } = st;
        for view in apps.views.iter_mut() {
            let pane = panes.iter().find(|p| p.id == view.pane);
            let rect = pane
                // A pane tucked away by a zoom has no place in the layout.
                .filter(|p| p.target.w > 0.0 && p.opacity > 0.5)
                .filter(|_| !covered && !view.hidden && picker.as_ref().is_none_or(|k| k.pane != view.pane))
                .map(|p| view_bounds(p.rect, *scale));
            if let Some(bounds) = rect
                && view.bounds != Some(bounds)
                && view.web.set_bounds(to_web_rect(bounds)).is_ok()
            {
                view.bounds = Some(bounds);
            }
            let show = rect.is_some();
            if view.shown != show {
                if !show && apps.keys == Some(view.pane) {
                    let _ = view.web.focus_parent();
                    apps.keys = None;
                }
                if view.web.set_visible(show).is_ok() {
                    view.shown = show;
                }
            }
        }
        // Never pull the keyboard over from another app.
        if !*window_focused {
            return;
        }
        let want = apps.active(*focused).is_some_and(|v| v.shown).then_some(*focused);
        if apps.keys == want {
            return;
        }
        match want {
            Some(id) => {
                if let Some(view) = apps.active(id) {
                    let _ = view.web.focus();
                }
            }
            None => {
                for view in apps.views.iter().filter(|v| Some(v.pane) == apps.keys && v.shown) {
                    let _ = view.web.focus_parent();
                }
            }
        }
        apps.keys = want;
    }
}

#[allow(clippy::too_many_arguments)]
fn build_view(
    st: &mut State,
    proxy: &winit::event_loop::EventLoopProxy<UserEvent>,
    keymap: &Keymap,
    pane: PaneId,
    kind: WebKind,
    dir: PathBuf,
    origin: &str,
    url: &str,
) -> Result<(), String> {
    let Some(rect) = st.panes.iter().find(|p| p.id == pane).map(|p| p.rect) else { return Ok(()) };
    let context = st.web.context.get_or_insert_with(|| {
        // Without a folder of its own, WebView2 writes next to the exe.
        let usual = dirs::data_local_dir().map(|d| d.join("rustimal").join("webview"));
        // For testing beside a rustimal that is in use: processes sharing a folder must
        // start the web view alike.
        let test = std::env::var_os("RUSTIMAL_WEB_DATA").filter(|_| cfg!(debug_assertions)).map(PathBuf::from);
        WebContext::new(test.or(usual))
    });
    let (own, navigation, proxy) = (origin.to_string(), origin.to_string(), proxy.clone());
    let web = WebViewBuilder::new_with_web_context(context)
        .with_bounds(to_web_rect(view_bounds(rect, st.scale)))
        .with_visible(false)
        .with_focused(false)
        .with_background_color((10, 10, 10, 255))
        .with_clipboard(true)
        // Dark pages, to match; VS Code picks its first theme by this.
        .with_theme(wry::Theme::Dark)
        // The browser's own shortcuts (reload, find, print, developer tools) would take
        // keys from the page: F5 and Ctrl+Shift+I are VS Code's, and rustimal's.
        .with_browser_accelerator_keys(false)
        .with_initialization_script(bridge_script(keymap, kind))
        .with_ipc_handler(move |request| {
            if same_origin(&request.uri().to_string(), &own) {
                let _ = proxy.send_event(UserEvent::WebMessage(pane, kind, request.into_body()));
            }
        })
        .with_navigation_handler(move |url| {
            let stays = same_origin(&url, &navigation);
            if !stays {
                open_outside(&url);
            }
            stays
        })
        .with_new_window_req_handler(|url, _| {
            open_outside(&url);
            NewWindowResponse::Deny
        })
        .build_as_child(&*st.window)
        .map_err(|e| format!("could not start the web view: {e}"))?;
    if kind == WebKind::Code {
        prefer_english_unless_chosen(&web, origin);
        if let Err(e) = web.zoom(CODE_ZOOM) {
            log::warn!("could not zoom VS Code: {e}");
        }
    }
    web.load_url(url).map_err(|e| e.to_string())?;
    st.web.views.push(View {
        pane,
        kind,
        web,
        dir,
        hidden: false,
        shown: false,
        bounds: None,
        origin: origin.to_string(),
        wanted: url.to_string(),
        paired_at: None,
        pairing: false,
        pair_tries: 0,
    });
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn only_pane_shortcuts_come_back_from_the_page() {
        let (keymap, _) = Keymap::build(&Default::default());
        let chords = forwarded_chords(&keymap);
        // The same in both apps: switching, splitting, moving between panes, closing.
        for (chord, id) in [
            ("ctrl+shift+t", "embed"),
            ("alt+t", "embed"),
            ("alt+shift+left", "resize_left"),
            ("ctrl+shift+r", "review"),
            ("ctrl+shift+i", "code"),
            ("ctrl+shift+right", "split_right"),
            ("ctrl+shift+down", "split_down"),
            ("alt+left", "focus_left"),
            ("ctrl+shift+w", "close_pane"),
        ] {
            assert_eq!(chords.get(chord), Some(&id), "{chord}");
        }
        assert_eq!(chords.get("ctrl+v"), None, "pasting stays the page's");
        assert_eq!(chords.get("ctrl+shift+p"), None, "so does VS Code's command palette");
        assert_eq!(forwarded_action("key:split_left"), Some(Action::SplitLeft));
        assert_eq!(forwarded_action("key:paste"), None, "a page can't ask for anything else");
        assert_eq!(forwarded_action("focus"), None);
        for kind in [WebKind::T3, WebKind::Code] {
            let script = bridge_script(&keymap, kind);
            assert!(
                script.contains(r#""ctrl+shift+t":"embed""#) && script.contains(r#""ctrl+shift+right":"split_right""#)
            );
        }
        assert!(!bridge_script(&keymap, WebKind::Code).contains("\"pair\""), "only T3 Code is paired");
        assert!(bridge_script(&keymap, WebKind::T3).contains("\"pair\""));
    }

    #[test]
    fn a_pairing_address_is_loaded_anew() {
        let script = go_and_reload("http://127.0.0.1:1/pair#token=A\"); alert(1);//");
        assert_eq!(
            script,
            "location.replace(\"http://127.0.0.1:1/pair#token=A\\\"); alert(1);//\"); location.reload();"
        );
    }

    #[test]
    fn a_folder_is_recognised_in_an_address() {
        let folder = Path::new(r"C:\my app\x");
        assert!(shows_folder("http://127.0.0.1:1/?folder=%2Fc%3A%2Fmy%20app%2Fx", folder));
        assert!(shows_folder("http://127.0.0.1:1/?tkn=k&folder=/C:/My%20App/x/&payload=1", folder));
        assert!(!shows_folder("http://127.0.0.1:1/?folder=/c:/my%20app", folder));
        assert!(!shows_folder("http://127.0.0.1:1/", folder));
    }

    #[test]
    fn the_shells_request_is_read() {
        let here = std::env::current_dir().unwrap();
        let text = here.to_string_lossy();
        assert_eq!(read_request(&format!("{text}\n")), Some((here.clone(), None, None)));
        assert_eq!(read_request(&format!("{text}\n12:5\n")), Some((here.clone(), Some(12), Some(5))));
        assert_eq!(read_request(&format!("{text}\nx:5")), Some((here, None, None)));
        assert_eq!(read_request("C:\\no\\such\\thing\n1"), None);
        assert_eq!(read_request(""), None);
    }

    #[test]
    fn other_sites_are_not_the_local_server() {
        let origin = "http://127.0.0.1:3773";
        assert!(same_origin("http://127.0.0.1:3773/e/t", origin));
        assert!(same_origin(origin, origin));
        assert!(!same_origin("http://127.0.0.1:37730/", origin));
        assert!(!same_origin("http://127.0.0.1:3773@example.com/", origin));
        assert!(!same_origin("https://example.com/", origin));
    }

    #[test]
    fn the_view_leaves_the_pane_rim_showing() {
        assert_eq!(view_bounds(Rect::new(10.0, 20.0, 300.0, 200.0), 1.0), [16, 26, 288, 188]);
        assert_eq!(view_bounds(Rect::new(0.0, 0.0, 4.0, 4.0), 1.0), [6, 6, 1, 1], "never empty");
    }
}