Randomly offset new windows

This commit is contained in:
Gregory Schier
2023-04-14 14:05:23 -07:00
parent 24117f7c8d
commit 9cc6e62f28
2 changed files with 39 additions and 27 deletions

View File

@@ -9,13 +9,13 @@ extern crate objc;
use std::collections::HashMap; use std::collections::HashMap;
use std::env::current_dir; use std::env::current_dir;
use std::fs;
use std::fs::{create_dir_all, File}; use std::fs::{create_dir_all, File};
use std::io::Write; use std::io::Write;
use base64::Engine; use base64::Engine;
use http::header::{HeaderName, ACCEPT, USER_AGENT}; use http::header::{HeaderName, ACCEPT, USER_AGENT};
use http::{HeaderMap, HeaderValue, Method}; use http::{HeaderMap, HeaderValue, Method};
use rand::random;
use reqwest::redirect::Policy; use reqwest::redirect::Policy;
use serde::Serialize; use serde::Serialize;
use sqlx::migrate::Migrator; use sqlx::migrate::Migrator;
@@ -31,6 +31,8 @@ use tokio::sync::Mutex;
use window_ext::WindowExt; use window_ext::WindowExt;
use crate::models::generate_id;
mod models; mod models;
mod runtime; mod runtime;
mod window_ext; mod window_ext;
@@ -225,29 +227,33 @@ async fn actually_send_ephemeral_request(
response.url = v.url().to_string(); response.url = v.url().to_string();
let body_bytes = v.bytes().await.expect("Failed to get body").to_vec(); let body_bytes = v.bytes().await.expect("Failed to get body").to_vec();
response.content_length = Some(body_bytes.len() as i64); 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"); // Write body to FS
let body_path = base_dir.join(response.id.clone()); let dir = app_handle.path_resolver().app_data_dir().unwrap();
let mut f = File::options() let base_dir = dir.join("responses");
.create(true) create_dir_all(base_dir.clone()).expect("Failed to create responses dir");
.write(true) let body_path = base_dir.join(response.id.clone());
.open(&body_path) let mut f = File::options()
.expect("Failed to open file"); .create(true)
f.write_all(body_bytes.as_slice()) .write(true)
.expect("Failed to write to file"); .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 // Also store body directly on the model, if small enough
if body_bytes.len() < 100_000 { if body_bytes.len() < 100_000 {
response.body = Some(body_bytes); 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.elapsed = start.elapsed().as_millis() as i64;
response = models::update_response_if_id(&response, pool) response = models::update_response_if_id(&response, pool)
.await .await
@@ -688,7 +694,7 @@ fn create_window(handle: &AppHandle<Wry>, url: Option<&str>) -> Window<Wry> {
let submenu = Submenu::new("Test Menu", test_menu); let submenu = Submenu::new("Test Menu", test_menu);
let window_num = handle.windows().len(); 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 menu = default_menu.add_submenu(submenu);
let win = tauri::WindowBuilder::new( let win = tauri::WindowBuilder::new(
handle, handle,
@@ -699,6 +705,11 @@ fn create_window(handle: &AppHandle<Wry>, url: Option<&str>) -> Window<Wry> {
.fullscreen(false) .fullscreen(false)
.resizable(true) .resizable(true)
.inner_size(1100.0, 600.0) .inner_size(1100.0, 600.0)
.position(
// Randomly offset so windows don't stack exactly
100.0 + random::<f64>() * 30.0,
100.0 + random::<f64>() * 30.0,
)
.hidden_title(true) .hidden_title(true)
.title(match is_dev() { .title(match is_dev() {
true => "Yaak Dev", true => "Yaak Dev",

View File

@@ -177,7 +177,7 @@ pub async fn create_workspace(
description: &str, description: &str,
pool: &Pool<Sqlite>, pool: &Pool<Sqlite>,
) -> Result<Workspace, sqlx::Error> { ) -> Result<Workspace, sqlx::Error> {
let id = generate_id("wk"); let id = generate_id(Some("wk"));
sqlx::query!( sqlx::query!(
r#" r#"
INSERT INTO workspaces (id, name, description) INSERT INTO workspaces (id, name, description)
@@ -241,7 +241,7 @@ pub async fn upsert_request(
let id = match id { let id = match id {
Some(v) => v, Some(v) => v,
None => { None => {
generated_id = generate_id("rq"); generated_id = generate_id(Some("rq"));
generated_id.as_str() generated_id.as_str()
} }
}; };
@@ -382,7 +382,7 @@ pub async fn create_response(
pool: &Pool<Sqlite>, pool: &Pool<Sqlite>,
) -> Result<HttpResponse, sqlx::Error> { ) -> Result<HttpResponse, sqlx::Error> {
let req = get_request(request_id, pool).await?; let req = get_request(request_id, pool).await?;
let id = generate_id("rp"); let id = generate_id(Some("rp"));
let headers_json = Json(headers); let headers_json = Json(headers);
sqlx::query!( sqlx::query!(
r#" r#"
@@ -573,9 +573,10 @@ pub async fn delete_all_responses(
Ok(()) Ok(())
} }
pub fn generate_id(prefix: &str) -> String { pub fn generate_id(prefix: Option<&str>) -> String {
format!( let id = Alphanumeric.sample_string(&mut rand::thread_rng(), 10);
"{prefix}_{}", return match prefix {
Alphanumeric.sample_string(&mut rand::thread_rng(), 10) None => id,
) Some(p) => format!("{p}_{id}"),
};
} }