Skip to content

Commit

Permalink
fix: added missing global values (dry, allow_toasts, etc.)
Browse files Browse the repository at this point in the history
Signed-off-by: Enrico Stemmer <enrico@h3rmt.zip>
  • Loading branch information
H3rmt committed Feb 6, 2025
1 parent f74284f commit b2a55ab
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 30 deletions.
2 changes: 1 addition & 1 deletion config/hyprswitch.ron
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Config(
windows: (
show_title: true,
workspaces_per_row: 4,
strip_html_from_title: true,
strip_html_from_workspace_title: true,
// removed
icon_size: 512,
// removed
Expand Down
2 changes: 1 addition & 1 deletion src/config/config_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Windows {
#[default = 5]
pub workspaces_per_row: u8,
#[default = true]
pub strip_html_from_title: bool,
pub strip_html_from_workspace_title: bool,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/gui/gui_handle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ops::Deref;
use crate::daemon::cache::cache_run;
use crate::daemon::gui::launcher::show_launch_spawn;
use crate::daemon::gui::reload_desktop_maps;
Expand All @@ -6,7 +7,6 @@ use crate::handle::{clear_recent_clients, run_program, switch_to_active};
use crate::{global, Active, ClientId, MonitorId, Warn, WorkspaceId};
use anyhow::Context;
use gtk4::glib::clone;
use std::ops::Deref;
use std::thread;
use tracing::{trace, warn};

Expand Down
22 changes: 17 additions & 5 deletions src/daemon/gui/launcher/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::daemon::gui::gui_handle::{
use crate::daemon::gui::maps::get_all_desktop_files;
use crate::daemon::gui::LauncherRefs;
use crate::daemon::{Exec, GUISend, LaunchState, ReverseKey, Share, UpdateCause};
use crate::Warn;
use crate::{global, Warn};
use async_channel::Sender;
use gtk4::gdk::{Key, Texture};
use gtk4::glib::{clone, ControlFlow, Propagation};
Expand Down Expand Up @@ -106,6 +106,8 @@ pub(super) fn update_launcher(
selected: Option<usize>,
launch_state: &LaunchState,
reverse_key: &ReverseKey,
launcher_max_items: u8,
show_launcher_execs: bool,
) -> Vec<Exec> {
while let Some(child) = list.first_child() {
list.remove(&child);
Expand Down Expand Up @@ -137,8 +139,10 @@ pub(super) fn update_launcher(
}
}

for (index, (name, icon, exec, path, terminal)) in
matches.into_iter().take(*LAUNCHER_MAX_ITEMS).enumerate()
for (index, (name, icon, exec, path, terminal)) in matches
.into_iter()
.take(launcher_max_items as usize)
.enumerate()
{
let i = index as i32 - selected.unwrap_or(0) as i32;
let widget = create_launch_widget(
Expand Down Expand Up @@ -171,6 +175,7 @@ pub(super) fn update_launcher(
} else {
None
},
show_launcher_execs,
);
list.append(&widget);
execs.push(Exec {
Expand All @@ -191,6 +196,7 @@ fn create_launch_widget(
raw_index: usize,
index: &str,
selected: Option<&LaunchState>,
show_launcher_execs: bool,
) -> ListBoxRow {
let hbox = gtk4::Box::builder()
.orientation(Orientation::Horizontal)
Expand Down Expand Up @@ -228,7 +234,7 @@ fn create_launch_widget(
.build();
hbox.append(&title);

if *SHOW_LAUNCHER_EXECS {
if show_launcher_execs {
let exec = Label::builder()
.halign(Align::Start)
.valign(Align::Center)
Expand Down Expand Up @@ -300,7 +306,13 @@ pub fn show_launch_spawn(share: Share, cause: Option<u8>) {
trace!("Received refresh finish from GUI: {rec:?}");

// wait for the GUI to update
thread::sleep(Duration::from_millis(*LAUNCHER_ANIMATE_LAUNCH_TIME));
thread::sleep(Duration::from_millis(
global::OPTS
.get()
.map(|o| o.animate_launch_time)
.warn("Failed to access global animate_launch_time")
.unwrap_or(300),
));

{
let mut lat = latest.lock().expect("Failed to lock");
Expand Down
3 changes: 3 additions & 0 deletions src/daemon/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ async fn handle_update(
init_gui_config.show_title,
data.gui_config.show_workspaces_on_all_monitors,
init_gui_config.size_factor,
init_gui_config.strip_html_workspace_title,
);

trace!("Refresh window {:?}", window);
Expand Down Expand Up @@ -233,6 +234,8 @@ async fn handle_update(
data.launcher_data.selected,
&data.launcher_data.launch_state,
&data.submap_config.reverse_key,
init_gui_config.launcher_max_items,
init_gui_config.show_execs,
);
data.launcher_data.execs = execs;
});
Expand Down
3 changes: 2 additions & 1 deletion src/daemon/gui/windows/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub fn init_windows(
show_title: bool,
show_workspaces_on_all_monitors: bool,
size_factor: f64,
strip_html_workspace_title: bool,
) {
// clear_monitor(monitor_data);
let workspaces = {
Expand All @@ -40,7 +41,7 @@ pub fn init_windows(

let id_string = wid.to_string();
let title = if show_title && !workspace.name.trim().is_empty() {
if *REMOVE_HTML_FROM_WORKSPACE_NAME {
if strip_html_workspace_title {
regex.replace_all(&workspace.name, "$1")
} else {
Cow::from(&workspace.name)
Expand Down
7 changes: 6 additions & 1 deletion src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ mod handle_fns;

pub use data::*;

pub use gui::{debug_desktop_files, debug_list, debug_search_class};
pub use cache::get_cached_runs;
pub use gui::{debug_desktop_files, debug_list, debug_search_class};
#[derive(Debug, Clone)]
pub(crate) enum GUISend {
Refresh,
Expand Down Expand Up @@ -63,6 +63,11 @@ pub struct InitGuiConfig {
pub show_title: bool,
pub workspaces_per_row: u8,
pub size_factor: f64,
pub launcher_max_items: u8,
pub default_terminal: Option<String>,
pub show_execs: bool,
pub animate_launch_time: u64,
pub strip_html_workspace_title: bool,
}

pub fn start_daemon(init_gui_config: InitGuiConfig) -> anyhow::Result<()> {
Expand Down
37 changes: 29 additions & 8 deletions src/handle/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hyprland::prelude::HyprDataActive;
use hyprland::shared::{Address, MonitorId, WorkspaceId};
use tracing::{debug, span, warn, Level};

use crate::{global, to_client_address, Active, FindByFirst, HyprlandData};
use crate::{global, to_client_address, Active, FindByFirst, HyprlandData, Warn};

pub fn switch_to_active(
active: Option<&Active>,
Expand All @@ -30,13 +30,24 @@ pub fn switch_to_active(
id: data.workspace,
name: workspace_data.name.clone(),
},
*global::DRY.get().expect("DRY not set"),
global::OPTS
.get()
.map(|o| o.dry)
.warn("Failed to access global dry")
.unwrap_or(false),
)
.with_context(|| {
format!("Failed to execute switch workspace with workspace_data {workspace_data:?}")
})?;
switch_client(&to_client_address(*addr), *global::DRY.get().expect("DRY not set"))
.with_context(|| format!("Failed to execute with addr {addr:?}"))?;
switch_client(
&to_client_address(*addr),
global::OPTS
.get()
.map(|o| o.dry)
.warn("Failed to access global dry")
.unwrap_or(false),
)
.with_context(|| format!("Failed to execute with addr {addr:?}"))?;
}
Some(Active::Workspace(wid)) => {
let workspace_data = clients_data
Expand All @@ -48,16 +59,26 @@ pub fn switch_to_active(
id: *wid,
name: workspace_data.name.clone(),
},
*global::DRY.get().expect("DRY not set"),
global::OPTS
.get()
.map(|o| o.dry)
.warn("Failed to access global dry")
.unwrap_or(false),
)
.with_context(|| {
format!("Failed to execute switch workspace with workspace_data {workspace_data:?}")
})?;
}
Some(Active::Monitor(mid)) => {
switch_monitor(mid, *global::DRY.get().expect("DRY not set")).with_context(|| {
format!("Failed to execute switch monitor with monitor_id {mid:?}")
})?;
switch_monitor(
mid,
global::OPTS
.get()
.map(|o| o.dry)
.warn("Failed to access global dry")
.unwrap_or(false),
)
.with_context(|| format!("Failed to execute switch monitor with monitor_id {mid:?}"))?;
}
None => {
warn!("Not executing switch (active = Unknown)");
Expand Down
10 changes: 7 additions & 3 deletions src/handle/run.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use crate::Warn;
use std::ops::Deref;
use crate::{global, Warn};
use std::os::unix::prelude::CommandExt;
use std::process::{Command, Stdio};
use std::{io, thread};
use tracing::{debug, info};

pub fn run_program(run: &str, path: &Option<Box<str>>, terminal: bool) {
if terminal {
if let Some(term) = DEFAULT_TERMINAL.deref() {
if let Some(term) = global::OPTS
.get()
.map(|o| o.default_terminal.as_ref())
.warn("Failed to access global default terminal")
.unwrap_or_default()
{
let mut process = Command::new(term);
process.arg("-e");
run_command(&mut process, run, path).warn("Failed to run command");
Expand Down
22 changes: 17 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ pub use hypr_data::*;
const MIN_VERSION: Version = Version::new(0, 42, 0);

pub mod global {
/// global variable to store if we are in dry mode
pub static DRY: std::sync::OnceLock<bool> = std::sync::OnceLock::new();

/// global variable to store if gui is open
/// global variable to store if gui is open TODO check if this can be put in shared data
pub static OPEN: std::sync::OnceLock<std::sync::Mutex<bool>> = std::sync::OnceLock::new();

/// immutable global variable to store global options
pub static OPTS: std::sync::OnceLock<Global> = std::sync::OnceLock::new();

pub struct Global {
pub dry: bool,
pub toasts_allowed: bool,
pub animate_launch_time: u64,
pub default_terminal: Option<String>,
}
}

type WorkspaceId = i32;
Expand Down Expand Up @@ -95,7 +102,12 @@ impl<A, E: Display> Warn<A> for Result<A, E> {
}

pub fn toast(_body: &str) {
if !*envs::DISABLE_TOASTS {
if global::OPTS
.get()
.map(|o| o.toasts_allowed)
.warn("Failed to access global toasts_allowed")
.unwrap_or(true)
{
#[cfg(not(debug_assertions))]
let _ = notify_rust::Notification::new()
.summary(&format!(
Expand Down
16 changes: 12 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@ fn main() -> anyhow::Result<()> {
.finish();
tracing::subscriber::set_global_default(subscriber).warn("Unable to initialize logging");

check_version().warn("Unable to check Hyprland version, continuing anyway");

global::DRY
.set(cli.global_opts.dry_run)
.expect("unable to set DRY (already filled???)");
.warn("unable to set DRY (already filled???)");
global::OPEN
.set(Mutex::new(false))
.expect("unable to set ACTIVE (already filled???)");
.warn("unable to set ACTIVE (already filled???)");

check_version().warn("Unable to check Hyprland version, continuing anyway");

match cli.command {
cli::Command::Run { config_file, .. } => {
info!("Loading config");
let config = hyprswitch::config::load(config_file).context("Failed to load config")?;
global::TOASTS_ALLOWED
.set(!config.general.disable_toast)
.warn("unable to set TOASTS_ALLOWED (already filled???)");
trace!(
"Config read: {}",
serde_json::to_string(&config).unwrap_or("Failed to serialize config".to_string())
Expand All @@ -67,6 +70,11 @@ fn main() -> anyhow::Result<()> {
show_title: config.general.windows.show_title,
workspaces_per_row: config.general.windows.workspaces_per_row,
size_factor: config.general.size_factor,
launcher_max_items: config.general.launcher.items,
default_terminal: config.general.launcher.default_terminal.clone(),
show_execs: config.general.launcher.show_execs,
animate_launch_time: config.general.launcher.animate_launch_time_ms,
strip_html_workspace_title: config.general.windows.strip_html_from_workspace_title,
};
let list = hyprswitch::config::create_binds_and_submaps(config)
.context("Failed to create binds and submaps")?;
Expand Down

0 comments on commit b2a55ab

Please sign in to comment.