mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
* macos 27.3: require keystroke permission for paste, request it, and fail visibly macOS 27.3 splits the old Accessibility grant into Accessibility (assistive technologies) and System & App Protection Override (apps that control other apps, posting ⌘V included). The Accessibility grant no longer proves Hex may post a keystroke, so setup read Ready from AXIsProcessTrusted alone, never asked macOS, and CGEventPost silently dropped every paste while Hex logged a successful delivery and wrote History. - Setup counts only the post-event preflight on 27.3+, reusing the native 27.3 version check already used for the pane's name; below 27.3 nothing changes. Allow now calls CGRequestPostEventAccess so macOS can list Hex and post its Privacy & Security notification, during setup and in the "fix it" flow. - The post-event preflight keeps its launch-time answer, so a row that stays untrusted after a grant reads "Needs a restart" and offers Restart Hex in the tour, and the Settings row does the same instead of running a tccutil reset that would drop the grant the person just made. - A paste that would post a keystroke refuses before touching the clipboard when 27.3+ has not granted posting, with a HUD error naming System & App Protection Override. The shared delivery path already keeps the text for Paste last dictation and writes no History entry on a failed insert. Docs note the 27.3 behavior and that no 27.3 device has verified it. * macos 27.3: restart once per grant, explain a refused paste, and guard captures Review fixes for the 27.3 keystroke grant. - Restart Hex was a dead end: every stale Accessibility row on 27.3 offered it, so a grant that never landed looped between "Needs a restart" and a launch, and the tour row had no way back to the guide. The one restart is now recorded in onboarding-accessibility-restart (cleared when Accessibility reads Ready, and by a setup reset or identity change). After that restart a stale row falls back to the existing "Still grey? Fix it" reset-and-prompt flow, in the tour and in Settings > Permissions needed. - A refused paste's HUD notice was gated by Labs > Failure notices (off by default), so the one failure that needs a person to act showed nothing. Its message is now a shared constant that the HUD explains regardless of the lab, through tr() with the six main languages and the four extra ones. - Captures take the same check before touching the clipboard: Paster's paste_capture and the capture hand-off. The capture helper's Allow asks with CGRequestPostEventAccess so 27.3 can list Hex. - The keystroke check runs before Labs > Return to app brings another app forward, so a refusal doesn't switch apps for a paste that cannot land. - CGRequestPostEventAccess is a safe fn; request_post_event_access returns () and logs its answer instead of returning a bool no caller could use. The reset flow's request stays: after the restart fallback it is reachable again. Docs now say what the person sees, which tests cover it, and that the refusal itself has not run on a 27.3 device. * macos 27.3: re-arm the restart after a reset, and explain a refused paste last Second review fixes. - After the one Restart Hex, "Still grey? Fix it" could never succeed: the marker was only cleared on Ready/setup reset/identity change, so reset -> grant still showed the reset row and Restart never came back. The reset dispatch now clears the one-restart marker for Accessibility on 27.3, so every cycle is reset -> grant -> one restart, user-driven. The poll re-reads it; Flow and Settings tests cover restart -> stale -> reset -> stale -> Restart again. - A refused Paste Last only played the error tone. It now sends the same capture-level failure the dictation path uses, so the HUD notice explains it even with Labs > Failure notices off; other Paste Last failures keep the tone and log only. A refused capture hand-off still only logs, and the docs now say so instead of claiming every path shows the notice. - Record the one restart only for the Accessibility row: the tour's restarts_for_post_event and the Settings stale row's restart. A Screen Recording or microphone Restart no longer spends it. - Clear the marker only when accessibility_restart_done is set, and skip the marker stat entirely below 27.3.
229 lines
7.8 KiB
Rust
229 lines
7.8 KiB
Rust
//! 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 => {
|
||
if need.kind() == PermissionKind::Accessibility {
|
||
// macOS 27.3 adds an app to System & App Protection Override
|
||
// only after it asks to post key events.
|
||
crate::desktop::permission_guide::request_post_event_access();
|
||
}
|
||
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);
|
||
}
|
||
}
|