Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(desktop): be able to apply suggestions (#4175)
Elijah Potter9ff1597a3073
diff --git a/harper-desktop/src-tauri/src/windows_broker/automation_service.rs b/harper-desktop/src-tauri/src/windows_broker/automation_service.rsindex 4a385c873..3ecfb13e5 100644--- a/harper-desktop/src-tauri/src/windows_broker/automation_service.rs+++ b/harper-desktop/src-tauri/src/windows_broker/automation_service.rs@@ -6,10 +6,13 @@ use std::time::{Duration, Instant};use crate::rect::Rect;use crate::windows_broker::get_focused_monitor_scale;-use harper_core::Span;+use harper_core::{Span, linting::Suggestion};use is_macro::Is;use uiautomation::types::{TextPatternRangeEndpoint, TextUnit};-use uiautomation::{UIAutomation, UIElement, patterns::UITextPattern};+use uiautomation::{+ UIAutomation, UIElement,+ patterns::{UITextPattern, UIValuePattern},+};use windows::Win32::Foundation::HWND;use windows::Win32::Graphics::Gdi::{MONITOR_DEFAULTTONEAREST, MONITOR_DEFAULTTONULL, MonitorFromWindow,@@ -25,15 +28,25 @@ struct WorkerData {receiver: Receiver<JobResult>,}+#[derive(Debug)]+struct ApplySuggestionRequest {+ window: isize,+ expected_text: String,+ span: Span<char>,+ suggestion: Suggestion,+}+#[derive(Debug, Is)]enum JobArgument {Span(Span<char>),Window(isize, bool),+ ApplySuggestion(ApplySuggestionRequest),}/// The result of a job run by the worker thread.#[derive(Debug, Is)]enum JobResult {+ None,String(String),GroupedRects(Vec<Vec<Rect>>),Err,@@ -159,6 +172,29 @@ impl AutomationService {}}+ pub fn apply_suggestion(+ &mut self,+ expected_text: String,+ span: Span<char>,+ suggestion: Suggestion,+ ) {+ let Some(window) = self.resolve_focused_window().map(|(window, _)| window) else {+ return;+ };++ let request = ApplySuggestionRequest {+ window,+ expected_text,+ span,+ suggestion,+ };++ let _ = self.run_worker_job(+ apply_suggestion_job,+ vec![JobArgument::ApplySuggestion(request)],+ );+ }+/// Pass a collection of text spans to the worker and have it compute the associated bounding boxes for each span./// Each span may have multiple bounding boxes./// Input spans share the same index as their output bounding box.@@ -196,6 +232,85 @@ impl AutomationService {}}+fn apply_suggestion_job(state: &mut WorkerState, mut arguments: Vec<JobArgument>) -> JobResult {+ let Some(JobArgument::ApplySuggestion(request)) = arguments.pop() else {+ return JobResult::Err;+ };++ if !arguments.is_empty() {+ return JobResult::Err;+ }++ let Some(element) = state.cached_element_for_window(request.window) else {+ eprintln!(+ "Unable to apply Windows suggestion: the source text element is no longer active"+ );+ return JobResult::None;+ };++ let Ok(current_text) = get_text(&element) else {+ state.clear_cached_element();+ eprintln!("Unable to apply Windows suggestion: the source text can no longer be read");+ return JobResult::None;+ };++ let updated_text = match apply_suggestion_to_text(+ ¤t_text,+ &request.expected_text,+ request.span,+ &request.suggestion,+ ) {+ Ok(updated_text) => updated_text,+ Err(error) => {+ eprintln!("Unable to apply Windows suggestion: {error}");+ return JobResult::None;+ }+ };++ let Ok(value_pattern) = element.get_pattern::<UIValuePattern>() else {+ eprintln!(+ "Unable to apply Windows suggestion: the text element has no writable value pattern"+ );+ return JobResult::None;+ };++ match value_pattern.is_readonly() {+ Ok(true) => {+ eprintln!("Unable to apply Windows suggestion: the text element is read-only");+ }+ Ok(false) => {+ if let Err(error) = value_pattern.set_value(&updated_text) {+ eprintln!("Unable to apply Windows suggestion: {error}");+ state.clear_cached_element();+ }+ }+ Err(error) => {+ eprintln!("Unable to determine whether the Windows text element is writable: {error}");+ }+ }++ JobResult::None+}++fn apply_suggestion_to_text(+ current_text: &str,+ expected_text: &str,+ span: Span<char>,+ suggestion: &Suggestion,+) -> std::result::Result<String, &'static str> {+ if current_text != expected_text {+ return Err("the source text changed after linting");+ }++ let mut chars = current_text.chars().collect::<Vec<_>>();+ if span.end > chars.len() {+ return Err("the lint span is outside the source text");+ }++ suggestion.apply(span, &mut chars);+ Ok(chars.into_iter().collect())+}+fn get_text(element: &UIElement) -> uiautomation::Result<String> {let pattern: UITextPattern = element.get_pattern()?;let range = pattern.get_document_range()?;diff --git a/harper-desktop/src-tauri/src/windows_broker/mod.rs b/harper-desktop/src-tauri/src/windows_broker/mod.rsindex e959f2573..d0e5f662e 100644--- a/harper-desktop/src-tauri/src/windows_broker/mod.rs+++ b/harper-desktop/src-tauri/src/windows_broker/mod.rs@@ -1,4 +1,8 @@-use std::collections::{self, BTreeMap};+use std::{+ cell::RefCell,+ collections::{self, BTreeMap},+ rc::Rc,+};use crate::windows_broker::automation_service::AutomationService;use crate::{os_broker::AccessibilityPermissionStatus, os_broker::OsBroker, rect::ActionableLint};@@ -15,13 +19,13 @@ use windows::Win32::UI::WindowsAndMessaging::{mod automation_service;pub struct WindowsBroker {- service: AutomationService,+ service: Rc<RefCell<AutomationService>>,}impl WindowsBroker {pub fn new() -> Self {Self {- service: AutomationService::create_and_start(),+ service: Rc::new(RefCell::new(AutomationService::create_and_start())),}}}@@ -31,7 +35,7 @@ impl OsBroker for WindowsBroker {&mut self,lint_text: &mut dyn FnMut(&str) -> BTreeMap<String, Vec<Lint>>,) -> Vec<ActionableLint> {- let text = self.service.get_text();+ let text = self.service.borrow_mut().get_text();if let Some(text) = text {if text.len() > 16_000 {return Vec::new();@@ -40,30 +44,40 @@ impl OsBroker for WindowsBroker {let lints = lint_text(text.as_str());let all_lint_iter = lints.values().map(|r| r.iter()).flatten();- let rects = self+ let Some(rects) = self.service- .get_bounding_boxes(all_lint_iter.map(|l| l.span));+ .borrow_mut()+ .get_bounding_boxes(all_lint_iter.map(|l| l.span))+ else {+ return Vec::new();+ };lints.into_iter().map(|(lint_id, lints)| lints.into_iter().map(move |l| (lint_id.clone(), l))).flatten()- .zip(rects.into_iter())+ .zip(rects).map(|((lint_id, lint), rects)| {let text = text.clone();- rects- .into_iter()- .map(|r| r.into_iter())- .flatten()- .map(move |r| {- ActionableLint::new(- r,- lint_id.clone(),- lint.clone(),- text.clone(),- |_| {},- )- })+ let service = Rc::clone(&self.service);+ rects.into_iter().map(move |r| {+ let service = Rc::clone(&service);+ let suggestion_text = text.clone();+ let suggestion_span = lint.span;+ ActionableLint::new(+ r,+ lint_id.clone(),+ lint.clone(),+ text.clone(),+ move |suggestion| {+ service.borrow_mut().apply_suggestion(+ suggestion_text,+ suggestion_span,+ suggestion,+ );+ },+ )+ })}).flatten().collect()@@ -85,6 +99,7 @@ impl OsBroker for WindowsBroker {point.x as f32 / monitor_scale as f32,point.y as f32 / monitor_scale as f32,);+Some(pos)}