Merge branch 'app_grid' of https://github.com/StardustXR/protostar into StardustXR-app_grid

This commit is contained in:
Nicola Guerrera
2023-05-14 19:23:10 +02:00
11 changed files with 1701 additions and 398 deletions

602
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -5,12 +5,13 @@ edition = "2021"
[dependencies]
cached = "0.42.0"
cached = "0.43.0"
clap = { version = "4.1.3", features = ["derive"] }
color-eyre = "0.6.2"
directories = "4.0.1"
dirs = "4.0.0"
directories = "5.0.0"
dirs = "5.0.0"
ez-pixmap = "0.2.2"
glam = { version = "0.22.0", features = ["mint"] }
glam = { version = "0.24.0", features = ["mint"] }
image = "0.24.5"
lazy_static = "1.4.0"
linicon = "2.3.0"
@@ -25,11 +26,13 @@ serde_json = "1.0.94"
serde_with = "2.3.1"
stardust-xr-fusion = "0.40.0"
stardust-xr-molecules = "0.24.3"
serde = "1.0.155"
serde_json = "1.0.94"
serde_with = "2.3.1"
stardust-xr-fusion = "0.40.2"
stardust-xr-molecules = "0.24.3"
tokio = { version = "1.24.1", features = ["full"] }
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
tween = "2.0.0"
ustr = "0.9.0"
walkdir = "2.3.2"
[dev-dependencies]
tempdir = "0.3.7"

Binary file not shown.

View File

