Files
Dax 08df53e71a fix(screenshots): keep the record picker through the permission step and block shortcut collisions (#1631)
* fix(screenshots): keep the record picker through permissions and split its key from annotate's

a clip capture's permission refusal now reopens the screenshots picker
instead of ending the recording, and the picker holds through that step.

screenshots.record_hotkey and annotate's capture/replay shortcuts never
share a chord, whether annotate or the lab is on, so turning either on
can't leave the record shortcut dead while a whole-screen recording takes
its key. settings saved before that rule are repaired when they load: a
record shortcut on capture.hotkey or capture.replay_hotkey is cleared with
a warning, beside the left-click repair. that also fixes a record shortcut
saved on the replay chord turning replay off with the lab off.

* fix(screenshots): clear a shared record shortcut after the repairs that reset annotate's key

with labs › mouse shortcuts off, repair_mouse_shortcuts puts a mouse-button
annotate shortcut back on its default ⌥⇧S, so a record shortcut saved on that
chord was cleared before the reset and survived the load, sharing annotate's
key. the clear now runs from read_saved_from after every repair that can
reset capture.hotkey, and still counts as a migration so the file is
written back. the load test covers the mouse-button case and the write-back.
2026-11-10 08:41:33 -05:00

223 lines
7.4 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.
//! What a capture needs from macOS before it starts: Screen Recording (not
//! for an iPhone), the microphone its narration comes from, and
//! Accessibility for its shortcut and pointer input. A capture that lacks
//! one never starts; the capsule's permission helper says which, and
//! follows the grant while it's made.
use std::sync::OnceLock;
use crate::desktop::onboarding::{PermissionKind, PermissionState};
/// A permission a capture needs.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CaptureNeed {
ScreenRecording,
Microphone,
Accessibility,
}
impl CaptureNeed {
/// Its name in System Settings.
pub fn name(self) -> &'static str {
match self {
Self::ScreenRecording => "Screen Recording",
Self::Microphone => "Microphone",
Self::Accessibility => "Accessibility",
}
}
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
pub fn kind(self) -> PermissionKind {
match self {
Self::ScreenRecording => PermissionKind::ScreenRecording,
Self::Microphone => PermissionKind::Microphone,
Self::Accessibility => PermissionKind::Accessibility,
}
}
}
/// How a missing permission stands.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NeedState {
/// Not granted.
Off,
/// Granted, but this Hex can use it only once it restarts.
Restart,
}
/// What asked for the permission helper: a capture starting, or Labs ›
/// Screenshots' picker. The helper's grant returns to what asked: a capture
/// records the screen, while the picker opens again to shoot or record.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AskedBy {
Capture,
/// Labs › Screenshots, which reopens to shoot, or to record when
/// `record` is set.
Screenshots {
record: bool,
},
}
impl AskedBy {
/// What a capture refused for a permission asks: a Screenshots recording
/// (`clip`) returns to its picker; any other capture is Annotate's.
pub fn for_capture(clip: bool) -> Self {
if clip {
Self::Screenshots { record: true }
} else {
Self::Capture
}
}
}
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
fn need_state(state: PermissionState) -> Option<NeedState> {
match state {
PermissionState::Ready => None,
PermissionState::NeedsRestart => Some(NeedState::Restart),
PermissionState::NeedsRequest | PermissionState::NeedsSettings => Some(NeedState::Off),
}
}
/// The first permission a capture lacks, Screen Recording first and only
/// when `screen` (an iPhone records no screen). Reads Screen Recording
/// afresh when the cheap check says no, so a grant made since launch
/// counts.
#[cfg(target_os = "macos")]
pub fn missing(screen: bool) -> Option<(CaptureNeed, NeedState)> {
let order = [
CaptureNeed::ScreenRecording,
CaptureNeed::Microphone,
CaptureNeed::Accessibility,
];
order
.into_iter()
.filter(|need| screen || *need != CaptureNeed::ScreenRecording)
.find_map(|need| state_of(need, true).map(|state| (need, state)))
}
/// How `need` stands now: `None` once a capture can use it. Never blocks
/// unless `fresh`: Screen Recording then reads through a new helper, else it
/// follows the watched cache a visible permission surface polls.
#[cfg(target_os = "macos")]
pub fn state_of(need: CaptureNeed, fresh: bool) -> Option<NeedState> {
use super::ScreenAccess;
match need {
CaptureNeed::ScreenRecording => {
if scrawl_capture::capture::screen_capture_usable(false) {
return None;
}
let access = if fresh {
super::screen_access_now()
} else {
super::screen_access_watched()
};
match access {
ScreenAccess::Allowed => None,
ScreenAccess::NeedsRestart => Some(NeedState::Restart),
ScreenAccess::Off => Some(NeedState::Off),
}
}
CaptureNeed::Microphone => {
need_state(crate::desktop::onboarding::status_with_transcription_model(true).microphone)
}
CaptureNeed::Accessibility => need_state(
crate::desktop::onboarding::status_with_transcription_model(true).accessibility,
),
}
}
/// Linux asks nothing of the person before a capture: the portal asks.
#[cfg(not(target_os = "macos"))]
pub fn missing(_screen: bool) -> Option<(CaptureNeed, NeedState)> {
None
}
#[cfg(not(target_os = "macos"))]
pub fn state_of(_need: CaptureNeed, _fresh: bool) -> Option<NeedState> {
None
}
/// The helper's Allow: macOS's own prompt where it still has one, else
/// System Settings with the guide floating beside it. Main thread only.
#[cfg(target_os = "macos")]
pub fn allow(need: CaptureNeed) {
use crate::desktop::onboarding::PermissionState;
match need {
CaptureNeed::Microphone
if crate::desktop::onboarding::status_with_transcription_model(true).microphone
== PermissionState::NeedsRequest =>
{
crate::desktop::onboarding::request_microphone();
}
// Granted since the helper opened: the next poll shows it.
CaptureNeed::ScreenRecording if super::screen_access() != super::ScreenAccess::Off => {}
need => crate::desktop::permission_guide::show(need.kind()),
}
}
#[cfg(not(target_os = "macos"))]
pub fn allow(_need: CaptureNeed) {}
/// The helper's Restart Hex. Main thread only.
#[cfg(target_os = "macos")]
pub fn restart() {
if crate::desktop::onboarding::spawn_relaunch() {
crate::macos_rune::quit();
} else {
tracing::warn!("could not restart Hex for a capture permission");
}
}
#[cfg(not(target_os = "macos"))]
pub fn restart() {}
type Helper = Box<dyn Fn(CaptureNeed, AskedBy) + Send + Sync>;
static HELPER: OnceLock<Helper> = OnceLock::new();
/// Shows the permission helper with `show` whenever a capture is refused
/// for a missing permission. The capsule sets it once; without it a refusal
/// only answers with its error.
pub fn on_missing(show: impl Fn(CaptureNeed, AskedBy) + Send + Sync + 'static) {
let _ = HELPER.set(Box::new(show));
}
/// A capture was refused for `need`; `clip` is a Screenshots recording,
/// which returns to its picker rather than Annotate's whole-screen capture.
pub fn report_capture(need: CaptureNeed, clip: bool) {
report_asked(need, AskedBy::for_capture(clip));
}
/// A screenshot or recording was refused for `need`: the helper returns to
/// the Screenshots picker, to shoot or, with `record`, to record.
#[cfg(target_os = "macos")]
pub fn report_screenshots(need: CaptureNeed, record: bool) {
report_asked(need, AskedBy::Screenshots { record });
}
fn report_asked(need: CaptureNeed, asked_by: AskedBy) {
#[cfg(target_os = "macos")]
crate::desktop::permission_guide::clear_legacy_entries_async(need.kind());
if let Some(show) = HELPER.get() {
show(need, asked_by);
}
}
#[cfg(test)]
mod tests {
use super::*;
/// A Screenshots recording returns to its picker once granted; any other
/// capture returns to Annotate's own whole-screen capture.
#[test]
fn a_clip_capture_asks_for_the_screenshots_picker() {
assert_eq!(
AskedBy::for_capture(true),
AskedBy::Screenshots { record: true }
);
assert_eq!(AskedBy::for_capture(false), AskedBy::Capture);
}
}