From 6e4f3d670f045fd2826f8b99d5ea316ebe8f8313 Mon Sep 17 00:00:00 2001 From: Skyler Lehmkuhl Date: Fri, 17 Jan 2025 21:09:45 -0500 Subject: [PATCH] work on opening files --- src-tauri/src/lib.rs | 40 +++++++++++++++++++++++++++++++++++----- src/main.js | 11 ++++++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 444f7aa..65d9134 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -4,7 +4,7 @@ use tauri_plugin_log::{Target, TargetKind}; use log::{trace, info, debug, warn, error}; use tracing_subscriber::EnvFilter; use chrono::Local; -use tauri::{AppHandle, Manager, Url, WebviewUrl, WebviewWindowBuilder}; +use tauri::{webview, AppHandle, Manager, Url, WebviewUrl, WebviewWindowBuilder}; #[derive(Default)] @@ -39,23 +39,53 @@ fn error(msg: String) { error!("{}",msg); } +use tauri::PhysicalSize; + #[tauri::command] -async fn create_window(app: tauri::AppHandle) { +async fn create_window(app: tauri::AppHandle, path: Option) { let state = app.state::>(); // Lock the mutex to get mutable access: let mut state = state.lock().unwrap(); - + // Increment the counter and generate a unique window label let window_label = format!("window{}", state.counter); state.counter += 1; - + // Build the new window with the unique label - let _webview_window = WebviewWindowBuilder::new(&app, &window_label, WebviewUrl::App("index.html".into())) + let webview_window = WebviewWindowBuilder::new(&app, &window_label, WebviewUrl::App("index.html".into())) + .title("Lightningbeam") .build() .unwrap(); + + // Get the current monitor's screen size from the new window + if let Ok(Some(monitor)) = webview_window.current_monitor() { + let screen_size = monitor.size(); // Get the size of the monitor + let width = 4096; + let height = 4096; + + // Set the window size to be the smaller of the specified size or the screen size + let new_width = width.min(screen_size.width as u32); + let new_height = height.min(screen_size.height as u32 - 100); + + // Set the size using PhysicalSize + webview_window.set_size(tauri::Size::Physical(PhysicalSize::new(new_width, new_height))) + .expect("Failed to set window size"); + } else { + eprintln!("Could not detect the current monitor."); + } + + // Set the opened file if provided + if let Some(val) = path { + // Pass path data to the window via JavaScript + webview_window.eval(&format!("window.openedFiles = [\"{val}\"]")).unwrap(); + + // Set the window title if provided + webview_window.set_title(&val).expect("Failed to set window title"); + } } + fn handle_file_associations(app: AppHandle, files: Vec) { // -- Scope handling start -- diff --git a/src/main.js b/src/main.js index a1653e3..db0762d 100644 --- a/src/main.js +++ b/src/main.js @@ -4370,8 +4370,8 @@ function decrementFrame() { updateUI(); } -function newWindow() { - invoke("create_window", {app: window.__TAURI__.app}) +function newWindow(path) { + invoke("create_window", {app: window.__TAURI__.app, path: path}) } function _newFile(width, height, fps) { @@ -8318,4 +8318,9 @@ function renderAll() { renderAll(); -setTimeout(() => {console.log("Opened files", JSON.stringify(window.openedFiles))}, 1000) \ No newline at end of file +if (window.openedFiles?.length>0) { + _open(window.openedFiles[0]) + for (let i=1; i