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,
|
|
|
|
};
|
|
|
|
|
2024-07-01 00:24:23 +08:00
|
|
|
#[derive(Default)]
|
2022-05-12 17:35:25 +08:00
|
|
|
struct State {
|
|
|
|
ctx: Option<ClipboardContext>,
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
|
|
|
|
2024-07-01 00:24:23 +08:00
|
|
|
impl super::service::Reset for State {
|
|
|
|
fn reset(&mut self) {
|
|
|
|
*CONTENT.lock().unwrap() = Default::default();
|
|
|
|
self.ctx = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init(&mut self) {
|
2024-06-30 21:57:32 +08:00
|
|
|
let ctx = match ClipboardContext::new(true) {
|
2022-05-12 17:35:25 +08:00
|
|
|
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
|
|
|
};
|
2024-07-01 00:24:23 +08:00
|
|
|
self.ctx = ctx;
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
2022-05-12 17:35:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new() -> GenericService {
|
2023-10-08 21:44:54 +08:00
|
|
|
let svc = EmptyExtraFieldService::new(NAME.to_owned(), true);
|
|
|
|
GenericService::repeat::<State, _, _>(&svc.clone(), INTERVAL, run);
|
|
|
|
svc.sp
|
2022-05-12 17:35:25 +08:00
|
|
|
}
|
2021-10-27 20:02:17 +08:00
|
|
|
|
2023-10-08 21:44:54 +08:00
|
|
|
fn run(sp: EmptyExtraFieldService, state: &mut State) -> ResultType<()> {
|
2024-02-21 22:05:27 +08:00
|
|
|
if let Some(msg) = check_clipboard(&mut state.ctx, None) {
|
|
|
|
sp.send(msg);
|
2021-10-27 20:02:17 +08:00
|
|
|
}
|
2024-02-21 22:05:27 +08:00
|
|
|
sp.snapshot(|sps| {
|
2024-06-30 21:57:32 +08:00
|
|
|
let data = crate::CONTENT.lock().unwrap().clone();
|
|
|
|
if !data.is_empty() {
|
|
|
|
let msg_out = data.create_msg();
|
2024-02-21 22:05:27 +08:00
|
|
|
sps.send_shared(Arc::new(msg_out));
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
})?;
|
2022-05-12 17:35:25 +08:00
|
|
|
Ok(())
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|