Skip to main content

hyprshell_exec_lib/
binds.rs

1use core_lib::binds::{ExecBind, ExecBindBehavior};
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 dispatcher = dispatcher_for_bind(bind, bind.exec.clone());
56    let binding = Binding {
57        mods: binds,
58        key: bind.key.to_string(),
59        flags: {
60            let mut flags = vec![Flag::AutoConsuming, Flag::Description(desc)];
61            match bind.behavior {
62                ExecBindBehavior::Repeating => flags.push(Flag::Repeating),
63                ExecBindBehavior::Release => flags.push(Flag::Release),
64                ExecBindBehavior::TransparentReleaseIgnoringMods { .. } => {
65                    // Do not swallow ordinary key presses just to watch for releases.
66                    flags.extend([
67                        Flag::Release,
68                        Flag::Transparent,
69                        Flag::IgnoreMods,
70                        Flag::NonConsuming,
71                    ]);
72                }
73            }
74            flags
75        },
76        dispatcher,
77    };
78    trace!("binding exec: {binding:?}");
79    binding.unbind()?;
80    binding.bind()?;
81    Ok(())
82}
83
84fn dispatcher_for_bind(bind: &ExecBind, exec: String) -> Dispatch {
85    let dispatcher = Dispatch::ExecCmd(exec, None);
86    if let ExecBindBehavior::TransparentReleaseIgnoringMods {
87        unless_keys_down: Some((left, right)),
88    } = bind.behavior
89    {
90        // Evaluate in Hyprland at the key release, before spawning the socket command.
91        // The Tab release must not close the switcher while Alt is still held.
92        Dispatch::Unimplemented(format!(
93            "function() if not hl.is_key_down(\"{left}\") and not hl.is_key_down(\"{right}\") then hl.dispatch({dispatcher}) end end"
94        ))
95    } else {
96        dispatcher
97    }
98}