Dispatch

Struct Dispatch 

Source
pub struct Dispatch;
Expand description

The struct that provides all dispatching methods

Implementations§

Source§

impl Dispatch

Source

pub fn call(dispatch_type: DispatchType<'_>) -> Result<()>

This function calls a specified dispatcher (blocking)

use hyprland::dispatch::{DispatchType,Dispatch};
// This is an example of just one dispatcher, there are many more!
Dispatch::call(DispatchType::Exec("kitty"))
Examples found in repository?
examples/almost_everything.rs (line 17)
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}
Source

pub async fn call_async(dispatch_type: DispatchType<'_>) -> Result<()>

This function calls a specified dispatcher (async)

use hyprland::dispatch::{DispatchType,Dispatch};
// This is an example of just one dispatcher, there are many more!
Dispatch::call_async(DispatchType::Exec("kitty")).await?;
Examples found in repository?
examples/almost_everything_async.rs (line 20)
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}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.