webizen_studio/
endpoints.rs1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub enum HostSurface {
11 PublicWeb,
12 DesktopWebview,
13}
14
15const DEFAULT_DAEMON_PORT: u16 = 8080;
16pub const NATIVE_WS: &str = "ws://127.0.0.1:4242";
18pub const MODULE_RPC_WS: &str = "ws://127.0.0.1:9001";
20
21pub fn daemon_http() -> String {
23 format!("http://127.0.0.1:{}", settings_port())
24}
25
26#[cfg(target_arch = "wasm32")]
27fn settings_port() -> u16 {
28 js_sys::Reflect::get(
29 &js_sys::global(),
30 &wasm_bindgen::JsValue::from_str("__WEBIZEN_SETTINGS_PORT"),
31 )
32 .ok()
33 .and_then(|v| v.as_f64())
34 .map(|v| v as u16)
35 .filter(|port| *port > 0)
36 .unwrap_or(DEFAULT_DAEMON_PORT)
37}
38
39#[cfg(not(target_arch = "wasm32"))]
40fn settings_port() -> u16 {
41 DEFAULT_DAEMON_PORT
42}
43
44pub fn manifest_url() -> String {
46 format!("{}/manifest", daemon_http())
47}
48
49pub fn manifest_history_url() -> String {
51 format!("{}/manifest/history", daemon_http())
52}
53
54pub fn portal_design_studio_url() -> String {
56 format!("{}/design-studio.html", daemon_http())
57}
58
59pub fn assets_catalog_url() -> String {
61 format!("{}/api/assets/catalog", daemon_http())
62}
63
64pub fn assets_enqueue_url() -> String {
66 format!("{}/api/assets/enqueue", daemon_http())
67}
68
69pub fn job_url(job_id: &str) -> String {
71 format!("{}/api/jobs/{job_id}", daemon_http())
72}
73
74pub fn manifest_replay_url(revision: u64) -> String {
76 format!("{}/manifest/replay/{revision}", daemon_http())
77}
78
79pub fn generate_pane_url() -> String {
81 format!("{}/generate_pane", daemon_http())
82}
83
84pub fn manifest_undo_chain_url() -> String {
86 format!("{}/manifest/undo-chain", daemon_http())
87}
88
89pub fn manifest_undo_frame_url(stack_index: u16) -> String {
91 format!(
92 "{}/manifest/undo-frame?stack_index={stack_index}",
93 daemon_http()
94 )
95}
96
97pub fn telemetry_url() -> String {
99 format!("{}/telemetry", daemon_http())
100}
101
102pub fn logs_page_url() -> String {
104 format!("{}/logs", daemon_http())
105}
106
107pub fn native_handshake_ws() -> &'static str {
109 NATIVE_WS
110}
111
112pub fn host_surface_label(surface: HostSurface) -> &'static str {
114 match surface {
115 HostSurface::PublicWeb => "Public web demo",
116 HostSurface::DesktopWebview => "Desktop webview",
117 }
118}
119
120pub fn all_host_surfaces() -> [HostSurface; 2] {
122 [HostSurface::DesktopWebview, HostSurface::PublicWeb]
123}
124
125#[cfg(not(target_arch = "wasm32"))]
127pub fn probe_native_handshake_port() -> bool {
128 let addr = NATIVE_WS
129 .trim_start_matches("ws://")
130 .trim_start_matches("wss://");
131 std::net::TcpStream::connect(addr).is_ok()
132}
133
134#[cfg(target_arch = "wasm32")]
135pub fn probe_native_handshake_port() -> bool {
137 false
138}
139
140pub const SHOELACE_VENDOR_BASE: &str = "/vendor/shoelace";
142pub const SHOELACE_CDN_BASE: &str =
143 "https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.15.0/cdn";
144
145pub fn shoelace_base() -> &'static str {
147 if is_native_host() {
148 SHOELACE_VENDOR_BASE
149 } else {
150 SHOELACE_CDN_BASE
151 }
152}
153
154pub fn shoelace_stylesheet_href() -> String {
156 format!("{}/themes/dark.css", shoelace_base())
157}
158
159pub fn shoelace_autoloader_src() -> String {
161 format!("{}/shoelace-autoloader.js", shoelace_base())
162}
163
164#[cfg(target_arch = "wasm32")]
170pub fn is_native_host() -> bool {
171 current_host_surface() != HostSurface::PublicWeb
172}
173
174#[cfg(not(target_arch = "wasm32"))]
176pub fn is_native_host() -> bool {
177 true
178}
179
180#[cfg(target_arch = "wasm32")]
181pub fn current_host_surface() -> HostSurface {
183 let tauri_present = js_sys::Reflect::get(
184 &js_sys::global(),
185 &wasm_bindgen::JsValue::from_str("__TAURI__"),
186 )
187 .map(|v| !v.is_undefined() && !v.is_null())
188 .unwrap_or(false);
189
190 if tauri_present {
191 HostSurface::DesktopWebview
192 } else {
193 HostSurface::PublicWeb
194 }
195}
196
197#[cfg(not(target_arch = "wasm32"))]
198pub fn current_host_surface() -> HostSurface {
200 HostSurface::DesktopWebview
201}
202
203pub fn supports_browser_pane() -> bool {
205 matches!(current_host_surface(), HostSurface::DesktopWebview)
206}