Cleaned up dependecies

This commit is contained in:
nik012003
2023-02-25 00:39:15 +01:00
committed by Nova
parent 692fded271
commit 8595199dcf
6 changed files with 156 additions and 139 deletions

View File

@@ -2,9 +2,7 @@ use crate::xdg::{DesktopFile, Icon, IconType};
use color_eyre::eyre::{eyre, Result};
use glam::Quat;
use mint::Vector3;
use fork::{daemon, Fork, setsid};
use std::process::{Command,Stdio};
use std::os::unix::process::CommandExt;
use nix::unistd::setsid;
use stardust_xr_molecules::{
fusion::{
client::{Client, FrameInfo, RootHandler},
@@ -17,17 +15,18 @@ use stardust_xr_molecules::{
},
GrabData, Grabbable,
};
use stardust_xr_molecules::{GrabData, Grabbable};
use std::{f32::consts::PI, ffi::CStr, sync::Arc};
use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio};
use std::{f32::consts::PI, sync::Arc};
use tween::{QuartInOut, Tweener};
use ustr::ustr;
use nix::unistd::fork;
fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
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]);
let t = Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[0.03, 0.03, 0.03],
);
let model = Model::create(
parent,
@@ -37,7 +36,7 @@ fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
model.set_material_parameter(
1,
"color",
MaterialParameter::Color([0.0,1.0,1.0,1.0]),
MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]),
)?;
model.set_material_parameter(
0,
@@ -75,19 +74,16 @@ impl ProtoStar {
IconType::Gltf => true,
_ => false,
})
.or(
raw_icons
.into_iter()
.max_by_key(|i| i.size)
);
.or(raw_icons.into_iter().max_by_key(|i| i.size));
match icon{
match icon {
Some(i) => {
icon = match i.cached_process(128) {
Ok(i) => Some(i),
_ => None,
}},
None => {},
}
}
None => {}
}
Self::new_raw(
@@ -120,7 +116,10 @@ impl ProtoStar {
.unwrap_or_else(|| {
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]),
Transform::from_rotation_scale(
Quat::from_xyzw(0.0, 0.707, 0.707, 0.0),
[0.03, 0.03, 0.03],
),
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?)
})?;
@@ -148,15 +147,15 @@ impl RootHandler for ProtoStar {
self.icon
.set_scale(None, Vector3::from([scale; 3]))
.unwrap();
}
if let Some(icon_grow) = &mut self.icon_shrink {
if !icon_grow.is_finished(){
let scale = icon_grow.move_by(info.delta);
self.icon
.set_scale(None, Vector3::from([scale; 3]))
.unwrap();
}
}
if let Some(icon_grow) = &mut self.icon_shrink {
if !icon_grow.is_finished() {
let scale = icon_grow.move_by(info.delta);
self.icon
.set_scale(None, Vector3::from([scale; 3]))
.unwrap();
}
}
} else if self.grabbable.grab_action().actor_stopped() {
let startup_settings = StartupSettings::create(&self.field.client().unwrap()).unwrap();
self.icon
@@ -180,15 +179,15 @@ impl RootHandler for ProtoStar {
std::env::set_var("STARDUST_STARTUP_TOKEN", future.await.unwrap());
unsafe {
Command::new(executable)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.pre_exec(|| {
setsid();
Ok(())
})
.spawn()
.expect("Failed to start child process")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.pre_exec(|| {
setsid();
Ok(())
})
.spawn()
.expect("Failed to start child process")
}
});
self.icon_grow = Some(Tweener::quart_in_out(0.00, 0.03, 0.25)); //TODO make the scale a parameter

View File

