make hyperlinks show underlined on hover

Adds some plumbing to track the current mouse position and
extract the hyperlink from the cell that is being hovered over.

We render those cells with underline, possibly in a different color.

We don't yet do anything on click.
This commit is contained in:
Wez Furlong committed 2018-02-11 12:11:02 -08:00
1 parent 1546c2ef0b
commit b6aa2cffea
5 files changed
+114 -18

No files matched your search

+4 -2
View File
@@ -360,7 +360,7 @@ impl Font {
}
} else if let Some(start) = first_fallback_pos {
// End of a fallback run
debug!("range: {:?}-{:?} needs fallback", start, pos);
//debug!("range: {:?}-{:?} needs fallback", start, pos);
let substr = &s[start..pos];
let mut shape = self.shape(font_idx + 1, substr)?;
@@ -384,12 +384,14 @@ impl Font {
// fallback run.
if let Some(start) = first_fallback_pos {
let substr = &s[start..];
debug!(
if false {
debug!(
"at end {:?}-{:?} needs fallback {}",
start,
s.len() - 1,
substr,
);
}
let mut shape = self.shape(font_idx + 1, substr)?;
// Fixup the cluster member to match our current offset
for info in shape.iter_mut() {
+21 -2
View File
@@ -656,10 +656,16 @@ impl<'a> TerminalWindow<'a> {
&term::color::ColorAttribute::Background,
);
let current_highlight = self.terminal.current_highlight();
// Break the line into clusters of cells with the same attributes
let cell_clusters = line.cluster();
for cluster in cell_clusters {
let attrs = &cluster.attrs;
let is_highlited_hyperlink = match (&attrs.hyperlink, &current_highlight) {
(&Some(ref this), &Some(ref highlight)) => this == highlight,
_ => false,
};
let style = self.fonts.match_style(attrs);
let metric_width = {
let font = self.fonts.cached_font(style)?;
@@ -773,7 +779,20 @@ impl<'a> TerminalWindow<'a> {
);
}
self.render_underline(x, base_y, cluster_width, attrs.underline(), glyph_color);
// Figure out what we're going to draw for the underline.
// If the current cell is part of the current URL highlight
// then we want to show the underline. If that text is already
// underlined we pick a different color for the underline to
// make it more distinct.
// TODO: make that highlight underline color configurable.
let (underline, under_color) = match (is_highlited_hyperlink, attrs.underline()) {
(true, Underline::None) => (Underline::Single, glyph_color),
(true, Underline::Single) => (Underline::Single, self.palette.cursor()),
(true, Underline::Double) => (Underline::Single, self.palette.cursor()),
(false, underline) => (underline, glyph_color),
};
self.render_underline(x, base_y, cluster_width, underline, under_color);
if attrs.strikethrough() {
self.render_strikethrough(x, y, base_y, cluster_width, glyph_color.into());
@@ -790,7 +809,7 @@ impl<'a> TerminalWindow<'a> {
self.buffer_image.borrow_mut().clear_rect(
x,
y,
self.width as usize - x as usize,
self.width.saturating_sub(x as u16) as usize,
self.cell_height,
background_color.into(),
);
+21 -2
View File
@@ -6,6 +6,7 @@ use super::*;
pub struct Line {
pub cells: Vec<Cell>,
dirty: bool,
has_hyperlink: bool,
}
/// A CellCluster is another representation of a Line.
@@ -49,7 +50,11 @@ impl Line {
pub fn new(cols: usize) -> Line {
let mut cells = Vec::with_capacity(cols);
cells.resize(cols, Default::default());
Line { cells, dirty: true }
Line {
cells,
dirty: true,
has_hyperlink: false,
}
}
/// Recompose line into the corresponding utf8 string.
@@ -106,7 +111,11 @@ impl Line {
cells.push(Cell::new(sub, attrs))
}
Line { cells, dirty: true }
Line {
cells,
dirty: true,
has_hyperlink: false,
}
}
#[inline]
@@ -123,6 +132,16 @@ impl Line {
pub fn set_clean(&mut self) {
self.dirty = false;
}
#[inline]
pub fn has_hyperlink(&self) -> bool {
self.has_hyperlink
}
#[inline]
pub fn set_has_hyperlink(&mut self, has: bool) {
self.has_hyperlink = has;
}
}
impl<'a> From<&'a str> for Line {
+4 -8
View File
@@ -79,14 +79,6 @@ impl Screen {
}
}
/// Clears the dirty flag for a line. The line is relative to the visible origin.
#[inline]
#[allow(dead_code)]
fn clean_line(&mut self, idx: VisibleRowIndex) {
let line_idx = self.phys_row(idx);
self.lines[line_idx].set_clean();
}
/// Returns a slice over the visible lines in the screen (no scrollback)
#[cfg(test)]
pub fn visible_lines(&self) -> &[Line] {
@@ -113,6 +105,10 @@ impl Screen {
attr
);
if attr.hyperlink.is_some() {
self.line_mut(line_idx).set_has_hyperlink(true);
}
let cells = &mut self.line_mut(line_idx).cells;
let width = cells.len();
// if the line isn't wide enough, pad it out with the default attributes
+64 -4
View File
@@ -44,8 +44,13 @@ pub struct TerminalState {
sgr_mouse: bool,
button_event_mouse: bool,
current_mouse_button: MouseButton,
mouse_position: CursorPosition,
cursor_visible: bool,
/// Which hyperlink is considered to be highlighted, because the
/// mouse_position is over a cell with a Hyperlink attribute.
current_highlight: Option<Rc<Hyperlink>>,
/// Keeps track of double and triple clicks
last_mouse_click: Option<LastMouseClick>,
@@ -92,6 +97,8 @@ impl TerminalState {
button_event_mouse: false,
cursor_visible: true,
current_mouse_button: MouseButton::None,
mouse_position: CursorPosition::default(),
current_highlight: None,
last_mouse_click: None,
viewport_offset: 0,
selection_range: None,
@@ -153,7 +160,53 @@ impl TerminalState {
self.selection_start = None;
}
fn hyperlink_for_cell(
&self,
x: usize,
y: ScrollbackOrVisibleRowIndex,
) -> Option<Rc<Hyperlink>> {
let screen = self.screen();
let idx = screen.scrollback_or_visible_row(y);
let line = &screen.lines[idx];
match line.cells.get(x) {
Some(cell) => cell.attrs.hyperlink.as_ref().cloned(),
None => None,
}
}
/// Invalidate rows that have hyperlinks
fn invalidate_hyperlinks(&mut self) {
let screen = self.screen_mut();
for line in screen.lines.iter_mut() {
if line.has_hyperlink() {
line.set_dirty();
}
}
}
/// Called after a mouse move or viewport scroll to recompute the
/// current highlight
fn recompute_highlight(&mut self) {
self.current_highlight = self.hyperlink_for_cell(
self.mouse_position.x,
self.mouse_position.y as ScrollbackOrVisibleRowIndex -
self.viewport_offset as ScrollbackOrVisibleRowIndex,
);
self.invalidate_hyperlinks();
}
pub fn mouse_event(&mut self, event: MouseEvent, host: &mut TerminalHost) -> Result<(), Error> {
// Remember the last reported mouse position so that we can use it
// for highlighting clickable things elsewhere.
let new_position = CursorPosition {
x: event.x,
y: event.y as VisibleRowIndex,
};
if new_position != self.mouse_position {
self.mouse_position = new_position;
self.recompute_highlight();
}
// First pass to figure out if we're messing with the selection
let send_event = self.sgr_mouse && !event.modifiers.contains(KeyModifiers::SHIFT);
@@ -309,7 +362,6 @@ impl TerminalState {
}
}
match event {
MouseEvent {
kind: MouseEventKind::Press,
@@ -595,6 +647,11 @@ impl TerminalState {
}
}
/// Returns the currently highlighted hyperlink
pub fn current_highlight(&self) -> Option<Rc<Hyperlink>> {
self.current_highlight.as_ref().cloned()
}
/// Sets the cursor position. x and y are 0-based and relative to the
/// top left of the visible screen.
/// TODO: DEC origin mode impacts the interpreation of these
@@ -633,10 +690,13 @@ impl TerminalState {
self.viewport_offset = position;
let top = self.screen().lines.len() - (rows + position as usize);
let screen = self.screen_mut();
for y in top..top + rows {
screen.line_mut(y).set_dirty();
{
let screen = self.screen_mut();
for y in top..top + rows {
screen.line_mut(y).set_dirty();
}
}
self.recompute_highlight();
}
/// Adjust the scroll position of the viewport by delta.