2021-03-29 15:59:14 +08:00
|
|
|
use super::*;
|
|
|
|
pub use crate::common::{
|
|
|
|
check_clipboard, ClipboardContext, CLIPBOARD_INTERVAL as INTERVAL, CLIPBOARD_NAME as NAME,
|
|
|
|
CONTENT,
|
|
|
|
};
|
|
|
|
|
2022-05-12 17:35:25 +08:00
|
|
|
struct State {
|
|
|
|
ctx: Option<ClipboardContext>,
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
|
2022-05-12 17:35:25 +08:00
|
|
|
impl Default for State {
|
|
|
|
fn default() -> Self {
|
|
|
|
let ctx = match ClipboardContext::new() {
|
|
|
|
Ok(ctx) => Some(ctx),
|
2021-10-27 20:02:17 +08:00
|
|
|
Err(err) => {
|
|
|
|
log::error!("Failed to start {}: {}", NAME, err);
|
2022-05-12 17:35:25 +08:00
|
|
|
None
|
2021-10-26 16:59:31 +08:00
|
|
|
}
|
2021-10-27 20:02:17 +08:00
|
|
|
};
|
2022-05-12 17:35:25 +08:00
|
|
|
Self { ctx }
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 16:59:31 +08:00
|
|
|
|
2022-05-12 17:35:25 +08:00
|
|
|
impl super::service::Reset for State {
|
|
|
|
fn reset(&mut self) {
|
2021-10-25 16:25:23 +08:00
|
|
|
*CONTENT.lock().unwrap() = Default::default();
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
2022-05-12 17:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new() -> GenericService {
|
|
|
|
let sp = GenericService::new(NAME, true);
|
|
|
|
sp.repeat::<State, _>(INTERVAL, run);
|
|
|
|
sp
|
|
|
|
}
|
2021-10-27 20:02:17 +08:00
|
|
|
|
2022-05-12 17:35:25 +08:00
|
|
|
fn run(sp: GenericService, state: &mut State) -> ResultType<()> {
|
|
|
|
if let Some(ctx) = state.ctx.as_mut() {
|
|
|
|
if let Some(msg) = check_clipboard(ctx, None) {
|
|
|
|
sp.send(msg);
|
|
|
|
}
|
|
|
|
sp.snapshot(|sps| {
|
|
|
|
let txt = crate::CONTENT.lock().unwrap().clone();
|
|
|
|
if !txt.is_empty() {
|
|
|
|
let msg_out = crate::create_clipboard_msg(txt);
|
|
|
|
sps.send_shared(Arc::new(msg_out));
|
2021-12-23 17:44:44 +08:00
|
|
|
}
|
2022-05-12 17:35:25 +08:00
|
|
|
Ok(())
|
|
|
|
})?;
|
2021-10-27 20:02:17 +08:00
|
|
|
}
|
2022-05-12 17:35:25 +08:00
|
|
|
Ok(())
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|