@@ -1,29 +1,29 @@
use cached::proc_macro::cached;
use color_eyre::eyre::Result;
use linicon;
use resvg::render;
use resvg::tiny_skia::{Pixmap, Transform};
use resvg::usvg::{FitTo, Tree};
use std::ffi::OsString;
use std::fs::{create_dir_all};
use std::os::unix::fs::{symlink};
use std::fs::create_dir_all;
use std::io::{BufRead, BufReader, ErrorKind};
use std::os::unix::fs::symlink;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{env, fs};
use walkdir::WalkDir;
use linicon;
use cached::proc_macro::cached;
fn get_data_dirs() -> Vec<PathBuf> {
let xdg_data_dirs_str = std::env::var("XDG_DATA_DIRS")
.unwrap_or_default();
let xdg_data_dirs_str = std::env::var("XDG_DATA_DIRS").unwrap_or_default();
let xdg_data_dirs = xdg_data_dirs_str
let xdg_data_dirs = xdg_data_dirs_str
.split(":")
.filter_map(|dir| PathBuf::from_str(dir).ok());
let data_home = dirs::home_dir()
.unwrap_or(PathBuf::from_str("/usr/share/")
.expect("No XDG_DATA_DIR set, no HOME directory found and no /usr/share direcotry found"))
.unwrap_or(PathBuf::from_str("/usr/share/").expect(
"No XDG_DATA_DIR set, no HOME directory found and no /usr/share direcotry found",
))
.join(".local")
.join("share");
@@ -33,7 +33,7 @@ fn get_data_dirs() -> Vec<PathBuf> {
.collect()
}
fn get_app_dirs() -> Vec<PathBuf>{
fn get_app_dirs() -> Vec<PathBuf> {
get_data_dirs()
.into_iter()
.map(|dir| dir.join("applications"))
@@ -107,7 +107,7 @@ pub fn parse_desktop_file(path: PathBuf) -> Result<DesktopFile, String> {
Some((key, value)) => (key, value),
None => continue,
};
// Parse the key-value pair based on the key
match key {
"Name" => name = Some(value.to_string()),
@@ -120,10 +120,12 @@ pub fn parse_desktop_file(path: PathBuf) -> Result<DesktopFile, String> {
.collect()
}
"Icon" => icon = Some(value.to_string()),
"NoDisplay" => no_display = match value{
"true" => true,
_ => false
},
"NoDisplay" => {
no_display = match value {
"true" => true,
_ => false,
}
}
_ => (), // Ignore unknown keys
}
}
@@ -175,27 +177,29 @@ impl DesktopFile {
let Some(icon_name) = self.icon.as_ref() else { return Vec::new(); };
let test_icon_path = self.path.join(Path::new(icon_name));
if test_icon_path.exists() {
if let Some(icon) = Icon::from_path(test_icon_path,128) {
return vec![icon]
if let Some(icon) = Icon::from_path(test_icon_path, 128) {
return vec![icon];
}
}
let cache_icon_path = get_image_cache_dir().join(icon_name).canonicalize();
if cache_icon_path.is_ok() {
return vec![Icon::from_path(cache_icon_path.unwrap(), 128).unwrap()]
return vec![Icon::from_path(cache_icon_path.unwrap(), 128).unwrap()];
}
let mut icons_iter= linicon::lookup_icon(icon_name).use_fallback_themes(false).peekable();
if icons_iter.peek().is_none(){
let mut icons_iter = linicon::lookup_icon(icon_name)
.use_fallback_themes(false)
.peekable();
if icons_iter.peek().is_none() {
//dbg!("No icons found in current theme");
icons_iter= linicon::lookup_icon(icon_name).peekable();
icons_iter = linicon::lookup_icon(icon_name).peekable();
}
let sized_png : Vec<Icon> = icons_iter
let sized_png: Vec<Icon> = icons_iter
.filter_map(|i| i.ok())
.filter(|i| i.icon_type != linicon::IconType::XMP) //TODO: support XMP
.map(|i| Icon::from_path(i.path,i.max_size - 2 ).unwrap())
.map(|i| Icon::from_path(i.path, i.max_size - 2).unwrap())
.collect();
sized_png
}
@@ -215,23 +219,29 @@ pub enum IconType {
Gltf,
}
impl Icon {
pub fn from_path(path: PathBuf, size: u16) -> Option<Icon>{
pub fn from_path(path: PathBuf, size: u16) -> Option<Icon> {
let icon_type = match path.extension().and_then(|ext| ext.to_str()) {
Some("png") => Some(IconType::Png),
Some("svg") => Some(IconType::Svg),
Some("glb") | Some("gltf") => Some(IconType::Gltf),
_ => {return None},
}.unwrap();
return Some(Icon{icon_type,path,size})
_ => return None,
}
.unwrap();
return Some(Icon {
icon_type,
path,
size,
});
}
pub fn cached_process(self, size: u16) -> Result<Icon, std::io::Error> {
let new_path = get_image_cache_dir().join(self.path.with_extension("").file_name().unwrap());
if !new_path.exists(){
let new_path =
get_image_cache_dir().join(self.path.with_extension("").file_name().unwrap());
if !new_path.exists() {
_ = symlink(self.path.clone(), new_path);
}
match self.icon_type {
IconType::Svg => Ok(Icon::from_path(get_png_from_svg(self.path, size)?,size).unwrap()),
IconType::Svg => Ok(Icon::from_path(get_png_from_svg(self.path, size)?, size).unwrap()),
_ => Ok(self),
}
}
@@ -254,39 +264,43 @@ fn test_get_icon_path() {
dbg!(&icon_paths);
// Assert that the get_icon_path() function returns the expected result
assert!(icon_paths.contains(&Icon::from_path(PathBuf::from("/usr/share/icons/hicolor/32x32/apps/krita.png"),32).unwrap()));
assert!(icon_paths.contains(
&Icon::from_path(
PathBuf::from("/usr/share/icons/hicolor/32x32/apps/krita.png"),
32
)
.unwrap()
));
}
#[cached]
pub fn get_image_cache_dir() -> PathBuf {
let cache_dir;
if let Ok(xdg_cache_home) = std::env::var("XDG_CACHE_HOME") {
cache_dir = PathBuf::from_str(&xdg_cache_home).unwrap_or(
dirs::home_dir().unwrap().join(".cache")
)
cache_dir =
PathBuf::from_str(&xdg_cache_home).unwrap_or(dirs::home_dir().unwrap().join(".cache"))
} else {
cache_dir = dirs::home_dir().unwrap().join(".cache");
}
let image_cache_dir = cache_dir.join("protostar_icon_cache");
create_dir_all(&image_cache_dir).expect("Could not create image cache directory");
return image_cache_dir
return image_cache_dir;
}
pub fn get_png_from_svg(svg_path: impl AsRef<Path>, size: u16,) -> Result<PathBuf, std::io::Error> {
pub fn get_png_from_svg(svg_path: impl AsRef<Path>, size: u16) -> Result<PathBuf, std::io::Error> {
let svg_path = fs::canonicalize(svg_path)?;
let svg_data = fs::read(svg_path.as_path())?;
let tree = Tree::from_data(
svg_data.as_slice(),
&resvg::usvg::Options::default(),
)
.map_err(|_| ErrorKind::InvalidData)?;
let png_path = get_image_cache_dir()
.join(format!("{}-{}.png",svg_path.file_name().unwrap().to_str().unwrap(), svg_data.len()));
let tree = Tree::from_data(svg_data.as_slice(), &resvg::usvg::Options::default())
.map_err(|_| ErrorKind::InvalidData)?;
let png_path = get_image_cache_dir().join(format!(
"{}-{}.png",
svg_path.file_name().unwrap().to_str().unwrap(),
svg_data.len()
));
if png_path.exists() {
return Ok(png_path)
return Ok(png_path);
}
let mut pixmap = Pixmap::new(size.into(), size.into()).unwrap();