system_message code clean

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2023-01-06 10:45:55 +08:00
parent 658c6500d9
commit b35b426f12

View File

@ -4,6 +4,7 @@ use hbb_common::{allow_err, bail, log};
use libc::{c_char, c_int, c_void}; use libc::{c_char, c_int, c_void};
use std::{ use std::{
cell::RefCell, cell::RefCell,
collections::HashMap,
path::PathBuf, path::PathBuf,
sync::{ sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
@ -719,47 +720,38 @@ pub fn get_double_click_time() -> u32 {
/// forever: may not work /// forever: may not work
pub fn system_message(title: &str, msg: &str, forever: bool) -> ResultType<()> { pub fn system_message(title: &str, msg: &str, forever: bool) -> ResultType<()> {
if std::process::Command::new("notify-send") let cmds: HashMap<&str, Vec<&str>> = HashMap::from([
.arg(title) ("notify-send", [title, msg].to_vec()),
.arg(msg) (
.spawn() "zenity",
.is_ok() [
{ "--info",
return Ok(()); "--timeout",
} if forever { "0" } else { "3" },
if std::process::Command::new("zenity") "--title",
.arg("--info") title,
.arg("--timeout") "--text",
.arg(if forever { "0" } else { "3" }) msg,
.arg("--title") ]
.arg(title) .to_vec(),
.arg("--text") ),
.arg(msg) ("kdialog", ["--title", title, "--msgbox", msg].to_vec()),
.spawn() (
.is_ok() "xmessage",
{ [
return Ok(()); "-center",
} "-timeout",
if std::process::Command::new("kdialog") if forever { "0" } else { "3" },
.arg("--title") title,
.arg(title) msg,
.arg("--msgbox") ]
.arg(msg) .to_vec(),
.spawn() ),
.is_ok() ]);
{ for (k, v) in cmds {
return Ok(()); if std::process::Command::new(k).args(v).spawn().is_ok() {
} return Ok(());
if std::process::Command::new("xmessage") }
.arg("-center")
.arg("-timeout")
.arg(if forever { "0" } else { "3" })
.arg(title)
.arg(msg)
.spawn()
.is_ok()
{
return Ok(());
} }
bail!("failed to post system message"); bail!("failed to post system message");
} }