DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Binary/Build/WindowBuild.rs
1//! # Window Build Module
2//!
3//! Creates and configures the main application window.
4
5use tauri::{App, WebviewUrl, WebviewWindow, WebviewWindowBuilder, Wry};
6
7use crate::IPC::WindServiceHandlers::Utilities::RecentlyOpened::ReadRecentlyOpened;
8
9/// Creates and configures the main application window.
10///
11/// # Arguments
12///
13/// * `Application` - The Tauri application instance
14/// * `LocalhostUrl` - The localhost URL for the webview content
15///
16/// # Returns
17///
18/// A configured `WebviewWindow<Wry>` instance.
19///
20/// # Platform-Specific Behavior
21///
22/// - **macOS**: `TitleBarStyle::Overlay` + `hidden_title(true)` keeps the
23/// traffic-light buttons at the top-left but hides the native title bar
24/// strip, so VS Code's custom titlebar (which has `-webkit-app-region: drag`
25/// baked into its CSS) lights up the entire top row as a drag region. The
26/// previous `maximized(true)` was the direct cause of "can't drag the editor
27/// around" - maximized macOS windows are pinned to the screen and refuse all
28/// drag events, regardless of `app-region` CSS.
29/// - **Windows / Linux**: `decorations(false)` keeps the window chrome-less so
30/// the workbench draws its own. We still start `resizable(true)` so the
31/// window can be moved by the drag region.
32/// - **Debug builds**: DevTools auto-open.
33pub fn WindowBuild(Application:&mut App, LocalhostUrl:String) -> tauri::WebviewWindow<Wry> {
34 // Restore the most-recently-opened folder so the webview boots
35 // directly into the workspace. Without this, every launch lands
36 // on the Welcome tab, the user clicks "Open Folder", and the
37 // `pickFolderAndOpen` handler fires `Window.navigate()` - a hard
38 // reload that wipes workbench state mid-initialisation and is
39 // the direct cause of the purple splash flash, stuttering paint,
40 // empty `@builtin` sidebar, and broken keybindings (every layer
41 // has to boot twice and the second pass often loses references
42 // to the first). Using `?folder=...` in the initial URL skips
43 // that destructive round-trip.
44 let InitialUrl = BuildInitialUrl(&LocalhostUrl);
45
46 let WindowUrl = WebviewUrl::External(InitialUrl.parse().expect("FATAL: Failed to parse initial webview URL"));
47
48 // Configure window builder with base settings.
49 //
50 // `visible(false)` is the hidden-until-ready pattern. Tauri's default
51 // is to show the window the instant it's built, which paints the native
52 // chrome + Base.astro's `#1e1e1e` inline background + VS Code theme CSS
53 // + workbench DOM in four separate repaints over the first ~200 ms -
54 // observed as the "purple/dark flash" and panel-pop flicker.
55 //
56 // Mountain shows the window explicitly when the frontend's
57 // `lifecycle:advancePhase(3)` (Restored) arrives, which fires after
58 // `.monaco-workbench` is attached and the first frame is ready. A 3 s
59 // safety timer in `AppLifecycle` guarantees the window appears even if
60 // Sky crashes before signalling phase 3.
61 // Diagnostic initialization script. Runs at `document_start`, BEFORE any
62 // page <script> tag executes. Captures the state of Tauri's IPC bridge
63 // at the earliest possible point so a missing injection (forked Tauri
64 // runtime, mis-scoped capability, race condition) is detectable from
65 // the captured DevTools console + `window.__MOUNTAIN_TAURI_DIAG`
66 // snapshot - without depending on the rest of the bundle loading.
67 let TauriDiagnosticScript = r#"(function() {
68 if (window.__MOUNTAIN_TAURI_DIAG) { return; }
69 const Stamp = (Reason) => ({
70 at: Date.now(),
71 reason: Reason,
72 hasTAURI_INTERNALS: typeof window.__TAURI_INTERNALS__ === 'object' && window.__TAURI_INTERNALS__ !== null,
73 invokeOnInternals: typeof window.__TAURI_INTERNALS__?.invoke === 'function',
74 hasTAURI: typeof window.__TAURI__ === 'object' && window.__TAURI__ !== null,
75 invokeOnTauriCore: typeof window.__TAURI__?.core?.invoke === 'function',
76 invokeOnTauriDirect: typeof window.__TAURI__?.invoke === 'function',
77 hasIPCPostMessage: typeof window.ipc?.postMessage === 'function',
78 origin: window.location.origin,
79 url: window.location.href,
80 });
81 window.__MOUNTAIN_TAURI_DIAG = { initial: Stamp('initialization_script') };
82 try {
83 window.addEventListener('DOMContentLoaded', () => {
84 window.__MOUNTAIN_TAURI_DIAG.dom_content_loaded = Stamp('DOMContentLoaded');
85 });
86 window.addEventListener('load', () => {
87 window.__MOUNTAIN_TAURI_DIAG.load = Stamp('load');
88 });
89 } catch {}
90 })();"#;
91
92 let mut WindowBuilder = WebviewWindowBuilder::new(Application, "main", WindowUrl)
93 .use_https_scheme(false)
94 .initialization_script(TauriDiagnosticScript)
95 .zoom_hotkeys_enabled(true)
96 .browser_extensions_enabled(false)
97 // macOS first-responder: by default WKWebView swallows the
98 // first click on an unfocused window as a "make me key"
99 // no-op and the click never reaches the inner content. With
100 // `Inspect=1` running DevTools alongside the main window
101 // every switch-back to the editor needed two clicks - first
102 // to focus the NSWindow, second to actually focus the
103 // Monaco textarea - which the user reports as "I clicked,
104 // I'm typing, nothing's happening". `accept_first_mouse`
105 // flips the responder chain so the first click already
106 // reaches WKWebView's content and the textarea picks up
107 // keyboard input immediately.
108 .accept_first_mouse(true)
109 .title("Mountain")
110 .resizable(true)
111 .inner_size(1400.0, 900.0)
112 .shadow(true)
113 .visible(false);
114
115 #[cfg(target_os = "macos")]
116 {
117 // Overlay style lets VS Code's custom titlebar paint behind the
118 // traffic-light buttons. `hidden_title(true)` suppresses the OS
119 // title text so it doesn't collide with the workbench menubar.
120 // `decorations(true)` is REQUIRED for the traffic lights to
121 // render - turning decorations off also removes the buttons and
122 // breaks the native drag + resize handles entirely on macOS.
123 // `content_protected(true)` tells macOS to reserve a safe zone
124 // around the traffic-light cluster so WKWebView content doesn't
125 // render underneath them, preventing click-through and visual
126 // overlap with the workbench titlebar.
127 WindowBuilder = WindowBuilder
128 .title_bar_style(tauri::TitleBarStyle::Overlay)
129 .hidden_title(true)
130 .decorations(true)
131 .content_protected(true);
132 }
133
134 #[cfg(any(target_os = "windows", target_os = "linux"))]
135 {
136 WindowBuilder = WindowBuilder.decorations(false);
137 }
138
139 // Enable WKWebView inspection when InDebug mode + Inspect=1.
140 // This sets WKWebView.isInspectable via Wry's devtools flag so that
141 // external inspectors (Safari/Web Inspector) can attach.
142 #[cfg(debug_assertions)]
143 {
144 let enable_debugtools = std::env::var("Inspect").map(|v| v != "0" && !v.is_empty()).unwrap_or(false);
145 if enable_debugtools {
146 WindowBuilder = WindowBuilder.devtools(true);
147 }
148 }
149
150 #[cfg(debug_assertions)]
151 {
152 let enable_debug_server = std::env::var("DebugServer").map(|v| v != "0" && !v.is_empty()).unwrap_or(false);
153 if enable_debug_server {
154 WindowBuilder = WindowBuilder.on_page_load(|window, _payload| {
155 let _ = window.eval(
156 r#"(function() {
157 if (!window.__MOUNTAIN_DEBUG_CONSOLE) {
158 window.__MOUNTAIN_DEBUG_CONSOLE = [];
159 const origLog = console.log;
160 const origError = console.error;
161 const origWarn = console.warn;
162 const origInfo = console.info;
163 const origDebug = console.debug;
164 function pushLog(level, args) {
165 const argStrings = args.map(arg => {
166 if (typeof arg === 'object') {
167 try { return JSON.stringify(arg); } catch { return String(arg); }
168 } else {
169 return String(arg);
170 }
171 });
172 window.__MOUNTAIN_DEBUG_CONSOLE.push({ level, messages: argStrings, timestamp: Date.now() });
173 if (window.__MOUNTAIN_DEBUG_CONSOLE.length > 1000) {
174 window.__MOUNTAIN_DEBUG_CONSOLE = window.__MOUNTAIN_DEBUG_CONSOLE.slice(-1000);
175 }
176 }
177 console.log = function(...args) { pushLog('log', args); origLog.apply(console, args); };
178 console.error = function(...args) { pushLog('error', args); origError.apply(console, args); };
179 console.warn = function(...args) { pushLog('warn', args); origWarn.apply(console, args); };
180 console.info = function(...args) { pushLog('info', args); origInfo.apply(console, args); };
181 console.debug = function(...args) { pushLog('debug', args); origDebug.apply(console, args); };
182 }
183 })()"#,
184 );
185 });
186 }
187 }
188
189 // Build the main window
190 let MainWindow = WindowBuilder.build().expect("FATAL: Main window build failed");
191
192 // DevTools auto-open lives in `Binary/Main/AppLifecycle.rs:174`
193 // (gated on `cfg(debug_assertions)`, with a `[Window] Debug build:
194 // opening DevTools.` log line). Calling `open_devtools()` here as
195 // well opened a SECOND DevTools window on every debug launch -
196 // reported as "two DevTools" after the last rebuild. Single-source
197 // the call to AppLifecycle so the log line and the window match.
198
199 MainWindow
200}
201
202/// Build the initial webview URL, optionally appending `?folder=<path>`
203/// when `~/.fiddee/workspaces/RecentlyOpened.json` has an entry for the
204/// previous session's workspace. Falls back to plain `index.html` if
205/// the file is missing, malformed, or has no resolvable path.
206///
207/// The returned string is already URL-encoded and safe to feed to
208/// `WebviewUrl::External`.
209fn BuildInitialUrl(LocalhostUrl:&str) -> String {
210 let Base = format!("{}/index.html", LocalhostUrl);
211
212 let Recent = match ReadRecentlyOpened() {
213 Ok(Value) => Value,
214
215 Err(_) => return Base,
216 };
217
218 let Workspaces = match Recent.get("workspaces").and_then(|V| V.as_array()) {
219 Some(Array) if !Array.is_empty() => Array,
220
221 _ => return Base,
222 };
223
224 // VS Code's Recently-Opened record can store the folder under a few
225 // different shapes depending on whether the entry came from the
226 // extension host, the workbench, or a `$deltaWorkspaceFolders`
227 // broadcast. Probe them in the same priority order the workbench
228 // itself uses in `getRecentlyOpenedWorkspaces`.
229 let Probe = |Entry:&serde_json::Value| -> Option<String> {
230 // Mountain's own writer emits `{ uri: "file://…", label }` (see
231 // `RecentlyOpened.json` on a freshly closed window). VS Code's
232 // historical `folderUri` / `workspace.configPath` shapes are kept
233 // as fallbacks so imported profiles and third-party writers keep
234 // working.
235 if let Some(Uri) = Entry.get("uri").and_then(|V| V.as_str()) {
236 return Some(Uri.to_string());
237 }
238
239 if let Some(Uri) = Entry.get("folderUri").and_then(|V| V.as_str()) {
240 return Some(Uri.to_string());
241 }
242
243 if let Some(Path) = Entry.get("folderUri").and_then(|V| V.get("path")).and_then(|V| V.as_str()) {
244 return Some(Path.to_string());
245 }
246
247 if let Some(Path) = Entry
248 .get("workspace")
249 .and_then(|V| V.get("configPath"))
250 .and_then(|V| V.get("path"))
251 .and_then(|V| V.as_str())
252 {
253 return Some(Path.to_string());
254 }
255
256 None
257 };
258
259 let FolderPath = match Workspaces.iter().find_map(Probe) {
260 Some(Path) => Path,
261
262 None => return Base,
263 };
264
265 // Strip any `file://` scheme so the query param is a plain path
266 // the workbench will stringify into a `file:` URI itself; leaving
267 // the scheme in doubles up and breaks the URL-decode on the other
268 // side (observed as the second `?folder=` boot path appearing as
269 // `file:/Volumes/...` in `wb:boot`).
270 let WithoutScheme = FolderPath.strip_prefix("file://").unwrap_or(FolderPath.as_str()).to_string();
271
272 // RecentlyOpened.json stores workspace URIs with a trailing slash
273 // (`file:///Volumes/.../Mountain/`). Drop it before encoding into
274 // the URL so the workbench-side `URI.revive({ scheme: "file",
275 // path: <param> })` produces a folder URI that matches the
276 // workbench's own `URI.from(<file>)` results - which never carry
277 // a trailing slash on the parent directory. The mismatch caused
278 // `IUriIdentityService.extUri.relativePath` to return absolute
279 // paths and breadcrumbs / quick-pick / Problems-panel labels to
280 // render absolute `/Volumes/<vol>/...` paths instead of the workspace-relative
281 // short form. Preserve `/` itself when the path IS root (vanishing
282 // edge case but cheap to guard).
283 let TrailingTrimmed = WithoutScheme.trim_end_matches('/');
284
285 let Normalised = if TrailingTrimmed.is_empty() {
286 "/".to_string()
287 } else {
288 TrailingTrimmed.to_string()
289 };
290
291 if !std::path::Path::new(&Normalised).is_dir() {
292 return Base;
293 }
294
295 let Encoded = url::form_urlencoded::Serializer::new(String::new())
296 .append_pair("folder", &Normalised)
297 .finish();
298
299 format!("{}/?{}", LocalhostUrl, Encoded)
300}