Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "anny-dock"
version = "0.0.2"
version = "0.0.3"
edition = "2021"
description = "A modern, animated dock for Hyprland built with Rust and Bevy Engine"
readme = "README.md"
Expand Down
105 changes: 53 additions & 52 deletions src/components/favorites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub struct Favorites(pub Vec<String>);
#[derive(Component, Debug)]
pub struct Favorite;

use crate::{config::Config, ClientAddress, IconText};
use crate::config::Config;
use crate::types::{ClientAddress, IconTitleText};

pub(crate) fn add_client_address(commands: &mut Commands, entity: Entity, address: String) {
commands.entity(entity).insert(ClientAddress(address));
Expand All @@ -31,39 +32,66 @@ pub(crate) fn add_favorite(

pub(crate) fn add_icon_text(
commands: &mut Commands,
entity: Entity,
parent_icon: Entity,
class: &str,
transform: Transform,
scale: f32,
_asset_server: &AssetServer,
config: &Res<Config>,
) {
const TEXT_OFFSET: f32 = 8.0;

commands
.spawn(Text2dBundle {
text: Text::from_section(
class.to_string(),
TextStyle {
// font: asset_server.load(FONT_PATH),
font: TextStyle::default().font,
font_size: 8.0 * scale,
color: Color::WHITE,
commands.entity(parent_icon).with_children(|parent| {
parent.spawn((
Text2dBundle {
text: Text::from_section(
class.to_string(),
TextStyle {
font: TextStyle::default().font,
font_size: config.font_size,
color: Color::WHITE,
},
)
.with_alignment(TextAlignment::Center),
transform: Transform {
translation: Vec3::new(
0.0,
-(config.icon_size / 2.0) - TEXT_OFFSET,
0.1,
),
..default()
},
)
.with_alignment(TextAlignment::Center),
transform: Transform {
translation: Vec3::new(
transform.translation.x,
transform.translation.y - (config.icon_size * scale / 2.0) - TEXT_OFFSET,
transform.translation.z - 0.01,
),
scale: Vec3::splat(scale),
visibility: Visibility::Hidden,
..default()
},
..default()
})
.insert(IconText(entity));
IconTitleText,
));
});
}

pub(crate) fn set_favorite_pin(
commands: &mut Commands,
images: &mut Assets<Image>,
parent_entity: Entity,
config: &Res<Config>,
) {
const PIN_ICON_SVG: &[u8] = include_bytes!("../../assets/icons/pin_stroke_rounded.svg");
if let Some(image) = load_svg_pin_from_bytes(PIN_ICON_SVG) {
let handle = images.add(image);
commands.entity(parent_entity).with_children(|parent| {
let transform = Transform {
translation: Vec3::new(config.icon_size / 3.0, config.icon_size / 3.0, 0.1),
scale: Vec3::splat(0.4),
..default()
};

parent
.spawn(SpriteBundle {
texture: handle,
transform,
..Default::default()
})
.insert(FavoritePin);
});
}
}

fn load_svg_pin_from_bytes(svg_bytes: &[u8]) -> Option<Image> {
Expand Down Expand Up @@ -113,30 +141,3 @@ fn load_svg_pin_from_bytes(svg_bytes: &[u8]) -> Option<Image> {
TextureFormat::Rgba8Unorm,
))
}

pub(crate) fn set_favorite_pin(
commands: &mut Commands,
images: &mut Assets<Image>,
parent_entity: Entity,
config: &Res<Config>,
) {
const PIN_ICON_SVG: &[u8] = include_bytes!("../../assets/icons/pin_stroke_rounded.svg");
if let Some(image) = load_svg_pin_from_bytes(PIN_ICON_SVG) {
let handle = images.add(image);
commands.entity(parent_entity).with_children(|parent| {
let transform = Transform {
translation: Vec3::new(config.icon_size / 3.0, config.icon_size / 3.0, 0.1),
scale: Vec3::splat(0.4),
..default()
};

parent
.spawn(SpriteBundle {
texture: handle,
transform,
..Default::default()
})
.insert(FavoritePin);
});
}
}
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Config {
pub scroll_speed: f32,
pub visible_items: usize,
pub tilt_y: f32,
pub font_size: f32,
}

impl Default for Config {
Expand All @@ -31,6 +32,7 @@ impl Default for Config {
scroll_speed: 15.0,
visible_items: 8,
tilt_y: 0.25,
font_size: 16.0,
}
}
}
Expand Down
100 changes: 46 additions & 54 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ use std::sync::{mpsc::channel, Arc, Mutex};
use crate::systems::*;
use systems::animation::ScrollAnimationState;

#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
enum AppSystemSet {
Input,
Logic,
Animation,
}

fn main() {
let config = load_config();
let client_list = load_clients();
Expand Down Expand Up @@ -58,7 +65,7 @@ fn main() {
.add_plugins(SvgPlugin)
.insert_resource(ClearColor(Color::NONE))
.insert_resource(ClientList(client_list))
.insert_resource(IconPositions::default())

.insert_resource(ShowTitles(false))
.insert_resource(UiState::default())
.insert_resource(favorites)
Expand All @@ -73,30 +80,43 @@ fn main() {
.add_systems(Startup, setup_hyprland_monitor)

.add_systems(Update, cleanup_duplicate_cameras)
.configure_sets(
Update,
(AppSystemSet::Input, AppSystemSet::Logic, AppSystemSet::Animation).chain(),
)
.add_systems(
Update,
(
scroll_system,
scroll_with_arrows,
hover_system,
hover_animation_system,
icon_scale_animation_system,
collect_icon_data.before(update_text_positions),
update_text_positions,
icon_click_system,
toggle_favorite_system.in_set(StateUpdate),
toggle_titles,
drag_register_click_system,
drag_check_system,
drag_update_system,
drag_end_system.in_set(StateUpdate),
reset_positions_system,
reorder_icons_system.in_set(ReorderIcons),
process_hyprland_events,
exit_on_esc_or_q,
keybind_launch_visible_icons,
)
.chain(),
// Input Systems
(
scroll_system,
scroll_with_arrows,
icon_click_system,
toggle_titles,
drag_register_click_system,
exit_on_esc_or_q,
keybind_launch_visible_icons,
process_hyprland_events,
).in_set(AppSystemSet::Input),

// Logic Systems
(
drag_check_system,
drag_update_system,

hover_system,
toggle_favorite_system,
drag_end_system,
reorder_icons_system,
reset_positions_system,
).in_set(AppSystemSet::Logic),

// Animation Systems
(
hover_animation_system,
icon_scale_animation_system,
).in_set(AppSystemSet::Animation),
),
)
.run();
}
Expand All @@ -107,7 +127,7 @@ fn setup(
mut images: ResMut<Assets<Image>>,
client_list: Res<ClientList>,
windows: Query<&Window, With<PrimaryWindow>>,
show_titles: Res<ShowTitles>,
_show_titles: Res<ShowTitles>,
favorites: Res<Favorites>,
config: Res<Config>,
) {
Expand Down Expand Up @@ -195,6 +215,8 @@ fn setup(
hover_exit_timer: None,
});

add_icon_text(&mut commands, icon_entity, class, &asset_server, &config);

if let Some(client) = client_opt {
add_client_address(&mut commands, icon_entity, client.address.clone());
commands
Expand All @@ -203,31 +225,11 @@ fn setup(
commands
.entity(icon_entity)
.insert(ClientClass(client.class.clone()));
} else if *is_favorite {
let placeholder_address = format!("pinned:{}", class);
add_client_address(&mut commands, icon_entity, placeholder_address.clone());
commands
.entity(icon_entity)
.insert(ClientAddress(placeholder_address));
commands
.entity(icon_entity)
.insert(ClientClass(class.clone()));
}

if *is_favorite {
add_favorite(&mut commands, icon_entity, &mut images, &config);
}
if show_titles.0 {
add_icon_text(
&mut commands,
icon_entity,
class,
transform,
scale,
&asset_server,
&config,
);
}
}
}

Expand Down Expand Up @@ -582,8 +584,6 @@ fn process_new_windows(
commands,
icon_entity,
&client.class,
transform,
scale,
asset_server,
config,
);
Expand Down Expand Up @@ -787,15 +787,7 @@ fn handle_hypr_open_window(
.insert(ClientClass(client.class.clone()));

if show_titles.0 {
add_icon_text(
commands,
icon_entity,
&client.class,
transform,
scale,
asset_server,
config,
);
add_icon_text(commands, icon_entity, &class, asset_server, config);
}
}

Expand Down
21 changes: 15 additions & 6 deletions src/systems/drag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,26 @@ pub fn drag_check_system(
if let Some(world_cursor) =
camera.viewport_to_world_2d(camera_transform, cursor_pos)
{
for (entity, hover, transform) in q_icons.iter() {
let mut top_dragged: Option<(Entity, f32, Vec2)> = None;

for (entity, _hover, transform) in q_icons.iter() {
let pos = transform.translation.truncate();
let size = Vec2::splat(config.icon_size * hover.original_scale);
let size = Vec2::splat(config.icon_size * transform.scale.x);
let rect = Rect::from_center_size(pos, size * 1.1);

if rect.contains(world_cursor) {
let offset = world_cursor - pos;
commands.entity(entity).insert(Dragging { offset });
ui_state.dragging = Some(entity);
break;
let z = transform.translation.z;
if top_dragged.is_none() || z > top_dragged.unwrap().1 {
top_dragged = Some((entity, z, pos));
}
}
}

if let Some((entity, _z, pos)) = top_dragged {
let offset = world_cursor - pos;
commands.entity(entity).insert(Dragging { offset });
ui_state.dragging = Some(entity);
}
}
}
}
Expand Down
29 changes: 1 addition & 28 deletions src/systems/icon.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
use crate::types::*;
use crate::config::Config;
use crate::utils::calculate_icon_transform;
use crate::IconText;

use bevy::prelude::*;
use bevy::window::PrimaryWindow;

pub fn collect_icon_data(
query: Query<(Entity, &Transform, &HoverTarget)>,
mut icon_positions: ResMut<IconPositions>,
) {
icon_positions.0.clear();
for (entity, transform, _hover) in query.iter() {
icon_positions
.0
.insert(entity, (transform.translation, transform.scale));
}
}

pub fn update_text_positions(
icon_query: Query<(&Transform, &ClientClass), Without<IconText>>,
mut text_query: Query<(&mut Transform, &IconText)>,
config: Res<Config>,
) {
for (mut text_transform, icon_text) in text_query.iter_mut() {
if let Ok((icon_transform, _)) = icon_query.get(icon_text.0) {
let scale = icon_transform.scale.y;
text_transform.translation.x = icon_transform.translation.x;
text_transform.translation.y =
icon_transform.translation.y - (config.icon_size * scale / 2.0) - 2.0;
text_transform.translation.z = icon_transform.translation.z - 0.01;
text_transform.scale = Vec3::splat(scale);
}
}
}

pub fn reorder_icons_system(
mut q_icons: Query<(Entity, &ClientAddress, &mut Transform, &mut HoverTarget)>,
Expand Down
Loading