hyprshell_exec_lib/
util.rs1use anyhow::Context;
2use core_lib::{Active, ClientId, Warn};
3use hyprland::ctl::{notify, reload, Color};
4use hyprland::data::{Client, Monitor, Monitors, Workspace};
5use hyprland::dispatch::{Dispatch, DispatchType};
6use hyprland::keyword::Keyword;
7use hyprland::prelude::*;
8use tracing::{debug, info, span, trace, warn, Level};
9
10pub fn get_monitors() -> Vec<Monitor> {
11 Monitors::get().map_or(vec![], |monitors| monitors.to_vec())
12}
13
14pub fn get_current_monitor() -> Option<Monitor> {
15 Monitor::get_active().ok()
16}
17
18pub fn reload_config() {
19 debug!("Reloading hyprland config");
20 reload::call().warn("Failed to reload hyprland config");
21}
22
23pub fn toast(body: &str) {
24 warn!("{}", body);
25 let _ = notify::call(
26 notify::Icon::Warning,
27 std::time::Duration::from_secs(10),
28 Color::new(255, 0, 0, 255),
29 format!("hyprshell Error: {}", body),
30 );
31}
32
33pub fn apply_keybinds(list: Vec<(&str, String)>) {
34 trace!("Applying binds and submaps");
35 for (a, b) in list {
36 trace!("{}={}", a, b);
37 Keyword::set(a, b).warn("Failed to apply bind and submap");
38 }
39}
40
41pub fn to_client_id(id: &hyprland::shared::Address) -> ClientId {
43 u64::from_str_radix(id.to_string().trim_start_matches("0x"), 16)
44 .expect("Failed to parse client id, this should never happen")
45}
46pub fn to_client_address(id: ClientId) -> hyprland::shared::Address {
48 hyprland::shared::Address::new(format!("{:x}", id))
49}
50
51pub fn activate_submap(submap_name: &str) -> anyhow::Result<()> {
52 let _span = span!(Level::TRACE, "submap").entered();
53 Dispatch::call(DispatchType::Custom("submap", submap_name)).warn("unable to activate submap");
54 debug!("Activated submap: {}", submap_name);
55 Ok(())
56}
57
58pub fn get_initial_active() -> anyhow::Result<Active> {
59 let active_client = Client::get_active()?.map(|c| to_client_id(&c.address));
60 let active_ws = Workspace::get_active()?.id;
61 let active_monitor = Monitor::get_active()?.id;
62 Ok(Active {
63 client: active_client,
64 workspace: active_ws,
65 monitor: active_monitor,
66 })
67}
68
69pub fn get_version() -> anyhow::Result<String> {
70 let version = hyprland::data::Version::get()
71 .context("Failed to get version! (hyprland is probably outdated or too new??)")?;
72
73 trace!("hyprland {version:?}");
74 info!(
75 "Starting hyprshell {} on hyprland {}",
76 env!("CARGO_PKG_VERSION"),
77 version.version.clone().unwrap_or(version.tag.clone()),
78 );
79
80 Ok(version
81 .version
82 .unwrap_or(version.tag.trim_start_matches('v').to_string()))
83}