hdrshot-gui 2.6.0

HDR image tool: desktop app for converting HDR screenshots and images
//! HDR screenshots on Windows, as the app takes them. The capturing and
//! saving is in `hdrshot_core::capture`, shared with the command line; a
//! shortcut that works in games, or the tray menu or the window's camera
//! button, takes them, and the app then converts each with the current
//! settings. A second shortcut takes a part of the screen (see `region`).

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};

/// The shortcut, registered with the system while this exists. Each press
/// saves a screenshot with the options it shares with the app and sends
/// the outcome.
pub(crate) struct Hotkey {
    _hotkey: CoreHotkey,
}

impl Hotkey {
    /// Registers `shortcut`; fails when another app has it already.
    #[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 || {
            // It would capture the overlay, dimmed.
            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 })
    }

    /// Registers `shortcut` for region screenshots: each press freezes the
    /// screen for the user to drag a box on (see `region`).
    #[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();
            // Nothing to send when the user cancelled.
            if let Some(outcome) = crate::region::take(&options)
                && taken.send(outcome).is_ok()
            {
                ctx.request_repaint();
            }
        })
        .map(|hotkey| Self { _hotkey: hotkey })
    }
}

/// Listens for `buttons` on game controllers while this exists. Each
/// press saves a screenshot like the keyboard shortcut does.
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 || {
            // It would capture the overlay, dimmed.
            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,
        })
    }
}

/// Learns a controller shortcut on a thread of its own: the buttons the
/// user presses together next. Dropping it stops listening.
pub(crate) struct Recording {
    cancel: Arc<std::sync::atomic::AtomicBool>,
    result: std::sync::mpsc::Receiver<Option<Buttons>>,
}

/// How long recording waits for the 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 }
    }

    /// The buttons pressed once done: `Some(None)` when none were in time.
    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);
    }
}

/// Takes a screenshot of `target` after `delay`, on a thread of its own,
/// and sends the outcome.
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);
        // While a region is chosen it would capture the overlay, dimmed.
        if crate::region::showing() {
            return;
        }
        let outcome = hdrshot_core::capture::take(target, &options);
        if taken.send(outcome).is_ok() {
            ctx.request_repaint();
        }
    });
}