Skip to content

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

refactor(desktop): remove default implementations of `OsBroker` (#4176)

Elijah Potter7d0b275fe132
diff --git a/harper-desktop/src-tauri/src/mac_broker/app_catalog.rs b/harper-desktop/src-tauri/src/mac_broker/app_catalog.rs
index 6e9255019..2843f407d 100644
--- a/harper-desktop/src-tauri/src/mac_broker/app_catalog.rs
+++ b/harper-desktop/src-tauri/src/mac_broker/app_catalog.rs
@@ -6,12 +6,12 @@ const APPLICATION_BUNDLE_CONTENT_TYPE: &str = "com.apple.application-bundle";
pub fn app_search_result_from_bundle_id(bundle_id: &str) -> AppSearchResult {
AppSearchResult {
- name: system_integration_display_name(bundle_id),
+ name: integration_display_name(bundle_id),
bundle_id: bundle_id.to_string(),
}
}
-pub fn system_integration_display_name(bundle_id: &str) -> String {
+pub fn integration_display_name(bundle_id: &str) -> String {
application_path_for_bundle_id(bundle_id)
.and_then(|path| display_name_from_app_path(&path))
.unwrap_or_else(|| bundle_id.to_owned())
diff --git a/harper-desktop/src-tauri/src/mac_broker/mod.rs b/harper-desktop/src-tauri/src/mac_broker/mod.rs
index 786c5c7fc..a853178b9 100644
--- a/harper-desktop/src-tauri/src/mac_broker/mod.rs
+++ b/harper-desktop/src-tauri/src/mac_broker/mod.rs
@@ -386,8 +386,8 @@ impl OsBroker for MacBroker {
}
}
- fn system_integration_display_name(&self, bundle_id: &str) -> String {
- app_catalog::system_integration_display_name(bundle_id)
+ fn integration_display_name(&self, bundle_id: &str) -> String {
+ app_catalog::integration_display_name(bundle_id)
}
fn application_icon_png(&self, bundle_id: &str) -> Result<Vec<u8>, String> {
@@ -430,6 +430,10 @@ impl OsBroker for MacBroker {
Ok(())
}
+ fn installed_application_bundle_ids(&self) -> Result<Vec<String>, String> {
+ app_catalog::installed_application_bundle_ids()
+ }
+
fn search_apps(&self, query: &str) -> Result<Vec<AppSearchResult>, String> {
let mut lock = self
.installed_app_search_index
diff --git a/harper-desktop/src-tauri/src/os_broker.rs b/harper-desktop/src-tauri/src/os_broker.rs
index 294feb287..3c91fddba 100644
--- a/harper-desktop/src-tauri/src/os_broker.rs
+++ b/harper-desktop/src-tauri/src/os_broker.rs
@@ -19,52 +19,43 @@ pub enum AccessibilityPermissionStatus {
/// those APIs are platform-specific. This trait keeps the event loop and renderer independent from
/// macOS accessibility and pointer APIs.
pub trait OsBroker {
+ /// Get the actionable lint boxes from the OS, provided a linting source.
fn get_boxes(
&mut self,
lint_text: &mut dyn FnMut(&str) -> BTreeMap<String, Vec<Lint>>,
) -> Vec<ActionableLint>;
+ /// Grab the position of the user's cursor on the screen.
fn cursor_position(&self) -> Option<egui::Pos2>;
- fn accessibility_permission_status(&self) -> AccessibilityPermissionStatus {
- AccessibilityPermissionStatus::Unsupported
- }
-
- fn request_accessibility_permission(&self) -> AccessibilityPermissionStatus {
- self.accessibility_permission_status()
- }
+ /// Check whether Harper has permission to access the OS' native accessibility API.
+ fn accessibility_permission_status(&self) -> AccessibilityPermissionStatus;
- fn system_integration_display_name(&self, bundle_id: &str) -> String {
- bundle_id.to_owned()
- }
+ /// Request permission to access the OS' native accessibility API.
+ fn request_accessibility_permission(&self) -> AccessibilityPermissionStatus;
+ /// Given an application identifier, find that integration's human-readable name.
fn integration_display_name(&self, bundle_id: &str) -> String {
- self.system_integration_display_name(bundle_id)
+ bundle_id.to_owned()
}
/// Returns the bundle identifiers for installed graphical applications.
///
/// Implementations should return stable bundle ID strings, sorted and deduplicated where
/// possible. Platforms that do not support bundle IDs should return an error.
- fn installed_application_bundle_ids(&self) -> Result<Vec<String>, String> {
- Err("Listing installed application bundle IDs is only supported on macOS.".to_string())
- }
+ fn installed_application_bundle_ids(&self) -> Result<Vec<String>, String>;
/// Returns the application icon for `bundle_id` encoded as PNG bytes.
///
/// The broker returns raw bytes so callers can choose their own transport format, such as a
/// Tauri command converting them into a data URL.
- fn application_icon_png(&self, _bundle_id: &str) -> Result<Vec<u8>, String> {
- Err("Reading application icons by bundle ID is only supported on macOS.".to_string())
- }
+ fn application_icon_png(&self, _bundle_id: &str) -> Result<Vec<u8>, String>;
- fn launch_app_bundle(&self, _bundle_id: &str) -> Result<(), String> {
- Err("Launching apps by bundle ID is only supported on macOS.".to_string())
- }
+ /// Start an application given its bundle ID.
+ fn launch_app_bundle(&self, _bundle_id: &str) -> Result<(), String>;
- fn search_apps(&self, _query: &str) -> Result<Vec<AppSearchResult>, String> {
- Err("App search is only supported on macOS.".to_string())
- }
+ /// Search for an application in the OS' global list of installed apps.
+ fn search_apps(&self, _query: &str) -> Result<Vec<AppSearchResult>, String>;
}
#[derive(Debug, Clone, Serialize)]
@@ -77,11 +68,9 @@ pub struct AppSearchResult {
///
/// This lets the highlighter compile on non-macOS platforms while making it explicit that there is
/// currently no accessibility or cursor integration there.
-#[cfg(not(target_os = "macos"))]
#[derive(Default)]
pub struct NoopBroker;
-#[cfg(not(target_os = "macos"))]
impl OsBroker for NoopBroker {
fn get_boxes(
&mut self,
@@ -93,4 +82,28 @@ impl OsBroker for NoopBroker {
fn cursor_position(&self) -> Option<egui::Pos2> {
None
}
+
+ fn accessibility_permission_status(&self) -> AccessibilityPermissionStatus {
+ AccessibilityPermissionStatus::Unsupported
+ }
+
+ fn request_accessibility_permission(&self) -> AccessibilityPermissionStatus {
+ AccessibilityPermissionStatus::Unsupported
+ }
+
+ fn installed_application_bundle_ids(&self) -> Result<Vec<String>, String> {
+ Ok(Vec::new())
+ }
+
+ fn application_icon_png(&self, _bundle_id: &str) -> Result<Vec<u8>, String> {
+ Err("Cannot get application icons.".to_string())
+ }
+
+ fn launch_app_bundle(&self, _bundle_id: &str) -> Result<(), String> {
+ Ok(())
+ }
+
+ fn search_apps(&self, _query: &str) -> Result<Vec<AppSearchResult>, String> {
+ Ok(Vec::new())
+ }
}
diff --git a/harper-desktop/src-tauri/src/windows_broker/automation_service.rs b/harper-desktop/src-tauri/src/windows_broker/automation_service.rs
index 3ecfb13e5..ddf37dec5 100644
--- a/harper-desktop/src-tauri/src/windows_broker/automation_service.rs
+++ b/harper-desktop/src-tauri/src/windows_broker/automation_service.rs
@@ -1,8 +1,7 @@
-use std::fmt::Arguments;
use std::iter::once;
-use std::sync::mpsc::{Receiver, Sender, SyncSender, TryRecvError, TrySendError, sync_channel};
+use std::sync::mpsc::{Receiver, SyncSender, TryRecvError, TrySendError, sync_channel};
use std::thread::{JoinHandle, sleep};
-use std::time::{Duration, Instant};
+use std::time::Duration;
use crate::rect::Rect;
use crate::windows_broker::get_focused_monitor_scale;
@@ -14,16 +13,12 @@ use uiautomation::{
patterns::{UITextPattern, UIValuePattern},
};
use windows::Win32::Foundation::HWND;
-use windows::Win32::Graphics::Gdi::{
- MONITOR_DEFAULTTONEAREST, MONITOR_DEFAULTTONULL, MonitorFromWindow,
-};
use windows::Win32::UI::Accessibility::IUIAutomationTextRange;
-use windows::Win32::UI::HiDpi::{GetDpiForMonitor, MDT_EFFECTIVE_DPI};
use windows::Win32::UI::WindowsAndMessaging::{GetForegroundWindow, GetWindowThreadProcessId};
/// Information about a worker thread.
struct WorkerData {
- thread_handle: JoinHandle<()>,
+ _thread_handle: JoinHandle<()>,
sender: SyncSender<(WorkerJob, Vec<JobArgument>)>,
receiver: Receiver<JobResult>,
}
@@ -136,7 +131,7 @@ impl AutomationService {
self.worker_data = Some(WorkerData {
receiver: result_receiver,
sender: job_sender,
- thread_handle: handle,
+ _thread_handle: handle,
});
}
@@ -232,6 +227,12 @@ impl AutomationService {
}
}
+impl Drop for AutomationService {
+ fn drop(&mut self) {
+ self.stop_worker_thread();
+ }
+}
+
fn apply_suggestion_job(state: &mut WorkerState, mut arguments: Vec<JobArgument>) -> JobResult {
let Some(JobArgument::ApplySuggestion(request)) = arguments.pop() else {
return JobResult::Err;
diff --git a/harper-desktop/src-tauri/src/windows_broker/mod.rs b/harper-desktop/src-tauri/src/windows_broker/mod.rs
index d0e5f662e..ac9ada350 100644
--- a/harper-desktop/src-tauri/src/windows_broker/mod.rs
+++ b/harper-desktop/src-tauri/src/windows_broker/mod.rs
@@ -1,21 +1,16 @@
-use std::{
- cell::RefCell,
- collections::{self, BTreeMap},
- rc::Rc,
-};
+use std::{cell::RefCell, collections::BTreeMap, rc::Rc};
use crate::windows_broker::automation_service::AutomationService;
-use crate::{os_broker::AccessibilityPermissionStatus, os_broker::OsBroker, rect::ActionableLint};
+use crate::{
+ os_broker::{AccessibilityPermissionStatus, AppSearchResult, OsBroker},
+ rect::ActionableLint,
+};
use egui::Pos2;
use harper_core::linting::Lint;
-use uiautomation::Result;
-use uiautomation::{UIAutomation, UIElement, UITreeWalker};
use windows::Win32::Foundation::POINT;
use windows::Win32::Graphics::Gdi::{MONITOR_DEFAULTTONEAREST, MonitorFromWindow};
use windows::Win32::UI::HiDpi::{GetDpiForMonitor, MDT_EFFECTIVE_DPI};
-use windows::Win32::UI::WindowsAndMessaging::{
- GetCursorPos, GetForegroundWindow, GetPhysicalCursorPos,
-};
+use windows::Win32::UI::WindowsAndMessaging::{GetCursorPos, GetForegroundWindow};
mod automation_service;
pub struct WindowsBroker {
@@ -106,6 +101,31 @@ impl OsBroker for WindowsBroker {
fn accessibility_permission_status(&self) -> AccessibilityPermissionStatus {
AccessibilityPermissionStatus::Granted
}
+
+ fn request_accessibility_permission(&self) -> AccessibilityPermissionStatus {
+ AccessibilityPermissionStatus::Granted
+ }
+
+ fn integration_display_name(&self, bundle_id: &str) -> String {
+ bundle_id.to_string()
+ }
+
+ fn installed_application_bundle_ids(&self) -> Result<Vec<String>, String> {
+ Ok(Vec::new())
+ }
+
+ fn application_icon_png(&self, _bundle_id: &str) -> Result<Vec<u8>, String> {
+ Err("Not supported".to_string())
+ }
+
+ fn launch_app_bundle(&self, _bundle_id: &str) -> Result<(), String> {
+ Ok(())
+ }
+
+ /// Search for an application in the OS' global list of installed apps.
+ fn search_apps(&self, _query: &str) -> Result<Vec<AppSearchResult>, String> {
+ Ok(Vec::new())
+ }
}
fn get_focused_monitor_scale() -> f64 {
@@ -116,7 +136,7 @@ fn get_focused_monitor_scale() -> f64 {
let mut x = 0;
let mut y = 0;
- GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &mut x, &mut y);
+ let _ = GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &mut x, &mut y);
let effective_scale = x as f64 / 96.;
effective_scale