Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(desktop): major barriers to good performance (#4394)

Elijah Potter33b14410f7e4
diff --git a/harper-desktop/src-tauri/Cargo.lock b/harper-desktop/src-tauri/Cargo.lock
index ab22079ad..1bf478672 100644
--- a/harper-desktop/src-tauri/Cargo.lock
+++ b/harper-desktop/src-tauri/Cargo.lock
@@ -3828,6 +3828,7 @@ dependencies = [
"objc2-app-kit 0.3.2",
"objc2-foundation 0.3.2",
"pollster",
+ "send_wrapper",
"serde",
"serde_json",
"tauri",
@@ -7117,6 +7118,12 @@ dependencies = [
"serde_core",
]
+[[package]]
+name = "send_wrapper"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73"
+
[[package]]
name = "seq-macro"
version = "0.3.6"
diff --git a/harper-desktop/src-tauri/Cargo.toml b/harper-desktop/src-tauri/Cargo.toml
index 2b3ca7a63..c1339df26 100644
--- a/harper-desktop/src-tauri/Cargo.toml
+++ b/harper-desktop/src-tauri/Cargo.toml
@@ -51,6 +51,7 @@ is-macro = "0.3.7"
[target.'cfg(target_os = "macos")'.dependencies]
accessibility = "0.2.0"
+send_wrapper = "0.6.0"
core-foundation = "0.10.1"
objc2-app-kit = { version = "0.3.2", default-features = false, features = [
"std",
diff --git a/harper-desktop/src-tauri/src/highlighter/window.rs b/harper-desktop/src-tauri/src/highlighter/window.rs
index f03716658..1a7bdb87f 100644
--- a/harper-desktop/src-tauri/src/highlighter/window.rs
+++ b/harper-desktop/src-tauri/src/highlighter/window.rs
@@ -1,6 +1,7 @@
use std::num::NonZeroU32;
use std::sync::Arc;
+use egui_wgpu::wgpu::PresentMode;
use egui_wgpu::winit::Painter;
use egui_wgpu::{RendererOptions, WgpuConfiguration, WgpuSetup};
use winit::dpi::{PhysicalPosition, PhysicalSize};
@@ -64,6 +65,7 @@ impl Window {
let mut painter = Painter::new(
context,
WgpuConfiguration {
+ present_mode: PresentMode::Fifo,
wgpu_setup: WgpuSetup::from_display_handle(event_loop.owned_display_handle()),
..Default::default()
},
diff --git a/harper-desktop/src-tauri/src/highlighter/window_manager.rs b/harper-desktop/src-tauri/src/highlighter/window_manager.rs
index 81e67fafb..8600586ed 100644
--- a/harper-desktop/src-tauri/src/highlighter/window_manager.rs
+++ b/harper-desktop/src-tauri/src/highlighter/window_manager.rs
@@ -3,7 +3,6 @@ use std::time::{Duration, Instant};
use winit::application::ApplicationHandler;
use winit::event::{ElementState, MouseButton, WindowEvent};
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
-use winit::monitor::MonitorHandle;
#[cfg(target_os = "macos")]
use winit::platform::macos::{ActivationPolicy, EventLoopBuilderExtMacOS};
use winit::window::WindowId;
@@ -18,7 +17,6 @@ use super::window::Window;
use crate::os_broker::{LintText, OsBroker};
use crate::rect::ActionableLint;
-const DEFAULT_READ_INTERVAL: Duration = Duration::from_nanos(16_666_667);
const CONFIG_POLL_INTERVAL: Duration = Duration::from_secs(1);
/// Owns the winit event loop and the overlay windows created for each monitor.
@@ -95,9 +93,8 @@ impl WindowManager {
},
);
- self.event_loop.set_control_flow(ControlFlow::WaitUntil(
- Instant::now() + DEFAULT_READ_INTERVAL,
- ));
+ // Run continuously without a read timer; VSync presentation provides backpressure.
+ self.event_loop.set_control_flow(ControlFlow::Poll);
let result = self.event_loop.run_app(&mut app);
if let Some(error) = app.error {
@@ -114,8 +111,6 @@ struct WindowManagerApp {
render_state: RenderState,
os_broker: Box<dyn OsBroker>,
lint_text: LintText,
- read_interval: Duration,
- last_read: Instant,
last_config_poll: Instant,
refresh_config: RefreshConfig,
hovered_lint: Option<usize>,
@@ -132,7 +127,6 @@ impl WindowManagerApp {
os_broker: Box<dyn OsBroker>,
callbacks: WindowManagerCallbacks,
) -> Self {
- let read_interval = DEFAULT_READ_INTERVAL;
Self {
context,
windows: Vec::new(),
@@ -144,8 +138,6 @@ impl WindowManagerApp {
),
os_broker,
lint_text: callbacks.lint_text,
- read_interval,
- last_read: Instant::now() - read_interval,
last_config_poll: Instant::now(),
refresh_config: callbacks.refresh_config,
hovered_lint: None,
@@ -221,21 +213,11 @@ impl WindowManagerApp {
}
}
-fn monitor_refresh_interval(monitor: &MonitorHandle) -> Option<Duration> {
- monitor
- .refresh_rate_millihertz()
- .filter(|refresh_rate| *refresh_rate > 0)
- .map(|refresh_rate| Duration::from_nanos(1_000_000_000_000 / u64::from(refresh_rate)))
-}
-
impl ApplicationHandler for WindowManagerApp {
fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
let now = Instant::now();
- if now.duration_since(self.last_read) >= self.read_interval {
- self.read_rect_updates();
- self.last_read = now;
- }
+ self.read_rect_updates();
if now.duration_since(self.last_config_poll) >= CONFIG_POLL_INTERVAL {
self.refresh_config();
@@ -243,10 +225,6 @@ impl ApplicationHandler for WindowManagerApp {
}
self.update_cursor_hittest(event_loop);
-
- let next_read = self.last_read + self.read_interval;
- let next_config_poll = self.last_config_poll + CONFIG_POLL_INTERVAL;
- event_loop.set_control_flow(ControlFlow::WaitUntil(next_read.min(next_config_poll)));
}
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
@@ -256,10 +234,6 @@ impl ApplicationHandler for WindowManagerApp {
let monitors = event_loop.available_monitors().collect::<Vec<_>>();
- if let Some(refresh_interval) = monitors.iter().filter_map(monitor_refresh_interval).min() {
- self.read_interval = refresh_interval;
- }
-
for monitor in monitors {
match pollster::block_on(Window::new(event_loop, monitor, self.context.clone())) {
Ok(window) => self.windows.push(window),
diff --git a/harper-desktop/src-tauri/src/mac_broker/focused_target.rs b/harper-desktop/src-tauri/src/mac_broker/focused_target.rs
new file mode 100644
index 000000000..9c4fa0834
--- /dev/null
+++ b/harper-desktop/src-tauri/src/mac_broker/focused_target.rs
@@ -0,0 +1,22 @@
+use accessibility::ui_element::AXUIElement;
+use accessibility_sys::pid_t;
+
+/// The last externally focused application and its exact focused accessibility element.
+///
+/// The owned AX reference keeps the element handle alive while the overlay has focus, but does not
+/// prevent the source application from removing the element. A failed focus lookup leaves no element
+/// rather than retaining an older field or remembering an application-wide traversal fallback.
+#[derive(Clone)]
+pub struct FocusedTarget {
+ pub pid: pid_t,
+ pub element: Option<AXUIElement>,
+}
+
+impl FocusedTarget {
+ /// Selects the remembered element, allowing application-wide fallback only during external focus.
+ pub fn traversal_root(&self, highlighter_focused: bool) -> Option<AXUIElement> {
+ self.element
+ .clone()
+ .or_else(|| (!highlighter_focused).then(|| AXUIElement::application(self.pid)))
+ }
+}
diff --git a/harper-desktop/src-tauri/src/mac_broker/mod.rs b/harper-desktop/src-tauri/src/mac_broker/mod.rs
index 2a83a3317..a2085f769 100644
--- a/harper-desktop/src-tauri/src/mac_broker/mod.rs
+++ b/harper-desktop/src-tauri/src/mac_broker/mod.rs
@@ -4,13 +4,15 @@ mod accessibility_text;
mod app_catalog;
mod app_icons;
mod core_foundation_utilities;
+mod focused_target;
mod focused_window_pid;
mod window_stability;
use accessibility::TreeWalker;
use accessibility::ui_element::AXUIElement;
use accessibility_sys::{
- AXIsProcessTrusted, AXIsProcessTrustedWithOptions, kAXTrustedCheckOptionPrompt,
+ AXIsProcessTrusted, AXIsProcessTrustedWithOptions, kAXFocusedUIElementAttribute,
+ kAXTrustedCheckOptionPrompt,
};
use accessibility_sys::{error_string, pid_t};
use core_foundation::base::TCFType;
@@ -21,6 +23,7 @@ use core_graphics::event::CGEvent;
use core_graphics::event_source::{CGEventSource, CGEventSourceStateID};
use harper_core::linting::Lint;
use objc2_app_kit::NSRunningApplication;
+use send_wrapper::SendWrapper;
use std::process::Command;
use std::time::Duration;
use std::{
@@ -42,6 +45,8 @@ use self::accessibility_activation::{
set_enhanced_user_interface_preserving_previous, verify_accessibility_activation,
};
use self::accessibility_text::RectCollector;
+use self::core_foundation_utilities::ax_element_attribute;
+use self::focused_target::FocusedTarget;
use self::window_stability::{
WINDOW_MOVEMENT_SETTLE_DURATION, WindowMovementState, frontmost_window_frame_for_pid,
settled_window_state, window_frame_changed,
@@ -50,11 +55,12 @@ use self::window_stability::{
/// macOS implementation of the OS data the highlighter needs.
///
/// `MacBroker` owns focus memory because clicking the overlay can make the highlighter process the
-/// focused application. Remembering the last non-highlighter PID lets accessibility reads continue
-/// targeting the app the user was reviewing.
+/// focused application. Remembering the last non-highlighter PID and exact UI element lets
+/// accessibility reads continue targeting the field the user was reviewing.
pub struct MacBroker {
- /// The PID of the most recently focused PID, along with the time the measurement was taken.
- last_focused: Option<(pid_t, Instant)>,
+ /// Tauri requires a Send broker; retained AX handles must stay on their capturing thread.
+ /// The wrapper enforces that restriction for access and drop. Only the highlighter populates it.
+ last_focused: Option<SendWrapper<FocusedTarget>>,
integrations: Arc<Mutex<Vec<Integration>>>,
application_icon_cache: Mutex<HashMap<String, Vec<u8>>>,
window_movement: Option<WindowMovementState>,
@@ -72,27 +78,26 @@ impl MacBroker {
}
}
- /// The process ID of the currently focused window.
- /// In the interest of performance, the returned value may be slightly stale.
- fn target_pid(&mut self) -> Result<Option<pid_t>, Box<dyn StdError>> {
- if let Some((last_focused, measurement_time)) = self.last_focused
- && Instant::now().duration_since(measurement_time).as_secs() < 3
- {
- return Ok(Some(last_focused));
+ /// Refreshes the exact target during external focus and preserves it during overlay focus.
+ ///
+ /// The element lookup runs on every external-focus read, even within the same application.
+ /// A failed lookup clears the remembered element; it must not silently retain a different field.
+ fn resolve_target(
+ &mut self,
+ focused_pid: pid_t,
+ read_element: impl FnOnce(pid_t) -> Option<AXUIElement>,
+ ) -> Option<FocusedTarget> {
+ if focused_pid != std::process::id() as pid_t {
+ self.last_focused = Some(SendWrapper::new(FocusedTarget {
+ pid: focused_pid,
+ element: read_element(focused_pid),
+ }));
}
- let focused_pid = focused_window_pid::focused_window_pid()?;
- let current_pid = std::process::id() as pid_t;
-
- if focused_pid == current_pid {
- Ok(self.last_focused.map(|v| v.0))
- } else {
- self.last_focused = Some((focused_pid, Instant::now()));
- Ok(Some(focused_pid))
- }
+ self.last_focused.as_deref().cloned()
}
- /// Check if the fronmost window for a given process is currently moving.
+ /// Check if the frontmost window for a given process is currently moving.
fn window_is_moving(&mut self, pid: pid_t) -> bool {
let Some(frame) = frontmost_window_frame_for_pid(pid) else {
self.window_movement = None;
@@ -289,13 +294,8 @@ pub(super) type LintCallback<'a> = dyn FnMut(&str) -> BTreeMap<String, Vec<Lint>
impl OsBroker for MacBroker {
fn get_boxes(&mut self, lint_text: &mut LintCallback) -> Option<Vec<ActionableLint>> {
- let pid = match self.target_pid() {
- Ok(Some(pid)) => pid,
- Ok(None) => {
- self.window_movement = None;
- self.reset_accessibility_activation();
- return Some(Vec::new());
- }
+ let focused_pid = match focused_window_pid::focused_window_pid() {
+ Ok(pid) => pid,
Err(err) => {
self.window_movement = None;
self.reset_accessibility_activation();
@@ -303,6 +303,14 @@ impl OsBroker for MacBroker {
return None;
}
};
+ let Some(target) = self.resolve_target(focused_pid, |pid| {
+ ax_element_attribute(&AXUIElement::application(pid), kAXFocusedUIElementAttribute).ok()
+ }) else {
+ self.window_movement = None;
+ self.reset_accessibility_activation();
+ return Some(Vec::new());
+ };
+ let pid = target.pid;
let bundle_identifier = match bundle_identifier_for_pid(pid) {
Ok(Some(bundle_identifier)) => bundle_identifier,
@@ -345,10 +353,15 @@ impl OsBroker for MacBroker {
return None;
}
+ let Some(focused) = target.traversal_root(focused_pid == std::process::id() as pid_t)
+ else {
+ return Some(Vec::new());
+ };
+
let walker = TreeWalker::new();
let collector = RectCollector::new(lint_text);
- walker.walk(&el, &collector);
+ walker.walk(&focused, &collector);
collector.unwrap_rects()
}