use std::sync::{Arc, Mutex};
use std::time::Duration;
pub(crate) use hdrshot_core::capture::controller::Buttons;
#[cfg(test)]
pub(crate) use hdrshot_core::capture::file_stem;
pub(crate) use hdrshot_core::capture::{
Area, Options, SUPPORTED, Shortcut, Shot, Taken, Target, default_folder,
};
use hdrshot_core::capture::{Hotkey as CoreHotkey, HotkeyKind};
pub(crate) struct Hotkey {
_hotkey: CoreHotkey,
}
impl Hotkey {
#[cfg_attr(not(windows), allow(dead_code))]
pub(crate) fn register(
shortcut: Shortcut,
options: Arc<Mutex<Options>>,
taken: std::sync::mpsc::Sender<Taken>,
ctx: egui::Context,
) -> Result<Self, String> {
CoreHotkey::register(shortcut, HotkeyKind::Screenshot, move || {
if crate::region::showing() {
return;
}
let options = options.lock().map(|o| o.clone()).unwrap_or_default();
let outcome = hdrshot_core::capture::take(Target::Foreground, &options);
if taken.send(outcome).is_ok() {
ctx.request_repaint();
}
})
.map(|hotkey| Self { _hotkey: hotkey })
}
#[cfg_attr(not(windows), allow(dead_code))]
pub(crate) fn register_region(
shortcut: Shortcut,
options: Arc<Mutex<Options>>,
taken: std::sync::mpsc::Sender<Taken>,
ctx: egui::Context,
) -> Result<Self, String> {
CoreHotkey::register(shortcut, HotkeyKind::Region, move || {
let options = options.lock().map(|o| o.clone()).unwrap_or_default();
if let Some(outcome) = crate::region::take(&options)
&& taken.send(outcome).is_ok()
{
ctx.request_repaint();
}
})
.map(|hotkey| Self { _hotkey: hotkey })
}
}
pub(crate) struct Controller {
_listener: hdrshot_core::capture::controller::Listener,
}
impl Controller {
#[cfg_attr(not(windows), allow(dead_code))]
pub(crate) fn start(
buttons: Buttons,
options: Arc<Mutex<Options>>,
taken: std::sync::mpsc::Sender<Taken>,
ctx: egui::Context,
) -> Result<Self, String> {
hdrshot_core::capture::controller::Listener::start(buttons, move || {
if crate::region::showing() {
return;
}
let options = options.lock().map(|o| o.clone()).unwrap_or_default();
let outcome = hdrshot_core::capture::take(Target::Foreground, &options);
if taken.send(outcome).is_ok() {
ctx.request_repaint();
}
})
.map(|listener| Self {
_listener: listener,
})
}
}
pub(crate) struct Recording {
cancel: Arc<std::sync::atomic::AtomicBool>,
result: std::sync::mpsc::Receiver<Option<Buttons>>,
}
const RECORD_FOR: Duration = Duration::from_secs(10);
impl Recording {
pub(crate) fn start(ctx: egui::Context) -> Self {
let cancel = Arc::new(std::sync::atomic::AtomicBool::new(false));
let (tx, result) = std::sync::mpsc::channel();
let stop = cancel.clone();
std::thread::spawn(move || {
let buttons = hdrshot_core::capture::controller::record(RECORD_FOR, &stop);
if tx.send(buttons).is_ok() {
ctx.request_repaint();
}
});
Self { cancel, result }
}
pub(crate) fn done(&self) -> Option<Option<Buttons>> {
self.result.try_recv().ok()
}
}
impl Drop for Recording {
fn drop(&mut self) {
self.cancel
.store(true, std::sync::atomic::Ordering::Relaxed);
}
}
pub(crate) fn take_later(
delay: Duration,
target: Target,
options: Options,
taken: std::sync::mpsc::Sender<Taken>,
ctx: egui::Context,
) {
std::thread::spawn(move || {
std::thread::sleep(delay);
if crate::region::showing() {
return;
}
let outcome = hdrshot_core::capture::take(target, &options);
if taken.send(outcome).is_ok() {
ctx.request_repaint();
}
});
}