mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-08-17 23:01:04 +02:00
Merge pull request #1060 from Username404-59/master
Add a launch_command setting
This commit is contained in:
commit
bb7e927309
8 changed files with 91 additions and 66 deletions
|
@ -481,3 +481,7 @@ pub const fn default_false() -> bool {
|
|||
pub const fn default_true() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn default_launch_command() -> String {
|
||||
String::from("gtk-launch {app_name}")
|
||||
}
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
use crate::spawn;
|
||||
use color_eyre::Result;
|
||||
use color_eyre::{Help, Report, Result};
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::Arc;
|
||||
use tokio::io::{AsyncBufReadExt, BufReader};
|
||||
use tokio::sync::Mutex;
|
||||
use tracing::debug;
|
||||
use tracing::{debug, error};
|
||||
use walkdir::{DirEntry, WalkDir};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -323,6 +324,25 @@ fn files(dir: &Path) -> Vec<PathBuf> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
/// Starts a `.desktop` file with the provided formatted command.
|
||||
pub fn open_program(file_name: &str, str: &str) {
|
||||
let expanded = str.replace("{app_name}", file_name);
|
||||
let launch_command_parts: Vec<&str> = expanded.split_whitespace().collect();
|
||||
if let Err(err) = Command::new(&launch_command_parts[0])
|
||||
.args(&launch_command_parts[1..])
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
{
|
||||
error!(
|
||||
"{:?}",
|
||||
Report::new(err)
|
||||
.wrap_err("Failed to run launch command.")
|
||||
.suggestion("Perhaps the applications file is invalid?")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
@ -7,17 +7,19 @@ use self::open_state::OpenState;
|
|||
use super::{Module, ModuleInfo, ModuleParts, ModulePopup, ModuleUpdateEvent, WidgetContext};
|
||||
use crate::channels::{AsyncSenderExt, BroadcastReceiverExt};
|
||||
use crate::clients::wayland::{self, ToplevelEvent};
|
||||
use crate::config::{CommonConfig, EllipsizeMode, LayoutConfig, TruncateMode};
|
||||
use crate::config::{
|
||||
CommonConfig, EllipsizeMode, LayoutConfig, TruncateMode, default_launch_command,
|
||||
};
|
||||
use crate::desktop_file::open_program;
|
||||
use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt};
|
||||
use crate::modules::launcher::item::ImageTextButton;
|
||||
use crate::modules::launcher::pagination::{IconContext, Pagination};
|
||||
use crate::{arc_mut, lock, module_impl, spawn, write_lock};
|
||||
use color_eyre::{Help, Report};
|
||||
use color_eyre::Report;
|
||||
use gtk::prelude::*;
|
||||
use gtk::{Button, Orientation};
|
||||
use indexmap::IndexMap;
|
||||
use serde::Deserialize;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc;
|
||||
use tracing::{debug, error, trace, warn};
|
||||
|
@ -108,6 +110,12 @@ pub struct LauncherModule {
|
|||
#[serde(default, flatten)]
|
||||
layout: LayoutConfig,
|
||||
|
||||
/// Command used to launch applications.
|
||||
///
|
||||
/// **Default**: `gtk-launch`
|
||||
#[serde(default = "default_launch_command")]
|
||||
launch_command: String,
|
||||
|
||||
/// See [common options](module-level-options#common-options).
|
||||
#[serde(flatten)]
|
||||
pub common: Option<CommonConfig>,
|
||||
|
@ -373,25 +381,14 @@ impl Module<gtk::Box> for LauncherModule {
|
|||
let wl = context.client::<wayland::Client>();
|
||||
|
||||
let desktop_files = context.ironbar.desktop_files();
|
||||
let launch_command_str: String = self.launch_command.clone();
|
||||
|
||||
spawn(async move {
|
||||
while let Some(event) = rx.recv().await {
|
||||
if let ItemEvent::OpenItem(app_id) = event {
|
||||
match desktop_files.find(&app_id).await {
|
||||
Ok(Some(file)) => {
|
||||
if let Err(err) = Command::new("gtk-launch")
|
||||
.arg(file.file_name)
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
{
|
||||
error!(
|
||||
"{:?}",
|
||||
Report::new(err)
|
||||
.wrap_err("Failed to run gtk-launch command.")
|
||||
.suggestion("Perhaps the applications file is invalid?")
|
||||
);
|
||||
}
|
||||
open_program(&file.file_name, &launch_command_str);
|
||||
}
|
||||
Ok(None) => warn!("Could not find applications file for {}", app_id),
|
||||
Err(err) => error!("Failed to find parse file for {}: {}", app_id, err),
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::config::default_launch_command;
|
||||
use crate::config::{CommonConfig, TruncateMode};
|
||||
use crate::modules::menu::{MenuEntry, XdgSection};
|
||||
use indexmap::IndexMap;
|
||||
|
@ -123,6 +124,12 @@ pub struct MenuModule {
|
|||
/// See [common options](module-level-options#common-options).
|
||||
#[serde(flatten)]
|
||||
pub common: Option<CommonConfig>,
|
||||
|
||||
/// Command used to launch applications.
|
||||
///
|
||||
/// **Default**: `gtk-launch`
|
||||
#[serde(default = "default_launch_command")]
|
||||
pub launch_command: String,
|
||||
}
|
||||
|
||||
impl Default for MenuModule {
|
||||
|
@ -139,6 +146,7 @@ impl Default for MenuModule {
|
|||
label_icon: None,
|
||||
label_icon_size: default_menu_popup_icon_size(),
|
||||
common: Some(CommonConfig::default()),
|
||||
launch_command: default_launch_command(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -262,8 +262,13 @@ impl Module<Button> for MenuModule {
|
|||
for entry in $entries.values() {
|
||||
let container1 = container.clone();
|
||||
let tx = context.tx.clone();
|
||||
let (button, sub_menu) =
|
||||
ui::make_entry(entry, tx, &image_provider, truncate_mode);
|
||||
let (button, sub_menu) = ui::make_entry(
|
||||
entry,
|
||||
tx,
|
||||
&image_provider,
|
||||
truncate_mode,
|
||||
&self.launch_command,
|
||||
);
|
||||
|
||||
if let Some(sub_menu) = sub_menu.clone() {
|
||||
sub_menu.set_valign(alignment);
|
||||
|
|
|
@ -1,14 +1,13 @@
|
|||
use super::MenuEntry;
|
||||
use crate::channels::AsyncSenderExt;
|
||||
use crate::config::TruncateMode;
|
||||
use crate::desktop_file::open_program;
|
||||
use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt};
|
||||
use crate::modules::ModuleUpdateEvent;
|
||||
use crate::script::Script;
|
||||
use crate::{image, spawn};
|
||||
use color_eyre::{Help, Report};
|
||||
use gtk::prelude::*;
|
||||
use gtk::{Align, Button, Label, Orientation};
|
||||
use std::process::{Command, Stdio};
|
||||
use tokio::sync::mpsc;
|
||||
use tracing::{debug, error};
|
||||
|
||||
|
@ -17,6 +16,7 @@ pub fn make_entry<R>(
|
|||
tx: mpsc::Sender<ModuleUpdateEvent<R>>,
|
||||
image_provider: &image::Provider,
|
||||
truncate_mode: TruncateMode,
|
||||
launch_command_str: &str,
|
||||
) -> (Button, Option<gtk::Box>)
|
||||
where
|
||||
R: Send + Clone + 'static,
|
||||
|
@ -96,22 +96,11 @@ where
|
|||
{
|
||||
let sub_menu = sub_menu.clone();
|
||||
let file_name = sub_entry.file_name.clone();
|
||||
let command = launch_command_str.to_string();
|
||||
let tx = tx.clone();
|
||||
|
||||
button.connect_clicked(move |_button| {
|
||||
if let Err(err) = Command::new("gtk-launch")
|
||||
.arg(&file_name)
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()
|
||||
{
|
||||
error!(
|
||||
"{:?}",
|
||||
Report::new(err)
|
||||
.wrap_err("Failed to run gtk-launch command.")
|
||||
.suggestion("Perhaps the applications file is invalid?")
|
||||
);
|
||||
}
|
||||
open_program(&file_name, &command);
|
||||
|
||||
sub_menu.hide();
|
||||
tx.send_spawn(ModuleUpdateEvent::ClosePopup);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue