hyprshell_exec_lib/
switch.rs1use anyhow::Context;
2use hyprland::data::Workspace;
3use hyprland::dispatch::{
4 Dispatch, DispatchType, WindowIdentifier, WorkspaceIdentifierWithSpecial,
5};
6use hyprland::prelude::*;
7use hyprland::shared::{Address, WorkspaceId};
8use tracing::{trace, warn};
9
10pub fn switch_client(address: Address) -> anyhow::Result<()> {
11 trace!("execute switch to client: {}", address);
12 Dispatch::call(DispatchType::FocusWindow(WindowIdentifier::Address(
13 address,
14 )))?;
15 Dispatch::call(DispatchType::BringActiveToTop)?;
16 Ok(())
17}
18
19pub fn switch_client_by_initial_class(class: &str) -> anyhow::Result<()> {
20 trace!("execute switch to client: {} by initial_class", class);
21 Dispatch::call(DispatchType::FocusWindow(
22 WindowIdentifier::ClassRegularExpression(&format!("initialclass:{}", class.to_ascii_lowercase())),
23 ))?;
24 Dispatch::call(DispatchType::BringActiveToTop)?;
25 Ok(())
26}
27
28pub fn switch_workspace(workspace_id: WorkspaceId) -> anyhow::Result<()> {
29 let current_workspace = Workspace::get_active();
31 if let Ok(workspace) = current_workspace {
32 if workspace_id == workspace.id {
33 trace!("Already on workspace {}", workspace_id);
34 return Ok(());
35 }
36 }
37
38 if workspace_id < 0 {
39 warn!(
40 "Special workspace id detected, not switching to special workspace, TODO not supported"
41 );
42 } else {
43 switch_normal_workspace(workspace_id).with_context(|| {
44 format!(
45 "Failed to execute switch workspace with id {}",
46 workspace_id
47 )
48 })?;
49 }
50 Ok(())
51}
52
53fn switch_normal_workspace(workspace_id: WorkspaceId) -> anyhow::Result<()> {
54 trace!("execute switch to workspace {workspace_id}");
55 Dispatch::call(DispatchType::Workspace(WorkspaceIdentifierWithSpecial::Id(
56 workspace_id,
57 )))?;
58 Ok(())
59}