@@ -231,3 +231,446 @@ impl RootHandler for Button {
self.grabbable.update(&info).unwrap();
}
}
use color_eyre::eyre::Result;
use glam::Quat;
use manifest_dir_macros::directory_relative_path;
use mint::Vector3;
use protostar::{
application::Application,
xdg::{get_desktop_files, parse_desktop_file, DesktopFile, Icon, IconType},
};
use stardust_xr_fusion::{
client::{Client, FrameInfo, RootHandler},
core::values::Transform,
drawable::{Alignment, Bounds, MaterialParameter, Model, ResourceID, Text, TextFit, TextStyle},
fields::BoxField,
node::NodeError,
node::NodeType,
spatial::Spatial,
};
use stardust_xr_molecules::{touch_plane::TouchPlane, GrabData, Grabbable};
use std::f32::consts::PI;
use tween::TweenTime;
use tween::{QuartInOut, Tweener};
const APP_SIZE: f32 = 0.06;
const PADDING: f32 = 0.005;
const ACTIVATION_DISTANCE: f32 = 0.5;
#[derive(Clone)]
struct Hex {
q: isize,
r: isize,
s: isize,
}
const HEX_CENTER: Hex = Hex { q: 0, r: 0, s: 0 };
const HEX_DIRECTION_VECTORS: [Hex; 6] = [
Hex { q: 1, r: 0, s: -1 },
Hex { q: 1, r: -1, s: 0 },
Hex { q: 0, r: -1, s: 1 },
Hex { q: -1, r: 0, s: 1 },
Hex { q: -1, r: 1, s: 0 },
Hex { q: 0, r: 1, s: -1 },
];
impl Hex {
fn new(q: isize, r: isize, s: isize) -> Self {
Hex { q, r, s }
}
fn get_coords(&self) -> [f32; 3] {
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]
}
fn add(self, vec: &Hex) -> Self {
Hex::new(self.q + vec.q, self.r + vec.r, self.s + vec.s)
}
fn neighbor(self, direction: usize) -> Self {
self.add(&HEX_DIRECTION_VECTORS[direction])
}
fn scale(self, factor: isize) -> Self {
Hex::new(self.q * factor, self.r * factor, self.s * factor)
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
color_eyre::install().unwrap();
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.pretty()
.init();
let (client, event_loop) = Client::connect_with_async_loop().await?;
client.set_base_prefixes(&[directory_relative_path!("res")]);
let _root = client.wrap_root(AppHexGrid::new(&client))?;
tokio::select! {
_ = tokio::signal::ctrl_c() => (),
e = event_loop => e??,
};
Ok(())
}
struct AppHexGrid {
apps: Vec<App>,
button: Button,
}
impl AppHexGrid {
fn new(client: &Client) -> Self {
let button = Button::new(client).unwrap();
let mut desktop_files: Vec<DesktopFile> = get_desktop_files()
.into_iter()
.filter_map(|d| parse_desktop_file(d).ok())
.filter(|d| !d.no_display)
.collect();
desktop_files.sort_by_key(|d| d.clone().name.unwrap_or_default());
let mut apps = Vec::new();
let mut radius = 1;
while !desktop_files.is_empty() {
let mut hex = HEX_CENTER.add(&HEX_DIRECTION_VECTORS[4].clone().scale(radius));
for i in 0..6 {
if desktop_files.is_empty() {
break;
};
for _ in 0..radius {
if desktop_files.is_empty() {
break;
};
apps.push(
App::create_from_desktop_file(
button.grabbable.content_parent(),
hex.get_coords(),
desktop_files.pop().unwrap(),
)
.unwrap(),
);
hex = hex.neighbor(i);
}
}
radius += 1;
}
AppHexGrid { apps, button }
}
}
impl RootHandler for AppHexGrid {
fn frame(&mut self, info: FrameInfo) {
self.button.frame(info);
if self.button.touch_plane.touch_started() {
let color = [0.0, 1.0, 0.0, 1.0];
self.button
.model
.model_part("Hex")
.unwrap()
.set_material_parameter("color", MaterialParameter::Color(color))
.unwrap();
for app in &mut self.apps {
app.toggle();
}
} else if self.button.touch_plane.touch_stopped() {
let color = [0.0, 0.0, 1.0, 1.0];
self.button
.model
.model_part("Hex")
.unwrap()
.set_material_parameter("color", MaterialParameter::Color(color))
.unwrap();
}
for app in &mut self.apps {
app.frame(info);
}
}
}
struct Button {
touch_plane: TouchPlane,
grabbable: Grabbable,
model: Model,
}
impl Button {
fn new(client: &Client) -> Result<Self, NodeError> {
let field = BoxField::create(client.get_root(), Transform::default(), [APP_SIZE; 3])?;
let grabbable = Grabbable::create(
client.get_root(),
Transform::default(),
&field,
GrabData {
max_distance: 0.01,
..Default::default()
},
)?;
field.set_spatial_parent(grabbable.content_parent())?;
let touch_plane = TouchPlane::create(
grabbable.content_parent(),
Transform::default(),
[(APP_SIZE + PADDING) / 2.0; 2],
(APP_SIZE + PADDING) / 2.0,
0.0..1.0,
0.0..1.0,
)?;
let model = Model::create(
grabbable.content_parent(),
Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[0.03, 0.03, 0.03],
),
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?;
model
.model_part("Hex")?
.set_material_parameter("color", MaterialParameter::Color([0.0, 0.0, 1.0, 1.0]))?;
Ok(Button {
touch_plane,
grabbable,
model,
})
}
}
impl RootHandler for Button {
fn frame(&mut self, info: FrameInfo) {
let _ = self.grabbable.update(&info);
if self.grabbable.grab_action().actor_started() {
let _ = self.touch_plane.set_enabled(false);
}
if self.grabbable.grab_action().actor_stopped() {
let _ = self.touch_plane.set_enabled(true);
}
self.touch_plane.update();
}
}
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),
[APP_SIZE * 0.5; 3],
);
let model = Model::create(
parent,
t,
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?;
model
.model_part("Hex")?
.set_material_parameter("color", MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]))?;
model.model_part("Icon")?.set_material_parameter(
"diffuse",
MaterialParameter::Texture(ResourceID::Direct(icon.path.clone())),
)?;
Ok(model)
}
IconType::Gltf => Ok(Model::create(
parent,
Transform::from_scale([0.05; 3]),
&ResourceID::new_direct(icon.path.clone())?,
)?),
_ => panic!("Invalid Icon Type"),
};
}
pub struct App {
application: Application,
parent: Spatial,
position: Vector3<f32>,
grabbable: Grabbable,
_field: BoxField,
icon: Model,
label: Option<Text>,
grabbable_shrink: Option<Tweener<f32, f64, QuartInOut>>,
grabbable_grow: Option<Tweener<f32, f64, QuartInOut>>,
grabbable_move: Option<Tweener<f32, f64, QuartInOut>>,
currently_shown: bool,
}
impl App {
pub fn create_from_desktop_file(
parent: &Spatial,
position: impl Into<Vector3<f32>>,
desktop_file: DesktopFile,
) -> Result<Self> {
let position = position.into();
let field = BoxField::create(parent, Transform::default(), [APP_SIZE; 3])?;
let application = Application::create(&parent.client()?, desktop_file)?;
let icon = application.icon(128, false);
let grabbable = Grabbable::create(
parent,
Transform::from_position(position),
&field,
GrabData {
max_distance: 0.01,
frame_cancel_threshold: 50,
..Default::default()
},
)?;
grabbable.content_parent().set_spatial_parent(parent)?;
field.set_spatial_parent(grabbable.content_parent())?;
let icon = icon
.map(|i| model_from_icon(grabbable.content_parent(), &i))
.unwrap_or_else(|| {
Ok(Model::create(
grabbable.content_parent(),
Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[APP_SIZE * 0.5; 3],
),
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?)
})?;
let label_style = TextStyle {
character_height: APP_SIZE * 2.0,
bounds: Some(Bounds {
bounds: [1.0; 2].into(),
fit: TextFit::Wrap,
bounds_align: Alignment::XCenter | Alignment::YCenter,
}),
text_align: Alignment::Center.into(),
..Default::default()
};
let label = application.name().and_then(|name| {
Text::create(
&icon,
Transform::from_position_rotation(
[0.0, 0.1, -(APP_SIZE * 4.0)],
Quat::from_rotation_x(PI * 0.5),
),
name,
label_style,
)
.ok()
});
Ok(App {
parent: parent.alias(),
position,
grabbable,
_field: field,
label,
application,
icon,
grabbable_shrink: None,
grabbable_grow: None,
grabbable_move: None,
currently_shown: true,
})
}
pub fn content_parent(&self) -> &Spatial {
self.grabbable.content_parent()
}
pub fn toggle(&mut self) {
self.grabbable.set_enabled(!self.currently_shown).unwrap();
if self.currently_shown {
self.grabbable_move = Some(Tweener::quart_in_out(1.0, 0.0001, 0.25)); //TODO make the scale a parameter
} else {
self.icon.set_enabled(true).unwrap();
self.label.as_ref().map(|l| l.set_enabled(true).unwrap());
self.grabbable_move = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25));
}
self.currently_shown = !self.currently_shown;
}
}
impl RootHandler for App {
fn frame(&mut self, info: FrameInfo) {
let _ = self.grabbable.update(&info);
if let Some(grabbable_move) = &mut self.grabbable_move {
if !grabbable_move.is_finished() {
let scale = grabbable_move.move_by(info.delta);
self.grabbable
.content_parent()
.set_position(
Some(&self.parent),
[
self.position.x * scale,
self.position.y * scale,
self.position.z * scale,
],
)
.unwrap();
} else {
if grabbable_move.final_value() == 0.0001 {
self.icon.set_enabled(false).unwrap();
self.label.as_ref().map(|l| l.set_enabled(false).unwrap());
}
self.grabbable_move = None;
}
}
if let Some(grabbable_shrink) = &mut self.grabbable_shrink {
if !grabbable_shrink.is_finished() {
let scale = grabbable_shrink.move_by(info.delta);
self.grabbable
.content_parent()
.set_scale(Some(&self.parent), Vector3::from([scale; 3]))
.unwrap();
} else {
self.grabbable
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
if self.currently_shown {
self.grabbable_grow = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25));
self.grabbable.cancel_angular_velocity();
self.grabbable.cancel_linear_velocity();
}
self.grabbable_shrink = None;
self.grabbable
.content_parent()
.set_position(Some(&self.parent), self.position)
.unwrap();
self.grabbable
.content_parent()
.set_rotation(Some(&self.parent), Quat::default())
.unwrap();
self.icon
.set_rotation(
None,
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
)
.unwrap();
}
} else if let Some(grabbable_grow) = &mut self.grabbable_grow {
if !grabbable_grow.is_finished() {
let scale = grabbable_grow.move_by(info.delta);
self.grabbable
.content_parent()
.set_scale(Some(&self.parent), Vector3::from([scale; 3]))
.unwrap();
} else {
self.grabbable
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
self.grabbable_grow = None;
}
} else if self.grabbable.valid() && self.grabbable.grab_action().actor_stopped() {
self.grabbable_shrink = Some(Tweener::quart_in_out(APP_SIZE * 0.5, 0.0001, 0.25));
let Ok(distance_future) = self.grabbable
.content_parent()
.get_position_rotation_scale(&self.parent)
else {return};
let application = self.application.clone();
let space = self.content_parent().alias();
//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.powi(2) + distance_vector.y.powi(2)).sqrt()
+ distance_vector.z.powi(2))
.sqrt();
if dbg!(distance) > ACTIVATION_DISTANCE {
let _ = application.launch(&space);
}
});
}
}
}

