mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
One key swaps the dictation model and language with a saved other one, and back, installed only, through the same save and reload path the speech-model picker uses. Requested in #2654.
2008 lines
77 KiB
Rust
2008 lines
77 KiB
Rust
//! The macOS window's host: the runtime thread `macos_rune` starts in this
|
||
//! process, which owns model preparation and publishes the listener, model,
|
||
//! update, and settings state, plus the OS-only work the panes ask for
|
||
//! (permissions, the login item, Hex Microphone, meetings).
|
||
//!
|
||
//! Shortcut capture stays in the window: an AppKit local event
|
||
//! monitor records the chord, because a modifier-only binding such as Option
|
||
//! or Fn never reaches Rune as a key event. The listener's event tap is
|
||
//! suspended meanwhile so the current binding can be recorded again.
|
||
|
||
use std::cell::RefCell;
|
||
use std::path::PathBuf;
|
||
use std::ptr::NonNull;
|
||
use std::rc::Rc;
|
||
use std::sync::Arc;
|
||
use std::sync::mpsc::{self, Receiver, Sender};
|
||
use std::time::{Duration, Instant};
|
||
|
||
use block2::RcBlock;
|
||
use color_eyre::Result;
|
||
use color_eyre::eyre::eyre;
|
||
use objc2::rc::Retained;
|
||
use objc2::runtime::AnyObject;
|
||
use objc2_app_kit::{NSEvent, NSEventMask, NSEventType};
|
||
|
||
use super::hotkey_keys::hotkey_key;
|
||
use super::meetings::Meeting;
|
||
use super::meetings_host::MeetingFeed;
|
||
use super::platform::{
|
||
CalendarRequest, CalendarView, HudDemo, HudLab, MeetingRequest, NativeHost, NotesAvailability,
|
||
PlatformRequest, PlatformState, UpcomingMeeting, poll_voice_filter, voice_filter_request,
|
||
};
|
||
use super::practice::PracticeFeed;
|
||
use super::settings::Chord;
|
||
use super::{HostState, ServiceView};
|
||
use crate::app_settings::{
|
||
CalendarSettings, HotkeyBinding, HotkeyKey, HotkeyModifiers, modifiers_from_flags,
|
||
};
|
||
use crate::calendar::{AccessAnswer, CalendarAccess, CalendarSnapshot};
|
||
use crate::desktop::host::DesktopAction;
|
||
use crate::desktop::i18n::tr;
|
||
use crate::desktop::login_item::{LoginItemFailure, LoginItemRequest, LoginItemWorker};
|
||
use crate::desktop::onboarding::{
|
||
Input, PermissionAction, PermissionKind, PermissionState, ReturnFromSettings,
|
||
StalePermissionTracker,
|
||
};
|
||
use crate::macos_rune::RuntimeHandle;
|
||
pub(super) use crate::macos_rune::{Request, ShortcutTarget};
|
||
|
||
/// The window saves a recorded shortcut; the listener follows `settings.json`.
|
||
pub(super) const HOST_SAVES_SHORTCUTS: bool = false;
|
||
pub(super) const RECORDS_KEY_EVENTS: bool = false;
|
||
pub(super) const TERMINAL_PASTE: bool = false;
|
||
/// Quit Hex lives in the menu bar and the app menu (⌘Q).
|
||
pub(super) const QUIT_IN_WINDOW: bool = false;
|
||
|
||
/// How often the native host re-reads permissions and driver state.
|
||
const PLATFORM_REFRESH: Duration = Duration::from_secs(1);
|
||
/// How often the Calendar card rereads EventKit when nothing changed, so
|
||
/// started meetings leave the list.
|
||
const CALENDAR_REFRESH: Duration = Duration::from_secs(30);
|
||
const ESCAPE_KEY_CODE: u16 = 53;
|
||
|
||
/// The runtime is found through its own module; nothing is handed over.
|
||
#[derive(Default)]
|
||
pub(crate) struct Connect {
|
||
_nothing: (),
|
||
}
|
||
|
||
pub(super) struct Backend {
|
||
runtime: Option<RuntimeHandle>,
|
||
wake: Arc<dyn Fn() + Send + Sync>,
|
||
chords: Option<ChordWatch>,
|
||
}
|
||
|
||
impl Backend {
|
||
pub(super) fn connect(_: Connect, wake: Arc<dyn Fn() + Send + Sync>) -> Self {
|
||
let runtime = crate::macos_rune::runtime();
|
||
if let Some(runtime) = &runtime {
|
||
runtime.subscribe(wake.clone());
|
||
}
|
||
Self {
|
||
runtime,
|
||
wake,
|
||
chords: None,
|
||
}
|
||
}
|
||
|
||
/// Runs on the main thread (Rune's handlers), where the event monitor
|
||
/// lives.
|
||
pub(super) fn send(&mut self, request: Request) -> Result<()> {
|
||
match request {
|
||
Request::CaptureShortcutFor(_) => {
|
||
self.chords = Some(ChordWatch::start(self.wake.clone())?);
|
||
Ok(())
|
||
}
|
||
Request::CancelShortcut => {
|
||
self.chords = None;
|
||
Ok(())
|
||
}
|
||
Request::Desktop(
|
||
action @ (DesktopAction::CheckForUpdates
|
||
| DesktopAction::OpenUpdate
|
||
| DesktopAction::RestartIntoUpdate
|
||
| DesktopAction::ConfirmUpdateSwitch
|
||
| DesktopAction::WindowFocused),
|
||
) => crate::macos_rune::update_action(&action),
|
||
request => match &self.runtime {
|
||
Some(runtime) => runtime.send(request),
|
||
None => Err(eyre!("Hex's runtime is not running")),
|
||
},
|
||
}
|
||
}
|
||
|
||
/// Saves `settings.json`, then asks the runtime to reload it. The
|
||
/// listener follows the saved file itself.
|
||
pub(super) fn save(
|
||
&mut self,
|
||
base: &crate::app_settings::AppSettings,
|
||
settings: &crate::app_settings::AppSettings,
|
||
) -> Result<crate::app_settings::AppSettings> {
|
||
let saved = base.update(settings)?;
|
||
if let Err(error) = self.send(Request::ReloadSettings) {
|
||
tracing::warn!(%error, "settings saved but runtime reload could not be queued");
|
||
}
|
||
Ok(saved)
|
||
}
|
||
|
||
pub(super) fn quit(&mut self) {
|
||
crate::macos_rune::quit();
|
||
}
|
||
|
||
/// Rune hides the window (`keep_running`); the window's own close hook
|
||
/// restores the Dock policy (`macos_rune::window_closed`).
|
||
pub(super) fn closed(&mut self) {}
|
||
|
||
/// The runtime publishes into shared memory; nothing to pull.
|
||
pub(super) fn refresh(&mut self) {}
|
||
|
||
pub(super) fn error(&self) -> Option<String> {
|
||
self.runtime
|
||
.is_none()
|
||
.then(|| "Hex's runtime is not running".into())
|
||
}
|
||
|
||
pub(super) fn state(&self) -> Option<HostState> {
|
||
let published = self.runtime.as_ref()?.published()?;
|
||
Some(HostState {
|
||
view: ServiceView {
|
||
listener: published.listener.clone(),
|
||
operation_error: None,
|
||
transcription: published.transcription.clone(),
|
||
update_status: published.update_status.clone(),
|
||
update_notes: published.update_notes.clone(),
|
||
update_blocked: None,
|
||
editing: false,
|
||
},
|
||
settings: published.settings.clone(),
|
||
})
|
||
}
|
||
|
||
pub(super) fn take_chord(&mut self) -> Option<Chord> {
|
||
// Presses the listener's session tap took for the recorder: macOS
|
||
// never gave them to the window, so the monitor below cannot see them.
|
||
let captured = crate::suppression::dictation_key::take_captured();
|
||
let watch = self.chords.as_mut()?;
|
||
for key in captured {
|
||
if let Some(chord) = recorded_microphone_key(&mut watch.capture.borrow_mut(), key) {
|
||
// The same channel the monitor's chords arrive on, so they
|
||
// read in the order they were recorded.
|
||
let _ = watch.sender.send(chord);
|
||
}
|
||
}
|
||
let chord = watch.results.try_recv().ok();
|
||
if matches!(chord, Some(Chord::Cancelled)) {
|
||
self.chords = None;
|
||
}
|
||
chord
|
||
}
|
||
}
|
||
|
||
/// Records chords from the window's keyboard events while Settings listens,
|
||
/// with the listener's event tap suspended. Removing the monitor ends it.
|
||
struct ChordWatch {
|
||
/// The monitor's chords, and the listener's microphone-key presses once
|
||
/// they are read.
|
||
results: Receiver<Chord>,
|
||
sender: Sender<Chord>,
|
||
/// The same recorder the monitor feeds, for presses the listener's session
|
||
/// tap took instead (Labs › Microphone key alone).
|
||
capture: Rc<RefCell<ChordCapture>>,
|
||
/// This watch's registration with the session tap's recorder feed.
|
||
watching: u64,
|
||
monitor: Retained<AnyObject>,
|
||
}
|
||
|
||
impl ChordWatch {
|
||
fn start(wake: Arc<dyn Fn() + Send + Sync>) -> Result<Self> {
|
||
crate::app_settings::set_hotkey_capture_active(true);
|
||
// A press the listener took for an earlier watch is not this one's.
|
||
crate::suppression::dictation_key::clear_captured();
|
||
let watching = crate::suppression::dictation_key::watch(wake.clone());
|
||
let (sender, results) = mpsc::channel();
|
||
let chord_sender = sender.clone();
|
||
// Labs › Left and right keys is read once, as recording starts.
|
||
let capture = Rc::new(RefCell::new(ChordCapture::with_sides(
|
||
crate::labs::sided_modifier_chords(),
|
||
)));
|
||
let events = capture.clone();
|
||
let block = RcBlock::new(move |event: NonNull<NSEvent>| -> *mut NSEvent {
|
||
// SAFETY: AppKit passes a live event for the handler's duration.
|
||
let event_ref = unsafe { event.as_ref() };
|
||
// NSEvent's flags carry the same device-dependent side bits, and
|
||
// macOS's filter can rewrite a remapped Caps Lock there too.
|
||
let flags = crate::app_settings::normalized_modifier_flags(
|
||
event_ref.modifierFlags().bits() as u64,
|
||
crate::suppression::physical_keys::snapshot(),
|
||
);
|
||
let chord = match event_ref.r#type() {
|
||
NSEventType::KeyDown => {
|
||
let characters = event_ref
|
||
.charactersIgnoringModifiers()
|
||
.map(|characters| characters.to_string());
|
||
events.borrow_mut().key_down(
|
||
event_ref.keyCode(),
|
||
event_ref.isARepeat(),
|
||
characters.as_deref(),
|
||
flags,
|
||
)
|
||
}
|
||
NSEventType::FlagsChanged => events.borrow_mut().flags_changed(flags),
|
||
// Mouse buttons record only with Labs › Mouse shortcuts on;
|
||
// otherwise the click reaches the window as usual.
|
||
NSEventType::LeftMouseDown
|
||
| NSEventType::RightMouseDown
|
||
| NSEventType::OtherMouseDown
|
||
if crate::labs::mouse_shortcuts() =>
|
||
{
|
||
events
|
||
.borrow_mut()
|
||
.mouse_down(event_ref.buttonNumber(), flags)
|
||
}
|
||
// Key releases still reach the window so its key state
|
||
// stays balanced.
|
||
_ => return event.as_ptr(),
|
||
};
|
||
if let Some(chord) = chord
|
||
&& sender.send(chord).is_ok()
|
||
{
|
||
wake();
|
||
}
|
||
std::ptr::null_mut()
|
||
});
|
||
// SAFETY: the block lives as long as AppKit retains it; the monitor
|
||
// is removed on drop, on the main thread that installed it.
|
||
let monitor = unsafe {
|
||
NSEvent::addLocalMonitorForEventsMatchingMask_handler(
|
||
NSEventMask::KeyDown
|
||
| NSEventMask::FlagsChanged
|
||
| NSEventMask::LeftMouseDown
|
||
| NSEventMask::RightMouseDown
|
||
| NSEventMask::OtherMouseDown,
|
||
&block,
|
||
)
|
||
};
|
||
match monitor {
|
||
Some(monitor) => Ok(Self {
|
||
results,
|
||
sender: chord_sender,
|
||
capture,
|
||
watching,
|
||
monitor,
|
||
}),
|
||
None => {
|
||
crate::suppression::dictation_key::stop_watching(watching);
|
||
crate::app_settings::set_hotkey_capture_active(false);
|
||
Err(eyre!("could not watch the keyboard to record a shortcut"))
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
impl Drop for ChordWatch {
|
||
fn drop(&mut self) {
|
||
// SAFETY: the monitor came from addLocalMonitor on this thread.
|
||
unsafe { NSEvent::removeMonitor(&self.monitor) };
|
||
crate::suppression::dictation_key::stop_watching(self.watching);
|
||
crate::suppression::dictation_key::clear_captured();
|
||
crate::app_settings::set_hotkey_capture_active(false);
|
||
}
|
||
}
|
||
|
||
/// The chord a press the listener's session tap took records, through the same
|
||
/// capture the window's own key events use (Labs › Microphone key alone: macOS
|
||
/// never dispatches that key to the window).
|
||
fn recorded_microphone_key(
|
||
capture: &mut ChordCapture,
|
||
key: crate::suppression::dictation_key::CapturedMicrophoneKey,
|
||
) -> Option<Chord> {
|
||
capture.key_down(key.code, key.repeat, None, key.flags)
|
||
}
|
||
|
||
/// Turns key and modifier events into a chord: a key with its held
|
||
/// modifiers, or the most modifier keys held at once when they are all
|
||
/// released without a key, with the sides `recorded_sides` keeps (or,
|
||
/// with Labs › Left and right keys on, the side each was pressed on).
|
||
struct ChordCapture {
|
||
peak: HotkeyModifiers,
|
||
/// A key went down with these modifiers; their release records nothing.
|
||
keyed: bool,
|
||
/// A modifier-only chord keeps every modifier's side, such as
|
||
/// Right ⌘ + Right ⌥, instead of widening each to either side.
|
||
sided: bool,
|
||
/// Names a key from the active layout when AppKit's characters are empty,
|
||
/// as they are for a dead key (`^`, `´`, `` ` ``).
|
||
layout_label: fn(u16) -> Option<String>,
|
||
}
|
||
|
||
impl Default for ChordCapture {
|
||
fn default() -> Self {
|
||
Self {
|
||
peak: HotkeyModifiers::default(),
|
||
keyed: false,
|
||
sided: false,
|
||
layout_label: |_| None,
|
||
}
|
||
}
|
||
}
|
||
|
||
impl ChordCapture {
|
||
fn with_sides(sided: bool) -> Self {
|
||
Self {
|
||
sided,
|
||
layout_label: crate::keyboard::key_label,
|
||
..Self::default()
|
||
}
|
||
}
|
||
|
||
fn mouse_down(&mut self, button: isize, flags: u64) -> Option<Chord> {
|
||
let key = u16::try_from(button).ok().and_then(HotkeyKey::mouse)?;
|
||
let mut modifiers = modifiers_from_flags(flags).without_side_constraints();
|
||
modifiers.function = false;
|
||
self.keyed = !modifiers.is_empty();
|
||
self.peak = HotkeyModifiers::default();
|
||
Some(Chord::Captured(Ok(HotkeyBinding {
|
||
modifiers,
|
||
key: Some(key),
|
||
})))
|
||
}
|
||
|
||
fn key_down(
|
||
&mut self,
|
||
code: u16,
|
||
repeat: bool,
|
||
characters: Option<&str>,
|
||
flags: u64,
|
||
) -> Option<Chord> {
|
||
if repeat {
|
||
return None;
|
||
}
|
||
if code == ESCAPE_KEY_CODE {
|
||
return Some(Chord::Cancelled);
|
||
}
|
||
let mut modifiers = modifiers_from_flags(flags).without_side_constraints();
|
||
// Arrow and function keys report Fn; a chord never binds it.
|
||
modifiers.function = false;
|
||
// Only modifiers held with the key are spent by it.
|
||
self.keyed = !modifiers.is_empty();
|
||
self.peak = HotkeyModifiers::default();
|
||
let key = match hotkey_key(code, characters, || (self.layout_label)(code)) {
|
||
Ok(key) => key,
|
||
Err(message) => return Some(Chord::Captured(Err(message))),
|
||
};
|
||
// Function and navigation keys type nothing, so one may be a shortcut
|
||
// alone. The section key does type something, but the recorder takes it
|
||
// alone too: `settings::validate` refuses it while Labs › Section key
|
||
// alone is off, as Linux's recorder does. The microphone key has no
|
||
// character either, and the recorder likewise takes it alone: `validate`
|
||
// refuses it while Labs › Microphone key alone is off. Plain ⌫ turns an
|
||
// optional shortcut off, and `settings::validate` refuses it as a
|
||
// binding.
|
||
let stands_alone = HotkeyKey::is_function_name(&key.label)
|
||
|| is_navigation_key(code)
|
||
|| key.is_section()
|
||
|| key.is_microphone()
|
||
|| key.is_erase();
|
||
if modifiers.is_empty() && !stands_alone {
|
||
return Some(Chord::Captured(Err(tr("Add a modifier"))));
|
||
}
|
||
Some(Chord::Captured(Ok(HotkeyBinding {
|
||
modifiers,
|
||
key: Some(key),
|
||
})))
|
||
}
|
||
|
||
/// While modifiers are down, reports the ones their release would record.
|
||
fn flags_changed(&mut self, flags: u64) -> Option<Chord> {
|
||
let held = modifiers_from_flags(flags);
|
||
let current = if self.sided {
|
||
held
|
||
} else {
|
||
held.recorded_sides()
|
||
};
|
||
if !current.is_empty() {
|
||
if self.keyed {
|
||
return None;
|
||
}
|
||
if current.count() >= self.peak.count() {
|
||
self.peak = current;
|
||
}
|
||
return Some(Chord::Held(self.peak));
|
||
}
|
||
let keyed = std::mem::take(&mut self.keyed);
|
||
let peak = std::mem::take(&mut self.peak);
|
||
if keyed || peak.is_empty() {
|
||
return None;
|
||
}
|
||
Some(Chord::Captured(Ok(HotkeyBinding {
|
||
modifiers: peak,
|
||
key: None,
|
||
})))
|
||
}
|
||
}
|
||
|
||
/// Home, End, Page Up, Page Down, and Help (Insert on PC keyboards): keys
|
||
/// that type nothing, so, like a function key, one may be a shortcut alone,
|
||
/// as it could before (#1138).
|
||
fn is_navigation_key(code: u16) -> bool {
|
||
matches!(code, 115 | 119 | 116 | 121 | 114)
|
||
}
|
||
|
||
/// macOS accepts every binding the capture can produce, except that Paste
|
||
/// Last and the screenshot shortcut fire only on a key press
|
||
/// (`suppression::paste_action`), and the capture shortcut, pressed once to start and again to finish, and the
|
||
/// send-to list's need a key.
|
||
pub(super) fn validate_hotkey(
|
||
target: ShortcutTarget,
|
||
binding: &HotkeyBinding,
|
||
) -> Result<(), &'static str> {
|
||
if matches!(
|
||
target,
|
||
ShortcutTarget::PasteLast
|
||
| ShortcutTarget::ModelSwitch
|
||
| ShortcutTarget::Cancel
|
||
| ShortcutTarget::Screenshot
|
||
| ShortcutTarget::ScreenshotRecord
|
||
) && binding.key.is_none()
|
||
{
|
||
return Err(tr("Add a key"));
|
||
}
|
||
#[cfg(hex_capture)]
|
||
if matches!(
|
||
target,
|
||
ShortcutTarget::Capture | ShortcutTarget::CaptureReplay
|
||
) && binding.key.is_none()
|
||
{
|
||
return Err(tr("Add a key"));
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
/// The listener's retained history, shared with the window.
|
||
pub(super) fn history(
|
||
_: &Connect,
|
||
retention: crate::history::HistoryRetention,
|
||
) -> Option<crate::history::History> {
|
||
match crate::macos_rune::runtime() {
|
||
Some(runtime) => runtime.history(),
|
||
None => crate::history::History::open_default(retention)
|
||
.inspect_err(|error| tracing::warn!(%error, "could not open retained history"))
|
||
.ok(),
|
||
}
|
||
}
|
||
|
||
/// The listener knows the frontmost application.
|
||
pub(super) fn focused_application_known() -> bool {
|
||
true
|
||
}
|
||
|
||
/// The native host reports the login item (`PlatformState::login_item`).
|
||
pub(super) fn launch_at_login() -> Option<Result<bool, String>> {
|
||
None
|
||
}
|
||
|
||
pub(super) fn set_launch_at_login(_: bool) -> Result<bool, String> {
|
||
Err("Launch at login goes through the macOS host".into())
|
||
}
|
||
|
||
pub(super) fn native_host(wake: Arc<dyn Fn() + Send + Sync>) -> Box<dyn NativeHost> {
|
||
Box::new(MacHost::new(wake))
|
||
}
|
||
|
||
fn stale_slot(kind: PermissionKind) -> Option<usize> {
|
||
match kind {
|
||
PermissionKind::Accessibility => Some(0),
|
||
PermissionKind::SystemAudio => Some(1),
|
||
PermissionKind::ScreenRecording => Some(2),
|
||
PermissionKind::Microphone => None,
|
||
}
|
||
}
|
||
|
||
/// Performs the panes' OS-only requests and refreshes their state once a
|
||
/// second (permission checks and the driver status are not free per frame).
|
||
struct MacHost {
|
||
wake: Arc<dyn Fn() + Send + Sync>,
|
||
login: Option<LoginItemWorker>,
|
||
login_checking: bool,
|
||
folder_error: Option<String>,
|
||
refresh_at: Instant,
|
||
notes: Option<NotesAvailability>,
|
||
meetings: MeetingFeed,
|
||
practice: Option<PracticeFeed>,
|
||
/// The meetings folder panel, while it is open.
|
||
folder_choice: Option<crate::desktop::file_dialog::Pending>,
|
||
calendar: CalendarFeed,
|
||
/// Tracks System Settings return timing for Accessibility, System Audio,
|
||
/// and Screen Recording.
|
||
stale: [StalePermissionTracker; 3],
|
||
/// Brings Hex back once an asked permission is granted in System Settings.
|
||
returning: ReturnFromSettings,
|
||
/// In-flight `"Still grey? Fix it"` resets, indexed by [`stale_slot`].
|
||
resetting: [bool; 3],
|
||
reset_tx: mpsc::Sender<PermissionKind>,
|
||
reset_rx: Receiver<PermissionKind>,
|
||
#[cfg(hex_capture)]
|
||
scrawl: ScrawlHost,
|
||
}
|
||
|
||
impl MacHost {
|
||
fn new(wake: Arc<dyn Fn() + Send + Sync>) -> Self {
|
||
let login = LoginItemWorker::new()
|
||
.inspect_err(|error| tracing::warn!(%error, "login item worker is unavailable"))
|
||
.ok();
|
||
let (reset_tx, reset_rx) = mpsc::channel();
|
||
let mut host = Self {
|
||
wake,
|
||
login,
|
||
login_checking: false,
|
||
folder_error: None,
|
||
refresh_at: Instant::now(),
|
||
notes: None,
|
||
meetings: MeetingFeed::default(),
|
||
practice: None,
|
||
folder_choice: None,
|
||
calendar: CalendarFeed::default(),
|
||
stale: Default::default(),
|
||
returning: ReturnFromSettings::default(),
|
||
resetting: [false; 3],
|
||
reset_tx,
|
||
reset_rx,
|
||
#[cfg(hex_capture)]
|
||
scrawl: ScrawlHost::default(),
|
||
};
|
||
let _ = host.request_login(LoginItemRequest::Status);
|
||
host
|
||
}
|
||
|
||
fn note_permission_asked(&mut self, kind: PermissionKind) {
|
||
if let Some(slot) = stale_slot(kind) {
|
||
self.stale[slot].note_asked(Instant::now());
|
||
}
|
||
}
|
||
|
||
fn finish_permission_reset(&mut self, kind: PermissionKind) {
|
||
if let Some(slot) = stale_slot(kind) {
|
||
self.resetting[slot] = false;
|
||
self.stale[slot].note_asked(Instant::now());
|
||
}
|
||
match kind {
|
||
PermissionKind::Accessibility => {
|
||
if crate::desktop::onboarding::status_with_transcription_model(true).accessibility
|
||
!= PermissionState::Ready
|
||
{
|
||
let _ = crate::desktop::permission_guide::prompt_accessibility_trust();
|
||
crate::desktop::permission_guide::show(PermissionKind::Accessibility);
|
||
}
|
||
}
|
||
PermissionKind::SystemAudio => {
|
||
crate::meeting::reset_system_audio_request();
|
||
if !crate::meeting::system_audio_ready()
|
||
&& !crate::meeting::request_system_audio_access()
|
||
{
|
||
crate::desktop::permission_guide::show(PermissionKind::SystemAudio);
|
||
}
|
||
}
|
||
PermissionKind::ScreenRecording => {
|
||
#[cfg(hex_capture)]
|
||
crate::capture_host::invalidate_screen_access();
|
||
if !crate::meeting::screen_recording_permitted() {
|
||
let _ = crate::meeting::request_screen_recording();
|
||
crate::desktop::permission_guide::show(PermissionKind::ScreenRecording);
|
||
}
|
||
}
|
||
PermissionKind::Microphone => {}
|
||
}
|
||
self.refresh_at = Instant::now();
|
||
}
|
||
|
||
fn request_login(&mut self, request: LoginItemRequest) -> Result<(), String> {
|
||
let login = self
|
||
.login
|
||
.as_mut()
|
||
.ok_or("Launch at login is unavailable")?;
|
||
login.request(request).inspect_err(|error| {
|
||
tracing::warn!(%error, "could not ask for the login item");
|
||
})?;
|
||
self.login_checking = true;
|
||
Ok(())
|
||
}
|
||
|
||
fn runtime(&self) -> Result<RuntimeHandle, String> {
|
||
crate::macos_rune::runtime().ok_or_else(|| "Hex's runtime is not running".into())
|
||
}
|
||
|
||
fn meeting(&mut self, request: MeetingRequest) -> Result<(), String> {
|
||
match request {
|
||
MeetingRequest::Start => self
|
||
.runtime()?
|
||
.meeting(crate::meeting::MeetingRequest::Start { title: None }),
|
||
MeetingRequest::Stop => self
|
||
.runtime()?
|
||
.meeting(crate::meeting::MeetingRequest::Stop),
|
||
MeetingRequest::Import(path) => self
|
||
.runtime()?
|
||
.meeting(crate::meeting::MeetingRequest::Import(path)),
|
||
MeetingRequest::DeleteTranscript { id, entries } => self
|
||
.runtime()?
|
||
.meeting(crate::meeting::MeetingRequest::DeleteTranscript { id, entries }),
|
||
MeetingRequest::SetMicrophoneMuted(muted) => self
|
||
.runtime()?
|
||
.meeting(crate::meeting::MeetingRequest::SetMicrophoneMuted(muted)),
|
||
MeetingRequest::Summarize(id) => self
|
||
.runtime()?
|
||
.meeting(crate::meeting::MeetingRequest::Summarize(id)),
|
||
MeetingRequest::Rename { id, title } => crate::meeting::rename(&id, &title)
|
||
.map(|_| ())
|
||
.map_err(|error| format!("{error:#}")),
|
||
MeetingRequest::Trash(id) => {
|
||
crate::meeting::trash(&id).map_err(|error| format!("{error:#}"))
|
||
}
|
||
MeetingRequest::Reveal(target) => {
|
||
// `open -R` selects the exported Markdown in Finder; a
|
||
// recording folder opens itself.
|
||
if target.is_markdown() {
|
||
crate::meeting::reveal(target.path());
|
||
} else {
|
||
crate::meeting::open_folder(target.path());
|
||
}
|
||
Ok(())
|
||
}
|
||
MeetingRequest::RevealFolder => {
|
||
let folder = crate::app_settings::meeting_output_folder();
|
||
match std::fs::create_dir_all(&folder) {
|
||
Ok(()) => {
|
||
crate::meeting::open_folder(&folder);
|
||
self.folder_error = None;
|
||
}
|
||
Err(error) => self.folder_error = Some(error.to_string()),
|
||
}
|
||
Ok(())
|
||
}
|
||
MeetingRequest::ChooseFolder => {
|
||
if self.folder_choice.is_none() {
|
||
self.folder_choice = Some(choose_folder());
|
||
}
|
||
Ok(())
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
impl NativeHost for MacHost {
|
||
fn send(&mut self, request: PlatformRequest) -> Result<(), String> {
|
||
match request {
|
||
PlatformRequest::Permission(PermissionAction::RequestMicrophone) => {
|
||
crate::desktop::onboarding::request_microphone();
|
||
}
|
||
PlatformRequest::Permission(PermissionAction::ShowGuide(kind)) => {
|
||
// Screen Recording may have been granted since the last
|
||
// refresh; the refresh below then shows Allowed, or Restart.
|
||
if kind != PermissionKind::ScreenRecording
|
||
|| crate::capture_host::screen_access()
|
||
== crate::capture_host::ScreenAccess::Off
|
||
{
|
||
self.note_permission_asked(kind);
|
||
crate::desktop::permission_guide::show(kind);
|
||
}
|
||
}
|
||
PlatformRequest::Permission(PermissionAction::ResetAndPrompt(kind)) => {
|
||
if let Some(slot) = stale_slot(kind)
|
||
&& !self.resetting[slot]
|
||
{
|
||
self.resetting[slot] = true;
|
||
self.stale[slot].note_asked(Instant::now());
|
||
let tx = self.reset_tx.clone();
|
||
let wake = self.wake.clone();
|
||
crate::desktop::permission_guide::reset_and_reopen_async(kind, move |done| {
|
||
let _ = tx.send(done);
|
||
wake();
|
||
});
|
||
}
|
||
}
|
||
PlatformRequest::Permission(PermissionAction::Restart) => {
|
||
if !crate::desktop::onboarding::spawn_relaunch() {
|
||
return Err("could not restart Hex".into());
|
||
}
|
||
crate::macos_rune::quit();
|
||
}
|
||
PlatformRequest::SystemAudio => {
|
||
self.note_permission_asked(PermissionKind::SystemAudio);
|
||
crate::desktop::permission_guide::clear_legacy_entries_async(
|
||
PermissionKind::SystemAudio,
|
||
);
|
||
// macOS prompts once; after that the switch is in System
|
||
// Settings, with the guide beside it.
|
||
if !crate::meeting::request_system_audio_access() {
|
||
crate::desktop::permission_guide::show(PermissionKind::SystemAudio);
|
||
}
|
||
}
|
||
PlatformRequest::SetLaunchAtLogin(enabled) => {
|
||
self.request_login(LoginItemRequest::SetEnabled(enabled))?;
|
||
}
|
||
PlatformRequest::OpenLoginItemSettings => {
|
||
let _ = self.request_login(LoginItemRequest::OpenSettings);
|
||
}
|
||
PlatformRequest::InstallHexMicrophone
|
||
| PlatformRequest::UninstallHexMicrophone
|
||
| PlatformRequest::SetTuning(_)
|
||
| PlatformRequest::HexMicrophoneDemo(_) => voice_filter_request(request),
|
||
PlatformRequest::Meeting(request) => self.meeting(request)?,
|
||
PlatformRequest::Calendar(CalendarRequest::Connect) => self.calendar.connect(),
|
||
PlatformRequest::Calendar(CalendarRequest::OpenPrivacySettings) => {
|
||
self.calendar.opening_privacy_settings();
|
||
crate::calendar::open_privacy_settings();
|
||
}
|
||
PlatformRequest::Relaunch => {
|
||
if let Err(error) = crate::desktop::onboarding::record_relaunch(
|
||
crate::desktop::onboarding::unix_ms(),
|
||
) {
|
||
tracing::warn!(%error, "could not record the onboarding relaunch");
|
||
}
|
||
if !crate::desktop::onboarding::spawn_relaunch() {
|
||
return Err("could not relaunch Hex".into());
|
||
}
|
||
crate::macos_rune::quit();
|
||
}
|
||
PlatformRequest::ConfigureHud(lab) => configure_hud(&self.runtime()?, lab),
|
||
PlatformRequest::TuneHud(lab) => tune_hud(&self.runtime()?, lab),
|
||
PlatformRequest::EndHudDemo => self
|
||
.runtime()?
|
||
.indicator(crate::dictation::indicator::DictationIndicatorEvent::Reset),
|
||
PlatformRequest::SaveDotScope(tuning) => {
|
||
tuning.save().map_err(|error| format!("{error:#}"))?;
|
||
}
|
||
#[cfg(hex_capture)]
|
||
PlatformRequest::Scrawl(request) => self.scrawl.send(request),
|
||
}
|
||
// Show the result of an action at once, not on the next refresh.
|
||
self.refresh_at = Instant::now();
|
||
Ok(())
|
||
}
|
||
|
||
fn poll(&mut self, state: &mut PlatformState) -> bool {
|
||
let mut changed = false;
|
||
for &kind in &state.permissions_shown {
|
||
crate::desktop::permission_guide::clear_legacy_entries_async(kind);
|
||
}
|
||
while let Ok(kind) = self.reset_rx.try_recv() {
|
||
self.finish_permission_reset(kind);
|
||
state.clear_stale(kind);
|
||
changed = true;
|
||
}
|
||
// The playground's HUD stories change the tuning HUD Lab shows.
|
||
let shared = crate::rune_hud::dot_scope::lab();
|
||
if shared != state.hud.tuning {
|
||
state.hud.tuning = shared;
|
||
changed = true;
|
||
}
|
||
if let Some(login) = &mut self.login {
|
||
match login.poll() {
|
||
Ok(Some(answer)) => {
|
||
// A later click may already be running behind this answer.
|
||
let settled = login.idle();
|
||
self.login_checking = !settled;
|
||
state.login_item_answered(answer, settled);
|
||
changed = true;
|
||
}
|
||
Ok(None) => {}
|
||
Err(message) => {
|
||
self.login = None;
|
||
self.login_checking = false;
|
||
let failure = LoginItemFailure {
|
||
status: None,
|
||
message,
|
||
};
|
||
state.login_item_answered(Err(failure), true);
|
||
changed = true;
|
||
}
|
||
}
|
||
}
|
||
if let Some(choice) = self.folder_choice.as_ref().and_then(|p| p.poll()) {
|
||
self.folder_choice = None;
|
||
match choice {
|
||
Ok(Some(folder)) => self.folder_error = save_meetings_folder(folder).err(),
|
||
Ok(None) => {}
|
||
Err(error) => self.folder_error = Some(error),
|
||
}
|
||
changed = true;
|
||
}
|
||
let refresh = Instant::now() >= self.refresh_at;
|
||
changed |= poll_voice_filter(state, refresh);
|
||
#[cfg(hex_capture)]
|
||
{
|
||
changed |= self.scrawl.poll(state, Instant::now() >= self.refresh_at);
|
||
}
|
||
if Instant::now() < self.refresh_at {
|
||
return changed;
|
||
}
|
||
self.refresh_at = Instant::now() + PLATFORM_REFRESH;
|
||
// Input sources are read only while keyboard model switching is on.
|
||
let switching = crate::macos_rune::runtime()
|
||
.and_then(|runtime| runtime.published())
|
||
.is_some_and(|published| published.settings.keyboard_model_switching.enabled);
|
||
if let Some(mtm) = objc2::MainThreadMarker::new() {
|
||
changed |= update(
|
||
&mut state.input_sources,
|
||
if switching {
|
||
crate::keyboard_input_sources::enabled(mtm)
|
||
} else {
|
||
Vec::new()
|
||
},
|
||
);
|
||
}
|
||
changed |= self.calendar.poll(&mut state.calendar);
|
||
changed |= update(
|
||
&mut state.accessibility_renamed,
|
||
crate::desktop::permission_guide::list_name(PermissionKind::Accessibility)
|
||
!= "Accessibility",
|
||
);
|
||
changed |= update(
|
||
&mut state.accessibility_drag_supported,
|
||
crate::desktop::permission_guide::drag_supported(PermissionKind::Accessibility),
|
||
);
|
||
changed |= update(
|
||
&mut state.activity_log,
|
||
crate::app_paths::logs_dir()
|
||
.ok()
|
||
.map(|logs| logs.join("live.ndjson")),
|
||
);
|
||
let permissions = crate::desktop::onboarding::status_with_transcription_model(
|
||
state.permissions.transcription_model,
|
||
);
|
||
let system_audio = crate::meeting::system_audio_ready();
|
||
changed |= update(&mut state.permissions, permissions);
|
||
changed |= update(&mut state.system_audio, system_audio);
|
||
// A Screen Recording guide has nothing left to point at once Hex can
|
||
// record, or knows it must restart to: the guide's own preflight
|
||
// keeps the answer it read at launch, so the read that saw the grant
|
||
// closes it. It is watched while it shows, like the pane's row.
|
||
let screen_recording_guide =
|
||
crate::desktop::permission_guide::active(PermissionKind::ScreenRecording);
|
||
let screen_access = if screen_recording_guide
|
||
|| state
|
||
.permissions_shown
|
||
.contains(&PermissionKind::ScreenRecording)
|
||
{
|
||
crate::capture_host::screen_access_watched()
|
||
} else {
|
||
crate::capture_host::screen_access()
|
||
};
|
||
if screen_recording_guide
|
||
&& crate::desktop::permission_guide::screen_recording_answered(screen_access)
|
||
{
|
||
crate::desktop::permission_guide::close(PermissionKind::ScreenRecording);
|
||
}
|
||
let asked_len = state.permissions_asked.len();
|
||
state.permissions_asked.retain(|kind| match kind {
|
||
PermissionKind::Microphone => permissions.microphone != PermissionState::Ready,
|
||
PermissionKind::Accessibility => permissions.accessibility != PermissionState::Ready,
|
||
PermissionKind::SystemAudio => !system_audio,
|
||
PermissionKind::ScreenRecording => {
|
||
screen_access == crate::capture_host::ScreenAccess::Off
|
||
}
|
||
});
|
||
changed |= state.permissions_asked.len() != asked_len;
|
||
let now = Instant::now();
|
||
let focus = (self.returning.is_pending()
|
||
|| self.stale.iter().any(StalePermissionTracker::is_waiting))
|
||
.then(crate::desktop::permission_guide::settings_focus)
|
||
.unwrap_or_default();
|
||
let mut next_stale = Vec::new();
|
||
for (kind, untrusted) in [
|
||
(
|
||
PermissionKind::Accessibility,
|
||
matches!(
|
||
permissions.accessibility,
|
||
PermissionState::NeedsRequest | PermissionState::NeedsSettings
|
||
),
|
||
),
|
||
(PermissionKind::SystemAudio, !system_audio),
|
||
(PermissionKind::ScreenRecording, {
|
||
screen_access == crate::capture_host::ScreenAccess::Off
|
||
}),
|
||
] {
|
||
let Some(slot) = stale_slot(kind) else {
|
||
continue;
|
||
};
|
||
if !untrusted && self.stale[slot].is_waiting() {
|
||
self.returning.note_granted(now);
|
||
}
|
||
if !self.resetting[slot] && self.stale[slot].poll(now, untrusted, focus) {
|
||
next_stale.push(kind);
|
||
}
|
||
}
|
||
if self.returning.poll(now, focus) {
|
||
tracing::info!("permission granted in System Settings; bringing Hex back");
|
||
crate::macos_rune::bring_window_forward();
|
||
}
|
||
changed |= update(&mut state.permissions_stale, next_stale);
|
||
changed |= update(&mut state.login_item_checking, self.login_checking);
|
||
changed |= update(
|
||
&mut state.notes,
|
||
*self.notes.get_or_insert_with(notes_availability),
|
||
);
|
||
changed |= update(&mut state.meeting_folder_error, self.folder_error.clone());
|
||
changed
|
||
}
|
||
|
||
fn meetings(&mut self) -> Option<Result<Vec<Meeting>, String>> {
|
||
self.meetings.poll()
|
||
}
|
||
|
||
fn tour_inputs(&mut self, practicing: bool) -> Vec<Input> {
|
||
let mut inputs = Vec::new();
|
||
if crate::desktop::onboarding::take_listener_failure() {
|
||
inputs.push(Input::ListenerFailed {
|
||
relaunch_allowed: crate::desktop::onboarding::relaunch_allowed(
|
||
crate::desktop::onboarding::unix_ms(),
|
||
),
|
||
});
|
||
}
|
||
if !practicing {
|
||
self.practice = None;
|
||
return inputs;
|
||
}
|
||
if self.practice.is_none() {
|
||
self.practice =
|
||
crate::macos_rune::runtime().map(|runtime| PracticeFeed::new(runtime.event_path()));
|
||
}
|
||
if let Some(practice) = &mut self.practice {
|
||
inputs.extend(practice.observations().into_iter().map(Input::Dictation));
|
||
}
|
||
inputs
|
||
}
|
||
}
|
||
|
||
/// The Calendar card's state. EventKit is touched only on the reader thread,
|
||
/// never the window's. Connect asks macOS once, on the person's click.
|
||
#[derive(Default)]
|
||
struct CalendarFeed {
|
||
connect: Option<Receiver<AccessAnswer>>,
|
||
reader: Option<CalendarReader>,
|
||
connect_error: Option<String>,
|
||
/// macOS's last answer in this process. EventKit's status can keep
|
||
/// saying "not determined" after it until Hex relaunches; once the reader
|
||
/// sees a decision, that is current and this is forgotten.
|
||
answered: Option<CalendarAccess>,
|
||
/// The person asked Hex to connect (Connect, or Open settings from the
|
||
/// blocked card) and it is not saved yet, so full access granted in System
|
||
/// Settings afterwards connects. A grant Hex did not start never does.
|
||
asked: bool,
|
||
}
|
||
|
||
struct CalendarReader {
|
||
snapshots: Receiver<CalendarSnapshot>,
|
||
/// Dropping this stops the reader thread.
|
||
_stop: mpsc::SyncSender<()>,
|
||
}
|
||
|
||
/// The Calendar card's reader: rereads when the settings, macOS access, or
|
||
/// (once connected) the EventKit store change, and every
|
||
/// [`CALENDAR_REFRESH`] so started meetings leave the list.
|
||
fn calendar_reader(snapshots: mpsc::SyncSender<CalendarSnapshot>, stop: Receiver<()>) {
|
||
let mut read_for: Option<(CalendarSettings, CalendarAccess, u64)> = None;
|
||
let mut read_at: Option<Instant> = None;
|
||
let mut logged: Option<(CalendarAccess, bool, usize, Option<String>)> = None;
|
||
let mut wait = Duration::ZERO;
|
||
loop {
|
||
if let Err(mpsc::RecvTimeoutError::Disconnected) = stop.recv_timeout(wait) {
|
||
return;
|
||
}
|
||
wait = PLATFORM_REFRESH;
|
||
if !crate::app_settings::meetings_enabled() {
|
||
read_for = None;
|
||
continue;
|
||
}
|
||
let settings = crate::app_settings::calendar_settings();
|
||
let changes = if settings.connected {
|
||
crate::calendar::change_count()
|
||
} else {
|
||
0
|
||
};
|
||
let key = (settings.clone(), crate::calendar::access(), changes);
|
||
if read_for.as_ref() == Some(&key)
|
||
&& read_at.is_some_and(|at| at.elapsed() < CALENDAR_REFRESH)
|
||
{
|
||
continue;
|
||
}
|
||
let snapshot = crate::calendar::snapshot(&settings, crate::calendar::wall_clock_ms());
|
||
let summary = (
|
||
snapshot.access,
|
||
settings.connected,
|
||
snapshot.calendars.len(),
|
||
snapshot.error.clone(),
|
||
);
|
||
if logged.as_ref() != Some(&summary) {
|
||
tracing::info!(
|
||
access = ?snapshot.access,
|
||
connected = settings.connected,
|
||
calendars = snapshot.calendars.len(),
|
||
upcoming = snapshot.upcoming.len(),
|
||
error = snapshot.error.as_deref().unwrap_or(""),
|
||
"calendar: read"
|
||
);
|
||
logged = Some(summary);
|
||
}
|
||
read_for = Some(key);
|
||
read_at = Some(Instant::now());
|
||
// The window dropped its feed: stop.
|
||
if snapshots.send(snapshot).is_err() {
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
impl CalendarFeed {
|
||
fn connect(&mut self) {
|
||
if self.connect.is_some() {
|
||
tracing::info!("calendar: connect clicked while macOS is still asking");
|
||
return;
|
||
}
|
||
tracing::info!("calendar: connect clicked; asking macOS for full access");
|
||
self.asked = true;
|
||
let (sender, receiver) = mpsc::sync_channel(1);
|
||
let spawned = std::thread::Builder::new()
|
||
.name("hex-calendar-connect".into())
|
||
.spawn(move || {
|
||
let _ = sender.send(crate::calendar::request_access());
|
||
});
|
||
match spawned {
|
||
Ok(_) => {
|
||
self.connect = Some(receiver);
|
||
self.connect_error = None;
|
||
}
|
||
Err(error) => {
|
||
tracing::warn!(%error, "calendar: could not start the access request");
|
||
self.connect_error = Some(error.to_string());
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Open settings on the blocked card: switching Hex on there connects.
|
||
fn opening_privacy_settings(&mut self) {
|
||
tracing::info!("calendar: opening Privacy & Security › Calendars");
|
||
self.asked = true;
|
||
}
|
||
|
||
/// Applies macOS's answer: a grant marks the calendar connected (through
|
||
/// `save`) and shows full access; anything else shows what macOS left.
|
||
fn answer(
|
||
&mut self,
|
||
answer: &AccessAnswer,
|
||
view: &mut CalendarView,
|
||
save: impl FnOnce() -> Result<(), String>,
|
||
) {
|
||
let access = answer.access();
|
||
tracing::info!(
|
||
granted = answer.granted,
|
||
status = ?answer.status,
|
||
error = answer.error.as_deref().unwrap_or(""),
|
||
?access,
|
||
"calendar: macOS answered"
|
||
);
|
||
self.connect_error = answer.error.clone();
|
||
if access != CalendarAccess::NotDetermined {
|
||
self.answered = Some(access);
|
||
}
|
||
if access == CalendarAccess::FullAccess {
|
||
self.save_connected(save);
|
||
}
|
||
view.access = access;
|
||
view.error = self.connect_error.clone();
|
||
}
|
||
|
||
fn save_connected(&mut self, save: impl FnOnce() -> Result<(), String>) {
|
||
match save() {
|
||
Ok(()) => {
|
||
tracing::info!("calendar: saved connected; settings reloading");
|
||
self.asked = false;
|
||
}
|
||
Err(error) => {
|
||
tracing::warn!(%error, "calendar: could not save connected");
|
||
self.connect_error = Some(error);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Takes a reader snapshot: forgets the answer once EventKit reports a
|
||
/// decision, and connects when full access arrives after the person asked
|
||
/// Hex to connect (refused the prompt, then switched Hex on in System
|
||
/// Settings).
|
||
fn read(
|
||
&mut self,
|
||
snapshot: CalendarSnapshot,
|
||
connected: bool,
|
||
view: &CalendarView,
|
||
now_ms: i64,
|
||
save: impl FnOnce() -> Result<(), String>,
|
||
) -> CalendarView {
|
||
if snapshot.access != CalendarAccess::NotDetermined {
|
||
self.answered = None;
|
||
}
|
||
if self.asked && !connected && snapshot.access == CalendarAccess::FullAccess {
|
||
tracing::info!("calendar: full access granted after connect; connecting");
|
||
self.connect_error = None;
|
||
self.save_connected(save);
|
||
}
|
||
self.shown(snapshot, view, now_ms)
|
||
}
|
||
|
||
/// A reader snapshot as the card shows it.
|
||
fn shown(&self, snapshot: CalendarSnapshot, view: &CalendarView, now_ms: i64) -> CalendarView {
|
||
let access = match (snapshot.access, self.answered) {
|
||
(CalendarAccess::NotDetermined, Some(answered)) => answered,
|
||
(access, _) => access,
|
||
};
|
||
CalendarView {
|
||
access,
|
||
calendars: snapshot.calendars,
|
||
upcoming: snapshot
|
||
.upcoming
|
||
.iter()
|
||
.map(|meeting| upcoming_meeting(meeting, now_ms))
|
||
.collect(),
|
||
connecting: view.connecting,
|
||
error: snapshot.error.or_else(|| self.connect_error.clone()),
|
||
}
|
||
}
|
||
|
||
fn poll(&mut self, view: &mut CalendarView) -> bool {
|
||
let mut changed = false;
|
||
if let Some(receiver) = &self.connect
|
||
&& let Ok(answer) = receiver.try_recv()
|
||
{
|
||
self.connect = None;
|
||
self.answer(&answer, view, save_calendar_connected);
|
||
changed = true;
|
||
}
|
||
changed |= update(&mut view.connecting, self.connect.is_some());
|
||
if self.reader.is_none() && crate::app_settings::meetings_enabled() {
|
||
let (sender, snapshots) = mpsc::sync_channel(1);
|
||
let (stop, stopped) = mpsc::sync_channel(1);
|
||
let spawned = std::thread::Builder::new()
|
||
.name("hex-calendar-read".into())
|
||
.spawn(move || calendar_reader(sender, stopped));
|
||
match spawned {
|
||
Ok(_) => {
|
||
self.reader = Some(CalendarReader {
|
||
snapshots,
|
||
_stop: stop,
|
||
});
|
||
}
|
||
Err(error) => tracing::warn!(%error, "calendar: could not start the reader"),
|
||
}
|
||
}
|
||
if let Some(reader) = &self.reader
|
||
&& let Ok(snapshot) = reader.snapshots.try_recv()
|
||
{
|
||
let connected = crate::app_settings::calendar_settings().connected;
|
||
let shown = self.read(
|
||
snapshot,
|
||
connected,
|
||
view,
|
||
crate::calendar::wall_clock_ms(),
|
||
save_calendar_connected,
|
||
);
|
||
changed |= update(view, shown);
|
||
}
|
||
changed
|
||
}
|
||
}
|
||
|
||
/// One upcoming meeting as the Calendar card lists it: "Today · 2:30 PM".
|
||
fn upcoming_meeting(meeting: &crate::calendar::CalendarMeeting, now_ms: i64) -> UpcomingMeeting {
|
||
let start = meeting.start_ms.max(0) as u64;
|
||
let now = now_ms.max(0) as u64;
|
||
let day = if crate::meeting::day_label(start, now) == "Today" {
|
||
tr("Today").to_string()
|
||
} else if crate::meeting::day_label(start, now + 86_400_000) == "Today" {
|
||
tr("Tomorrow").to_string()
|
||
} else {
|
||
crate::meeting::day_label(start, now)
|
||
};
|
||
UpcomingMeeting {
|
||
title: meeting.display_title(),
|
||
when: format!("{day} · {}", crate::meeting::clock(start)),
|
||
provider: meeting.link.as_ref().map(|link| link.provider),
|
||
}
|
||
}
|
||
|
||
/// Marks the calendar connected after macOS granted access; the runtime
|
||
/// publishes it to the window and starts reminders.
|
||
fn save_calendar_connected() -> Result<(), String> {
|
||
let mut settings =
|
||
crate::app_settings::AppSettings::load().map_err(|error| format!("{error:#}"))?;
|
||
let base = settings.clone();
|
||
settings.meetings.calendar.connected = true;
|
||
base.update(&settings)
|
||
.map_err(|error| format!("{error:#}"))?;
|
||
match crate::macos_rune::runtime() {
|
||
Some(runtime) => runtime
|
||
.send(Request::ReloadSettings)
|
||
.map_err(|error| format!("{error:#}"))?,
|
||
None => tracing::warn!("calendar: no runtime to reload settings"),
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
/// Opens the folder panel beside the window, starting at the current
|
||
/// meetings folder.
|
||
fn choose_folder() -> crate::desktop::file_dialog::Pending {
|
||
use crate::desktop::file_dialog::{Dialog, Target};
|
||
crate::desktop::file_dialog::open(
|
||
Dialog {
|
||
title: tr("Transcript folder").into(),
|
||
target: Target::Folder,
|
||
directory: Some(crate::app_settings::meeting_output_folder()),
|
||
},
|
||
|| crate::macos_rune::on_main_thread(|| {}),
|
||
)
|
||
}
|
||
|
||
/// The Scrawl pane's OS work: Scrawl's own app and the captures folder.
|
||
/// Its Screen Recording Allow opens the permission guide.
|
||
#[cfg(hex_capture)]
|
||
#[derive(Default)]
|
||
struct ScrawlHost {
|
||
folder_choice: Option<crate::desktop::file_dialog::Pending>,
|
||
folder_error: Option<String>,
|
||
}
|
||
|
||
#[cfg(hex_capture)]
|
||
impl ScrawlHost {
|
||
fn send(&mut self, request: super::platform::ScrawlRequest) {
|
||
use super::platform::ScrawlRequest;
|
||
match request {
|
||
// The pane opened or the window became key: read now.
|
||
ScrawlRequest::Refresh => crate::capture_host::invalidate_screen_access(),
|
||
ScrawlRequest::QuitScrawl => crate::capture_host::quit_scrawl(),
|
||
ScrawlRequest::RevealFolder => {
|
||
let folder = crate::app_settings::capture_settings().captures_dir();
|
||
match std::fs::create_dir_all(&folder) {
|
||
Ok(()) => {
|
||
crate::meeting::open_folder(&folder);
|
||
self.folder_error = None;
|
||
}
|
||
Err(error) => self.folder_error = Some(error.to_string()),
|
||
}
|
||
}
|
||
ScrawlRequest::ChooseFolder => {
|
||
if self.folder_choice.is_none() {
|
||
use crate::desktop::file_dialog::{Dialog, Target};
|
||
self.folder_choice = Some(crate::desktop::file_dialog::open(
|
||
Dialog {
|
||
title: tr("Annotations folder").into(),
|
||
target: Target::Folder,
|
||
directory: Some(crate::app_settings::capture_settings().captures_dir()),
|
||
},
|
||
|| crate::macos_rune::on_main_thread(|| {}),
|
||
));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// The pane shows while the `capture` experiment is on; `refresh` also
|
||
/// rereads the permission and whether Scrawl runs.
|
||
fn poll(&mut self, state: &mut PlatformState, refresh: bool) -> bool {
|
||
let mut changed = state.scrawl.is_none();
|
||
let view = state.scrawl.get_or_insert_default();
|
||
if let Some(choice) = self.folder_choice.as_ref().and_then(|p| p.poll()) {
|
||
self.folder_choice = None;
|
||
match choice {
|
||
Ok(Some(folder)) => {
|
||
self.folder_error = save_settings(|settings| {
|
||
settings.capture.folder = Some(folder);
|
||
})
|
||
.err();
|
||
}
|
||
Ok(None) => {}
|
||
Err(error) => self.folder_error = Some(error),
|
||
}
|
||
changed = true;
|
||
}
|
||
changed |= update(&mut view.folder_error, self.folder_error.clone());
|
||
if !(refresh || changed) {
|
||
return changed;
|
||
}
|
||
// The row on screen follows the grant within seconds; without it an
|
||
// idle read stands, so no helper runs just because a window polls.
|
||
let screen = if state
|
||
.permissions_shown
|
||
.contains(&PermissionKind::ScreenRecording)
|
||
{
|
||
crate::capture_host::screen_access_watched()
|
||
} else {
|
||
crate::capture_host::screen_access()
|
||
};
|
||
let screen_recording = screen == crate::capture_host::ScreenAccess::Allowed;
|
||
let needs_restart = screen == crate::capture_host::ScreenAccess::NeedsRestart;
|
||
changed |= update(&mut view.screen_needs_restart, needs_restart);
|
||
let running = crate::capture_host::scrawl_running();
|
||
changed |= update(&mut view.screen_recording, screen_recording);
|
||
changed |= update(&mut view.scrawl_running, running);
|
||
if !running {
|
||
changed |= update(&mut view.quitting_scrawl, false);
|
||
}
|
||
changed
|
||
}
|
||
}
|
||
|
||
/// Saves one change to the settings; the runtime publishes it to the
|
||
/// window.
|
||
#[cfg(hex_capture)]
|
||
pub(in crate::rune_ui) fn save_settings(
|
||
change: impl FnOnce(&mut crate::app_settings::AppSettings),
|
||
) -> Result<(), String> {
|
||
let mut settings =
|
||
crate::app_settings::AppSettings::load().map_err(|error| format!("{error:#}"))?;
|
||
let base = settings.clone();
|
||
change(&mut settings);
|
||
base.update(&settings)
|
||
.map_err(|error| format!("{error:#}"))?;
|
||
if let Some(runtime) = crate::macos_rune::runtime() {
|
||
runtime
|
||
.send(Request::ReloadSettings)
|
||
.map_err(|error| format!("{error:#}"))?;
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
/// Saves the chosen meetings folder; the runtime publishes it to the window.
|
||
fn save_meetings_folder(folder: PathBuf) -> Result<(), String> {
|
||
let mut settings =
|
||
crate::app_settings::AppSettings::load().map_err(|error| format!("{error:#}"))?;
|
||
let base = settings.clone();
|
||
settings.meetings.output_folder = Some(folder);
|
||
base.update(&settings)
|
||
.map_err(|error| format!("{error:#}"))?;
|
||
if let Some(runtime) = crate::macos_rune::runtime() {
|
||
runtime
|
||
.send(Request::ReloadSettings)
|
||
.map_err(|error| format!("{error:#}"))?;
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
/// Shows HUD Lab's tuning and demonstrated state on the dictation HUD.
|
||
fn configure_hud(runtime: &RuntimeHandle, lab: HudLab) {
|
||
use crate::dictation::indicator::DictationIndicatorEvent as Event;
|
||
tune_hud(runtime, lab);
|
||
runtime.indicator(Event::Reset);
|
||
match lab.demo {
|
||
HudDemo::Recording => {
|
||
runtime.indicator(Event::Started);
|
||
runtime.indicator(Event::DemoVoice);
|
||
}
|
||
HudDemo::Transcribing | HudDemo::Processing => {
|
||
runtime.indicator(Event::Submitted { job_id: 0 });
|
||
runtime.indicator(if lab.demo == HudDemo::Transcribing {
|
||
Event::Transcribing { job_id: 0 }
|
||
} else {
|
||
Event::Processing { job_id: 0 }
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Applies HUD Lab's tuning to whatever the HUD shows.
|
||
fn tune_hud(runtime: &RuntimeHandle, lab: HudLab) {
|
||
use crate::dictation::indicator::DictationIndicatorEvent as Event;
|
||
runtime.indicator(Event::ConfigureDotScope(lab.tuning));
|
||
}
|
||
|
||
fn update<T: PartialEq>(slot: &mut T, value: T) -> bool {
|
||
if *slot == value {
|
||
false
|
||
} else {
|
||
*slot = value;
|
||
true
|
||
}
|
||
}
|
||
|
||
fn notes_availability() -> NotesAvailability {
|
||
use crate::apple_intelligence::Availability;
|
||
match crate::apple_intelligence::availability() {
|
||
Availability::Available => NotesAvailability::Available,
|
||
Availability::DeviceNotEligible => NotesAvailability::DeviceNotEligible,
|
||
Availability::AppleIntelligenceNotEnabled => NotesAvailability::NotEnabled,
|
||
Availability::ModelNotReady => NotesAvailability::ModelNotReady,
|
||
Availability::UnsupportedSystem => NotesAvailability::UnsupportedSystem,
|
||
Availability::Unknown => NotesAvailability::Unknown,
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::super::hotkey_keys::hotkey_key_named;
|
||
use super::*;
|
||
use crate::app_settings::{ISO_SECTION_KEY_CODE, MICROPHONE_KEY_CODE, ModifierSide};
|
||
|
||
#[test]
|
||
fn paste_last_needs_a_key() {
|
||
let modifiers_only = HotkeyBinding {
|
||
modifiers: crate::app_settings::HotkeyModifiers::option_command(),
|
||
key: None,
|
||
};
|
||
assert_eq!(
|
||
validate_hotkey(ShortcutTarget::PasteLast, &modifiers_only),
|
||
Err(tr("Add a key"))
|
||
);
|
||
assert_eq!(
|
||
validate_hotkey(ShortcutTarget::Dictation, &modifiers_only),
|
||
Ok(())
|
||
);
|
||
let chord = HotkeyBinding::paste_last_default();
|
||
assert_eq!(validate_hotkey(ShortcutTarget::PasteLast, &chord), Ok(()));
|
||
}
|
||
|
||
const LEFT_OPTION: u64 = 0x0008_0020;
|
||
const RIGHT_OPTION: u64 = 0x0008_0040;
|
||
const LEFT_COMMAND: u64 = 0x0010_0008;
|
||
const FUNCTION: u64 = 0x0080_0000;
|
||
|
||
fn captured(chord: Option<Chord>) -> Result<HotkeyBinding, &'static str> {
|
||
match chord {
|
||
Some(Chord::Captured(result)) => result,
|
||
Some(Chord::Held(_)) => panic!("still held"),
|
||
Some(Chord::Cancelled) => panic!("cancelled"),
|
||
None => panic!("nothing recorded"),
|
||
}
|
||
}
|
||
|
||
fn held(chord: Option<Chord>) -> Vec<String> {
|
||
match chord {
|
||
Some(Chord::Held(modifiers)) => HotkeyBinding {
|
||
modifiers,
|
||
key: None,
|
||
}
|
||
.keycaps(),
|
||
_ => panic!("not holding"),
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn mouse_buttons_record_with_or_without_modifiers() {
|
||
for button in 0..32 {
|
||
for flags in [0, LEFT_OPTION, RIGHT_OPTION | LEFT_COMMAND] {
|
||
let mut capture = ChordCapture::default();
|
||
capture.flags_changed(flags);
|
||
let binding = captured(capture.mouse_down(button, flags)).unwrap();
|
||
assert_eq!(binding.key, HotkeyKey::mouse(button as u16));
|
||
assert_eq!(
|
||
binding.modifiers,
|
||
modifiers_from_flags(flags).without_side_constraints()
|
||
);
|
||
assert!(capture.flags_changed(0).is_none());
|
||
let text = crate::control::settings::format_hotkey(&binding);
|
||
let parsed = crate::control::settings::parse_hotkey(&text, |name| {
|
||
hotkey_key_named(name).map_err(str::to_owned)
|
||
});
|
||
// A bare left click is recorded, then refused (issue 411).
|
||
if binding.is_bare_primary_click() {
|
||
assert!(parsed.is_err());
|
||
} else {
|
||
assert_eq!(parsed.unwrap(), binding);
|
||
}
|
||
}
|
||
}
|
||
let mut capture = ChordCapture::default();
|
||
assert!(capture.mouse_down(-1, 0).is_none());
|
||
assert!(capture.mouse_down(32, 0).is_none());
|
||
assert!(matches!(
|
||
capture.key_down(ESCAPE_KEY_CODE, false, None, 0),
|
||
Some(Chord::Cancelled)
|
||
));
|
||
}
|
||
|
||
#[test]
|
||
fn mouse_bindings_persist_and_conflict_only_with_the_same_trigger() {
|
||
let binding = captured(ChordCapture::default().mouse_down(3, 0)).unwrap();
|
||
let restored: HotkeyBinding =
|
||
serde_json::from_str(&serde_json::to_string(&binding).unwrap()).unwrap();
|
||
assert_eq!(restored, binding);
|
||
assert_eq!(restored.keycaps(), ["Mouse 4"]);
|
||
assert!(binding.overlaps(&restored));
|
||
assert!(!binding.overlaps(&captured(ChordCapture::default().mouse_down(4, 0)).unwrap()));
|
||
assert!(!binding.overlaps(&HotkeyBinding::default()));
|
||
for name in ["mouse0", "mouse33", "mouse"] {
|
||
assert!(hotkey_key_named(name).is_err());
|
||
}
|
||
assert_eq!(
|
||
hotkey_key_named("mouse-1").unwrap(),
|
||
HotkeyKey::mouse(0).unwrap()
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn releasing_modifiers_records_the_most_held_with_their_side() {
|
||
let mut capture = ChordCapture::default();
|
||
assert_eq!(held(capture.flags_changed(LEFT_OPTION)), ["L⌥"]);
|
||
assert_eq!(
|
||
held(capture.flags_changed(LEFT_OPTION | LEFT_COMMAND)),
|
||
["⌥", "⌘"],
|
||
"two modifiers record from either side"
|
||
);
|
||
assert_eq!(
|
||
held(capture.flags_changed(LEFT_OPTION)),
|
||
["⌥", "⌘"],
|
||
"letting one go still shows what releasing records"
|
||
);
|
||
let binding = captured(capture.flags_changed(0)).unwrap();
|
||
assert_eq!(binding.key, None);
|
||
assert_eq!(binding.modifiers.option, Some(ModifierSide::Either));
|
||
assert_eq!(binding.modifiers.command, Some(ModifierSide::Either));
|
||
let binding = {
|
||
assert_eq!(held(capture.flags_changed(RIGHT_OPTION)), ["R⌥"]);
|
||
captured(capture.flags_changed(0)).unwrap()
|
||
};
|
||
assert_eq!(binding.modifiers.option, Some(ModifierSide::Right));
|
||
assert_eq!(held(capture.flags_changed(FUNCTION)), ["fn"]);
|
||
assert!(
|
||
captured(capture.flags_changed(0))
|
||
.unwrap()
|
||
.modifiers
|
||
.function
|
||
);
|
||
}
|
||
|
||
const RIGHT_COMMAND: u64 = 0x0010_0010;
|
||
|
||
#[test]
|
||
fn with_sided_chords_each_modifier_keeps_its_side() {
|
||
let mut capture = ChordCapture::with_sides(true);
|
||
assert_eq!(held(capture.flags_changed(RIGHT_COMMAND)), ["R⌘"]);
|
||
assert_eq!(
|
||
held(capture.flags_changed(RIGHT_COMMAND | RIGHT_OPTION)),
|
||
["R⌥", "R⌘"]
|
||
);
|
||
let binding = captured(capture.flags_changed(0)).unwrap();
|
||
assert_eq!(binding.key, None);
|
||
assert_eq!(binding.modifiers.option, Some(ModifierSide::Right));
|
||
assert_eq!(binding.modifiers.command, Some(ModifierSide::Right));
|
||
assert_eq!(binding.modifiers.count(), 2);
|
||
assert_eq!(binding.standalone_side(), None, "no side picker");
|
||
assert_eq!(binding.keycaps(), ["R⌥", "R⌘"]);
|
||
let runtime = binding.runtime();
|
||
assert!(runtime.exact_modifiers(RIGHT_COMMAND | RIGHT_OPTION));
|
||
assert!(!runtime.exact_modifiers(LEFT_COMMAND | LEFT_OPTION));
|
||
assert!(!runtime.exact_modifiers(LEFT_COMMAND | RIGHT_OPTION));
|
||
let restored: HotkeyBinding =
|
||
serde_json::from_str(&serde_json::to_string(&binding).unwrap()).unwrap();
|
||
assert_eq!(restored, binding);
|
||
// It still conflicts with ⌥⌘ from either side, not with the left keys.
|
||
let either = HotkeyBinding {
|
||
modifiers: HotkeyModifiers::option_command(),
|
||
key: None,
|
||
};
|
||
assert!(binding.overlaps(&either));
|
||
assert!(!binding.overlaps(&HotkeyBinding {
|
||
modifiers: HotkeyModifiers {
|
||
option: Some(ModifierSide::Left),
|
||
command: Some(ModifierSide::Left),
|
||
..Default::default()
|
||
},
|
||
key: None,
|
||
}));
|
||
}
|
||
|
||
#[test]
|
||
fn without_sided_chords_two_modifiers_record_from_either_side() {
|
||
let mut capture = ChordCapture::with_sides(false);
|
||
capture.flags_changed(RIGHT_COMMAND);
|
||
assert_eq!(
|
||
held(capture.flags_changed(RIGHT_COMMAND | RIGHT_OPTION)),
|
||
["⌥", "⌘"]
|
||
);
|
||
let binding = captured(capture.flags_changed(0)).unwrap();
|
||
assert_eq!(binding.modifiers, HotkeyModifiers::option_command());
|
||
assert!(
|
||
binding
|
||
.runtime()
|
||
.exact_modifiers(LEFT_COMMAND | LEFT_OPTION)
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn both_options_record_as_one_chord_of_two_keys() {
|
||
let mut capture = ChordCapture::default();
|
||
assert_eq!(held(capture.flags_changed(RIGHT_OPTION)), ["R⌥"]);
|
||
assert_eq!(
|
||
held(capture.flags_changed(LEFT_OPTION | RIGHT_OPTION)),
|
||
["L⌥", "R⌥"]
|
||
);
|
||
assert_eq!(
|
||
held(capture.flags_changed(LEFT_OPTION)),
|
||
["L⌥", "R⌥"],
|
||
"the peak stays"
|
||
);
|
||
let binding = captured(capture.flags_changed(0)).unwrap();
|
||
assert_eq!(binding.modifiers.option, Some(ModifierSide::Both));
|
||
assert_eq!(binding.modifiers.count(), 2);
|
||
assert_eq!(binding.standalone_side(), None, "no side picker");
|
||
assert_eq!(
|
||
crate::control::settings::format_hotkey(&binding),
|
||
"left-option+right-option"
|
||
);
|
||
assert!(!binding.overlaps(&HotkeyBinding {
|
||
modifiers: HotkeyModifiers {
|
||
option: Some(ModifierSide::Right),
|
||
..Default::default()
|
||
},
|
||
key: None,
|
||
}));
|
||
assert!(
|
||
binding.overlaps(&HotkeyBinding::default()),
|
||
"⌥ takes both too"
|
||
);
|
||
assert_eq!(
|
||
held(capture.flags_changed(LEFT_OPTION | RIGHT_OPTION | LEFT_COMMAND)),
|
||
["L⌥", "R⌥", "⌘"],
|
||
"an extra key shows while held"
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn a_key_records_its_modifiers_on_either_side_and_their_release_nothing() {
|
||
let mut capture = ChordCapture::default();
|
||
assert_eq!(held(capture.flags_changed(LEFT_OPTION)), ["L⌥"]);
|
||
let binding = captured(capture.key_down(9, false, Some("v"), LEFT_OPTION)).unwrap();
|
||
assert_eq!(binding.modifiers.option, Some(ModifierSide::Either));
|
||
assert_eq!(
|
||
binding.key,
|
||
Some(HotkeyKey {
|
||
code: 9,
|
||
label: "V".into()
|
||
})
|
||
);
|
||
assert!(capture.flags_changed(0).is_none());
|
||
assert!(capture.key_down(9, true, Some("v"), LEFT_OPTION).is_none());
|
||
}
|
||
|
||
#[test]
|
||
fn keys_without_modifiers_need_one_except_function_keys() {
|
||
let mut capture = ChordCapture::default();
|
||
assert_eq!(
|
||
captured(capture.key_down(0, false, Some("a"), 0)),
|
||
Err(tr("Add a modifier"))
|
||
);
|
||
assert_eq!(
|
||
held(capture.flags_changed(LEFT_OPTION)),
|
||
["L⌥"],
|
||
"a rejected bare key spends no modifiers"
|
||
);
|
||
assert!(captured(capture.flags_changed(0)).is_ok());
|
||
let f12 = captured(capture.key_down(111, false, None, FUNCTION)).unwrap();
|
||
assert!(f12.modifiers.is_empty());
|
||
assert_eq!(f12.key.unwrap().label, "F12");
|
||
// Issue 1138: End, and the other keys that type nothing, record alone.
|
||
for (code, label) in [
|
||
(119, "End"),
|
||
(115, "Home"),
|
||
(116, "Page Up"),
|
||
(121, "Page Down"),
|
||
(114, "Help"),
|
||
] {
|
||
let binding = captured(capture.key_down(code, false, None, FUNCTION)).unwrap();
|
||
assert!(binding.modifiers.is_empty());
|
||
assert_eq!(binding.key.as_ref().unwrap().label, label);
|
||
assert!(binding.runtime().matches_key_press(code, FUNCTION));
|
||
}
|
||
assert_eq!(
|
||
captured(capture.key_down(123, false, None, FUNCTION)),
|
||
Err(tr("Add a modifier")),
|
||
"arrows still need one"
|
||
);
|
||
assert!(matches!(
|
||
capture.key_down(ESCAPE_KEY_CODE, false, None, 0),
|
||
Some(Chord::Cancelled)
|
||
));
|
||
}
|
||
|
||
/// The recorder names a dead key from the active layout when AppKit gives
|
||
/// no characters, and with neither it still refuses the key.
|
||
#[test]
|
||
fn a_dead_key_records_its_layout_label() {
|
||
let mut capture = ChordCapture {
|
||
layout_label: |code| (code == ISO_SECTION_KEY_CODE).then(|| "^".into()),
|
||
..Default::default()
|
||
};
|
||
let binding =
|
||
captured(capture.key_down(ISO_SECTION_KEY_CODE, false, None, LEFT_OPTION)).unwrap();
|
||
assert_eq!(
|
||
binding.key,
|
||
Some(HotkeyKey {
|
||
code: ISO_SECTION_KEY_CODE,
|
||
label: "^".into()
|
||
})
|
||
);
|
||
assert!(
|
||
binding
|
||
.runtime()
|
||
.matches_key_press(ISO_SECTION_KEY_CODE, LEFT_OPTION)
|
||
);
|
||
|
||
let mut none = ChordCapture::default();
|
||
assert_eq!(
|
||
captured(none.key_down(ISO_SECTION_KEY_CODE, false, None, LEFT_OPTION)),
|
||
Err(tr("Unsupported key"))
|
||
);
|
||
assert_eq!(
|
||
hotkey_key(ISO_SECTION_KEY_CODE, None, || None),
|
||
Err(tr("Unsupported key"))
|
||
);
|
||
assert_eq!(
|
||
hotkey_key(ISO_SECTION_KEY_CODE, None, || Some("^".into()))
|
||
.unwrap()
|
||
.label,
|
||
"^"
|
||
);
|
||
}
|
||
|
||
/// The microphone key reaches the recorder through the listener's session
|
||
/// tap: macOS takes the key before the window's own key events, so
|
||
/// `recorded_microphone_key` is the whole capture for it, and it records
|
||
/// the same chord the monitor would.
|
||
#[test]
|
||
fn the_microphone_key_records_from_the_listener_tap() {
|
||
let mut capture = ChordCapture::default();
|
||
let press = crate::suppression::dictation_key::CapturedMicrophoneKey {
|
||
code: MICROPHONE_KEY_CODE,
|
||
repeat: false,
|
||
flags: FUNCTION,
|
||
};
|
||
let binding = captured(recorded_microphone_key(&mut capture, press)).unwrap();
|
||
assert!(binding.modifiers.is_empty());
|
||
assert_eq!(
|
||
binding.key,
|
||
Some(HotkeyKey {
|
||
code: MICROPHONE_KEY_CODE,
|
||
label: "Mic".into()
|
||
})
|
||
);
|
||
assert!(
|
||
recorded_microphone_key(
|
||
&mut ChordCapture::default(),
|
||
crate::suppression::dictation_key::CapturedMicrophoneKey {
|
||
repeat: true,
|
||
..press
|
||
}
|
||
)
|
||
.is_none(),
|
||
"a held key's repeat records nothing"
|
||
);
|
||
// One press records one chord, and its release records nothing more.
|
||
let mut capture = ChordCapture::default();
|
||
assert!(recorded_microphone_key(&mut capture, press).is_some());
|
||
assert!(
|
||
capture.flags_changed(0).is_none(),
|
||
"the release is not a modifier-only chord"
|
||
);
|
||
}
|
||
|
||
/// The recorder takes the microphone (dictation) key alone, as it takes
|
||
/// the section key; Labs › Microphone key alone is enforced by
|
||
/// `settings::validate` before saving, not by the recorder.
|
||
#[test]
|
||
fn the_microphone_key_records_alone() {
|
||
let mut capture = ChordCapture::default();
|
||
let binding =
|
||
captured(capture.key_down(MICROPHONE_KEY_CODE, false, None, FUNCTION)).unwrap();
|
||
assert!(binding.modifiers.is_empty());
|
||
assert_eq!(
|
||
binding.key,
|
||
Some(HotkeyKey {
|
||
code: MICROPHONE_KEY_CODE,
|
||
label: "Mic".into()
|
||
})
|
||
);
|
||
assert!(binding.uses_microphone_key());
|
||
assert!(
|
||
binding
|
||
.runtime()
|
||
.matches_key_press(MICROPHONE_KEY_CODE, FUNCTION)
|
||
);
|
||
assert_eq!(
|
||
hotkey_key(MICROPHONE_KEY_CODE, None, || None)
|
||
.unwrap()
|
||
.label,
|
||
"Mic"
|
||
);
|
||
assert_eq!(
|
||
hotkey_key_named("microphone").unwrap(),
|
||
HotkeyKey {
|
||
code: MICROPHONE_KEY_CODE,
|
||
label: "Mic".into()
|
||
}
|
||
);
|
||
assert_eq!(hotkey_key_named("mic").unwrap().code, MICROPHONE_KEY_CODE);
|
||
}
|
||
|
||
/// The recorder takes the ISO section key alone, as Linux's does; Labs ›
|
||
/// § key alone is enforced by `settings::validate` before saving,
|
||
/// not by the recorder.
|
||
#[test]
|
||
fn the_section_key_records_alone() {
|
||
let mut capture = ChordCapture {
|
||
layout_label: |_| Some("^".into()),
|
||
..Default::default()
|
||
};
|
||
let binding = captured(capture.key_down(ISO_SECTION_KEY_CODE, false, None, 0)).unwrap();
|
||
assert!(binding.modifiers.is_empty());
|
||
assert_eq!(
|
||
binding.key,
|
||
Some(HotkeyKey {
|
||
code: ISO_SECTION_KEY_CODE,
|
||
label: "^".into()
|
||
})
|
||
);
|
||
assert!(binding.is_lone_section_key());
|
||
assert!(binding.runtime().matches_key_press(ISO_SECTION_KEY_CODE, 0));
|
||
// Another key alone still needs a modifier.
|
||
assert_eq!(
|
||
captured(capture.key_down(0, false, Some("a"), 0)),
|
||
Err(tr("Add a modifier"))
|
||
);
|
||
}
|
||
|
||
/// Plain ⌫ and Forward Delete reach the window, which turns an optional
|
||
/// shortcut off with them; with a modifier they record as usual.
|
||
#[test]
|
||
fn delete_records_alone_for_the_window_to_turn_a_shortcut_off() {
|
||
let mut capture = ChordCapture::default();
|
||
for (code, flags) in [(51, 0), (117, FUNCTION)] {
|
||
let binding = captured(capture.key_down(code, false, None, flags)).unwrap();
|
||
assert!(binding.is_bare_erase(), "{code}");
|
||
}
|
||
let with_command = captured(capture.key_down(51, false, None, LEFT_COMMAND)).unwrap();
|
||
assert!(!with_command.is_bare_erase());
|
||
assert_eq!(with_command.modifiers.command, Some(ModifierSide::Either));
|
||
}
|
||
|
||
fn calendar_answer(granted: bool, status: CalendarAccess) -> AccessAnswer {
|
||
AccessAnswer {
|
||
granted,
|
||
status,
|
||
error: None,
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn a_granted_calendar_is_saved_connected_and_stays_connected() {
|
||
let mut feed = CalendarFeed::default();
|
||
let mut view = CalendarView::default();
|
||
let mut saved = false;
|
||
feed.answer(
|
||
&calendar_answer(true, CalendarAccess::NotDetermined),
|
||
&mut view,
|
||
|| {
|
||
saved = true;
|
||
Ok(())
|
||
},
|
||
);
|
||
assert!(saved);
|
||
assert_eq!(view.access, CalendarAccess::FullAccess);
|
||
assert_eq!(view.error, None);
|
||
// A snapshot read before the answer must not undo it.
|
||
let stale = CalendarSnapshot::default();
|
||
assert_eq!(
|
||
feed.shown(stale, &view, 0).access,
|
||
CalendarAccess::FullAccess
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn a_failed_save_shows_its_error() {
|
||
let mut feed = CalendarFeed::default();
|
||
let mut view = CalendarView::default();
|
||
feed.answer(
|
||
&calendar_answer(true, CalendarAccess::FullAccess),
|
||
&mut view,
|
||
|| Err("disk full".into()),
|
||
);
|
||
assert_eq!(view.error.as_deref(), Some("disk full"));
|
||
let shown = feed.shown(CalendarSnapshot::default(), &view, 0);
|
||
assert_eq!(shown.error.as_deref(), Some("disk full"));
|
||
}
|
||
|
||
#[test]
|
||
fn a_denied_calendar_is_not_saved_and_stays_blocked() {
|
||
let mut feed = CalendarFeed::default();
|
||
let mut view = CalendarView::default();
|
||
feed.answer(
|
||
&calendar_answer(false, CalendarAccess::NotDetermined),
|
||
&mut view,
|
||
|| panic!("a denial must not save"),
|
||
);
|
||
assert_eq!(view.access, CalendarAccess::Denied);
|
||
assert!(view.access.blocked());
|
||
let shown = feed.shown(CalendarSnapshot::default(), &view, 0);
|
||
assert_eq!(shown.access, CalendarAccess::Denied);
|
||
// System Settings granting it later is what the reader reports.
|
||
let granted = CalendarSnapshot {
|
||
access: CalendarAccess::FullAccess,
|
||
..CalendarSnapshot::default()
|
||
};
|
||
assert_eq!(
|
||
feed.shown(granted, &view, 0).access,
|
||
CalendarAccess::FullAccess
|
||
);
|
||
}
|
||
|
||
fn full_access() -> CalendarSnapshot {
|
||
CalendarSnapshot {
|
||
access: CalendarAccess::FullAccess,
|
||
..CalendarSnapshot::default()
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn granting_in_system_settings_after_a_refusal_connects() {
|
||
let mut feed = CalendarFeed::default();
|
||
let mut view = CalendarView::default();
|
||
feed.asked = true; // Connect was clicked.
|
||
feed.answer(
|
||
&calendar_answer(false, CalendarAccess::NotDetermined),
|
||
&mut view,
|
||
|| panic!("a denial must not save"),
|
||
);
|
||
let denied = CalendarSnapshot {
|
||
access: CalendarAccess::Denied,
|
||
..CalendarSnapshot::default()
|
||
};
|
||
feed.read(denied, false, &view, 0, || panic!("still denied"));
|
||
feed.opening_privacy_settings();
|
||
let mut saved = 0;
|
||
let shown = feed.read(full_access(), false, &view, 0, || {
|
||
saved += 1;
|
||
Ok(())
|
||
});
|
||
assert_eq!(saved, 1);
|
||
assert_eq!(shown.access, CalendarAccess::FullAccess);
|
||
// Saved once: the next read before the reload does not save again.
|
||
feed.read(full_access(), false, &view, 0, || panic!("saved twice"));
|
||
}
|
||
|
||
#[test]
|
||
fn a_grant_hex_did_not_ask_for_never_connects() {
|
||
let mut feed = CalendarFeed::default();
|
||
let shown = feed.read(full_access(), false, &CalendarView::default(), 0, || {
|
||
panic!("connecting needs the person's click")
|
||
});
|
||
assert_eq!(shown.access, CalendarAccess::FullAccess);
|
||
}
|
||
|
||
#[test]
|
||
fn a_decision_from_eventkit_replaces_the_remembered_answer() {
|
||
let mut feed = CalendarFeed::default();
|
||
let mut view = CalendarView::default();
|
||
feed.answer(
|
||
&calendar_answer(true, CalendarAccess::NotDetermined),
|
||
&mut view,
|
||
|| Ok(()),
|
||
);
|
||
feed.read(full_access(), true, &view, 0, || panic!("connected"));
|
||
// `tccutil reset` puts it back to not determined: Connect again.
|
||
let reset = feed.read(CalendarSnapshot::default(), true, &view, 0, || {
|
||
panic!("not granted")
|
||
});
|
||
assert_eq!(reset.access, CalendarAccess::NotDetermined);
|
||
}
|
||
|
||
#[test]
|
||
fn a_failed_request_keeps_connect_and_shows_the_error() {
|
||
let mut feed = CalendarFeed::default();
|
||
let mut view = CalendarView::default();
|
||
let answer = AccessAnswer {
|
||
granted: false,
|
||
status: CalendarAccess::NotDetermined,
|
||
error: Some("missing usage description".into()),
|
||
};
|
||
feed.answer(&answer, &mut view, || panic!("a failure must not save"));
|
||
assert_eq!(view.access, CalendarAccess::NotDetermined);
|
||
assert_eq!(view.error.as_deref(), Some("missing usage description"));
|
||
let shown = feed.shown(CalendarSnapshot::default(), &view, 0);
|
||
assert_eq!(shown.access, CalendarAccess::NotDetermined);
|
||
}
|
||
}
|