hex/src/screenshots.rs
Kit Langton 879c9b6e0e
feat(screenshots): shots fly into their card and zoom up into the editor (#2224)
* Fly screenshots into their card and zoom cards up into the editor

* Dissolve the shot into its card on close, and bring the panel in as the shot arrives

* Describe the screenshot flights in the feature map
2026-11-21 11:37:20 -08:00

1046 lines
36 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Labs › Screenshots (macOS): ⌘⇧4 picks an area (Space for a window), as
//! CleanShot does, and the shot lands on the clipboard, in the folder, and
//! in a thumbnail in the corner.
//!
//! The shortcut is taken in Hex's session tap ([`key`]), never by the
//! listener: a press opens a second capture picker, Escape closes it and
//! Space switches it between an area and a window. What it picks is shot
//! by ScreenCaptureKit at the display's own pixels, with Hex's overlays and
//! the pointer left out, on its own thread.
//!
//! The record shortcut (⌘⇧5) opens the recording toolbar ([`toolbar`]):
//! an area or a window through the same picker, or a whole display, with
//! the sound it keeps. It records as an Annotate capture, and pressing the
//! shortcut again stops; the recording then lands on the same card
//! ([`recording`]).
pub(crate) mod narration;
mod picker;
mod recording;
pub(crate) mod save;
pub(crate) mod system_shortcut;
mod toolbar;
mod toolbar_level;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::{Mutex, OnceLock};
use crate::app_settings::RuntimeHotkey;
use crate::capture_host::{CaptureNeed, CaptureTarget};
use crate::history::captures::{self, CaptureDraft, CaptureKind, CaptureWord};
use crate::rune_ui::capture_capsule::flow::{Action, Mode};
use crate::rune_ui::capture_capsule::picker::{PickerOverlay, PickerRole};
use crate::rune_ui::screenshot_thumbnail::{CardKey, Thumbnail};
struct Overlays {
picker: PickerOverlay,
thumbnail: Thumbnail,
}
static OVERLAYS: OnceLock<Overlays> = OnceLock::new();
/// While the picker shows, the tap hands it Escape and Space.
static PICKER: Mutex<picker::Picker> = Mutex::new(picker::Picker::new());
/// The picker's window, left out of the shot; 0 before it's made.
static PICKER_WINDOW: AtomicU32 = AtomicU32::new(0);
/// The folder the last shot, recording, or editor save couldn't be written
/// to and why, which Labs › Screenshots' card names until a save works or
/// another folder is chosen. It keeps its own folder: the setting may have
/// changed since through `hex settings` or `settings.json`.
static SAVE_ERROR: Mutex<Option<(PathBuf, String)>> = Mutex::new(None);
/// How many times [`SAVE_ERROR`] was recorded or cleared. A window's poll
/// that reports nothing redraws nothing, so the card compares this count
/// with the one it last drew and reports the difference.
static SAVE_ERROR_CHANGES: AtomicU64 = AtomicU64::new(0);
/// The last failed save's folder and reason, for the card.
pub(crate) fn save_error() -> Option<(PathBuf, String)> {
SAVE_ERROR
.lock()
.unwrap_or_else(|error| error.into_inner())
.clone()
}
/// How many times the last failed save changed: a save recorded an error or
/// cleared one.
pub(crate) fn save_error_generation() -> u64 {
SAVE_ERROR_CHANGES.load(Ordering::Relaxed)
}
/// Forgets the last failed save: a later one saved fine, or the folder
/// changed.
pub(crate) fn clear_save_error() {
*SAVE_ERROR.lock().unwrap_or_else(|error| error.into_inner()) = None;
SAVE_ERROR_CHANGES.fetch_add(1, Ordering::Relaxed);
}
/// Remembers a failed save of `folder` for the card, bounded so a long path
/// can't grow the note without limit.
pub(crate) fn record_save_error(folder: &Path, error: &anyhow::Error) {
let bounded: String = error.to_string().chars().take(300).collect();
*SAVE_ERROR.lock().unwrap_or_else(|error| error.into_inner()) =
Some((folder.to_path_buf(), bounded));
SAVE_ERROR_CHANGES.fetch_add(1, Ordering::Relaxed);
}
/// Adds the screenshot picker, thumbnail, and editor, hidden. They're made at
/// launch only while the Lab is on then.
pub(crate) fn install<S: 'static>(app: &mut rune::App<S>) {
let picker = PickerOverlay::install_as(
app,
picked,
PickerRole {
name: "Hex screenshot picker",
screen_label: crate::desktop::i18n::tr("Full screen · Click to capture"),
exclude_from_capture: true,
prewarm: crate::labs::screenshots(),
created: |window| PICKER_WINDOW.store(window, Ordering::Release),
},
);
let thumbnail = Thumbnail::install(app);
toolbar::install(app);
crate::rune_ui::screenshot_thumbnail::editor::install(app);
let _ = OVERLAYS.set(Overlays { picker, thumbnail });
crate::capture_host::on_clip_written(recording::written);
}
/// Opens the picker on an area, unless one is open or Screen Recording is
/// missing (then Annotate's permission helper shows). Safe from any thread;
/// true when it opens.
pub(crate) fn begin() -> bool {
open(false)
}
/// The record shortcut: stops the Screenshots recording under way, or opens
/// the recording toolbar. Nothing while an Annotate capture records. Safe
/// from any thread.
pub(crate) fn record() -> bool {
match crate::capture_host::snapshot().recording {
Some(recording) if recording.clip => {
let stop = crate::capture_host::CaptureControl::Scrawl(
crate::control::ScrawlCaptureAction::Stop,
);
if let Err(error) = crate::capture_host::send_to_listener(stop) {
tracing::warn!(%error, "could not stop the recording");
return false;
}
true
}
Some(_) => false,
None => crate::labs::screenshots() && toolbar::open(),
}
}
/// Opens the picker to shoot or, with `record`, to record. The picker's own
/// permission helper calls this again once its grant is made.
pub(crate) fn open(record: bool) -> bool {
open_as(record, false)
}
/// Opens the picker on an area, or with `window` on a window.
fn open_as(record: bool, window: bool) -> bool {
if !crate::labs::screenshots() || OVERLAYS.get().is_none() {
return false;
}
let Some(id) = PICKER
.lock()
.unwrap_or_else(|e| e.into_inner())
.begin(record, window)
else {
return false;
};
// Off the tap's thread: a fresh Screen Recording read may take a moment.
let spawned = std::thread::Builder::new()
.name("hex-screenshot-open".into())
.spawn(move || {
let allowed =
crate::capture_host::permission_state(CaptureNeed::ScreenRecording, true).is_none();
let mut picker = PICKER.lock().unwrap_or_else(|e| e.into_inner());
if !picker.permission(id, allowed) {
return;
}
if !allowed {
tracing::info!("screenshot needs Screen Recording");
// The helper returns here, to the picker, once it's granted:
// never to Annotate's whole-screen recording.
crate::capture_host::report_screenshot_permission(
CaptureNeed::ScreenRecording,
record,
);
return;
}
if let Some(overlays) = OVERLAYS.get() {
overlays.picker.set_screen_label(if record {
crate::desktop::i18n::tr("Full screen · Click to record")
} else {
crate::desktop::i18n::tr("Full screen · Click to capture")
});
}
show(id, Some(if window { Mode::Window } else { Mode::Area }));
});
if spawned.is_err() {
PICKER
.lock()
.unwrap_or_else(|e| e.into_inner())
.permission(id, false);
}
spawned.is_ok()
}
/// Restore Last Capture: brings back the card closed last. Safe from any
/// thread.
pub(crate) fn restore_last() {
if crate::labs::screenshots()
&& let Some(overlays) = OVERLAYS.get()
{
overlays.thumbnail.restore();
}
}
/// Whether Restore Last Capture has a card to bring back.
pub(crate) fn can_restore() -> bool {
crate::labs::screenshots() && crate::rune_ui::screenshot_thumbnail::restorable()
}
/// Closes the picker without a shot.
pub(crate) fn cancel() {
let mut picker = PICKER.lock().unwrap_or_else(|e| e.into_inner());
if let Some(pick) = picker.active.take() {
show(pick.id, None);
}
}
/// Switches the open picker between an area and a window.
fn toggle() {
let mut picker = PICKER.lock().unwrap_or_else(|e| e.into_inner());
if let Some(pick) = picker.active.as_mut().filter(|pick| pick.ready) {
pick.window = !pick.window;
show(
pick.id,
Some(if pick.window {
Mode::Window
} else {
Mode::Area
}),
);
}
}
pub(crate) fn picking() -> bool {
PICKER
.lock()
.unwrap_or_else(|e| e.into_inner())
.active
.is_some()
}
fn show(id: u64, mode: Option<Mode>) {
if let Some(overlays) = OVERLAYS.get() {
overlays.picker.set_if(mode, move || {
let picker = PICKER.lock().unwrap_or_else(|e| e.into_inner());
if mode.is_some() {
picker.current(id)
} else {
// Hide is enqueued under the same lock as every show, before
// a later opening can queue its show (even while it waits for permission).
true
}
});
}
}
/// The picker's pick, on the main thread: it closes, and the shot is taken
/// on its own thread, or the listener starts recording it.
fn picked(action: Action) {
let Action::Start(target) = action else {
return;
};
let record = {
let mut picker = PICKER.lock().unwrap_or_else(|e| e.into_inner());
if !picker.active.as_ref().is_some_and(|pick| pick.ready) {
return;
}
let pick = picker.active.take().unwrap();
show(pick.id, None);
pick.record
};
if record {
let clip = crate::capture_host::CaptureControl::Command(
crate::capture_host::CaptureCommand::Clip(target),
);
if let Err(error) = crate::capture_host::send_to_listener(clip) {
tracing::warn!(%error, "could not start a recording");
crate::feedback::play(crate::feedback::Tone::Error);
}
return;
}
let spawned = std::thread::Builder::new()
.name("hex-screenshot".into())
.spawn(move || shoot(target));
if let Err(error) = spawned {
tracing::warn!(%error, "could not start a screenshot");
}
}
/// `hex screenshot`: the whole display under the pointer, at once.
pub(crate) fn shoot_display() -> anyhow::Result<std::path::PathBuf> {
anyhow::ensure!(crate::labs::screenshots(), "Labs › Screenshots is off");
let at = scrawl_capture::screens::pointer().unwrap_or_default();
take(CaptureTarget::Display { at })
}
fn shoot(target: CaptureTarget) {
match take(target) {
Ok(path) => tracing::info!(path = %path.display(), "screenshot saved"),
Err(error) => {
tracing::warn!(%error, "screenshot failed");
crate::feedback::play(crate::feedback::Tone::Error);
}
}
}
/// Shoots `target`, saves it, copies it, and shows its thumbnail.
fn take(target: CaptureTarget) -> anyhow::Result<std::path::PathBuf> {
let shot = source(target)?;
let image = capture_core::screenshot_with(&shot.source, &exclusions(), None, false)?;
let scale = f64::from(image.width) / shot.points_wide.max(1.0);
let png = save::encode(image.width, image.height, &image.rgba, scale)?;
let folder = crate::app_settings::screenshot_settings().folder();
let path = save::write(&folder, &png);
match &path {
Ok(_) => {
// The shot reached the folder this time, even if the clipboard
// copy below fails.
clear_save_error();
}
Err(error) => {
tracing::warn!(%error, folder = %folder.display(), "could not save the screenshot");
record_save_error(&folder, error);
}
}
save::copy(&png, path.as_deref().ok())?;
let path = path?;
let (width, height, small) = shrink(image.width, image.height, &image.rgba, THUMB_PX);
remember(
Remembered {
kind: CaptureKind::Shot,
file: &path,
size: [image.width, image.height],
duration_ms: 0,
timed: Vec::new(),
},
(width, height, &small),
);
if let Some(overlays) = OVERLAYS.get() {
overlays
.thumbnail
.show(crate::rune_ui::screenshot_thumbnail::Taken {
path: path.clone(),
width,
height,
rgba: small,
sharp: sharp(image.width, image.height, &image.rgba),
screen: shot.screen,
capture: shot.capture,
});
}
Ok(path)
}
/// A saved capture for Captures to list.
struct Remembered<'a> {
kind: CaptureKind,
file: &'a std::path::Path,
size: [u32; 2],
duration_ms: u64,
timed: Vec<CaptureWord>,
}
/// Remembers a saved capture in Captures, with a thumbnail shrunk from
/// `rgba` (`width` × `height`).
fn remember(capture: Remembered, (width, height, rgba): (u32, u32, &[u8])) {
let (w, h, small) = shrink(width, height, rgba, captures::THUMB_PX);
let thumbnail_png = match save::encode(w, h, &small, 1.0) {
Ok(png) => png,
Err(error) => {
tracing::warn!(%error, "could not make the capture's thumbnail");
return;
}
};
let words = capture
.timed
.iter()
.map(|word| word.text.trim())
.filter(|text| !text.is_empty())
.collect::<Vec<_>>()
.join(" ");
captures::record(CaptureDraft {
kind: capture.kind,
file: capture.file.to_owned(),
size: capture.size,
duration_ms: capture.duration_ms,
words,
timed: capture.timed,
thumbnail_png,
});
}
/// What was said over the shot saved at `file` in the editor, for
/// Captures' search. Off the caller's thread.
pub(crate) fn append_words(file: std::path::PathBuf, words: String) {
use std::sync::mpsc::{SyncSender, sync_channel};
type Words = (std::path::PathBuf, String);
static WRITER: OnceLock<Option<SyncSender<Words>>> = OnceLock::new();
let writer = WRITER.get_or_init(|| {
let (send, receive) = sync_channel::<Words>(32);
match std::thread::Builder::new()
.name("hex-capture-words".into())
.spawn(move || {
// Preserve the UI's append order without doing disk I/O there.
for (file, words) in receive {
captures::append_words(&file, &words);
}
}) {
Ok(_) => Some(send),
Err(error) => {
tracing::warn!(%error, "could not start the capture words writer");
None
}
}
});
if let Some(writer) = writer
&& let Err(error) = writer.try_send((file, words))
{
tracing::warn!(%error, "could not keep the words said over the shot");
}
}
/// The display under the pointer, in points, where a recording's card rests.
fn screen_under_pointer() -> (f32, f32) {
use scrawl_capture::screens;
let displays = screens::displays();
screens::pointer()
.and_then(|at| screens::index_at(&displays, at))
.map(|i| (displays[i].frame[2] as f32, displays[i].frame[3] as f32))
.unwrap_or(crate::rune_ui::screenshot_thumbnail::NOMINAL)
}
/// The thumbnail's long edge, in pixels.
const THUMB_PX: u32 = 480;
/// The long edge of the copy a card shows while it's still large, flying
/// in from where it was taken.
const SHARP_PX: u32 = 2560;
/// A copy of a shot `width` × `height` pixels for the first frames of its
/// card, at most [`SHARP_PX`] on its long edge; none when the thumbnail
/// is already the whole shot.
fn sharp(width: u32, height: u32, rgba: &[u8]) -> Option<(u32, u32, Vec<u8>)> {
(width.max(height) > THUMB_PX).then(|| shrink(width, height, rgba, SHARP_PX))
}
/// What ScreenCaptureKit shoots for `target`: its source, its width in
/// points, the display it lands on, and where on that display it was taken
/// from when it was an area or a window.
struct Shot {
source: capture_core::Source,
points_wide: f64,
screen: (f32, f32),
capture: Option<[f32; 4]>,
}
fn source(target: CaptureTarget) -> anyhow::Result<Shot> {
use scrawl_capture::screens;
let displays = screens::displays();
let display_at = |at: [f64; 2]| {
screens::index_at(&displays, at)
.map(|i| &displays[i])
.ok_or_else(|| anyhow::anyhow!("no display there"))
};
let screen = |display: &screens::Screen| (display.frame[2] as f32, display.frame[3] as f32);
let whole = |display: &screens::Screen| {
let (w, h) = screen(display);
[0.0, 0.0, w, h]
};
Ok(match target {
CaptureTarget::Window { id } => {
let bounds = scrawl_capture::windows::raw_windows()
.into_iter()
.find(|w| w.id == id)
.map(|w| w.bounds);
let center = bounds.map_or([0.0, 0.0], |[x, y, w, h]| [x + w * 0.5, y + h * 0.5]);
let display = display_at(center)?;
let [dx, dy, ..] = display.frame;
Shot {
source: capture_core::Source::Window(id),
points_wide: bounds.map_or(0.0, |b| b[2]),
screen: screen(display),
capture: bounds
.map(|[x, y, w, h]| [(x - dx) as f32, (y - dy) as f32, w as f32, h as f32]),
}
}
CaptureTarget::Pointer => {
let at = screens::pointer().unwrap_or_default();
let display = display_at(at)?;
Shot {
source: capture_core::Source::Display(display.id),
points_wide: display.frame[2],
screen: screen(display),
capture: Some(whole(display)),
}
}
CaptureTarget::Phone => anyhow::bail!("a screenshot is never of an iPhone"),
CaptureTarget::Display { at } => {
let display = display_at(at)?;
Shot {
source: capture_core::Source::Display(display.id),
points_wide: display.frame[2],
screen: screen(display),
capture: Some(whole(display)),
}
}
CaptureTarget::Area { rect: [x, y, w, h] } => {
let display = display_at([x + w * 0.5, y + h * 0.5])?;
let [dx, dy, ..] = display.frame;
let rect = capture_core::Rect {
x: x - dx,
y: y - dy,
width: w,
height: h,
};
Shot {
source: capture_core::Source::Region {
display: display.id,
rect,
},
points_wide: w,
screen: screen(display),
capture: Some([(x - dx) as f32, (y - dy) as f32, w as f32, h as f32]),
}
}
})
}
/// Hex's own overlays (every window of Hex's above the normal layer) and
/// the picker, left out of the shot. Hex's settings window stays in.
fn exclusions() -> capture_core::Exclusions {
let pid = std::process::id() as i32;
let mut windows: Vec<u32> = scrawl_capture::windows::raw_windows()
.into_iter()
.filter(|w| w.pid == pid && w.layer != 0)
.map(|w| w.id)
.collect();
let picker = PICKER_WINDOW.load(Ordering::Acquire);
if picker != 0 && !windows.contains(&picker) {
windows.push(picker);
}
capture_core::Exclusions {
windows,
apps: Vec::new(),
}
}
/// `rgba` shrunk by whole-pixel box averages until its long edge is at
/// most `long_edge`; unchanged when it already is.
fn shrink(width: u32, height: u32, rgba: &[u8], long_edge: u32) -> (u32, u32, Vec<u8>) {
let step = width.max(height).div_ceil(long_edge.max(1)).max(1);
let (w, h) = (width / step, height / step);
if step == 1 || w == 0 || h == 0 {
return (width, height, rgba.to_vec());
}
let mut out = Vec::with_capacity((w * h * 4) as usize);
for y in 0..h {
for x in 0..w {
let mut sum = [0u32; 4];
for dy in 0..step {
let row = ((y * step + dy) * width + x * step) as usize * 4;
for px in rgba[row..row + step as usize * 4].as_chunks::<4>().0 {
for (total, v) in sum.iter_mut().zip(px) {
*total += u32::from(*v);
}
}
}
let n = step * step;
out.extend(sum.map(|total| (total / n) as u8));
}
}
(w, h, out)
}
/// What the session tap does with a key: lets it go, keeps it from the app
/// and the listener (Escape, so it cancels nothing else), or keeps it from
/// the app with the listener still seeing it, so a held modifier pressing
/// the shortcut stays a chord rather than a dictation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Take {
No,
Only,
AfterListener,
}
/// What a key does here.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Effect {
None,
Begin,
/// The record shortcut: opens the picker to record, or stops.
Record,
Cancel,
Toggle,
/// Escape, or the record shortcut, while the recording toolbar shows.
CloseToolbar,
/// Return while the recording toolbar shows: records what it shows.
RecordNow,
/// A key for the card under the pointer.
Card(CardKey),
}
/// The two shortcuts the tap matches: a screenshot, and a recording.
#[derive(Clone, Copy, Debug, Default)]
struct Shortcuts {
shot: Option<RuntimeHotkey>,
record: Option<RuntimeHotkey>,
}
const ESCAPE: u16 = 53;
const SPACE: u16 = 49;
const RETURN: u16 = 36;
const KEY_C: u16 = 8;
const KEY_S: u16 = 1;
const KEY_E: u16 = 14;
const KEY_W: u16 = 13;
/// Command, Shift, Control, and Option in a key's flags.
const COMMAND: u64 = 0x10_0000;
const MODIFIERS: u64 = 0x1E_0000;
/// What a key does to the card under the pointer, as CleanShot's overlay
/// takes them: ⌘C, ⌘S, ⌘E, ⌘W, and Escape and Return alone.
fn card_key(code: u16, flags: u64) -> Option<CardKey> {
match (code, flags & MODIFIERS) {
(KEY_C, COMMAND) => Some(CardKey::Copy),
(KEY_S, COMMAND) => Some(CardKey::Reveal),
(KEY_E, COMMAND) => Some(CardKey::Edit),
(KEY_W, COMMAND) | (ESCAPE, 0) => Some(CardKey::Close),
(RETURN, 0) => Some(CardKey::Keep),
_ => None,
}
}
/// What's open that takes keys: the picker, the recording toolbar, or
/// neither. While one of the toolbar's menus is open, Escape and Return are
/// the menu's.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Open {
Nothing,
Picker,
Toolbar,
ToolbarMenu,
}
/// The keys this took the press of, so their releases go too.
#[derive(Default)]
struct Taken(u128);
impl Taken {
fn decide(
&mut self,
key: Key,
shortcuts: Shortcuts,
open: Open,
card: bool,
suspended: bool,
) -> (Take, Effect) {
let Some(bit) = 1u128.checked_shl(u32::from(key.code)) else {
return (Take::No, Effect::None);
};
let take = if key.code == ESCAPE {
Take::Only
} else {
Take::AfterListener
};
if !key.down {
let ours = self.0 & bit != 0;
self.0 &= !bit;
return (if ours { take } else { Take::No }, Effect::None);
}
if suspended {
return (Take::No, Effect::None);
}
let pressed = |hotkey: Option<RuntimeHotkey>| {
hotkey.is_some_and(|h| h.matches_key_press(key.code, key.flags))
};
let (shot, record) = (pressed(shortcuts.shot), pressed(shortcuts.record));
let alone = key.flags & MODIFIERS == 0;
let effect = match key.code {
_ if (shot || record) && open == Open::Picker => Effect::None,
_ if record && matches!(open, Open::Toolbar | Open::ToolbarMenu) => {
Effect::CloseToolbar
}
_ if shot && matches!(open, Open::Toolbar | Open::ToolbarMenu) => Effect::None,
_ if shot => Effect::Begin,
_ if record => Effect::Record,
ESCAPE if open == Open::Picker => Effect::Cancel,
ESCAPE if open == Open::Toolbar => Effect::CloseToolbar,
RETURN if open == Open::Toolbar && alone => Effect::RecordNow,
SPACE if open == Open::Picker => Effect::Toggle,
code if card && open == Open::Nothing => match card_key(code, key.flags) {
Some(card) => Effect::Card(card),
None => return (Take::No, Effect::None),
},
_ => return (Take::No, Effect::None),
};
self.0 |= bit;
let effect = if key.repeat { Effect::None } else { effect };
(take, effect)
}
}
/// A key event as the session tap reads it.
#[derive(Clone, Copy, Debug)]
pub(crate) struct Key {
pub(crate) code: u16,
pub(crate) down: bool,
pub(crate) repeat: bool,
pub(crate) flags: u64,
}
/// The session tap's key, on its thread. Does nothing while the Lab is
/// off and no key it took is still down.
pub(crate) fn key(key: Key) -> Take {
static TAKEN: std::sync::Mutex<Taken> = std::sync::Mutex::new(Taken(0));
let mut taken = TAKEN.lock().unwrap_or_else(|error| error.into_inner());
let open = if picking() {
Open::Picker
} else if toolbar::is_open() {
if crate::rune_ui::capture_capsule::toolbar::menu_open() {
Open::ToolbarMenu
} else {
Open::Toolbar
}
} else {
Open::Nothing
};
if taken.0 == 0 && open == Open::Nothing && !crate::labs::screenshots() {
return Take::No;
}
let shortcuts = Shortcuts {
shot: crate::app_settings::screenshot_hotkey(),
record: crate::app_settings::screenshot_record_hotkey(),
};
let (take, effect) = taken.decide(
key,
shortcuts,
open,
crate::rune_ui::screenshot_thumbnail::hovered(),
crate::app_settings::shortcuts_suspended(),
);
drop(taken);
match effect {
Effect::None => {}
Effect::Begin => {
begin();
}
Effect::Record => {
record();
}
Effect::Cancel => cancel(),
Effect::Toggle => toggle(),
Effect::CloseToolbar => toolbar::close(),
Effect::RecordNow => toolbar::record(),
Effect::Card(card) => {
if let Some(overlays) = OVERLAYS.get() {
overlays.thumbnail.key(card);
}
}
}
take
}
/// Whether macOS's own screenshot shortcuts take one of Hex's now (⌘⇧4,
/// or ⌘⇧5 for the toolbar): they're matched in the window server before any
/// app's tap, so Hex never sees the press. Read again at most every 2 s,
/// since Settings draws it each frame.
pub(crate) fn system_takes_shortcut() -> bool {
use std::time::{Duration, Instant};
type Hotkeys = [Option<RuntimeHotkey>; 2];
static LAST: std::sync::Mutex<Option<(Instant, Hotkeys, bool)>> = std::sync::Mutex::new(None);
let hotkeys = [
crate::app_settings::screenshot_hotkey(),
crate::app_settings::screenshot_record_hotkey(),
];
let mut last = LAST.lock().unwrap_or_else(|error| error.into_inner());
if let Some((at, read_for, takes)) = *last
&& read_for == hotkeys
&& at.elapsed() < Duration::from_secs(2)
{
return takes;
}
let takes = hotkeys.into_iter().flatten().any(system_shortcut::takes);
*last = Some((Instant::now(), hotkeys, takes));
takes
}
#[cfg(test)]
mod tests {
use super::*;
use crate::app_settings::HotkeyBinding;
const COMMAND_SHIFT: u64 = 0x12_0000;
fn press(code: u16, flags: u64) -> Key {
Key {
code,
down: true,
repeat: false,
flags,
}
}
fn release(code: u16) -> Key {
Key {
code,
down: false,
repeat: false,
flags: 0,
}
}
fn hotkey() -> Shortcuts {
Shortcuts {
shot: Some(HotkeyBinding::screenshot_default().runtime()),
record: None,
}
}
#[test]
fn the_shortcut_opens_the_picker_and_its_release_stays_with_it() {
let mut taken = Taken::default();
assert_eq!(
taken.decide(
press(21, COMMAND_SHIFT),
hotkey(),
Open::Nothing,
false,
false
),
(Take::AfterListener, Effect::Begin)
);
assert_eq!(
taken.decide(release(21), hotkey(), Open::Picker, false, false),
(Take::AfterListener, Effect::None)
);
assert_eq!(taken.0, 0);
}
#[test]
fn a_held_shortcut_opens_one_picker() {
let mut taken = Taken::default();
let mut repeat = press(21, COMMAND_SHIFT);
repeat.repeat = true;
taken.decide(
press(21, COMMAND_SHIFT),
hotkey(),
Open::Nothing,
false,
false,
);
assert_eq!(
taken.decide(repeat, hotkey(), Open::Picker, false, false),
(Take::AfterListener, Effect::None)
);
}
#[test]
fn escape_and_space_belong_to_the_open_picker_only() {
let mut taken = Taken::default();
assert_eq!(
taken.decide(press(ESCAPE, 0), hotkey(), Open::Nothing, false, false),
(Take::No, Effect::None)
);
assert_eq!(
taken.decide(press(SPACE, 0), hotkey(), Open::Picker, false, false),
(Take::AfterListener, Effect::Toggle)
);
assert_eq!(
taken.decide(press(ESCAPE, 0), hotkey(), Open::Picker, false, false),
(Take::Only, Effect::Cancel)
);
// Escape's release goes with it even once the picker closed.
assert_eq!(
taken.decide(release(ESCAPE), hotkey(), Open::Nothing, false, false),
(Take::Only, Effect::None)
);
}
#[test]
fn other_keys_and_other_chords_pass() {
let mut taken = Taken::default();
assert_eq!(
taken.decide(press(21, 0x10_0000), hotkey(), Open::Nothing, false, false),
(Take::No, Effect::None)
);
assert_eq!(
taken.decide(press(0, 0), hotkey(), Open::Picker, false, false),
(Take::No, Effect::None)
);
assert_eq!(
taken.decide(
press(21, COMMAND_SHIFT),
Shortcuts::default(),
Open::Nothing,
false,
false
),
(Take::No, Effect::None)
);
}
#[test]
fn the_record_shortcut_records_and_never_while_the_picker_shows() {
let mut taken = Taken::default();
let shortcuts = Shortcuts {
record: Some(HotkeyBinding::screenshot_default().runtime()),
shot: None,
};
assert_eq!(
taken.decide(
press(21, COMMAND_SHIFT),
shortcuts,
Open::Nothing,
false,
false
),
(Take::AfterListener, Effect::Record)
);
taken.decide(release(21), shortcuts, Open::Nothing, false, false);
assert_eq!(
taken.decide(
press(21, COMMAND_SHIFT),
shortcuts,
Open::Picker,
false,
false
),
(Take::AfterListener, Effect::None)
);
}
#[test]
fn a_hovered_card_takes_its_keys_and_only_then() {
let mut taken = Taken::default();
let command = |code| press(code, COMMAND | 0x8);
assert_eq!(
taken.decide(command(KEY_C), hotkey(), Open::Nothing, true, false),
(Take::AfterListener, Effect::Card(CardKey::Copy))
);
taken.decide(release(KEY_C), hotkey(), Open::Nothing, true, false);
assert_eq!(
taken.decide(command(KEY_W), hotkey(), Open::Nothing, true, false),
(Take::AfterListener, Effect::Card(CardKey::Close))
);
assert_eq!(
taken.decide(press(ESCAPE, 0), hotkey(), Open::Nothing, true, false),
(Take::Only, Effect::Card(CardKey::Close))
);
assert_eq!(
taken.decide(press(RETURN, 0), hotkey(), Open::Nothing, true, false),
(Take::AfterListener, Effect::Card(CardKey::Keep))
);
// Off the cards, with another modifier, or while picking: not ours.
let mut taken = Taken::default();
assert_eq!(
taken.decide(command(KEY_C), hotkey(), Open::Nothing, false, false),
(Take::No, Effect::None)
);
assert_eq!(
taken.decide(
press(KEY_C, COMMAND_SHIFT),
hotkey(),
Open::Nothing,
true,
false
),
(Take::No, Effect::None)
);
assert_eq!(
taken.decide(press(ESCAPE, 0), hotkey(), Open::Picker, true, false),
(Take::Only, Effect::Cancel)
);
}
#[test]
fn recording_a_shortcut_takes_nothing() {
let mut taken = Taken::default();
assert_eq!(
taken.decide(
press(21, COMMAND_SHIFT),
hotkey(),
Open::Nothing,
false,
true
),
(Take::No, Effect::None)
);
}
#[test]
fn a_large_shot_shrinks_for_its_thumbnail() {
let rgba = [10u8, 20, 30, 255].repeat(8 * 4);
let (w, h, small) = shrink(8, 4, &rgba, 4);
assert_eq!((w, h), (4, 2));
assert_eq!(&small[..4], &[10, 20, 30, 255]);
let (w, h, same) = shrink(8, 4, &rgba, 8);
assert_eq!((w, h, same.len()), (8, 4, rgba.len()));
}
#[test]
fn the_toolbar_takes_escape_return_and_its_shortcut_and_leaves_the_shot() {
let mut taken = Taken::default();
let shortcuts = Shortcuts {
shot: Some(HotkeyBinding::screenshot_default().runtime()),
record: Some(HotkeyBinding::screenshot_record_default().runtime()),
};
// With one of its menus open, Escape and Return go to the menu.
let menu = Open::ToolbarMenu;
for code in [ESCAPE, RETURN] {
assert_eq!(
taken.decide(press(code, 0), shortcuts, menu, false, false),
(Take::No, Effect::None)
);
taken.decide(release(code), shortcuts, menu, false, false);
}
let toolbar = Open::Toolbar;
assert_eq!(
taken.decide(press(ESCAPE, 0), shortcuts, toolbar, false, false),
(Take::Only, Effect::CloseToolbar)
);
taken.decide(release(ESCAPE), shortcuts, toolbar, false, false);
assert_eq!(
taken.decide(press(RETURN, 0), shortcuts, toolbar, true, false),
(Take::AfterListener, Effect::RecordNow)
);
taken.decide(release(RETURN), shortcuts, toolbar, false, false);
assert_eq!(
taken.decide(press(RETURN, COMMAND), shortcuts, toolbar, false, false),
(Take::No, Effect::None)
);
assert_eq!(
taken.decide(press(23, COMMAND_SHIFT), shortcuts, toolbar, false, false),
(Take::AfterListener, Effect::CloseToolbar)
);
taken.decide(release(23), shortcuts, toolbar, false, false);
assert_eq!(
taken.decide(press(21, COMMAND_SHIFT), shortcuts, toolbar, false, false),
(Take::AfterListener, Effect::None)
);
assert_eq!(
taken.decide(
press(23, COMMAND_SHIFT),
shortcuts,
Open::Nothing,
false,
false
),
(Take::AfterListener, Effect::Record)
);
}
}