ctb_io_webui/
webview.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Native desktop webview embedding and window integration.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29/// Opens an `http://` or `https://` URL in the user's default web browser.
30pub fn open_browser_url(url: &str) -> Result<()> {
31    #[cfg(not(target_os = "macos"))]
32    {
33        webbrowser::open(url).map_err(|err| anyhow::anyhow!("{err}"))
34    }
35
36    #[cfg(target_os = "macos")]
37    {
38        anyhow::ensure!(
39            url.starts_with("http://") || url.starts_with("https://"),
40            "Refusing to open non-HTTP(S) URL: {url}"
41        );
42        let status = std::process::Command::new("/usr/bin/open")
43            .arg("--")
44            .arg(url)
45            .status()
46            .context("Failed to execute /usr/bin/open")?;
47        anyhow::ensure!(
48            status.success(),
49            "/usr/bin/open exited with status {status}"
50        );
51        Ok(())
52    }
53}
54
55pub fn start_webview(url: String) {
56    if open_browser_url(&url).is_err() {
57        eprintln!("Failed to open web browser to URL: {url}");
58    }
59}