hyprshell_exec_lib/
collect.rs1use crate::to_client_id;
2use core_lib::{
3 ClientData, ClientId, FindByFirst, MonitorData, MonitorId, WorkspaceData, WorkspaceId,
4};
5use hyprland::data::{Client, Clients, Monitor, Monitors, Workspace, Workspaces};
6use hyprland::prelude::*;
7use tracing::{span, warn, Level};
8
9fn get_hypr_data() -> anyhow::Result<(Vec<Monitor>, Vec<Workspace>, Vec<Client>)> {
10 let _span = span!(Level::TRACE, "get_hypr_data").entered();
11 let monitors = Monitors::get()?.to_vec();
12 let workspaces = {
14 let mut workspaces = Workspaces::get()?
15 .into_iter()
16 .filter(|w| w.id != -1) .filter(|w| !w.id < 0) .collect::<Vec<_>>();
19
20 workspaces.sort_by(|a, b| a.id.cmp(&b.id));
21 workspaces
22 };
23 let clients = Clients::get()?
24 .into_iter()
25 .filter(|c| c.workspace.id != -1) .filter(|w| !w.workspace.id < 0) .collect::<Vec<_>>();
28
29 Ok((monitors, workspaces, clients))
30}
31
32#[allow(clippy::type_complexity)]
33pub fn collect_hypr_data() -> anyhow::Result<(
34 Vec<(ClientId, ClientData)>,
35 Vec<(WorkspaceId, WorkspaceData)>,
36 Vec<(MonitorId, MonitorData)>,
37 Option<(String, ClientId)>,
38 WorkspaceId,
39 MonitorId,
40)> {
41 let _span = span!(Level::TRACE, "convert_hypr_data").entered();
42
43 let (monitors, workspaces, clients) = get_hypr_data()?;
44
45 let mut monitor_data = {
48 let mut md: Vec<(MonitorId, MonitorData)> = Vec::with_capacity(monitors.iter().len());
49
50 monitors.iter().for_each(|monitor| {
51 md.push((
52 monitor.id,
53 MonitorData {
54 x: monitor.x,
55 y: monitor.y,
56 width: (monitor.width as f32 / monitor.scale) as u16,
57 height: (monitor.height as f32 / monitor.scale) as u16,
58 connector: monitor.name.clone(),
59 enabled: false, },
61 ));
62 });
63 md
64 };
65
66 let mut workspace_data = {
68 let mut wd: Vec<(WorkspaceId, WorkspaceData)> = Vec::with_capacity(workspaces.len());
69
70 for (monitor_id, monitor_data) in monitor_data.iter() {
71 let mut x_offset: i32 = 0;
72 workspaces
73 .iter()
74 .filter(|ws| ws.monitor_id == *monitor_id)
75 .for_each(|workspace| {
76 wd.push((
77 workspace.id,
78 WorkspaceData {
79 x: x_offset,
80 y: monitor_data.y,
81 name: workspace.name.clone(),
82 monitor: *monitor_id,
83 height: monitor_data.height,
84 width: monitor_data.width,
85 enabled: false, },
87 ));
88 x_offset += monitor_data.width as i32;
89 });
90 }
91 wd
92 };
93
94 let client_data = {
95 let mut cd: Vec<(ClientId, ClientData)> = Vec::with_capacity(clients.len());
96
97 for client in clients {
98 if workspace_data.find_by_first(&client.workspace.id).is_some() {
99 cd.push((
100 to_client_id(&client.address),
101 ClientData {
102 x: client.at.0,
103 y: client.at.1,
104 width: client.size.0,
105 height: client.size.1,
106 class: client.class.clone(),
107 workspace: client.workspace.id,
108 monitor: client.monitor,
109 focus_history_id: client.focus_history_id,
110 title: client.title.clone(),
111 floating: client.floating,
112 pid: client.pid,
113 enabled: false, },
115 ));
116 } else {
117 warn!(
118 "workspace {:?} not found for client {:?}",
119 client.workspace, client
120 );
121 }
122 }
123 cd
124 };
125
126 workspace_data.sort_by(|a, b| a.0.cmp(&b.0));
127 monitor_data.sort_by(|a, b| a.0.cmp(&b.0));
128
129 let active_ws = Workspace::get_active()?.id;
130 let active_monitor = Monitor::get_active()?.id;
131 let active_client = Client::get_active()?.map(|a| (a.class.clone(), to_client_id(&a.address)));
132
133 Ok((
134 client_data,
135 workspace_data,
136 monitor_data,
137 active_client,
138 active_ws,
139 active_monitor,
140 ))
141}