Skip to content

Commit

Permalink
moved workspace logic to a trait to make it easier to support other w…
Browse files Browse the repository at this point in the history
…ayland compositors
  • Loading branch information
it-a-me committed Jun 4, 2023
1 parent d9a6f9d commit e886781
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 25 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ members = [
"embargo",
"embargo_widget",
"hyprland_workspaces",
"embargo_workspace",
]
4 changes: 3 additions & 1 deletion embargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ anyhow = "1.0.71"
chrono = "0.4.25"
clap = { version = "4.3.1", features = ["derive", "cargo"] }
dirs = "5.0.1"
embargo_workspace = { version = "0.1.0", path = "../embargo_workspace", optional=true}
env_logger = "0.10.0"
human-repr = "1.1.0"
hyprland_workspaces = { version = "0.1.0", path = "../hyprland_workspaces", optional=true }
Expand All @@ -28,4 +29,5 @@ slint-build = "1.0.2"

[features]
default = ["hyprland"]
hyprland = ["dep:hyprland_workspaces"]
hyprland = ["dep:hyprland_workspaces", "workspaces"]
workspaces = ["dep:embargo_workspace"]
13 changes: 7 additions & 6 deletions embargo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ fn main() -> anyhow::Result<()> {
slint::platform::set_platform(Box::new(ui::BasicPlatform::new(window.clone()))).unwrap();
let ui = MainUi::new()?;
#[cfg(feature = "hyprland")]
ui.global::<Hyprland>()
.on_change_workspace(|id| hyprland_workspaces::change_workspace(id).unwrap());
ui.global::<Workspaces>()
.on_change_workspace(|id| hyprland_workspaces::change_workspace(id as u32).unwrap());
window.set_size(PhysicalSize::new(width, height));
let conn = Connection::connect_to_env()?;
let (mut bar, mut event_queue) = window::Bar::new(
Expand Down Expand Up @@ -98,11 +98,12 @@ fn main() -> anyhow::Result<()> {
}
Ok(())
}
#[cfg(feature = "hyprland")]
#[cfg(feature = "workspaces")]
pub mod hyprland {
use hyprland_workspaces::{Workspace, WorkspaceState};
use embargo_workspace::WorkspaceState;
use hyprland_workspaces::HyprlandWorkspace as DisplayWorkspace;
use slint::{Color, VecModel};
pub struct Workspaces(Vec<Workspace>);
pub struct Workspaces(Vec<DisplayWorkspace>);
impl Workspaces {
pub fn new() -> anyhow::Result<Self> {
let workspaces = hyprland_workspaces::workspaces()?;
Expand All @@ -115,7 +116,7 @@ pub mod hyprland {
.map(|w| {
//fkasjl
let color = Self::state_to_color(&w.state);
(color, color.brighter(0.2), w.id)
(color, color.brighter(0.2), w.position as i32)
})
.collect::<Vec<_>>();
VecModel::from(workspaces)
Expand Down
4 changes: 2 additions & 2 deletions embargo/ui/main.slint
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export global Hyprland {
export global Workspaces {
pure callback change_workspace(int);
}
export global HardwareMonitor {
Expand All @@ -22,7 +22,7 @@ export component Workspace inherits Rectangle {
}
touch := TouchArea {
clicked => {
Hyprland.change-workspace(root.id);
Workspaces.change-workspace(root.id);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions embargo_workspace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "embargo_workspace"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.71"
15 changes: 15 additions & 0 deletions embargo_workspace/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::sync::OnceLock;
pub trait Workspace {
fn state(&self) -> WorkspaceState;
fn position(&self) -> u32;
fn display_name(&self) -> &str {
static NAME: OnceLock<String> = OnceLock::new();
NAME.get_or_init(|| self.position().to_string())
}
}
#[derive(Debug, Clone, Copy)]
pub enum WorkspaceState {
Active,
Used,
Unused,
}
1 change: 1 addition & 0 deletions hyprland_workspaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition = "2021"

[dependencies]
anyhow = "1.0.71"
embargo_workspace = { version = "0.1.0", path = "../embargo_workspace" }
hyprland = "0.3.3"
44 changes: 28 additions & 16 deletions hyprland_workspaces/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,49 @@ use hyprland::data::Workspace as HyprWorkspace;
use hyprland::prelude::*;
use hyprland::shared::HyprError;
use std::collections::HashMap;
pub fn workspaces() -> anyhow::Result<Vec<Workspace>> {

use embargo_workspace::WorkspaceState;
pub fn workspaces() -> anyhow::Result<Vec<HyprlandWorkspace>> {
let workspaces = hyprland::data::Workspaces::get()?.to_vec();
let mut workspace_icons = (1..10)
.map(|id| (id, WorkspaceState::Unused))
.map(|id| (id as u32, WorkspaceState::Unused))
.collect::<HashMap<_, _>>();
for workspace in workspaces {
if workspace.windows > 0 {
workspace_icons.insert(workspace.id, WorkspaceState::Used);
workspace_icons.insert(workspace.id as u32, WorkspaceState::Used);
}
}
workspace_icons.insert(HyprWorkspace::get_active()?.id, WorkspaceState::Active);
workspace_icons.insert(
HyprWorkspace::get_active()?.id as u32,
WorkspaceState::Active,
);
let mut workspace_icons = workspace_icons
.into_iter()
.map(|(id, state)| Workspace { id, state })
.map(|(id, state)| HyprlandWorkspace {
position: id,
state,
})
.collect::<Vec<_>>();
workspace_icons.sort_by_key(|w| w.id);
workspace_icons.sort_by_key(|w| w.position);
Ok(workspace_icons)
}
pub fn change_workspace(id: i32) -> anyhow::Result<(), HyprError> {

pub fn change_workspace(id: u32) -> Result<(), HyprError> {
hyprland::dispatch::Dispatch::call(hyprland::dispatch::DispatchType::Workspace(
hyprland::dispatch::WorkspaceIdentifierWithSpecial::Id(id),
hyprland::dispatch::WorkspaceIdentifierWithSpecial::Id(id as i32),
))
}

#[derive(Debug, Clone, Copy)]
pub enum WorkspaceState {
Active,
Used,
Unused,
}
pub struct Workspace {
pub id: i32,
pub struct HyprlandWorkspace {
pub position: u32,
pub state: WorkspaceState,
}

impl embargo_workspace::Workspace for HyprlandWorkspace {
fn state(&self) -> WorkspaceState {
self.state
}
fn position(&self) -> u32 {
self.position
}
}

0 comments on commit e886781

Please sign in to comment.