Skip to content

Commit

Permalink
Find and run extra watchers from aw-qt.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
2e3s committed Nov 8, 2023
1 parent e04d1de commit 17bafd5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 8 deletions.
44 changes: 40 additions & 4 deletions src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,58 @@ mod server;

pub use menu::Tray;
use std::path::PathBuf;
use std::process::Command;
use tokio::sync::mpsc::UnboundedSender;

fn get_config_watchers() -> Option<Vec<String>> {
let mut config_path = aw_server::dirs::get_config_dir().ok()?;
config_path.pop();
config_path.push("aw-qt");
config_path.push("aw-qt.toml");
debug!("Reading aw-qt 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!("aw-qt config: {toml_content:?}");

Some(
toml_content
.get("aw-qt")?
.get("autostart_modules")?
.as_array()?
.iter()
.filter_map(|value| value.as_str())
.filter(|&s| !s.starts_with("aw-server"))
.filter(|&s| s != "aw-watcher-afk")
.filter(|&s| s != "aw-watcher-window")
.map(std::string::ToString::to_string)
.collect(),
)
}

pub async fn run(
host: String,
port: u32,
config_file: PathBuf,
no_tray: bool,
shutdown_sender: UnboundedSender<()>,
) {
let watchers: Vec<String> = get_config_watchers().unwrap_or_default();

for watcher in &watchers {
debug!("Starting an external watcher {}", watcher);
let _ = Command::new(watcher).spawn();
}

if !no_tray {
let service = ksni::TrayService::new(Tray {
server_host: host,
server_port: port,
let service = ksni::TrayService::new(Tray::new(
host,
port,
config_file,
shutdown_sender,
});
watchers,
));
service.spawn();
}

Expand Down
62 changes: 58 additions & 4 deletions src/bundle/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,29 @@ use tokio::sync::mpsc::UnboundedSender;

#[derive(Debug)]
pub struct Tray {
pub server_host: String,
pub server_port: u32,
pub config_file: PathBuf,
pub shutdown_sender: UnboundedSender<()>,
server_host: String,
server_port: u32,
config_file: PathBuf,
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 {
Expand All @@ -23,6 +42,35 @@ impl ksni::Tray for Tray {
"Awatcher".into()
}
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![
ksni::menu::StandardItem {
label: "ActivityWatch".into(),
Expand All @@ -38,6 +86,12 @@ impl ksni::Tray for Tray {
..Default::default()
}
.into(),
ksni::menu::SubMenu {
label: "Watchers".into(),
submenu: watchers_submenu,
..Default::default()
}
.into(),
ksni::menu::StandardItem {
label: "Configuration".into(),
icon_name: "preferences-other".into(),
Expand Down

0 comments on commit 17bafd5

Please sign in to comment.