Skip to main content

hyprshell_exec_lib/
binds.rs

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