Skip to main content

hyprshell_exec_lib/
switch.rs

1use crate::util::to_client_address;
2use anyhow::Context;
3use core_lib::{ClientId, Warn, WorkspaceAddress};
4use hyprland::data::{Client, Monitors, Workspace, WorkspaceKind, Workspaces};
5use hyprland::prelude::*;
6use tracing::{debug, instrument, trace};
7
8pub fn switch_client(address: ClientId) -> anyhow::Result<()> {
9    debug!("execute switch to client: {address}");
10    deactivate_special_workspace_if_needed().warn();
11    let disp = hyprland::dispatch_new::Dispatch::FocusWindow(
12        hyprland::dispatch_new::WindowIdentifier::Address(to_client_address(address)),
13    );
14    disp.apply().context("failed to execute dispatch")?;
15    let disp2 = hyprland::dispatch_new::Dispatch::WindowAlterZ(
16        hyprland::dispatch_new::ZOption::Top,
17        Some(hyprland::dispatch_new::WindowIdentifier::Address(
18            to_client_address(address),
19        )),
20    );
21    disp2.apply().context("failed to execute dispatch2")?;
22    Ok(())
23}
24
25pub fn switch_client_by_initial_class(class: &str) -> anyhow::Result<()> {
26    debug!("execute switch to client: {class} by initial_class");
27    deactivate_special_workspace_if_needed().warn();
28
29    let disp = hyprland::dispatch_new::Dispatch::FocusWindow(
30        hyprland::dispatch_new::WindowIdentifier::InitialClassRegularExpression(
31            class.to_ascii_lowercase(),
32        ),
33    );
34    disp.apply().context("failed to execute dispatch")?;
35    let disp2 = hyprland::dispatch_new::Dispatch::WindowAlterZ(
36        hyprland::dispatch_new::ZOption::Top,
37        Some(
38            hyprland::dispatch_new::WindowIdentifier::InitialClassRegularExpression(
39                class.to_ascii_lowercase(),
40            ),
41        ),
42    );
43    disp2.apply().context("failed to execute dispatch2")?;
44    Ok(())
45}
46
47#[instrument(level = "debug", ret(level = "trace"))]
48pub fn switch_workspace(workspace_address: WorkspaceAddress) -> anyhow::Result<()> {
49    if Workspace::get_active().is_ok_and(|workspace| workspace.address == workspace_address) {
50        trace!("Already on workspace {workspace_address}");
51        return Ok(());
52    }
53
54    if Monitors::get()?
55        .iter()
56        .any(|monitor| monitor.special_workspace.address == workspace_address)
57    {
58        trace!("Special workspace is already active on a monitor: {workspace_address}");
59        return Ok(());
60    }
61
62    deactivate_special_workspace_if_needed().warn();
63
64    let workspace = Workspaces::get()?
65        .into_iter()
66        .find(|workspace| workspace.address == workspace_address)
67        .context("workspace not found")?;
68
69    if workspace.workspace_type == WorkspaceKind::Special {
70        switch_special_workspace(&workspace_address).with_context(|| {
71            format!("Failed to execute switch special workspace {workspace_address}")
72        })?;
73    } else {
74        switch_normal_workspace(&workspace_address)
75            .with_context(|| format!("Failed to execute switch workspace {workspace_address}"))?;
76    }
77    Ok(())
78}
79
80fn switch_special_workspace(workspace_address: &WorkspaceAddress) -> anyhow::Result<()> {
81    let disp = hyprland::dispatch_new::Dispatch::FocusWorkspace(
82        hyprland::dispatch_new::WorkspaceIdentifier::Address(workspace_address.clone()),
83        false,
84    );
85    disp.apply().context("failed to execute dispatch")?;
86    Ok(())
87}
88
89fn switch_normal_workspace(workspace_address: &WorkspaceAddress) -> anyhow::Result<()> {
90    debug!("execute switch to workspace {workspace_address}");
91    let disp = hyprland::dispatch_new::Dispatch::FocusWorkspace(
92        hyprland::dispatch_new::WorkspaceIdentifier::Address(workspace_address.clone()),
93        false,
94    );
95    disp.apply().context("failed to execute dispatch")?;
96    Ok(())
97}
98
99/// always run when changing client or workspace
100///
101/// if client on special workspace is opened the workspace is activated
102#[instrument(level = "debug", ret(level = "trace"))]
103fn deactivate_special_workspace_if_needed() -> anyhow::Result<()> {
104    let Some(active_client) = Client::get_active().context("active client failed")? else {
105        return Ok(());
106    };
107    let workspace = active_client.workspace;
108
109    trace!("current window workspace: {}", workspace.address);
110    if workspace.workspace_type == WorkspaceKind::Special {
111        debug!("current window is on a special workspace, deactivating it");
112        let disp = hyprland::dispatch_new::Dispatch::ToggleSpecialWorkspace(
113            special_workspace_name(&workspace.name).to_string(),
114        );
115        disp.apply().context("failed to execute dispatch")?;
116    }
117    Ok(())
118}
119
120fn special_workspace_name(address: &WorkspaceAddress) -> &str {
121    address.strip_prefix("special:").unwrap_or(address)
122}