Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(desktop): only show the suggestion popup on the monitor being edited (#4651)
abdullah sultanbd95978ccb94
diff --git a/AGENTS.md b/AGENTS.mdindex 5cdc0c432..52f69ce83 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -216,7 +216,7 @@ Read the [Desktop contributor documentation](packages/web/src/routes/docs/contri### Known Review Findings-- Secondary-monitor highlighter coordinates may be wrong. Accessibility rectangles appear to be global screen coordinates, while each overlay window's egui origin is local to that monitor/window. Translate by monitor/window origin or render through one virtual-desktop-sized overlay.+- On Windows, the broker divides lint and cursor coordinates by the scale of the foreground window's monitor, while each overlay window's bounds use its own monitor's scale. With monitors at different scales, highlights can be misplaced on monitors other than the foreground window's.- macOS text range lookup may be wrong after emoji or other non-BMP characters. Harper spans are char-based, while macOS accessibility ranges are NSString/UTF-16 based. Convert spans before calling `AXBoundsForRangeParameterizedAttribute`.## On Writing New Rulesdiff --git a/harper-desktop/src-tauri/src/highlighter/render_state/mod.rs b/harper-desktop/src-tauri/src/highlighter/render_state/mod.rsindex 0bc49fc89..8a0953ff9 100644--- a/harper-desktop/src-tauri/src/highlighter/render_state/mod.rs+++ b/harper-desktop/src-tauri/src/highlighter/render_state/mod.rs@@ -173,8 +173,13 @@ impl RenderState {/// Draws highlights and the active popup from the same state used by hit-testing so visible/// regions and clickable regions do not drift apart.///+ /// Each monitor has its own window. `screen_rects` holds the bounds of every window, in the+ /// screen coordinates that lints and hit-testing use, and `window` is the index of the one being+ /// drawn. Lints are moved into that window's coordinates, and only one window draws the popup.+ ////// Each call also moves the sidebar toward its target by the time since the previous call.- pub fn render(&mut self, ui: &mut egui::Ui) {+ pub fn render(&mut self, ui: &mut egui::Ui, screen_rects: &[egui::Rect], window: usize) {+ let window_rect = screen_rects[window];let now = Instant::now();let time_since_last_render = self.last_render@@ -194,13 +199,18 @@ impl RenderState {}for positioned_lint in self.lints() {- draw_highlight(ui, &positioned_lint.rect, &positioned_lint.lint);+ draw_highlight(+ ui,+ &to_window_space(&positioned_lint.rect, window_rect),+ &positioned_lint.lint,+ );}if let Some(index) = self.highlighted_lint&& let Some(positioned_lint) = self.lints().get(index)+ && popup_window(screen_rects, &positioned_lint.rect) == Some(window){- let rect = positioned_lint.rect;+ let rect = to_window_space(&positioned_lint.rect, window_rect);let lint = positioned_lint.lint.clone();let source_text = positioned_lint.source_text.clone();@@ -289,8 +299,85 @@ fn rect_bounds(rect: &Rect) -> egui::Rect {)}+/// Moves a screen-space rectangle into the coordinates of the window at `window_rect`, whose+/// top-left corner egui draws at the origin.+fn to_window_space(rect: &Rect, window_rect: egui::Rect) -> Rect {+ Rect::new(+ rect.x - window_rect.min.x as f64,+ rect.y - window_rect.min.y as f64,+ rect.width,+ rect.height,+ )+}++/// Picks the index of the window that draws the popup for the lint at `lint_rect`: the window that+/// contains the lint's center, or the nearest window when the center is outside all of them.+///+/// Egui moves popups that would be outside a window back inside it, so exactly one window may draw+/// the popup. When the center is on the edge between two windows, the first one is picked.+fn popup_window(screen_rects: &[egui::Rect], lint_rect: &Rect) -> Option<usize> {+ let center = rect_bounds(lint_rect).center();++ screen_rects+ .iter()+ .map(|screen_rect| screen_rect.distance_sq_to_pos(center))+ .enumerate()+ .min_by(|(_, a), (_, b)| a.total_cmp(b))+ .map(|(index, _)| index)+}+fn lint_kind_color32(lint_kind: LintKind) -> egui::Color32 {let color = lint_kind_color(lint_kind);egui::Color32::from_rgb(color.r, color.g, color.b)}++#[cfg(test)]+mod tests {+ use super::*;++ /// A built-in display at the origin, with an external display to its left and above it.+ const BUILT_IN: egui::Rect =+ egui::Rect::from_min_max(egui::pos2(0.0, 0.0), egui::pos2(1512.0, 982.0));+ const EXTERNAL: egui::Rect =+ egui::Rect::from_min_max(egui::pos2(-3008.0, -392.0), egui::pos2(0.0, 1300.0));+ const SCREENS: [egui::Rect; 2] = [BUILT_IN, EXTERNAL];++ #[test]+ fn to_window_space_moves_rect_by_window_origin() {+ let rect = Rect::new(-2900.0, -300.0, 30.0, 12.0);++ assert_eq!(+ to_window_space(&rect, EXTERNAL),+ Rect::new(108.0, 92.0, 30.0, 12.0)+ );+ assert_eq!(to_window_space(&rect, BUILT_IN), rect);+ }++ #[test]+ fn popup_window_contains_lint() {+ let on_built_in = Rect::new(100.0, 100.0, 30.0, 12.0);+ let on_external = Rect::new(-2900.0, -300.0, 30.0, 12.0);++ assert_eq!(popup_window(&SCREENS, &on_built_in), Some(0));+ assert_eq!(popup_window(&SCREENS, &on_external), Some(1));+ }++ #[test]+ fn popup_window_is_nearest_when_lint_center_is_off_screen() {+ let below_built_in = Rect::new(100.0, 975.0, 30.0, 20.0);+ let left_of_external = Rect::new(-3030.0, 100.0, 30.0, 12.0);++ assert_eq!(popup_window(&SCREENS, &below_built_in), Some(0));+ assert_eq!(popup_window(&SCREENS, &left_of_external), Some(1));+ assert_eq!(popup_window(&[BUILT_IN], &left_of_external), Some(0));+ }++ #[test]+ fn popup_window_picks_one_window_for_lint_on_shared_edge() {+ let on_edge = Rect::new(-15.0, 100.0, 30.0, 12.0);++ assert_eq!(popup_window(&SCREENS, &on_edge), Some(0));+ assert_eq!(popup_window(&[EXTERNAL, BUILT_IN], &on_edge), Some(0));+ }+}diff --git a/harper-desktop/src-tauri/src/highlighter/render_state/popup_layout.rs b/harper-desktop/src-tauri/src/highlighter/render_state/popup_layout.rsindex fef95fc4a..9f9776d60 100644--- a/harper-desktop/src-tauri/src/highlighter/render_state/popup_layout.rs+++ b/harper-desktop/src-tauri/src/highlighter/render_state/popup_layout.rs@@ -14,8 +14,9 @@ pub const ANCHOR_GAP: f32 = 3.0;/// A viewport with no edges, so the cards are never clamped, flipped, or stacked.///-/// The highlighter does not yet know which monitor a popup is on. Until it does, both drawing and-/// hit-testing use this value, so the later change only has to replace it in one place.+/// The layout does not use the bounds of the popup's monitor yet. Both drawing and hit-testing use+/// this value, but drawing passes the anchor in window coordinates and hit-testing passes it in+/// screen coordinates, so a bounded viewport has to be given in the same coordinates as the anchor.pub const UNBOUNDED_VIEWPORT: egui::Rect = egui::Rect::EVERYTHING;/// The time constant, in seconds, of the sidebar's movement toward its target.diff --git a/harper-desktop/src-tauri/src/highlighter/window.rs b/harper-desktop/src-tauri/src/highlighter/window.rsindex 95651ef0a..2640a7f5e 100644--- a/harper-desktop/src-tauri/src/highlighter/window.rs+++ b/harper-desktop/src-tauri/src/highlighter/window.rs@@ -123,11 +123,34 @@ impl Window {}}- pub fn render(&mut self, render_state: &mut RenderState) {+ /// The window's bounds in the screen coordinates that lints and the cursor use.+ ///+ /// On platforms that cannot report where the window is, this assumes the window is at the+ /// screen's origin.+ pub fn screen_rect(&self) -> egui::Rect {+ let pixels_per_point =+ egui_winit::pixels_per_point(self.egui_state.egui_ctx(), &self.inner);++ egui_winit::inner_rect_in_points(&self.inner, pixels_per_point).unwrap_or_else(|| {+ let size = self.inner.inner_size();+ egui::Rect::from_min_size(+ egui::Pos2::ZERO,+ egui::vec2(size.width as f32, size.height as f32) / pixels_per_point,+ )+ })+ }++ /// Draws `render_state` into this window, which is `screen_rects[index]`.+ pub fn render(+ &mut self,+ render_state: &mut RenderState,+ screen_rects: &[egui::Rect],+ index: usize,+ ) {let context = self.egui_state.egui_ctx().clone();let input = self.egui_state.take_egui_input(&self.inner);let output = context.run_ui(input, |ui| {- render_state.render(ui);+ render_state.render(ui, screen_rects, index);});self.egui_statediff --git a/harper-desktop/src-tauri/src/highlighter/window_manager.rs b/harper-desktop/src-tauri/src/highlighter/window_manager.rsindex 7fe5cd0e9..f43a77f49 100644--- a/harper-desktop/src-tauri/src/highlighter/window_manager.rs+++ b/harper-desktop/src-tauri/src/highlighter/window_manager.rs@@ -260,16 +260,22 @@ impl ApplicationHandler for WindowManagerApp {});let should_render = matches!(&event, WindowEvent::RedrawRequested);+ let screen_rects = if should_render {+ self.windows.iter().map(Window::screen_rect).collect()+ } else {+ Vec::new()+ };- if let Some(window) = self+ if let Some((index, window)) = self.windows.iter_mut()- .find(|window| window.id() == window_id)+ .enumerate()+ .find(|(_, window)| window.id() == window_id){window.handle_event(&event);if should_render {- window.render(&mut self.render_state);+ window.render(&mut self.render_state, &screen_rects, index);}}