Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions crates/pullpen-ui/src/app/armed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,20 @@ impl App {
let mut out = vec![tabs(&tab, self.active, w, self.narrow(w), right), vec![]];
let (mut body, mut sel_line) = (Vec::new(), 0);
for (title, members) in groups.into_iter().filter(|(_, m)| !m.is_empty()) {
body.push(heading(&title, members.len()));
body.push((None, heading(&title, members.len())));
for (i, line) in members {
if i == sec.sel {
sel_line = body.len();
}
body.push(line);
body.push((Some(i), line));
}
body.push(vec![]);
body.push((None, vec![]));
}
let room = h.saturating_sub(4);
let start = sel_line.saturating_sub(room.saturating_sub(1));
out.extend(body.into_iter().skip(start).take(room));
let (hits, shown): (Vec<_>, Vec<Line>) = body.into_iter().skip(start).take(room).unzip();
self.set_hits(out.len(), hits);
out.extend(shown);
while out.len() < h.saturating_sub(2) {
out.push(vec![]);
}
Expand Down
15 changes: 13 additions & 2 deletions crates/pullpen-ui/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ pub struct App {
/// Seconds east of UTC, for showing clock times.
pub tz_offset: i64,
pub last_click: Option<(usize, Instant)>,
/// The item each screen row of the last frame showed, so a click lands
/// on what was drawn and not on a second copy of the layout.
pub(crate) hits: std::cell::RefCell<Vec<Option<usize>>>,
pub caps: HashMap<String, Capabilities>,
pub forges: Vec<Forge>,
pub theme: Theme,
Expand Down Expand Up @@ -265,6 +268,7 @@ impl App {
last_visit: None,
tz_offset: 0,
last_click: None,
hits: Default::default(),
caps: HashMap::new(),
forges: vec![Forge::GitHub, Forge::GitLab],
theme,
Expand Down Expand Up @@ -560,6 +564,7 @@ impl App {
status: self.status(w),
cmd: self.cmd_line(w),
};
self.hits.borrow_mut().clear();
let mut lines = match self
.pipe_lines(w, h)
.or_else(|| self.detail_lines(w, h))
Expand All @@ -568,7 +573,11 @@ impl App {
.or_else(|| self.merged_lines(w, h))
{
Some(l) => l,
None => list_screen(&screen, &ctx),
None => {
// Rows start under the tabs, a blank and the header.
self.set_hits(3, (sec.offset..rows.len()).take(body_rows(h)).map(Some));
list_screen(&screen, &ctx)
}
};
if let Some((menu, mw)) = self.menu() {
let bottom = lines.len().saturating_sub(2);
Expand Down Expand Up @@ -613,7 +622,9 @@ impl App {
let mut lines = crate::views::detail::detail_screen(&screen, &self.ctx());
if let (Some((filter, sel)), Some(files)) = (&d.picker, self.diffs.get(&d.id)) {
help::fade(&mut lines, h.saturating_sub(2));
help::place(&mut lines, &crate::views::detail::picker(files, filter, *sel, d.file, h), w);
let (panel, hits) = crate::views::detail::picker(files, filter, *sel, d.file, h);
help::place(&mut lines, &panel, w);
self.set_hits(help::TOP, hits);
}
Some(lines)
}
Expand Down
148 changes: 112 additions & 36 deletions crates/pullpen-ui/src/app/mouse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
//! The mouse, additive only (design §13): nothing here that a key cannot do.
//! A click finds what the last frame drew under it, then presses that key.

use std::time::{Duration, Instant};

use super::{App, Input};
use crate::views::detail::Pane;
use crate::views::list::body_rows;
use pullpen_core::View;

use super::{App, Effect, Input};
use crate::keymap::Key;
use crate::views::detail::{self, Pane};
use crate::views::list::PREVIEW_W;
use crate::views::pipeline::{self, JOB_TOP, steps};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Mouse {
Expand All @@ -14,52 +19,123 @@ pub enum Mouse {

/// Two clicks on one row this close together open it.
const DOUBLE: Duration = Duration::from_millis(400);
/// Rows above the list body: tabs, a blank, the header.
const LIST_TOP: u16 = 3;
/// Rows one notch of the wheel moves.
const WHEEL: usize = 3;

impl App {
pub fn mouse(&mut self, m: Mouse, now: Instant) {
/// Records which item each screen row from `top` down shows.
pub(super) fn set_hits(&self, top: usize, rows: impl IntoIterator<Item = Option<usize>>) {
let mut h = self.hits.borrow_mut();
*h = vec![None; top];
h.extend(rows);
}

pub fn mouse(&mut self, m: Mouse, now: Instant) -> Vec<Effect> {
// A prompt or a line being typed is answered by keys alone, so a
// stray click can neither confirm nor cancel it.
if !matches!(self.input, Input::Normal) {
return;
}
if let Some(d) = &mut self.detail {
if let Mouse::Scroll { x, down } = m {
let p = if usize::from(x) < 2 + 56 + 2 && !d.zoom { Pane::Overview } else { Pane::Diff };
let s = &mut d.scroll[p as usize];
*s = if down { *s + 3 } else { s.saturating_sub(3) };
}
return;
return vec![];
}
self.pending = None;
match m {
Mouse::Click { x, y: 0 } => {
if let Some(i) = self.tab_at(usize::from(x)) {
self.active = i;
}
}
Mouse::Click { y, .. } if y >= LIST_TOP && usize::from(y - LIST_TOP) < body_rows(self.height) => {
if self.sections[self.active].section.view == pullpen_core::View::Armed {
return;
Mouse::Click { x, y } => self.click(usize::from(x), usize::from(y), now),
Mouse::Scroll { x, down } => self.wheel(usize::from(x), down, now),
}
}

fn click(&mut self, x: usize, y: usize, now: Instant) -> Vec<Effect> {
let hit = self.hits.borrow().get(y).copied().flatten();
let h = self.height;
// Inside the two panes or on their headings, which sit a row above.
let panes = |top: usize| (top - 1..h.saturating_sub(2)).contains(&y);
let n_steps = self.open_job().map_or(0, |(_, j, _)| steps(j, self.current_log(), self.now).len());
// The key the click stands for, and the item it is on when the key
// waits for a second click.
let (key, item) = if let Some(p) = &mut self.pipe {
match (&mut p.job, hit) {
_ if y == 0 => (Key::Char('q'), None),
(Some(v), _) if panes(JOB_TOP) && x < 2 + pipeline::LW => {
v.steps = true;
let Some(i) = y.checked_sub(JOB_TOP).filter(|i| *i < n_steps) else { return vec![] };
v.step = i;
(Key::Enter, Some(i))
}
let i = self.section().offset + usize::from(y - LIST_TOP);
if i >= self.section().visible().len() {
return;
(Some(_), _) if panes(JOB_TOP) => (Key::Char('l'), None),
(None, Some(k)) => {
p.sel = k;
(Key::Enter, hit)
}
let double = self.last_click.is_some_and(|(j, at)| j == i && now.duration_since(at) < DOUBLE);
self.section_mut().sel = i;
self.last_click = Some((i, now));
if double {
self.open_detail(false);
_ => return vec![],
}
} else if let Some(d) = &mut self.detail {
match (&mut d.picker, hit) {
(Some((_, sel)), Some(r)) => {
*sel = r;
(Key::Enter, hit)
}
(Some(_), None) => return vec![],
_ if y == 0 => (Key::Char('q'), None),
_ if panes(detail::TOP) => (Key::Char(if x < 2 + detail::LW && !d.zoom { 'h' } else { 'l' }), None),
_ => return vec![],
}
Mouse::Scroll { down, .. } => {
let n = self.section().visible().len();
let s = self.section_mut();
s.sel = if down { (s.sel + 3).min(n.saturating_sub(1)) } else { s.sel.saturating_sub(3) };
} else if y == 0 {
if let Some(i) = self.tab_at(x) {
self.active = i;
}
Mouse::Click { .. } => {}
return vec![];
} else {
let Some(i) = hit.filter(|_| x < self.list_width()) else { return vec![] };
self.section_mut().sel = i;
(Key::Enter, hit)
};
match item {
Some(i) if !self.twice(i, now) => vec![],
_ => self.key(key, now),
}
}

/// Whether this is the second click on item `i` in quick succession.
fn twice(&mut self, i: usize, now: Instant) -> bool {
let double = self.last_click.is_some_and(|(j, at)| j == i && now.duration_since(at) < DOUBLE);
// Forgotten once used: the screen a double click opens has rows of
// its own, and a third click must not open one of those.
self.last_click = (!double).then_some((i, now));
double
}

/// The wheel scrolls the pane under the pointer, as the arrow keys
/// would if it had the focus, and leaves the focus where it was.
fn wheel(&mut self, x: usize, down: bool, now: Instant) -> Vec<Effect> {
let key = if down { Key::Down } else { Key::Up };
let roll = |a: &mut App| (0..WHEEL).flat_map(|_| a.key(key, now)).collect();
if let Some(v) = self.pipe.as_mut().and_then(|p| p.job.as_mut()) {
let was = std::mem::replace(&mut v.steps, x < 2 + pipeline::LW);
let out = roll(self);
if let Some(v) = self.pipe.as_mut().and_then(|p| p.job.as_mut()) {
v.steps = was;
}
return out;
}
if self.pipe.is_none()
&& let Some(d) = self.detail.as_mut().filter(|d| d.picker.is_none())
{
let over = if x < 2 + detail::LW && !d.zoom { Pane::Overview } else { Pane::Diff };
let was = std::mem::replace(&mut d.focus, over);
let out = roll(self);
if let Some(d) = &mut self.detail {
d.focus = was;
}
return out;
}
roll(self)
}

/// Where the list's rows end: the preview beside them is not a row.
fn list_width(&self) -> usize {
let w = self.last_width;
if self.view_is(self.active, View::List) && self.preview_shown(w) { w.saturating_sub(PREVIEW_W + 4) } else { w }
}

/// Which tab covers column `x`, laid out as `views::chrome::tabs` lays
/// them out.
fn tab_at(&self, x: usize) -> Option<usize> {
Expand Down
Loading
Loading