From fd0317142965906b05dd9d1cff4476a5e2eafcbf Mon Sep 17 00:00:00 2001 From: Nicola Guerrera Date: Wed, 1 Mar 2023 10:56:06 +0100 Subject: [PATCH] Clean up --- examples/hexagon_launcher.rs | 21 +++++++-------- src/protostar.rs | 50 +++++++++++++++++++++--------------- src/xdg.rs | 9 +++---- 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/examples/hexagon_launcher.rs b/examples/hexagon_launcher.rs index 7d00192..c337868 100644 --- a/examples/hexagon_launcher.rs +++ b/examples/hexagon_launcher.rs @@ -17,7 +17,9 @@ use stardust_xr_molecules::touch_plane::TouchPlane; use std::f32::consts::PI; use tween::TweenTime; -const APP_SIZE: f32 = 0.065; +const APP_SIZE: f32 = 0.06; +const PADDING: f32 = 0.005; + #[derive(Clone)] struct Hex { q: isize, @@ -41,9 +43,9 @@ impl Hex { } fn get_coords(&self) -> [f32; 3] { - let x = 3.0 / 2.0 * APP_SIZE / 2.0 * (-self.q - self.s).to_f32(); - let y = - 3.0_f32.sqrt() * APP_SIZE / 2.0 * ((-self.q - self.s).to_f32() / 2.0 + self.s.to_f32()); + let x = 3.0 / 2.0 * (APP_SIZE + PADDING) / 2.0 * (-self.q - self.s).to_f32(); + let y = 3.0_f32.sqrt() * (APP_SIZE + PADDING) / 2.0 + * ((-self.q - self.s).to_f32() / 2.0 + self.s.to_f32()); [x, y, 0.0] } @@ -126,7 +128,6 @@ impl RootHandler for AppHexGrid { fn frame(&mut self, info: FrameInfo) { self.button.touch_plane.update(); if self.button.touch_plane.touch_started() { - dbg!("Touch started"); let color = [0.0, 1.0, 0.0, 1.0]; self.button .model @@ -161,9 +162,9 @@ impl App { ) -> Option { let position = position.into(); let style = TextStyle { - character_height: APP_SIZE * 0.1, + character_height: (APP_SIZE + PADDING) * 0.1, bounds: Some(Bounds { - bounds: [APP_SIZE; 2].into(), + bounds: [(APP_SIZE + PADDING); 2].into(), fit: TextFit::Wrap, bounds_align: Alignment::XCenter | Alignment::YCenter, }), @@ -174,7 +175,7 @@ impl App { ProtoStar::create_from_desktop_file(parent, position, desktop_file.clone()).ok()?; let text = Text::create( protostar.content_parent(), - Transform::from_position_rotation([0.0, 0.0, 0.004], Quat::from_rotation_y(3.14)), + Transform::from_position_rotation([0.0, 0.0, 0.004], Quat::from_rotation_y(PI)), desktop_file.name.as_deref().unwrap_or("Unknown"), style, ) @@ -201,8 +202,8 @@ impl Button { let touch_plane = TouchPlane::new( client.get_root(), Transform::default(), - [APP_SIZE / 2.0, APP_SIZE / 2.0], - APP_SIZE / 2.0, + [(APP_SIZE + PADDING) / 2.0; 2], + (APP_SIZE + PADDING) / 2.0, )?; let model = Model::create( client.get_root(), diff --git a/src/protostar.rs b/src/protostar.rs index 7d6fed8..199c452 100644 --- a/src/protostar.rs +++ b/src/protostar.rs @@ -19,12 +19,15 @@ use std::process::{Command, Stdio}; use std::{f32::consts::PI, sync::Arc}; use tween::{QuartInOut, Tweener}; +const MODEL_SCALE: f32 = 0.03; +const ACTIVATION_DISTANCE: f32 = 1.0; + fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result { return match &icon.icon_type { IconType::Png => { let t = Transform::from_rotation_scale( Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI), - [0.03, 0.03, 0.03], + [MODEL_SCALE; 3], ); let model = Model::create( @@ -61,9 +64,9 @@ pub struct ProtoStar { icon: Model, grabbable_shrink: Option>, grabbable_grow: Option>, + grabbabe_move: Option>, execute_command: String, currently_shown: bool, - grabbabe_move: Option>, } impl ProtoStar { pub fn create_from_desktop_file( @@ -130,8 +133,8 @@ impl ProtoStar { Ok(Model::create( grabbable.content_parent(), Transform::from_rotation_scale( - Quat::from_xyzw(0.0, 0.707, 0.707, 0.0), - [0.03, 0.03, 0.03], + Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI), + [MODEL_SCALE; 3], ), &ResourceID::new_namespaced("protostar", "hexagon/hexagon"), )?) @@ -157,9 +160,9 @@ impl ProtoStar { self.grabbabe_move = Some(Tweener::quart_in_out(1.0, 0.0001, 0.25)); //TODO make the scale a parameter } else { self.grabbable - .content_parent() - .set_scale(None, Vector3::from([1.0; 3])) - .unwrap(); + .content_parent() + .set_scale(None, Vector3::from([1.0; 3])) + .unwrap(); self.grabbabe_move = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25)); } self.currently_shown = !self.currently_shown; @@ -167,7 +170,6 @@ impl ProtoStar { } impl RootHandler for ProtoStar { fn frame(&mut self, info: FrameInfo) { - self.grabbable.update(&info); if let Some(grabbabe_move) = &mut self.grabbabe_move { @@ -175,14 +177,21 @@ impl RootHandler for ProtoStar { let scale = grabbabe_move.move_by(info.delta); self.grabbable .content_parent() - .set_position(Some(self.client.get_root()), [self.position.x*scale, self.position.y*scale, self.position.z*scale]) + .set_position( + Some(self.client.get_root()), + [ + self.position.x * scale, + self.position.y * scale, + self.position.z * scale, + ], + ) .unwrap(); } else { if grabbabe_move.final_value() == 0.0001 { self.grabbable - .content_parent() - .set_scale(None, Vector3::from([0.001; 3])) - .unwrap(); + .content_parent() + .set_scale(None, Vector3::from([0.001; 3])) + .unwrap(); } self.grabbabe_move = None; } @@ -196,7 +205,7 @@ impl RootHandler for ProtoStar { .unwrap(); } else { if self.currently_shown { - self.grabbable_grow = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25)); //TODO make the scale a parameter + self.grabbable_grow = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25)); self.grabbable.cancel_angular_velocity(); self.grabbable.cancel_linear_velocity(); } @@ -231,7 +240,7 @@ impl RootHandler for ProtoStar { startup_settings .set_root(self.grabbable.content_parent()) .unwrap(); - self.grabbable_shrink = Some(Tweener::quart_in_out(0.03, 0.0001, 0.25)); //TODO make the scale a parameter + self.grabbable_shrink = Some(Tweener::quart_in_out(MODEL_SCALE, 0.0001, 0.25)); let distance_future = self .grabbable .content_parent() @@ -243,18 +252,19 @@ impl RootHandler for ProtoStar { //TODO: split the executable string for the args tokio::task::spawn(async move { let distance_vector = distance_future.await.ok().unwrap().0; - let distance = - distance_vector.x.abs() + distance_vector.y.abs() + distance_vector.z.abs(); - if dbg!(distance) > 1.0 { + let distance = ((distance_vector.x.powi(2) + distance_vector.y.powi(2)).sqrt() + + distance_vector.z.powi(2)) + .sqrt(); + if dbg!(distance) > ACTIVATION_DISTANCE { let future = startup_settings.generate_startup_token().unwrap(); std::env::set_var("STARDUST_STARTUP_TOKEN", future.await.unwrap()); let re = Regex::new(r"%[fFuUdDnNickvm]").unwrap(); let exec = re.replace_all(&executable, ""); - let mut executable_array : Vec<&str> = dbg!(exec.split_whitespace().collect()); unsafe { - Command::new(executable_array.remove(0)) - .args(executable_array) + Command::new("sh") + .arg("-c") + .arg(exec.to_string()) .stdin(Stdio::null()) .stdout(Stdio::null()) .stderr(Stdio::null()) diff --git a/src/xdg.rs b/src/xdg.rs index f047082..e1b9f1d 100644 --- a/src/xdg.rs +++ b/src/xdg.rs @@ -1,6 +1,7 @@ use cached::proc_macro::cached; use color_eyre::eyre::Result; use linicon; +use regex::Regex; use resvg::render; use resvg::tiny_skia::{Pixmap, Transform}; use resvg::usvg::{FitTo, Tree}; @@ -12,7 +13,6 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use std::{env, fs}; use walkdir::WalkDir; -use regex::Regex; fn get_data_dirs() -> Vec { let xdg_data_dirs_str = std::env::var("XDG_DATA_DIRS").unwrap_or_default(); @@ -88,13 +88,12 @@ pub fn parse_desktop_file(path: PathBuf) -> Result { let mut categories = Vec::new(); let mut icon = None; let mut no_display = false; - let mut desktop_entry_found = false; + let mut desktop_entry_found = false; let re = Regex::new(r"^\[([^\]]*)\]$").unwrap(); // Loop through each line of the file for line in reader.lines() { - let line = match line { Ok(line) => line, Err(err) => return Err(format!("Failed to read line: {}", err)), @@ -105,13 +104,13 @@ pub fn parse_desktop_file(path: PathBuf) -> Result { continue; } - if let Some(captures) = re.captures(&line){ + if let Some(captures) = re.captures(&line) { let entry = captures.get(1).unwrap(); desktop_entry_found = entry.as_str().contains("Desktop Entry"); } if !desktop_entry_found { - continue + continue; } // Split the line into a key-value pair by looking for the first "=" character let parts = line.split_once('=');