diff --git a/README.md b/README.md index 302e466..6421a55 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ cherry-pick and an undo all stop here instead of being thrown away. - [x] Force push with lease, and sending tags - [x] Pull four ways: fetch, merge, fast-forward only and rebase - [x] A column of branches and tags to the left of the graph +- [x] Every column of the graph as wide as you drag it, and it stays that way - [x] Search the history by message, author or hash - [x] A board of columns, worked out from the graph and storing nothing - [x] In review means a pull request is open, asked of the forge through diff --git a/crates/gitrust-core/src/lib.rs b/crates/gitrust-core/src/lib.rs index 083e1f4..9146e89 100644 --- a/crates/gitrust-core/src/lib.rs +++ b/crates/gitrust-core/src/lib.rs @@ -61,9 +61,10 @@ pub use remote_repo::RemoteRepo; pub use rewrite::{Plan, Step}; pub use repo::Repo; pub use settings::{ - Appearance, Caret, General, Panel, Settings, FEWEST_COMMITS, LARGEST_TERMINAL_FONT, - LONGEST_FETCH_WAIT, LOOSEST_TERMINAL_LINE, SHORTEST_TERMINAL, SMALLEST_TERMINAL_FONT, - TALLEST_TERMINAL, TIGHTEST_TERMINAL_LINE, + Appearance, Caret, General, GraphColumns, Panel, Settings, FEWEST_COMMITS, + LARGEST_TERMINAL_FONT, LONGEST_FETCH_WAIT, LOOSEST_TERMINAL_LINE, NARROWEST_COLUMN, + SHORTEST_TERMINAL, SMALLEST_TERMINAL_FONT, TALLEST_TERMINAL, TIGHTEST_TERMINAL_LINE, + WIDEST_COLUMN, }; pub use stash::StashEntry; pub use submodule::Sub; diff --git a/crates/gitrust-core/src/settings.rs b/crates/gitrust-core/src/settings.rs index b43fca9..39f12c3 100644 --- a/crates/gitrust-core/src/settings.rs +++ b/crates/gitrust-core/src/settings.rs @@ -171,6 +171,67 @@ pub const LOOSEST_TERMINAL_LINE: u32 = 500; pub const SHORTEST_TERMINAL: u32 = 90; pub const TALLEST_TERMINAL: u32 = 2_000; +/// The narrowest and the widest a column of the graph is remembered as, in +/// points. Under the first there is no room for a letter and the ellipsis +/// that says more were cut; the window decides how much of the second it +/// can have. +pub const NARROWEST_COLUMN: u32 = 32; +pub const WIDEST_COLUMN: u32 = 2_000; + +/// How wide the graph's columns were dragged, in points. +/// +/// Each is `None` until somebody drags it, and goes back to `None` when +/// they ask for it as it came. `None` is not a number written down in +/// advance: the branches' column measures the names it has to hold and the +/// lanes count the branches alive at once, so what they come as depends on +/// the repository. The message has no width of its own. It is what is left. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct GraphColumns { + pub refs: Option, + pub lanes: Option, + pub author: Option, + pub date: Option, + pub hash: Option, +} + +impl GraphColumns { + /// The columns by the name each goes under in the file. + fn named(&self) -> [(&'static str, Option); 5] { + [ + ("refs", self.refs), + ("lanes", self.lanes), + ("author", self.author), + ("date", self.date), + ("hash", self.hash), + ] + } + + /// Takes `author 140`. A column nobody has heard of, or a width that + /// is not a number, leaves everything as it was. + fn take(&mut self, value: &str) { + let Some((name, points)) = value.trim().split_once(' ') else { return }; + let Ok(points) = points.trim().parse::() else { return }; + let slot = match name { + "refs" => &mut self.refs, + "lanes" => &mut self.lanes, + "author" => &mut self.author, + "date" => &mut self.date, + "hash" => &mut self.hash, + _ => return, + }; + *slot = Some(points.clamp(NARROWEST_COLUMN, WIDEST_COLUMN)); + } + + /// Only the ones that were dragged: a column left alone is not in the + /// file, which is how it stays free to follow the repository. + fn render(&self) -> String { + self.named() + .into_iter() + .filter_map(|(name, width)| Some(format!("graph-column {name} {}\n", width?))) + .collect() + } +} + /// The fewest commits the graph will be asked to start with. Below this the /// history stops before most people have scrolled once. pub const FEWEST_COMMITS: usize = 500; @@ -404,6 +465,10 @@ pub struct Settings { pub terminal: Panel, /// How tall it is when all of it is showing, in points. pub terminal_height: u32, + /// How wide the graph's columns were dragged. One answer for the whole + /// window, like the terminal: it is how somebody likes to read a + /// history, not something about a repository. + pub columns: GraphColumns, /// The version somebody said not to mention again, as its tag. The /// one after it is mentioned: this is "not this one", not "never". pub skipped: String, @@ -420,6 +485,7 @@ impl Default for Settings { front: 0, terminal: Panel::default(), terminal_height: 280, + columns: GraphColumns::default(), skipped: String::new(), } } @@ -480,6 +546,7 @@ impl Settings { points.clamp(SHORTEST_TERMINAL, TALLEST_TERMINAL); } } + Some(("graph-column", value)) => settings.columns.take(value), Some(("skip-update", value)) => settings.skipped = value.trim().to_string(), Some((name, value)) => { settings.general.take(name, value); @@ -521,6 +588,7 @@ impl Settings { self.terminal.as_str(), self.terminal_height )); + text.push_str(&self.columns.render()); if !self.skipped.is_empty() { text.push_str(&format!("skip-update {}\n", self.skipped)); } @@ -680,6 +748,34 @@ mod tests { } } + #[test] + fn a_dragged_column_comes_back_as_wide_as_it_was_left() { + let columns = GraphColumns { + refs: Some(180), + lanes: None, + author: Some(64), + date: None, + hash: Some(90), + }; + let settings = Settings { columns, ..Settings::default() }; + let text = settings.render(); + assert_eq!(Settings::parse(&text).columns, columns); + // One that was never dragged is not written down at all, so it is + // still free to follow the repository. + assert!(!text.contains("graph-column lanes")); + assert!(!Settings::default().render().contains("graph-column")); + } + + #[test] + fn a_column_is_kept_inside_what_can_be_read() { + let columns = Settings::parse("graph-column author 3\ngraph-column refs 90000\n").columns; + assert_eq!(columns.author, Some(NARROWEST_COLUMN)); + assert_eq!(columns.refs, Some(WIDEST_COLUMN)); + // A column nobody has heard of, and a width that is not one. + let columns = Settings::parse("graph-column mood 80\ngraph-column date wide\n").columns; + assert_eq!(columns, GraphColumns::default()); + } + #[test] fn a_branch_name_with_a_space_in_it_is_not_taken() { let settings = Settings::parse("default-branch my branch\n"); diff --git a/crates/gitrust-ui/src/graph_view.rs b/crates/gitrust-ui/src/graph_view.rs index aced1ec..27ea3d9 100644 --- a/crates/gitrust-ui/src/graph_view.rs +++ b/crates/gitrust-ui/src/graph_view.rs @@ -6,16 +6,21 @@ //! //! The row is the unit of reading: the branches and tags in their column at //! the far left, then the lanes, then the message at full contrast, and -//! author, date and hash aligned right in fixed-width columns. It is those +//! author, date and hash aligned right in columns of their own. It is those //! columns that give the structure, so not one vertical line is needed. As //! the window narrows the columns withdraw in order of importance instead of //! colliding with each other. +//! +//! Every edge between two columns can be dragged, in the strip of names +//! above the rows or anywhere down the graph. The line that says so shows +//! only under the pointer. What a column was dragged to is the window's to +//! remember; double-clicking the edge forgets it. use chrono::{Local, TimeZone}; use eframe::egui::{ self, Align, Align2, Color32, CornerRadius, FontId, Pos2, Rect, RichText, Shape, Stroke, }; -use gitrust_core::{Graph, Oid, RefEntry, RefKind, Row, WorkingState}; +use gitrust_core::{Graph, GraphColumns, Oid, RefEntry, RefKind, Row, WorkingState}; use crate::refs_view::{self, Named, On, Pick}; use crate::ripple; @@ -27,12 +32,26 @@ const DOT_R: f32 = 4.5; const LINE_W: f32 = 2.0; const GUTTER: f32 = 12.0; -/// The right-hand columns, from the outside in. +/// The right-hand columns, from the outside in: how wide each comes until +/// somebody drags it. const PAD_R: f32 = 14.0; const HASH_W: f32 = 58.0; const DATE_W: f32 = 100.0; const AUTHOR_W: f32 = 116.0; const COL_GAP: f32 = 16.0; +/// The narrowest a column is dragged to. The lanes have a floor of their +/// own: one lane and its two gutters, because a graph with no lane in it is +/// a list. +const COL_MIN: f32 = gitrust_core::NARROWEST_COLUMN as f32; +const LANES_MIN: f32 = GUTTER * 2.0 + LANE_W; +/// The strip of column names above the rows. +const HEADER_H: f32 = 22.0; +/// How far either side of an edge the pointer still has hold of it. +const GRIP_REACH: f32 = 4.0; +/// How far into the lanes' gutter their two edges sit. On the seam itself +/// the first would touch the chips and the second the message's first +/// letter, and a click meant for either would move a column. +const GRIP_INSET: f32 = 5.0; /// Below this the message is cut so short that the column gets in the way /// more than it informs, and it withdraws. const SUMMARY_MIN: f32 = 200.0; @@ -144,6 +163,10 @@ pub struct Asked { /// knows somebody has reached the end of what was read, which is the /// moment to read more of it. pub bottom: bool, + /// Whether a column has just been let go of, or sent back to how it + /// came: the moment to write the widths down. Not while it is being + /// dragged, which is sixty times a second. + pub resized: bool, } /// A chip as it was actually drawn. @@ -181,6 +204,9 @@ pub fn show( // way it was before there was one: straight lanes, a flat band under the // row, and no frames asked for. waves: bool, + // How wide each column was dragged. Changed in here while one is being + // dragged; `Asked::resized` says when to write it down. + columns: &mut GraphColumns, ) -> Asked { if graph.rows.is_empty() { theme::empty(ui, "Open a repository to see its history"); @@ -205,14 +231,28 @@ pub fn show( let pending_label = theme::pending_label(work.staged.len(), work.unstaged.len()); // With a heavily branched history the lanes would eat the message's - // column, so past a certain point they are allowed to overlap. - let lanes = graph.lane_count.clamp(1, MAX_LANES); - let graph_w = GUTTER * 2.0 + lanes as f32 * LANE_W; + // column, so past a certain point they are allowed to overlap -- until + // somebody drags the column open, which can go as far as every lane + // there is and no further. + let lanes_w = |count: usize| GUTTER * 2.0 + count as f32 * LANE_W; + let lanes_auto = lanes_w(graph.lane_count.clamp(1, MAX_LANES)); + let lanes_full = lanes_w(graph.lane_count.max(1)); let mut clicked = None; let mut bottom = false; let mut chips: Vec = Vec::new(); + let mut laid = None; + + // The names go above the rows and do not scroll with them. The room is + // taken now and painted afterwards: where each name goes is only known + // once the rows have been laid out. + let gap = std::mem::replace(&mut ui.spacing_mut().item_spacing.y, 0.0); + let (header, _) = ui.allocate_exact_size( + egui::vec2(ui.available_width(), HEADER_H), + egui::Sense::hover(), + ); + ui.spacing_mut().item_spacing.y = gap; - egui::ScrollArea::vertical() + let scrolled = egui::ScrollArea::vertical() .id_salt("graph") .auto_shrink([false, false]) .show(ui, |ui| { @@ -227,8 +267,15 @@ pub fn show( // The branch column is measured before anything else: where // everything else starts depends on its width. - let refs_w = refs_width(ui, graph, rect.width()); - let lanes_left = rect.left() + refs_w; + let natural = Natural { + refs: refs_width(ui, graph, rect.width()), + lanes: lanes_auto, + lanes_full, + }; + let layout = Layout::of(rect.left(), rect.right(), natural, columns); + laid = Some(layout); + let lanes_left = layout.lanes_left(); + let graph_w = layout.lanes_w; // The lanes are painted against a strip of their own: that way a // history wider than the cap is cut off sharply instead of running @@ -344,8 +391,7 @@ pub fn show( ) }; - let text_left = lanes_left + graph_w; - let columns = Columns::for_width(text_left, rect.right()); + let text_left = layout.text_left(); // Bringing a row into view is asked for in here, which is where // we know which pixel it falls on. @@ -499,7 +545,7 @@ pub fn show( row_y(index), &mut chips, ); - draw_row(&painter, ui, row, text_left, row_y(index), &columns, dim); + draw_row(&painter, ui, row, row_y(index), &layout, dim); } } @@ -535,7 +581,294 @@ pub fn show( } }); - Asked { show: clicked, pick: picked, bottom } + // After the rows, so that an edge wins over the row under it: the edges + // are the last thing put in the pointer's way. + let resized = laid.is_some_and(|layout| { + let reach = Rect::from_min_max(header.min, scrolled.inner_rect.max); + edges(ui, header, reach, &layout, columns) + }); + + Asked { show: clicked, pick: picked, bottom, resized } +} + +/// One of the columns that has a width to drag. The message is not among +/// them: it is what is left once the others have taken theirs. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +enum Col { + Refs, + Lanes, + Author, + Date, + Hash, +} + +impl Col { + fn slot(self, columns: &mut GraphColumns) -> &mut Option { + match self { + Self::Refs => &mut columns.refs, + Self::Lanes => &mut columns.lanes, + Self::Author => &mut columns.author, + Self::Date => &mut columns.date, + Self::Hash => &mut columns.hash, + } + } +} + +/// What the repository asks of the two columns that measure themselves: +/// the branches' column by the names in it, the lanes by how many run side +/// by side -- as they come, and with none of them overlapping. +#[derive(Clone, Copy)] +struct Natural { + refs: f32, + lanes: f32, + lanes_full: f32, +} + +/// A right-hand column where it fell: its right edge, which is what its +/// text is set against, and its width. +#[derive(Clone, Copy, Debug, PartialEq)] +struct Slot { + edge: f32, + width: f32, +} + +/// An edge that can be dragged, and the one column it widens or narrows. +/// +/// The columns left of the message grow to the right and the ones right of +/// it grow to the left, so every edge moves exactly one column and the +/// message takes up the difference. That is why the edge stays under the +/// pointer whichever one it is. +#[derive(Clone, Copy, Debug, PartialEq)] +struct Grip { + col: Col, + /// Where the edge is now. + x: f32, + /// Where it would be with the column at no width at all. + anchor: f32, + /// Which way widens the column: 1 to the right, -1 to the left. + grows: f32, + lo: f32, + hi: f32, +} + +impl Grip { + /// How wide the column is with its edge at `x`, kept between the + /// narrowest it goes and what the message can spare. + fn width_at(&self, x: f32) -> u32 { + let width = self.grows * (x - self.anchor); + width.clamp(self.lo, self.hi.max(self.lo)).round() as u32 + } +} + +/// Where every column falls at the width there is. +#[derive(Clone, Copy, Debug, PartialEq)] +struct Layout { + left: f32, + refs_w: f32, + lanes_w: f32, + lanes_full: f32, + author: Option, + date: Option, + hash: Option, + summary_right: f32, + /// How much the message can still give up before a column would have + /// to withdraw to make room for it. A drag stops there: a column that + /// vanished because its neighbour was being widened would be the + /// window taking something away that nobody had touched. + slack: f32, +} + +impl Layout { + fn of(left: f32, right: f32, natural: Natural, columns: &GraphColumns) -> Self { + let width = right - left; + let dragged = |asked: Option, floor: f32| asked.map(|w| (w as f32).max(floor)); + + let lanes_full = natural.lanes_full.max(LANES_MIN); + let mut lanes_w = dragged(columns.lanes, LANES_MIN).unwrap_or(natural.lanes).min(lanes_full); + + // A column dragged wide in a wide window gives way in a narrow one + // before the message does, the same as the ones nobody dragged. A + // repository with no branch or tag in sight has no column to give + // a width to, whatever was asked. + let mut refs_w = natural.refs; + if let Some(asked) = dragged(columns.refs, COL_MIN).filter(|_| natural.refs > 0.0) { + refs_w = asked.min(width - lanes_w - SUMMARY_MIN).max(COL_MIN); + } + if columns.lanes.is_some() { + lanes_w = lanes_w.min(width - refs_w - SUMMARY_MIN).max(LANES_MIN); + } + + let text_left = left + refs_w + lanes_w; + let mut edge = right - PAD_R; + let (mut author, mut date, mut hash) = (None, None, None); + + // From the outside in, and each one only if the message has room left. + for (asked, default, slot) in [ + (columns.hash, HASH_W, &mut hash), + (columns.date, DATE_W, &mut date), + (columns.author, AUTHOR_W, &mut author), + ] { + let width = dragged(asked, COL_MIN).unwrap_or(default); + if edge - width - COL_GAP - text_left < SUMMARY_MIN { + break; + } + *slot = Some(Slot { edge, width }); + edge -= width + COL_GAP; + } + + Self { + left, + refs_w, + lanes_w, + lanes_full, + author, + date, + hash, + summary_right: edge - 8.0, + slack: (edge - text_left - SUMMARY_MIN).max(0.0), + } + } + + fn lanes_left(&self) -> f32 { + self.left + self.refs_w + } + + fn text_left(&self) -> f32 { + self.lanes_left() + self.lanes_w + } + + /// The edges there are to drag, left to right. A column that is not + /// showing has none: the branches' in a repository without any, and + /// whichever of the right-hand ones the window had no room for. + fn grips(&self) -> Vec { + let mut grips = Vec::new(); + + if self.refs_w > 0.0 { + grips.push(Grip { + col: Col::Refs, + x: self.lanes_left() + GRIP_INSET, + anchor: self.left + GRIP_INSET, + grows: 1.0, + lo: COL_MIN, + hi: self.refs_w + self.slack, + }); + } + grips.push(Grip { + col: Col::Lanes, + x: self.text_left() - GRIP_INSET, + anchor: self.lanes_left() - GRIP_INSET, + grows: 1.0, + lo: LANES_MIN, + hi: (self.lanes_w + self.slack).min(self.lanes_full), + }); + for (col, slot) in [(Col::Author, self.author), (Col::Date, self.date), (Col::Hash, self.hash)] { + let Some(slot) = slot else { continue }; + // Halfway across the gap between it and what is on its left. + let anchor = slot.edge - COL_GAP / 2.0; + grips.push(Grip { + col, + x: anchor - slot.width, + anchor, + grows: -1.0, + lo: COL_MIN, + hi: slot.width + self.slack, + }); + } + grips + } +} + +/// The strip of names, and the edges between the columns. +/// +/// Says whether the widths are to be written down: an edge was let go of, +/// or double-clicked back to how it came. +fn edges( + ui: &mut egui::Ui, + header: Rect, + reach: Rect, + layout: &Layout, + columns: &mut GraphColumns, +) -> bool { + let painter = ui.painter_at(reach); + painter.rect_filled(header, 0.0, theme::canvas()); + painter.hline(header.x_range(), header.bottom() - 0.5, Stroke::new(1.0, theme::hairline())); + + let grips = layout.grips(); + let font = theme::medium(10.5); + let y = header.center().y; + + // Each name is cut off at its own column's edges: a column dragged + // narrower than its name loses the end of the name, not its neighbour. + let name = |from: f32, to: f32, at: f32, align: Align2, text: &str| { + let room = Rect::from_min_max(egui::pos2(from, header.top()), egui::pos2(to, header.bottom())); + painter.with_clip_rect(room).text(Pos2::new(at, y), align, text, font.clone(), theme::faint()); + }; + + let lanes_left = layout.lanes_left(); + let text_left = layout.text_left(); + if layout.refs_w > 0.0 { + name(layout.left, lanes_left, layout.left + REFS_PAD, Align2::LEFT_CENTER, "Branch / tag"); + } + name(lanes_left + GRIP_INSET, text_left - GRIP_INSET, lanes_left + GUTTER, Align2::LEFT_CENTER, "Graph"); + name(text_left, layout.summary_right, text_left + 2.0, Align2::LEFT_CENTER, "Message"); + for (slot, text) in [(layout.author, "Author"), (layout.date, "Date"), (layout.hash, "Hash")] { + if let Some(slot) = slot { + name(slot.edge - slot.width, slot.edge, slot.edge, Align2::RIGHT_CENTER, text); + } + } + + let mut resized = false; + for grip in &grips { + let hold = Rect::from_min_max( + egui::pos2(grip.x - GRIP_REACH, reach.top()), + egui::pos2(grip.x + GRIP_REACH, reach.bottom()), + ); + let id = ui.id().with(("graph-column", grip.col)); + let response = ui.interact(hold, id, egui::Sense::click_and_drag()); + + // Where in the grip it was taken hold of, so that the edge does not + // jump to the pointer when the drag begins. Where the button went + // down, and not where the pointer is by now: a drag is only known + // to be one once it has gone a few points, and the edge has not + // moved in the meantime. + let taken = id.with("taken"); + if response.drag_started() { + if let Some(pos) = ui.input(|input| input.pointer.press_origin()) { + ui.data_mut(|data| data.insert_temp(taken, pos.x - grip.x)); + } + } + if response.dragged() { + let offset = ui.data(|data| data.get_temp::(taken)).unwrap_or(0.0); + if let Some(pos) = response.interact_pointer_pos() { + *grip.col.slot(columns) = Some(grip.width_at(pos.x - offset)); + } + } + if response.drag_stopped() { + resized = true; + } + // Back to how it came, which for the two that measure themselves + // is not a number anybody could have typed. + if response.double_clicked() { + *grip.col.slot(columns) = None; + resized = true; + } + + let held = response.dragged(); + if held || response.hovered() { + ui.ctx().set_cursor_icon(egui::CursorIcon::ResizeColumn); + let color = if held { theme::muted() } else { theme::faint() }; + painter.vline(grip.x, reach.y_range(), Stroke::new(1.0, color)); + } else { + // At rest only a tick in the strip of names says there is an + // edge here to take hold of. + painter.vline( + grip.x, + (header.top() + 6.0)..=(header.bottom() - 6.0), + Stroke::new(1.0, theme::tint(theme::faint(), 120)), + ); + } + } + resized } /// `swell` is what the wave is doing to this dot right now, as a share of its @@ -597,54 +930,17 @@ fn draw_working(painter: &egui::Painter, left: f32, y: f32, pending: &str) { } } -/// Where each right-hand column falls at the width there is. -struct Columns { - author: Option, - date: Option, - hash: Option, - summary_right: f32, -} - -impl Columns { - fn for_width(left: f32, right: f32) -> Self { - let mut edge = right - PAD_R; - let mut columns = Self { - author: None, - date: None, - hash: None, - summary_right: right - PAD_R, - }; - - // From the outside in, and each one only if the message has room left. - for (width, slot) in [ - (HASH_W, &mut columns.hash), - (DATE_W, &mut columns.date), - (AUTHOR_W, &mut columns.author), - ] { - if edge - width - COL_GAP - left < SUMMARY_MIN { - break; - } - *slot = Some(edge); - edge -= width + COL_GAP; - } - - columns.summary_right = edge - 8.0; - columns - } -} - fn draw_row( painter: &egui::Painter, ui: &egui::Ui, row: &Row, - left: f32, y: f32, - columns: &Columns, + layout: &Layout, dim: bool, ) { - let x = left + 2.0; + let x = layout.text_left() + 2.0; let summary_font = theme::body(13.0); - let summary = elide(ui, &row.commit.summary, columns.summary_right - x, &summary_font); + let summary = elide(ui, &row.commit.summary, layout.summary_right - x, &summary_font); painter.text( Pos2::new(x, y), Align2::LEFT_CENTER, @@ -653,18 +949,24 @@ fn draw_row( if dim { theme::faint() } else { theme::text() }, ); - if let Some(edge) = columns.author { + if let Some(Slot { edge, width }) = layout.author { let font = theme::body(11.5); painter.text( Pos2::new(edge, y), Align2::RIGHT_CENTER, - elide(ui, &row.commit.author, AUTHOR_W, &font), + elide(ui, &row.commit.author, width, &font), font, if dim { theme::faint() } else { theme::muted() }, ); } - if let Some(edge) = columns.date { + // The date and the hash may lean into the gap as far as the edge that + // is dragged, and are cut there. As they come the date is a few points + // wider than its column, and has always sat in the gap by that much. + let font = theme::mono(11.0); + let lean = COL_GAP / 2.0; + + if let Some(Slot { edge, width }) = layout.date { let when = Local .timestamp_opt(row.commit.time, 0) .single() @@ -673,18 +975,18 @@ fn draw_row( painter.text( Pos2::new(edge, y), Align2::RIGHT_CENTER, - when, - theme::mono(11.0), + elide(ui, &when, width + lean, &font), + font.clone(), theme::faint(), ); } - if let Some(edge) = columns.hash { + if let Some(Slot { edge, width }) = layout.hash { painter.text( Pos2::new(edge, y), Align2::RIGHT_CENTER, - &row.commit.short_id, - theme::mono(11.0), + elide(ui, &row.commit.short_id, width + lean, &font), + font, theme::faint(), ); } @@ -916,3 +1218,127 @@ fn named_of(row: &Row, head: Option<&str>) -> Option { kind: first.kind, }) } + +#[cfg(test)] +mod tests { + use super::*; + + const NATURAL: Natural = Natural { refs: 120.0, lanes: 80.0, lanes_full: 300.0 }; + + fn laid(right: f32, columns: &GraphColumns) -> Layout { + Layout::of(0.0, right, NATURAL, columns) + } + + /// Drags one edge by `by` points and lays the columns out again. + fn drag(right: f32, columns: &mut GraphColumns, col: Col, by: f32) -> (Grip, Layout) { + let before = laid(right, columns); + let grip = *before.grips().iter().find(|grip| grip.col == col).unwrap(); + *col.slot(columns) = Some(grip.width_at(grip.x + by)); + (grip, laid(right, columns)) + } + + #[test] + fn columns_nobody_dragged_fall_where_they_always_did() { + let layout = laid(1200.0, &GraphColumns::default()); + assert_eq!(layout.refs_w, 120.0); + assert_eq!(layout.lanes_w, 80.0); + assert_eq!(layout.hash, Some(Slot { edge: 1186.0, width: HASH_W })); + assert_eq!(layout.date, Some(Slot { edge: 1112.0, width: DATE_W })); + assert_eq!(layout.author, Some(Slot { edge: 996.0, width: AUTHOR_W })); + assert_eq!(layout.summary_right, 856.0); + } + + #[test] + fn a_dragged_edge_stays_under_the_pointer() { + for col in [Col::Refs, Col::Lanes, Col::Author, Col::Date, Col::Hash] { + for by in [-18.0, 23.0] { + let mut columns = GraphColumns::default(); + let (grip, after) = drag(1400.0, &mut columns, col, by); + let moved = after.grips().into_iter().find(|grip| grip.col == col).unwrap(); + assert_eq!(moved.x, grip.x + by, "{col:?} by {by}"); + } + } + } + + #[test] + fn an_edge_moves_its_own_column_and_the_message_alone() { + let mut columns = GraphColumns::default(); + let before = laid(1400.0, &columns); + let (_, after) = drag(1400.0, &mut columns, Col::Date, -40.0); + assert_eq!(after.date.unwrap().width, DATE_W + 40.0); + assert_eq!(after.hash, before.hash); + assert_eq!(after.author.unwrap().width, AUTHOR_W); + assert_eq!(after.summary_right, before.summary_right - 40.0); + assert_eq!(columns.author, None); + } + + #[test] + fn widening_one_column_never_costs_another_its_place() { + for col in [Col::Refs, Col::Lanes, Col::Author, Col::Date, Col::Hash] { + let mut columns = GraphColumns::default(); + let (grip, after) = drag(1000.0, &mut columns, col, grip_far(col)); + assert!(after.author.is_some() && after.date.is_some() && after.hash.is_some(), "{col:?}"); + assert!(after.summary_right + 8.0 - after.text_left() >= SUMMARY_MIN, "{col:?}"); + // And it did get wider, as far as there was room for. + assert!(col.slot(&mut columns).unwrap() as f32 > grip.lo, "{col:?}"); + } + } + + /// Further than any window is wide, in the direction that widens it. + fn grip_far(col: Col) -> f32 { + match col { + Col::Refs | Col::Lanes => 5_000.0, + Col::Author | Col::Date | Col::Hash => -5_000.0, + } + } + + #[test] + fn no_column_is_dragged_shut() { + for col in [Col::Refs, Col::Lanes, Col::Author, Col::Date, Col::Hash] { + let mut columns = GraphColumns::default(); + drag(1400.0, &mut columns, col, -grip_far(col)); + let floor = if col == Col::Lanes { LANES_MIN } else { COL_MIN }; + assert_eq!(col.slot(&mut columns).unwrap() as f32, floor, "{col:?}"); + } + } + + #[test] + fn the_lanes_open_as_far_as_there_are_lanes_and_no_further() { + let mut columns = GraphColumns::default(); + let (_, after) = drag(2400.0, &mut columns, Col::Lanes, 5_000.0); + assert_eq!(after.lanes_w, NATURAL.lanes_full); + // What was asked for in a repository with many lanes is not an + // empty strip in one with two. + let two = Natural { refs: 120.0, lanes: 58.0, lanes_full: 58.0 }; + assert_eq!(Layout::of(0.0, 2400.0, two, &columns).lanes_w, 58.0); + } + + #[test] + fn a_column_dragged_wide_gives_way_when_the_window_narrows() { + let columns = GraphColumns { refs: Some(700), lanes: Some(300), ..GraphColumns::default() }; + let layout = laid(600.0, &columns); + assert!(layout.text_left() <= 600.0 - SUMMARY_MIN); + assert!(layout.refs_w >= COL_MIN && layout.lanes_w >= LANES_MIN); + // The right-hand ones withdraw, as they did before anybody could + // drag them. + assert_eq!((layout.author, layout.date, layout.hash), (None, None, None)); + } + + #[test] + fn a_repository_with_no_branch_in_sight_has_no_column_for_them() { + let bare = Natural { refs: 0.0, ..NATURAL }; + let columns = GraphColumns { refs: Some(200), ..GraphColumns::default() }; + let layout = Layout::of(0.0, 1200.0, bare, &columns); + assert_eq!(layout.refs_w, 0.0); + assert!(layout.grips().iter().all(|grip| grip.col != Col::Refs)); + } + + #[test] + fn a_column_the_window_has_no_room_for_has_no_edge_to_drag() { + let layout = laid(700.0, &GraphColumns::default()); + assert_eq!(layout.author, None); + assert!(layout.date.is_some()); + let cols: Vec = layout.grips().iter().map(|grip| grip.col).collect(); + assert_eq!(cols, vec![Col::Refs, Col::Lanes, Col::Date, Col::Hash]); + } +} diff --git a/crates/gitrust-ui/src/main.rs b/crates/gitrust-ui/src/main.rs index 31fe3d8..568857a 100644 --- a/crates/gitrust-ui/src/main.rs +++ b/crates/gitrust-ui/src/main.rs @@ -4816,7 +4816,13 @@ impl Open { self.job.is_some(), self.is_remote(), settings.waves, + &mut settings.columns, ); + // Written down when the edge is let go of, and not + // sixty times a second while it is being dragged. + if asked.resized { + let _ = settings.save(); + } if let Some(oid) = asked.show { self.select_commit(oid); } diff --git a/tools/poke.c b/tools/poke.c index 755277e..ea39aa5 100644 --- a/tools/poke.c +++ b/tools/poke.c @@ -20,6 +20,8 @@ * move X Y put the pointer there * click N 1 left, 2 middle, 3 right, 4 and 5 the wheel * double N twice, inside the time a double click is allowed + * press N the button goes down and stays down + * release N and comes back up: a drag is a press, moves, a release * sleep MS wait, so the program can answer * * Several in one go, in the order given. @@ -87,6 +89,10 @@ int main(int argc, char **argv) { xclick(dpy, b, 0, 0); xflush(dpy); nap(60); xclick(dpy, b, 1, 0); xflush(dpy); nap(30); xclick(dpy, b, 0, 0); + } else if (!strcmp(what, "press") && i < argc) { + xclick(dpy, (unsigned int)atoi(argv[i++]), 1, 0); + } else if (!strcmp(what, "release") && i < argc) { + xclick(dpy, (unsigned int)atoi(argv[i++]), 0, 0); } else if (!strcmp(what, "sleep") && i < argc) { xflush(dpy); nap(atol(argv[i++])); } else {