mirror of
https://github.com/2e3s/awatcher.git
synced 2025-07-25 03:20:08 +00:00
Find and run extra watchers from aw-qt.toml
This commit is contained in:
parent
e04d1de80e
commit
0ccdba4dce
@ -2,9 +2,32 @@ mod menu;
|
|||||||
mod server;
|
mod server;
|
||||||
|
|
||||||
pub use menu::Tray;
|
pub use menu::Tray;
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::process::Command;
|
||||||
use tokio::sync::mpsc::UnboundedSender;
|
use tokio::sync::mpsc::UnboundedSender;
|
||||||
|
|
||||||
|
fn get_config_watchers(config_path: &Path) -> Option<Vec<String>> {
|
||||||
|
let mut config_path = config_path.parent()?.to_path_buf();
|
||||||
|
config_path.push("bundle-config.toml");
|
||||||
|
debug!("Reading bundle config at {}", config_path.display());
|
||||||
|
|
||||||
|
let config_content = std::fs::read_to_string(&config_path).ok()?;
|
||||||
|
let toml_content: toml::Value = toml::from_str(&config_content).ok()?;
|
||||||
|
|
||||||
|
trace!("Bundle config: {toml_content:?}");
|
||||||
|
|
||||||
|
Some(
|
||||||
|
toml_content
|
||||||
|
.get("watchers")?
|
||||||
|
.get("autostart")?
|
||||||
|
.as_array()?
|
||||||
|
.iter()
|
||||||
|
.filter_map(|value| value.as_str())
|
||||||
|
.map(std::string::ToString::to_string)
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn run(
|
pub async fn run(
|
||||||
host: String,
|
host: String,
|
||||||
port: u32,
|
port: u32,
|
||||||
@ -12,13 +35,22 @@ pub async fn run(
|
|||||||
no_tray: bool,
|
no_tray: bool,
|
||||||
shutdown_sender: UnboundedSender<()>,
|
shutdown_sender: UnboundedSender<()>,
|
||||||
) {
|
) {
|
||||||
|
let watchers: Vec<String> =
|
||||||
|
get_config_watchers(config_file.parent().unwrap()).unwrap_or_default();
|
||||||
|
|
||||||
|
for watcher in &watchers {
|
||||||
|
debug!("Starting an external watcher {}", watcher);
|
||||||
|
let _ = Command::new(watcher).spawn();
|
||||||
|
}
|
||||||
|
|
||||||
if !no_tray {
|
if !no_tray {
|
||||||
let service = ksni::TrayService::new(Tray {
|
let service = ksni::TrayService::new(Tray::new(
|
||||||
server_host: host,
|
host,
|
||||||
server_port: port,
|
port,
|
||||||
config_file,
|
config_file,
|
||||||
shutdown_sender,
|
shutdown_sender,
|
||||||
});
|
watchers,
|
||||||
|
));
|
||||||
service.spawn();
|
service.spawn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,10 +4,29 @@ use tokio::sync::mpsc::UnboundedSender;
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Tray {
|
pub struct Tray {
|
||||||
pub server_host: String,
|
server_host: String,
|
||||||
pub server_port: u32,
|
server_port: u32,
|
||||||
pub config_file: PathBuf,
|
config_file: PathBuf,
|
||||||
pub shutdown_sender: UnboundedSender<()>,
|
shutdown_sender: UnboundedSender<()>,
|
||||||
|
watchers: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tray {
|
||||||
|
pub fn new(
|
||||||
|
server_host: String,
|
||||||
|
server_port: u32,
|
||||||
|
config_file: PathBuf,
|
||||||
|
shutdown_sender: UnboundedSender<()>,
|
||||||
|
watchers: Vec<String>,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
server_host,
|
||||||
|
server_port,
|
||||||
|
config_file,
|
||||||
|
shutdown_sender,
|
||||||
|
watchers,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ksni::Tray for Tray {
|
impl ksni::Tray for Tray {
|
||||||
@ -23,6 +42,35 @@ impl ksni::Tray for Tray {
|
|||||||
"Awatcher".into()
|
"Awatcher".into()
|
||||||
}
|
}
|
||||||
fn menu(&self) -> Vec<ksni::MenuItem<Self>> {
|
fn menu(&self) -> Vec<ksni::MenuItem<Self>> {
|
||||||
|
let mut watchers_submenu: Vec<ksni::MenuItem<Self>> = vec![
|
||||||
|
ksni::menu::CheckmarkItem {
|
||||||
|
label: "Idle".into(),
|
||||||
|
enabled: false,
|
||||||
|
checked: true,
|
||||||
|
activate: Box::new(|_this: &mut Self| {}),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
ksni::menu::CheckmarkItem {
|
||||||
|
label: "Window".into(),
|
||||||
|
enabled: false,
|
||||||
|
checked: true,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
];
|
||||||
|
for watcher in &self.watchers {
|
||||||
|
watchers_submenu.push(
|
||||||
|
ksni::menu::CheckmarkItem {
|
||||||
|
label: watcher.clone(),
|
||||||
|
enabled: false,
|
||||||
|
checked: true,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
vec![
|
vec![
|
||||||
ksni::menu::StandardItem {
|
ksni::menu::StandardItem {
|
||||||
label: "ActivityWatch".into(),
|
label: "ActivityWatch".into(),
|
||||||
@ -38,6 +86,12 @@ impl ksni::Tray for Tray {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
|
ksni::menu::SubMenu {
|
||||||
|
label: "Watchers".into(),
|
||||||
|
submenu: watchers_submenu,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.into(),
|
||||||
ksni::menu::StandardItem {
|
ksni::menu::StandardItem {
|
||||||
label: "Configuration".into(),
|
label: "Configuration".into(),
|
||||||
icon_name: "preferences-other".into(),
|
icon_name: "preferences-other".into(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user