From dfa2b08c726a9b26febb6cebe0dd87956808bb41 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 5 Jan 2020 08:35:50 -0800 Subject: [PATCH] move URL highlight processing to the gui layer --- src/frontend/gui/termwindow.rs | 68 +++++++++++++++++++++++++++------- src/mux/renderable.rs | 14 ++----- src/server/tab.rs | 18 ++------- src/termwiztermtab.rs | 24 +++++------- 4 files changed, 70 insertions(+), 54 deletions(-) diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index 4bba1ae35..f79f359b1 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -36,6 +36,7 @@ use term::color::ColorPalette; use term::input::LastMouseClick; use term::{Line, StableRowIndex, Underline}; use termwiz::color::RgbColor; +use termwiz::hyperlink::Hyperlink; use termwiz::surface::CursorShape; #[derive(Debug, Clone, Copy)] @@ -148,6 +149,9 @@ pub struct TermWindow { /// Keeps track of double and triple clicks last_mouse_click: Option, + + /// The URL over which we are currently hovering + current_highlight: Option>, } struct Host<'a> { @@ -569,6 +573,7 @@ impl TermWindow { selection: Selection::default(), current_mouse_button: None, last_mouse_click: None, + current_highlight: None, }), )?; @@ -1657,7 +1662,6 @@ impl TermWindow { let dims = terminal.get_dimensions(); let num_cols = dims.cols; - let current_highlight = terminal.current_highlight(); let cursor_border_color = rgbcolor_to_window_color(palette.cursor_border); // Break the line into clusters of cells with the same attributes @@ -1666,7 +1670,7 @@ impl TermWindow { let config = configuration(); for cluster in cell_clusters { let attrs = &cluster.attrs; - let is_highlited_hyperlink = match (&attrs.hyperlink, ¤t_highlight) { + let is_highlited_hyperlink = match (&attrs.hyperlink, &self.current_highlight) { (&Some(ref this), &Some(ref highlight)) => Arc::ptr_eq(this, highlight), _ => false, }; @@ -1913,7 +1917,6 @@ impl TermWindow { let dims = terminal.get_dimensions(); let num_cols = dims.cols; - let current_highlight = terminal.current_highlight(); let cursor_border_color = rgbcolor_to_window_color(palette.cursor_border); // Break the line into clusters of cells with the same attributes @@ -1921,7 +1924,7 @@ impl TermWindow { let mut last_cell_idx = 0; for cluster in cell_clusters { let attrs = &cluster.attrs; - let is_highlited_hyperlink = match (&attrs.hyperlink, ¤t_highlight) { + let is_highlited_hyperlink = match (&attrs.hyperlink, &self.current_highlight) { (&Some(ref this), &Some(ref highlight)) => this == highlight, _ => false, }; @@ -2452,8 +2455,34 @@ impl TermWindow { event: &MouseEvent, context: &dyn WindowOps, ) { - // TODO: track current_highlight for ourselves - context.set_cursor(Some(if tab.renderer().current_highlight().is_some() { + let dims = tab.renderer().get_dimensions(); + let stable_row = dims.physical_top + y as StableRowIndex; + + let (top, mut lines) = tab.renderer().get_lines(stable_row..stable_row + 1); + let new_highlight = if top == stable_row { + let line = &mut lines[0]; + if let Some(cell) = line.cells().get(x) { + cell.attrs().hyperlink.as_ref().cloned() + } else { + None + } + } else { + None + }; + + match (self.current_highlight.as_ref(), new_highlight) { + (Some(old_link), Some(new_link)) if Arc::ptr_eq(&old_link, &new_link) => { + // Unchanged + } + (_, rhs) => { + // We're hovering over a different URL, so invalidate and repaint + // so that we render the underline correctly + self.current_highlight = rhs; + context.invalidate(); + } + }; + + context.set_cursor(Some(if self.current_highlight.is_some() { // When hovering over a hyperlink, show an appropriate // mouse cursor to give the cue that it is clickable MouseCursor::Hand @@ -2462,9 +2491,6 @@ impl TermWindow { })); if !tab.is_mouse_grabbed() || event.modifiers == Modifiers::SHIFT { - let dims = tab.renderer().get_dimensions(); - let stable_row = dims.physical_top + y as StableRowIndex; - match (&event.kind, self.last_mouse_click.as_ref()) { // Triple click to select a word ( @@ -2514,11 +2540,25 @@ impl TermWindow { .. }), ) => { - self.window - .as_ref() - .unwrap() - .set_clipboard(self.selection_text(&tab)); - context.invalidate(); + let text = self.selection_text(&tab); + + if text.is_empty() && self.current_highlight.is_some() { + // They clicked on a link, so let's open it! + // Ensure that we spawn the `open` call outside of the context + // of our window loop; on Windows it can cause a panic due to + // triggering our WndProc recursively. + let link = self.current_highlight.as_ref().unwrap().clone(); + promise::Future::with_executor(executor(), move || { + log::error!("clicking {}", link.uri()); + if let Err(err) = open::that(link.uri()) { + log::error!("failed to open {}: {:?}", link.uri(), err); + } + Ok(()) + }); + } else { + self.window.as_ref().unwrap().set_clipboard(text); + context.invalidate(); + } } // Dragging left mouse button diff --git a/src/mux/renderable.rs b/src/mux/renderable.rs index 070f2a452..160a5cb4d 100644 --- a/src/mux/renderable.rs +++ b/src/mux/renderable.rs @@ -1,9 +1,8 @@ +use crate::config::configuration; use downcast_rs::{impl_downcast, Downcast}; use serde::{Deserialize, Serialize}; use std::ops::Range; -use std::sync::Arc; -use term::{Line, StableRowIndex, Terminal, TerminalState}; -use termwiz::hyperlink::Hyperlink; +use term::{Line, StableRowIndex, Terminal}; /// Describes the location of the cursor #[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Deserialize, Serialize)] @@ -60,9 +59,6 @@ pub trait Renderable: Downcast { /// have its dirty bit set appropriately. fn get_lines(&mut self, lines: Range) -> (StableRowIndex, Vec); - /// Returns the currently highlighted hyperlink - fn current_highlight(&self) -> Option>; - /// Returns render related dimensions fn get_dimensions(&self) -> RenderableDimensions; } @@ -101,6 +97,7 @@ impl Renderable for Terminal { fn get_lines(&mut self, lines: Range) -> (StableRowIndex, Vec) { let screen = self.screen_mut(); let phys_range = screen.stable_range(&lines); + let config = configuration(); ( screen.phys_to_stable_row_index(phys_range.start), screen @@ -109,6 +106,7 @@ impl Renderable for Terminal { .skip(phys_range.start) .take(phys_range.end - phys_range.start) .map(|line| { + line.scan_and_create_hyperlinks(&config.hyperlink_rules); let cloned = line.clone(); line.clear_dirty(); cloned @@ -117,10 +115,6 @@ impl Renderable for Terminal { ) } - fn current_highlight(&self) -> Option> { - TerminalState::current_highlight(self) - } - fn get_dimensions(&self) -> RenderableDimensions { let screen = self.screen(); RenderableDimensions { diff --git a/src/server/tab.rs b/src/server/tab.rs index a8954beab..f2d939f5f 100644 --- a/src/server/tab.rs +++ b/src/server/tab.rs @@ -26,7 +26,6 @@ use term::{ Clipboard, KeyCode, KeyModifiers, Line, MouseButton, MouseEvent, MouseEventKind, StableRowIndex, TerminalHost, }; -use termwiz::hyperlink::Hyperlink; use termwiz::input::KeyEvent; struct MouseState { @@ -135,7 +134,6 @@ impl ClientTab { client: Arc::clone(client), remote_tab_id, }; - let highlight = Arc::new(Mutex::new(None)); let mouse = Arc::new(Mutex::new(MouseState { remote_tab_id, @@ -152,7 +150,6 @@ impl ClientTab { dead: false, poll_future: None, poll_interval: BASE_POLL_INTERVAL, - highlight, cursor_position: StableCursorPosition::default(), dirty_rows: RangeSet::new(), fetch_pending: RangeSet::new(), @@ -317,7 +314,6 @@ struct RenderableInner { dead: bool, poll_future: Option>, poll_interval: Duration, - highlight: Arc>>>, dirty_rows: RangeSet, fetch_pending: RangeSet, @@ -406,7 +402,9 @@ impl RenderableInner { let renderable = client_tab.renderable.borrow_mut(); let mut inner = renderable.inner.borrow_mut(); log::trace!("got {} lines", result.lines.len()); - for (stable_row, line) in result.lines.into_iter() { + let config = configuration(); + for (stable_row, mut line) in result.lines.into_iter() { + line.scan_and_create_hyperlinks(&config.hyperlink_rules); inner.lines.put(stable_row, line); inner.dirty_rows.add(stable_row); inner.fetch_pending.remove(stable_row); @@ -512,16 +510,6 @@ impl Renderable for RenderableState { result } - fn current_highlight(&self) -> Option> { - self.inner - .borrow() - .highlight - .lock() - .unwrap() - .as_ref() - .cloned() - } - fn get_dimensions(&self) -> RenderableDimensions { self.inner.borrow().dimensions } diff --git a/src/termwiztermtab.rs b/src/termwiztermtab.rs index efc3d666f..f4bcf10c5 100644 --- a/src/termwiztermtab.rs +++ b/src/termwiztermtab.rs @@ -3,6 +3,7 @@ //! input from the user as part of eg: setting up an ssh //! session. +use crate::config::configuration; use crate::font::FontConfiguration; use crate::frontend::{executor, front_end}; use crate::mux::domain::{alloc_domain_id, Domain, DomainId, DomainState}; @@ -21,11 +22,10 @@ use std::convert::TryInto; use std::ops::Range; use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use std::time::Duration; use term::color::ColorPalette; use term::{KeyCode, KeyModifiers, Line, MouseEvent, StableRowIndex, TerminalHost}; -use termwiz::hyperlink::Hyperlink; use termwiz::input::{InputEvent, KeyEvent}; use termwiz::lineedit::*; use termwiz::surface::{Change, SequenceNo, Surface}; @@ -34,7 +34,6 @@ use termwiz::terminal::{ScreenSize, Terminal, TerminalWaker}; struct RenderableInner { surface: Surface, something_changed: Arc, - highlight: Arc>>>, local_sequence: SequenceNo, dead: bool, render_rx: Receiver>, @@ -78,6 +77,8 @@ impl Renderable for RenderableState { // Reset the dirty bit inner.something_changed.store(false, Ordering::SeqCst); + let config = configuration(); + ( lines.start, inner @@ -86,7 +87,11 @@ impl Renderable for RenderableState { .into_iter() .skip(lines.start.try_into().unwrap()) .take((lines.end - lines.start).try_into().unwrap()) - .map(|line| line.into_owned()) + .map(|line| { + let mut line = line.into_owned(); + line.scan_and_create_hyperlinks(&config.hyperlink_rules); + line + }) .collect(), ) } @@ -117,16 +122,6 @@ impl Renderable for RenderableState { } } - fn current_highlight(&self) -> Option> { - self.inner - .borrow() - .highlight - .lock() - .unwrap() - .as_ref() - .cloned() - } - fn get_dimensions(&self) -> RenderableDimensions { let (cols, viewport_rows) = self.inner.borrow().surface.dimensions(); RenderableDimensions { @@ -406,7 +401,6 @@ pub fn run anyhow: let inner = RenderableInner { surface: Surface::new(width, height), - highlight: Arc::new(Mutex::new(None)), local_sequence: 0, dead: false, something_changed: Arc::new(AtomicBool::new(false)),