move URL highlight processing to the gui layer

This commit is contained in:
Wez Furlong committed 2020-01-05 14:37:17 -08:00
1 parent ad91742439
commit dfa2b08c72
4 files changed
+70 -54

No files matched your search

+54 -14
View File
@@ -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<LastMouseClick>,
/// The URL over which we are currently hovering
current_highlight: Option<Arc<Hyperlink>>,
}
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, &current_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, &current_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
+4 -10
View File
@@ -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>) -> (StableRowIndex, Vec<Line>);
/// Returns the currently highlighted hyperlink
fn current_highlight(&self) -> Option<Arc<Hyperlink>>;
/// 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>) -> (StableRowIndex, Vec<Line>) {
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<Arc<Hyperlink>> {
TerminalState::current_highlight(self)
}
fn get_dimensions(&self) -> RenderableDimensions {
let screen = self.screen();
RenderableDimensions {
+3 -15
View File
@@ -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<Future<UnitResponse>>,
poll_interval: Duration,
highlight: Arc<Mutex<Option<Arc<Hyperlink>>>>,
dirty_rows: RangeSet<StableRowIndex>,
fetch_pending: RangeSet<StableRowIndex>,
@@ -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<Arc<Hyperlink>> {
self.inner
.borrow()
.highlight
.lock()
.unwrap()
.as_ref()
.cloned()
}
fn get_dimensions(&self) -> RenderableDimensions {
self.inner.borrow().dimensions
}
+9 -15
View File
@@ -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<AtomicBool>,
highlight: Arc<Mutex<Option<Arc<Hyperlink>>>>,
local_sequence: SequenceNo,
dead: bool,
render_rx: Receiver<Vec<Change>>,
@@ -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<Arc<Hyperlink>> {
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<T: Send + 'static, F: Send + 'static + Fn(TermWizTerminal) -> 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)),