From d5774c60671e7df790b20ab0aa0322a19642a613 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 14 Apr 2023 14:05:23 -0700 Subject: [PATCH] Randomly offset new windows --- src-tauri/src/main.rs | 49 +++++++++++++++++++++++++---------------- src-tauri/src/models.rs | 17 +++++++------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 5a4ed8ba..8a3d9cb4 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -9,13 +9,13 @@ extern crate objc; use std::collections::HashMap; use std::env::current_dir; -use std::fs; use std::fs::{create_dir_all, File}; use std::io::Write; use base64::Engine; use http::header::{HeaderName, ACCEPT, USER_AGENT}; use http::{HeaderMap, HeaderValue, Method}; +use rand::random; use reqwest::redirect::Policy; use serde::Serialize; use sqlx::migrate::Migrator; @@ -31,6 +31,8 @@ use tokio::sync::Mutex; use window_ext::WindowExt; +use crate::models::generate_id; + mod models; mod runtime; mod window_ext; @@ -225,29 +227,33 @@ async fn actually_send_ephemeral_request( response.url = v.url().to_string(); let body_bytes = v.bytes().await.expect("Failed to get body").to_vec(); response.content_length = Some(body_bytes.len() as i64); - let dir = app_handle.path_resolver().app_data_dir().unwrap(); - let base_dir = dir.join("responses"); - create_dir_all(base_dir.clone()).expect("Failed to create responses dir"); - let body_path = base_dir.join(response.id.clone()); - let mut f = File::options() - .create(true) - .write(true) - .open(&body_path) - .expect("Failed to open file"); - f.write_all(body_bytes.as_slice()) - .expect("Failed to write to file"); + + { + // Write body to FS + let dir = app_handle.path_resolver().app_data_dir().unwrap(); + let base_dir = dir.join("responses"); + create_dir_all(base_dir.clone()).expect("Failed to create responses dir"); + let body_path = base_dir.join(response.id.clone()); + let mut f = File::options() + .create(true) + .write(true) + .open(&body_path) + .expect("Failed to open file"); + f.write_all(body_bytes.as_slice()) + .expect("Failed to write to file"); + response.body_path = Some( + body_path + .to_str() + .expect("Failed to get body path") + .to_string(), + ); + } // Also store body directly on the model, if small enough if body_bytes.len() < 100_000 { response.body = Some(body_bytes); } - response.body_path = Some( - body_path - .to_str() - .expect("Failed to get body path") - .to_string(), - ); response.elapsed = start.elapsed().as_millis() as i64; response = models::update_response_if_id(&response, pool) .await @@ -688,7 +694,7 @@ fn create_window(handle: &AppHandle, url: Option<&str>) -> Window { let submenu = Submenu::new("Test Menu", test_menu); let window_num = handle.windows().len(); - let window_id = format!("wnd_{}", window_num); + let window_id = format!("wnd_{}_{}", window_num, generate_id(None)); let menu = default_menu.add_submenu(submenu); let win = tauri::WindowBuilder::new( handle, @@ -699,6 +705,11 @@ fn create_window(handle: &AppHandle, url: Option<&str>) -> Window { .fullscreen(false) .resizable(true) .inner_size(1100.0, 600.0) + .position( + // Randomly offset so windows don't stack exactly + 100.0 + random::() * 30.0, + 100.0 + random::() * 30.0, + ) .hidden_title(true) .title(match is_dev() { true => "Yaak Dev", diff --git a/src-tauri/src/models.rs b/src-tauri/src/models.rs index 4f93252c..df7146c8 100644 --- a/src-tauri/src/models.rs +++ b/src-tauri/src/models.rs @@ -177,7 +177,7 @@ pub async fn create_workspace( description: &str, pool: &Pool, ) -> Result { - let id = generate_id("wk"); + let id = generate_id(Some("wk")); sqlx::query!( r#" INSERT INTO workspaces (id, name, description) @@ -241,7 +241,7 @@ pub async fn upsert_request( let id = match id { Some(v) => v, None => { - generated_id = generate_id("rq"); + generated_id = generate_id(Some("rq")); generated_id.as_str() } }; @@ -382,7 +382,7 @@ pub async fn create_response( pool: &Pool, ) -> Result { let req = get_request(request_id, pool).await?; - let id = generate_id("rp"); + let id = generate_id(Some("rp")); let headers_json = Json(headers); sqlx::query!( r#" @@ -573,9 +573,10 @@ pub async fn delete_all_responses( Ok(()) } -pub fn generate_id(prefix: &str) -> String { - format!( - "{prefix}_{}", - Alphanumeric.sample_string(&mut rand::thread_rng(), 10) - ) +pub fn generate_id(prefix: Option<&str>) -> String { + let id = Alphanumeric.sample_string(&mut rand::thread_rng(), 10); + return match prefix { + None => id, + Some(p) => format!("{p}_{id}"), + }; }