Skip to main content

hyprshell_exec_lib/
binds.rs

1use core_lib::binds::ExecBind;
2use core_lib::{LAUNCHER_NAMESPACE, OVERVIEW_NAMESPACE, SWITCH_NAMESPACE};
3use hyprland::bind_new::{Binding, Flag, Mod};
4use hyprland::dispatch_new::Dispatch;
5use hyprland::window_rule::{LayerEffect, LayerMatch, LayerRule};
6use tracing::{trace, warn};
7
8pub fn apply_layerrules() -> anyhow::Result<()> {
9    // TODO add option to enable blur
10    let rules = vec![
11        LayerRule {
12            name: None,
13            r#match: vec![LayerMatch::Namespace(LAUNCHER_NAMESPACE.into())],
14            effects: vec![LayerEffect::NoAnim(true), LayerEffect::Xray(false)],
15        },
16        LayerRule {
17            name: None,
18            r#match: vec![LayerMatch::Namespace(OVERVIEW_NAMESPACE.into())],
19            effects: vec![LayerEffect::NoAnim(true), LayerEffect::Xray(false)],
20        },
21        LayerRule {
22            name: None,
23            r#match: vec![LayerMatch::Namespace(SWITCH_NAMESPACE.into())],
24            effects: vec![
25                LayerEffect::NoAnim(true),
26                LayerEffect::Xray(false),
27                LayerEffect::DimAround(true),
28            ],
29        },
30    ];
31
32    for rule in rules {
33        rule.apply()?;
34    }
35    Ok(())
36}
37
38pub fn apply_exec_bind(bind: &ExecBind) -> anyhow::Result<()> {
39    let binds: Vec<_> = bind
40        .mods
41        .iter()
42        .filter_map(|m| match m.to_lowercase().as_str() {
43            "alt" => Some(Mod::Alt),
44            "control" | "ctrl" => Some(Mod::Ctrl),
45            "super" | "win" => Some(Mod::Super),
46            "shift" => Some(Mod::Shift),
47            _ => {
48                warn!("unknown mod: {m}");
49                None
50            }
51        })
52        .collect();
53    let desc = format!("hyprshell-keybind: {}", bind.desc);
54
55    let binding = Binding {
56        mods: binds,
57        key: bind.key.to_string(),
58        flags: {
59            let mut flags = vec![Flag::AutoConsuming, Flag::Description(desc)];
60            if bind.release {
61                flags.push(Flag::Release);
62            } else {
63                flags.push(Flag::Repeating);
64            }
65            if bind.transparent {
66                flags.push(Flag::Transparent);
67            }
68            flags
69        },
70        dispatcher: Dispatch::ExecCmd(bind.exec.clone(), None),
71    };
72    trace!("binding exec: {binding:?}");
73    binding.unbind()?;
74    binding.bind()?;
75    Ok(())
76}