almost_everything_async/
almost_everything_async.rs

1use hyprland::data::{Animations, Client, Clients, Monitors, Workspace};
2use hyprland::event_listener::AsyncEventListener;
3use hyprland::keyword::*;
4use hyprland::prelude::*;
5//use hyprland::shared::WorkspaceType;
6use hyprland::{async_closure, dispatch::*};
7
8#[tokio::main]
9async fn main() -> hyprland::Result<()> {
10    // We can call dispatchers with the dispatch function!
11
12    // Here we are telling hyprland to open kitty using the dispatch macro!
13    hyprland::dispatch!(async; Exec, "kitty").await?;
14
15    // Here we are adding a keybinding to Hyprland using the bind macro!
16    hyprland::bind!(async; SUPER, Key, "i" => ToggleFloating, None).await?;
17
18    // Here we are moving the cursor to the top left corner! We can also just use the Dispatch
19    // struct!
20    Dispatch::call_async(DispatchType::MoveCursorToCorner(Corner::TopLeft)).await?;
21
22    let border_size = match Keyword::get_async("general:border_size").await?.value {
23        OptionValue::Int(i) => i,
24        _ => panic!("border size can only be a int"),
25    };
26    println!("{border_size}");
27
28    // Here we change a keyword, yes its a dispatcher don't complain
29    Keyword::set_async("general:border_size", border_size * 2).await?;
30    // get all monitors
31    let monitors = Monitors::get_async().await?;
32    // and the active window
33    let win = Client::get_active_async().await?;
34    // and all open windows
35    let clients = Clients::get_async().await?;
36    // and the active workspace
37    let work = Workspace::get_active_async().await?;
38    // and printing them all out!
39    println!("monitors: {monitors:#?},\nactive window: {win:#?},\nclients {clients:#?}\nworkspace:{work:#?}");
40    let animations = Animations::get_async().await?;
41    println!("{animations:#?}");
42    // Create a event listener
43    let mut event_listener = AsyncEventListener::new();
44
45    //This changes the workspace to 5 if the workspace is switched to 9
46    //this is a performance and mutable state test
47    // event_listener.add_workspace_change_handler(async_closure! {|id, state| {
48    //     if id == WorkspaceType::Regular('9'.to_string()) {
49    //         *state.workspace = '2'.to_string();
50    //     }
51    // }});
52    /*
53    event_listener.add_workspace_change_handler(|id, state| {
54        Box::pin(async move {
55            if id == WorkspaceType::Regular('9'.to_string()) {
56                *state.workspace = '2'.to_string();
57            }
58        })
59    });
60
61    // This makes it so you can't turn on fullscreen lol
62    event_listener.add_fullscreen_state_change_handler(async_closure! {|fstate, state| {
63        if fstate {
64            *state.fullscreen = false;
65        }
66    }});
67    // Makes a monitor unfocusable
68    event_listener.add_active_monitor_change_handler(async_closure! {|data, state| {
69        let hyprland::event_listener::MonitorEventData{ monitor_name, .. } = data;
70
71        if monitor_name == *"DP-1".to_string() {
72            *state.monitor = "eDP-1".to_string()
73        }
74    }});
75    */
76    // add event, yes functions and closures both work!
77
78    event_listener.add_workspace_change_handler(
79        async_closure! { move |id| println!("workspace changed to {id:#?}")},
80    );
81    event_listener.add_active_window_change_handler(
82        async_closure! { move |data| println!("window changed to {data:#?}")},
83    );
84    // Waybar example
85    // event_listener.add_active_window_change_handler(|data| {
86    //     use hyprland::event_listener::WindowEventData;
87    //     let string = match data {
88    //         Some(WindowEventData(class, title)) => format!("{class}: {title}"),
89    //         None => "".to_string()
90    //     };
91    //     println!(r#"{{"text": "{string}", class: "what is this?"}}"#);
92    // });
93
94    // and execute the function
95    // here we are using the blocking variant
96    // but there is a async version too
97    event_listener.start_listener_async().await
98}