almost_everything/
almost_everything.rs

1use hyprland::data::{Client, Clients, Monitors, Workspace};
2use hyprland::dispatch::*;
3use hyprland::event_listener::EventListenerMutable as EventListener;
4use hyprland::keyword::*;
5use hyprland::prelude::*;
6use hyprland::shared::WorkspaceType;
7
8fn main() -> hyprland::Result<()> {
9    // We can call dispatchers with the dispatch macro, and struct!
10    // You can decide what you want to use, below are some examples of their usage
11
12    // Here we are telling hyprland to open kitty using the dispatch macro!
13    hyprland::dispatch!(Exec, "kitty")?;
14
15    // Here we are moving the cursor to the top left corner! We can also just use the Dispatch
16    // struct!
17    Dispatch::call(DispatchType::MoveCursorToCorner(Corner::TopLeft))?;
18
19    // Here we are adding a keybinding to Hyprland using the bind macro!
20    hyprland::bind!(SUPER, Key, "i" => ToggleFloating, None)?;
21
22    // Here we are getting the border size
23    let border_size = match Keyword::get("general:border_size")?.value {
24        OptionValue::Int(i) => i,
25        _ => panic!("border size can only be a int"),
26    };
27    println!("{border_size}");
28
29    // Here we change a keyword, in this case we are doubling the border size we got above
30    Keyword::set("general:border_size", border_size * 2)?;
31
32    // get all monitors
33    let monitors = Monitors::get()?;
34
35    // and the active window
36    let win = Client::get_active()?;
37
38    // and all open windows
39    let clients = Clients::get()?;
40
41    // and the active workspace
42    let work = Workspace::get_active()?;
43
44    // and printing them all out!
45    println!("monitors: {monitors:#?},\nactive window: {win:#?},\nclients {clients:#?}\nworkspace: {work:#?}");
46
47    // Create a event listener
48    let mut event_listener = EventListener::new();
49
50    // Shows when active window changes
51    event_listener.add_active_window_change_handler(|data, _| {
52        println!("{data:#?}");
53    });
54
55    // This changes the workspace to 5 if the workspace is switched to 9
56    // this is a performance and mutable state test
57    event_listener.add_workspace_change_handler(|id, state| {
58        if id == WorkspaceType::Regular('9'.to_string()) {
59            state.active_workspace = WorkspaceType::Regular('2'.to_string());
60        }
61    });
62    // This makes it so you can't turn on fullscreen lol
63    event_listener.add_fullscreen_state_change_handler(|fstate, state| {
64        if fstate {
65            state.fullscreen_state = false;
66        }
67    });
68    // Makes a monitor unfocusable
69    event_listener.add_active_monitor_change_handler(|data, state| {
70        let hyprland::event_listener::MonitorEventData { monitor_name, .. } = data;
71
72        if monitor_name == *"DP-1".to_string() {
73            state.active_monitor = "eDP-1".to_string()
74        }
75    });
76
77    // add event, yes functions and closures both work!
78    event_listener.add_workspace_change_handler(|id, _| println!("workspace changed to {id:#?}"));
79
80    // and execute the function
81    // here we are using the blocking variant
82    // but there is a async version too
83    event_listener.start_listener()
84}