gravel_provider_system/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//! System provider.
//! Provides system commands such as shutdown, log out or exiting gravel.

use anyhow::Result;
use gravel_ffi::prelude::*;
use serde::Deserialize;
use std::env;

#[cfg_attr(target_os = "linux", path = "linux.rs")]
#[cfg_attr(windows, path = "windows.rs")]
mod implementation;

const DEFAULT_CONFIG: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/config.yml"));

struct SystemProvider {
	hits: StaticHitCache,
}

#[gravel_provider("system")]
impl Provider for SystemProvider {
	fn new(config: &PluginConfigAdapter<'_>) -> Self {
		let config = config.get::<Config>(DEFAULT_CONFIG);
		let hits = [
			context_hit(config.exit, |ctx| ctx.exit()),
			context_hit(config.reload, |ctx| ctx.restart()),
			context_hit(config.clear_caches, clear_caches),
			shell_hit(config.lock, implementation::lock),
			shell_hit(config.logout, implementation::logout),
			shell_hit(config.restart, implementation::restart),
			shell_hit(config.shutdown, implementation::shutdown),
			shell_hit(config.sleep, implementation::sleep),
		];

		Self {
			hits: StaticHitCache::new(hits),
		}
	}

	fn query(&self, _query: &str) -> ProviderResult {
		ProviderResult::from_cached(self.hits.get())
	}
}

fn clear_caches(context: RefDynHitActionContext<'_>) {
	context.clear_caches();
	context.hide_frontend();
}

fn context_hit(
	config: CommandConfig,
	action: impl Fn(RefDynHitActionContext<'_>) + Send + Sync + 'static,
) -> SimpleHit {
	SimpleHit::new(config.title, config.subtitle, move |_, ctx| action(ctx))
}

fn shell_hit(config: ShellCommandConfig, action: impl Fn(&str) -> Result<()> + Send + Sync + 'static) -> SimpleHit {
	SimpleHit::new(config.title, config.subtitle, move |hit, context| {
		action(&config.command_linux)
			.inspect_err(|e| log::error!("unable to perform system operation {}: {e}", hit.title()))
			.ok();

		context.hide_frontend();
	})
}

#[derive(Deserialize, Debug)]
struct Config {
	pub exit: CommandConfig,
	pub reload: CommandConfig,
	pub clear_caches: CommandConfig,
	pub lock: ShellCommandConfig,
	pub logout: ShellCommandConfig,
	pub restart: ShellCommandConfig,
	pub shutdown: ShellCommandConfig,
	pub sleep: ShellCommandConfig,
}

#[derive(Deserialize, Debug)]
struct CommandConfig {
	pub title: String,
	pub subtitle: String,
}

#[derive(Deserialize, Debug)]
struct ShellCommandConfig {
	pub title: String,
	pub subtitle: String,
	pub command_linux: String,
}