Binary file not shown.

View File

@@ -1,6 +1,6 @@
{
"asset":{
"generator" : "Khronos glTF Blender I/O v3.4.50",
"generator":"Khronos glTF Blender I/O v3.5.30",
"version":"2.0"
},
"extensionsUsed":[
@@ -11,13 +11,18 @@
{
"name":"Scene",
"nodes":[
0
0,
1
]
}
],
"nodes":[
{
"mesh":0,
"name":"Icon"
},
{
"mesh":1,
"name":"Hex"
}
],
@@ -73,7 +78,12 @@
},
"indices":4,
"material":0
}
]
},
{
"name":"Circle.001",
"primitives":[
{
"attributes":{
"COLOR_0":5,

100
src/application.rs Normal file
View File

@@ -0,0 +1,100 @@
use crate::xdg::{DesktopFile, Icon, IconType};
use nix::unistd::setsid;
use regex::Regex;
use stardust_xr_fusion::{
client::Client,
node::{NodeError, NodeType},
spatial::Spatial,
startup_settings::StartupSettings,
};
use std::{
os::unix::process::CommandExt,
process::{Command, Stdio},
sync::Arc,
};
#[derive(Debug, Clone)]
pub struct Application {
desktop_file: DesktopFile,
startup_settings: Arc<StartupSettings>,
}
impl Application {
pub fn create(client: &Arc<Client>, desktop_file: DesktopFile) -> Result<Self, NodeError> {
if desktop_file.no_display {
return Err(NodeError::DoesNotExist);
}
let startup_settings = Arc::new(StartupSettings::create(client)?);
Ok(Application {
desktop_file,
startup_settings,
})
}
pub fn name(&self) -> Option<&str> {
self.desktop_file.name.as_deref()
}
pub fn categories(&self) -> &[String] {
self.desktop_file.categories.as_slice()
}
pub fn icon(&self, preferred_px_size: u16, prefer_3d: bool) -> Option<Icon> {
let raw_icons = self.desktop_file.get_raw_icons(preferred_px_size);
let mut icon = raw_icons.iter().max_by_key(|i| i.size).cloned();
if prefer_3d {
icon = raw_icons
.into_iter()
.find(|i| match i.icon_type {
IconType::Gltf => true,
_ => false,
})
.or(icon);
}
icon.and_then(|i| i.cached_process(preferred_px_size).ok())
}
pub fn launch(&self, launch_space: &Spatial) -> Result<(), NodeError> {
self.startup_settings.set_root(launch_space)?;
let future_startup_token = self.startup_settings.generate_startup_token()?;
let future_connection_env = self
.startup_settings
.node()
.client()?
.get_connection_environment()?;
let executable = self
.desktop_file
.command
.clone()
.ok_or(NodeError::DoesNotExist)?;
tokio::task::spawn(async move {
let Ok(startup_token) = future_startup_token.await else {return};
let Ok(connection_env) = future_connection_env.await else {return};
for (k, v) in connection_env.into_iter() {
std::env::set_var(k, v);
}
std::env::set_var("STARDUST_STARTUP_TOKEN", startup_token);
let re = Regex::new(r"%[fFuUdDnNickvm]").unwrap();
let exec = re.replace_all(&executable, "");
unsafe {
Command::new("sh")
.arg("-c")
.arg(exec.to_string())
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.pre_exec(|| {
_ = setsid();
Ok(())
})
.spawn()
.expect("Failed to start child process");
}
});
Ok(())
}
}

View File

@@ -1,2 +1,3 @@
pub mod application;
pub mod protostar;
pub mod xdg;

View File

@@ -1,8 +1,5 @@
use clap::Parser;
use color_eyre::{
eyre::{bail, Result},
Report,
};
use color_eyre::{eyre::Result, Report};
use manifest_dir_macros::directory_relative_path;
use protostar::{protostar::ProtoStar, xdg::parse_desktop_file};
use stardust_xr_fusion::client::Client;
@@ -11,21 +8,8 @@ use std::path::PathBuf;
#[derive(Debug, Parser)]
#[clap(author, version, about, long_about = None)]
struct Args {
#[clap(short, long, default_value_t = 0.1)]
size: f32,
#[clap(
short,
long,
conflicts_with = "command",
required_unless_present = "command",
conflicts_with = "icon",
required_unless_present = "icon"
)]
desktop_file: Option<PathBuf>,
#[clap(short, long, conflicts_with = "desktop_file", requires = "command")]
icon: Option<PathBuf>,
#[clap(short, long, conflicts_with = "desktop_file", requires = "icon")]
command: Option<String>,
// #[clap(short, long)]
desktop_file: PathBuf,
}
#[tokio::main(flavor = "current_thread")]

View File

@@ -1,5 +1,10 @@
use crate::xdg::{DesktopFile, Icon, IconType};
use color_eyre::eyre::{eyre, Result};
use crate::{
application::Application,
xdg::{DesktopFile, Icon, IconType},
};
use color_eyre::eyre::Result;
use glam::Quat;
use mint::Vector3;
use nix::unistd::setsid;
@@ -11,17 +16,20 @@ use stardust_xr_fusion::{
fields::BoxField,
node::NodeType,
spatial::Spatial,
startup_settings::StartupSettings,
};
use stardust_xr_molecules::{GrabData, Grabbable};
use std::f32::consts::PI;
use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio};
use std::f32::consts::PI;
use tween::{QuartInOut, Tweener};
const MODEL_SCALE: f32 = 0.03;
const ACTIVATION_DISTANCE: f32 = 1.0;
const MODEL_SCALE: f32 = 0.03;
const ACTIVATION_DISTANCE: f32 = 0.5;
fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
return match &icon.icon_type {
IconType::Png => {
@@ -39,6 +47,10 @@ fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
model_part
.set_material_parameter("color", MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]))?;
model_part.set_material_parameter(
model
.model_part("Hex")?
.set_material_parameter("color", MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]))?;
model.model_part("Icon")?.set_material_parameter(
"diffuse",
MaterialParameter::Texture(ResourceID::Direct(icon.path.clone())),
)?;
@@ -54,10 +66,13 @@ fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
}
pub struct ProtoStar {
parent: Spatial,
position: Vector3<f32>,
application: Application,
parent: Spatial,
position: Vector3<f32>,
grabbable: Grabbable,
field: BoxField,
_field: BoxField,
icon: Model,
label: Option<Text>,
grabbable_shrink: Option<Tweener<f32, f64, QuartInOut>>,
@@ -65,6 +80,11 @@ pub struct ProtoStar {
grabbable_move: Option<Tweener<f32, f64, QuartInOut>>,
execute_command: String,
currently_shown: bool,
label: Option<Text>,
grabbable_shrink: Option<Tweener<f32, f64, QuartInOut>>,
grabbable_grow: Option<Tweener<f32, f64, QuartInOut>>,
grabbable_move: Option<Tweener<f32, f64, QuartInOut>>,
currently_shown: bool,
}
impl ProtoStar {
pub fn create_from_desktop_file(
@@ -94,6 +114,16 @@ impl ProtoStar {
}
Self::new_raw(
pub fn create_from_desktop_file(
parent: &Spatial,
position: impl Into<Vector3<f32>>,
desktop_file: DesktopFile,
) -> Result<Self> {
let position = position.into();
let field = BoxField::create(parent, Transform::default(), [MODEL_SCALE * 2.0; 3])?;
let application = Application::create(&parent.client()?, desktop_file)?;
let icon = application.icon(128, false);
let grabbable = Grabbable::create(
parent,
position,
desktop_file.name.as_deref(),
@@ -113,6 +143,7 @@ impl ProtoStar {
let grabbable = Grabbable::create(
parent,
Transform::from_position(position),
Transform::from_position(position),
&field,
GrabData {
max_distance: 0.01,
@@ -156,18 +187,48 @@ impl ProtoStar {
)
.ok()
});
let label_style = TextStyle {
character_height: MODEL_SCALE * 4.0,
bounds: Some(Bounds {
bounds: [1.0; 2].into(),
fit: TextFit::Wrap,
bounds_align: Alignment::XCenter | Alignment::YCenter,
}),
text_align: Alignment::Center.into(),
..Default::default()
};
let label = application.name().and_then(|name| {
Text::create(
&icon,
Transform::from_position_rotation(
[0.0, 0.1, -(MODEL_SCALE * 8.0)],
Quat::from_rotation_x(PI * 0.5),
),
name,
label_style,
)
.ok()
});
Ok(ProtoStar {
parent: parent.alias(),
position,
grabbable,
field,
label,
_field: field,
label,
application,
icon,
grabbable_shrink: None,
grabbable_grow: None,
execute_command,
currently_shown: true,
grabbable_move: None,
grabbable_shrink: None,
grabbable_grow: None,
grabbable_move: None,
currently_shown: true,
})
}
pub fn content_parent(&self) -> &Spatial {
@@ -188,6 +249,7 @@ impl ProtoStar {
impl RootHandler for ProtoStar {
fn frame(&mut self, info: FrameInfo) {
self.grabbable.update(&info).unwrap();
let _ = self.grabbable.update(&info);
if let Some(grabbable_move) = &mut self.grabbable_move {
if !grabbable_move.is_finished() {
@@ -250,6 +312,19 @@ impl RootHandler for ProtoStar {
self.grabbable
.content_parent()
.set_scale(Some(&self.parent), Vector3::from([scale; 3]))
if let Some(grabbable_move) = &mut self.grabbable_move {
if !grabbable_move.is_finished() {
let scale = grabbable_move.move_by(info.delta);
self.grabbable
.content_parent()
.set_position(
Some(&self.parent),
[
self.position.x * scale,
self.position.y * scale,
self.position.z * scale,
],
)
.unwrap();
} else {
self.grabbable
@@ -257,6 +332,11 @@ impl RootHandler for ProtoStar {
.set_spatial_parent(&self.parent)
.unwrap();
self.grabbable_grow = None;
if grabbable_move.final_value() == 0.0001 {
self.icon.set_enabled(false).unwrap();
self.label.as_ref().map(|l| l.set_enabled(false).unwrap());
}
self.grabbable_move = None;
}
} else if self.grabbable.grab_action().actor_stopped() {
let startup_settings = StartupSettings::create(&self.field.client().unwrap()).unwrap();
@@ -272,6 +352,65 @@ impl RootHandler for ProtoStar {
let executable = self.execute_command.clone();
//TODO: split the executable string for the args
}
if let Some(grabbable_shrink) = &mut self.grabbable_shrink {
if !grabbable_shrink.is_finished() {
let scale = grabbable_shrink.move_by(info.delta);
self.grabbable
.content_parent()
.set_scale(Some(&self.parent), Vector3::from([scale; 3]))
.unwrap();
} else {
self.grabbable
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
if self.currently_shown {
self.grabbable_grow = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25));
self.grabbable.cancel_angular_velocity();
self.grabbable.cancel_linear_velocity();
}
self.grabbable_shrink = None;
self.grabbable
.content_parent()
.set_position(Some(&self.parent), self.position)
.unwrap();
self.grabbable
.content_parent()
.set_rotation(Some(&self.parent), Quat::default())
.unwrap();
self.icon
.set_rotation(
None,
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
)
.unwrap();
}
} else if let Some(grabbable_grow) = &mut self.grabbable_grow {
if !grabbable_grow.is_finished() {
let scale = grabbable_grow.move_by(info.delta);
self.grabbable
.content_parent()
.set_scale(Some(&self.parent), Vector3::from([scale; 3]))
.unwrap();
} else {
self.grabbable
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
self.grabbable_grow = None;
}
} else if self.grabbable.valid() && self.grabbable.grab_action().actor_stopped() {
self.grabbable_shrink = Some(Tweener::quart_in_out(MODEL_SCALE, 0.0001, 0.25));
let Ok(distance_future) = self.grabbable
.content_parent()
.get_position_rotation_scale(&self.parent)
else {return};
let application = self.application.clone();
let space = self.content_parent().alias();
//TODO: split the executable string for the args
tokio::task::spawn(async move {
let distance_vector = distance_future.await.ok().unwrap().0;
@@ -298,6 +437,12 @@ impl RootHandler for ProtoStar {
.spawn()
.expect("Failed to start child process");
}
let distance_vector = distance_future.await.ok().unwrap().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 _ = application.launch(&space);
}
});
}

View File

@@ -2,6 +2,9 @@ use color_eyre::eyre::Result;
use lazy_static::lazy_static;
use linicon;
use regex::Regex;
use lazy_static::lazy_static;
use linicon;
use regex::Regex;
use resvg::render;
use resvg::tiny_skia::{Pixmap, Transform};
use resvg::usvg::{FitTo, Tree};
@@ -9,14 +12,21 @@ use serde::{Deserialize, Serialize};
use serde_json;
use serde_with::serde_as;
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use serde_json;
use serde_with::serde_as;
use std::collections::HashMap;
use std::ffi::OsString;
use std::fs::create_dir_all;
use std::fs::File;
use std::fs::File;
use std::io::{BufRead, BufReader, ErrorKind};
use std::io::{Read, Write};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Mutex;
use std::sync::Mutex;
use std::{env, fs};
use walkdir::WalkDir;
@@ -62,14 +72,64 @@ lazy_static! {
get_image_cache_dir().join("imagechache.map")
));
}
#[serde_as]
#[derive(Deserialize, Serialize)]
struct ImageCache {
path: PathBuf,
#[serde_as(as = "Vec<(_, _)>")]
pub map: HashMap<String, PathBuf>,
}
impl ImageCache {
fn new(path: PathBuf) -> Self {
if let Ok(mut file) = File::open(&path) {
let mut buf = vec![];
if file.read_to_end(&mut buf).is_ok() {
if let Ok(cache) = serde_json::from_slice(&buf[..]) {
return cache;
}
}
}
//There was no file, or the file failed to load, create a new World.
ImageCache {
path,
map: HashMap::new(),
}
}
fn insert(&mut self, k: String, v: PathBuf) {
self.map.insert(k, v);
}
fn save(&self) {
let mut f = File::create(&self.path).unwrap();
let buf = serde_json::to_vec(&self).unwrap();
f.write_all(&buf[..]).unwrap();
}
}
lazy_static! {
static ref IMAGE_CACHE: Mutex<ImageCache> = Mutex::new(ImageCache::new(
get_image_cache_dir().join("imagechache.map")
));
}
fn get_data_dirs() -> Vec<PathBuf> {
let xdg_data_dirs_str = std::env::var("XDG_DATA_DIRS").unwrap_or_default();
fn get_data_dirs() -> Vec<PathBuf> {
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());
.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",
))
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",
@@ -90,12 +150,30 @@ fn get_app_dirs() -> Vec<PathBuf> {
.filter(|dir| dir.exists() && dir.is_dir())
.collect()
}
.join("share");
xdg_data_dirs
.chain([data_home].into_iter())
.filter(|dir| dir.exists() && dir.is_dir())
.collect()
}
fn get_app_dirs() -> Vec<PathBuf> {
get_data_dirs()
.into_iter()
.map(|dir| dir.join("applications"))
.filter(|dir| dir.exists() && dir.is_dir())
.collect()
}
pub fn get_desktop_files() -> Vec<PathBuf> {
pub fn get_desktop_files() -> Vec<PathBuf> {
let desktop_extension = OsString::from_str("desktop").unwrap();
// Get the list of directories to search
let app_dirs = get_app_dirs();
let app_dirs = get_app_dirs();
app_dirs
.into_iter()
.into_iter()
.flat_map(|dir| {
// Follow symlinks and recursively search directories
@@ -140,6 +218,10 @@ pub fn parse_desktop_file(path: PathBuf) -> Result<DesktopFile, String> {
let mut no_display = false;
let mut desktop_entry_found = false;
let re = Regex::new(r"^\[([^\]]*)\]$").unwrap();
let mut no_display = false;
let mut desktop_entry_found = false;
let re = Regex::new(r"^\[([^\]]*)\]$").unwrap();
// Loop through each line of the file
@@ -159,6 +241,14 @@ pub fn parse_desktop_file(path: PathBuf) -> Result<DesktopFile, String> {
desktop_entry_found = entry.as_str().contains("Desktop Entry");
}
if !desktop_entry_found {
continue;
}
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;
}
@@ -187,6 +277,12 @@ pub fn parse_desktop_file(path: PathBuf) -> Result<DesktopFile, String> {
_ => false,
}
}
"NoDisplay" => {
no_display = match value {
"true" => true,
_ => false,
}
}
_ => (), // Ignore unknown keys
}
}
@@ -199,6 +295,7 @@ pub fn parse_desktop_file(path: PathBuf) -> Result<DesktopFile, String> {
categories,
icon,
no_display,
no_display,
})
}
@@ -231,6 +328,7 @@ pub struct DesktopFile {
pub categories: Vec<String>,
pub icon: Option<String>,
pub no_display: bool,
pub no_display: bool,
}
impl DesktopFile {
pub fn get_raw_icons(&self) -> Vec<Icon> {
@@ -243,6 +341,13 @@ impl DesktopFile {
}
}
if let Some(cache_icon_path) = IMAGE_CACHE.lock().unwrap().map.get(icon_name) {
if cache_icon_path.exists() {
if let Some(icon) = Icon::from_path(cache_icon_path.to_owned(), 128) {
return vec![icon];
}
}
}
if let Some(cache_icon_path) = IMAGE_CACHE.lock().unwrap().map.get(icon_name) {
if cache_icon_path.exists() {
if let Some(icon) = Icon::from_path(cache_icon_path.to_owned(), 128) {
@@ -251,6 +356,9 @@ impl DesktopFile {
}
}
let mut icons_iter = linicon::lookup_icon(icon_name)
.use_fallback_themes(false)
.peekable();
let mut icons_iter = linicon::lookup_icon(icon_name)
.use_fallback_themes(false)
.peekable();
@@ -259,6 +367,10 @@ impl DesktopFile {
//dbg!("No icons found in current theme");
icons_iter = linicon::lookup_icon(icon_name).peekable();
}
if icons_iter.peek().is_none() {
//dbg!("No icons found in current theme");
icons_iter = linicon::lookup_icon(icon_name).peekable();
}
let sized_png: Vec<Icon> = icons_iter
.filter_map(|i| i.ok())
@@ -266,9 +378,20 @@ impl DesktopFile {
.map(|i| Icon::from_path(i.path, i.max_size - 2).unwrap())
.collect();
sized_png
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())
.collect();
sized_png
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Icon {
pub icon_type: IconType,
pub path: PathBuf,
pub size: u16,
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Icon {
pub icon_type: IconType,
@@ -276,6 +399,26 @@ pub struct Icon {
pub size: u16,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum IconType {
Png,
Svg,
Gltf,
}
impl 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") => IconType::Png,
Some("svg") => IconType::Svg,
Some("glb") | Some("gltf") => IconType::Gltf,
_ => return None,
};
return Some(Icon {
icon_type,
path,
size,
});
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum IconType {
Png,
@@ -297,6 +440,33 @@ impl Icon {
});
}
pub fn cached_process(self, size: u16) -> Result<Icon, std::io::Error> {
if !IMAGE_CACHE.lock().unwrap().map.contains_key(
&self
.path
.with_extension("")
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_owned(),
) {
dbg!("Saving value in the DB");
IMAGE_CACHE.lock().unwrap().insert(
self.path
.with_extension("")
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_owned(),
self.path.clone(),
);
IMAGE_CACHE.lock().unwrap().save();
}
match self.icon_type {
IconType::Svg => Ok(Icon::from_path(get_png_from_svg(self.path, size)?, size).unwrap()),
_ => Ok(self),
pub fn cached_process(self, size: u16) -> Result<Icon, std::io::Error> {
if !IMAGE_CACHE.lock().unwrap().map.contains_key(
&self
@@ -338,10 +508,11 @@ fn test_get_icon_path() {
categories: vec![],
icon: Some("krita".into()),
no_display: false,
no_display: false,
};
// Call the get_icon_path() function with a size argument and store the result
let icon_paths = desktop_file.get_raw_icons();
let icon_paths = desktop_file.get_raw_icons(128);
dbg!(&icon_paths);
// Assert that the get_icon_path() function returns the expected result
@@ -352,8 +523,26 @@ fn test_get_icon_path() {
)
.unwrap()
));
assert!(icon_paths.contains(
&Icon::from_path(
PathBuf::from("/usr/share/icons/hicolor/32x32/apps/krita.png"),
32
)
.unwrap()
));
}
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"))
} 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;
pub fn get_image_cache_dir() -> PathBuf {
let cache_dir;
if let Ok(xdg_cache_home) = std::env::var("XDG_CACHE_HOME") {
@@ -367,6 +556,7 @@ pub fn get_image_cache_dir() -> PathBuf {
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())?;
@@ -383,10 +573,26 @@ pub fn get_png_from_svg(svg_path: impl AsRef<Path>, size: u16) -> Result<PathBuf
return Ok(png_path);
}
let mut pixmap = Pixmap::new(size.into(), size.into()).unwrap();
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()
));
if png_path.exists() {
return Ok(png_path);
}
let mut pixmap = Pixmap::new(size.into(), size.into()).unwrap();
render(
&tree,
FitTo::Width(size.into()),
FitTo::Width(size.into()),
Transform::identity(),
pixmap.as_mut(),
);
@@ -411,6 +617,7 @@ fn test_render_svg_to_png() {
// Call the function with the test input and output paths and a size of 200
let png_path = get_png_from_svg(&svg_path, 200).unwrap();
let png_path = get_png_from_svg(&svg_path, 200).unwrap();
dbg!(&png_path);
// Check that the output file exists