Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

feat(gui): give recording masks colours, edge handles, nudging and undo

RoversXe852a6b716ce
diff --git a/thinkterm-i18n/i18n/de-DE.ftl b/thinkterm-i18n/i18n/de-DE.ftl
index d76f9f2e..bd41338d 100644
--- a/thinkterm-i18n/i18n/de-DE.ftl
+++ b/thinkterm-i18n/i18n/de-DE.ftl
@@ -323,7 +323,10 @@ menu-clear-recording-overlay = Aufnahme-Masken löschen
recording-overlay-done = Fertig (Esc)
recording-overlay-delete = Auswahl löschen
recording-overlay-clear = Alle löschen
-recording-overlay-hint = Zum Hinzufügen freie Fläche ziehen · Maske ziehen zum Verschieben · Unten rechts Größe ändern
+recording-overlay-hint = Zum Hinzufügen freie Fläche ziehen · Maske ziehen zum Verschieben · Kante oder Ecke ziehen ändert die Größe · Pfeiltasten justieren · { $undo } macht rückgängig
+recording-overlay-follow-background = Wie der Terminalhintergrund
+recording-overlay-pick = Farbe aus dem Terminal aufnehmen
+recording-overlay-picking = Zelle anklicken, um ihren Hintergrund zu übernehmen · Esc bricht ab
recording-overlay-limit = 64 Masken erreicht · Zum Hinzufügen eine Maske löschen
recording-overlay-drag-toolbar = Leiste am Griff verschieben
menu-paste = Einfügen
diff --git a/thinkterm-i18n/i18n/en-US.ftl b/thinkterm-i18n/i18n/en-US.ftl
index f53525ae..136e598d 100644
--- a/thinkterm-i18n/i18n/en-US.ftl
+++ b/thinkterm-i18n/i18n/en-US.ftl
@@ -323,7 +323,10 @@ menu-clear-recording-overlay = Clear Recording Masks
recording-overlay-done = Done (Esc)
recording-overlay-delete = Delete Selected
recording-overlay-clear = Clear All
-recording-overlay-hint = Drag empty space to add · Drag a mask to move · Drag its lower-right corner to resize
+recording-overlay-hint = Drag empty space to add · Drag a mask to move · Drag an edge or corner to resize · Arrow keys nudge · { $undo } undoes
+recording-overlay-follow-background = Match the terminal background
+recording-overlay-pick = Pick a color from the terminal
+recording-overlay-picking = Click a cell to use its background · Esc to cancel
recording-overlay-limit = 64 masks reached · Delete a mask to add another
recording-overlay-drag-toolbar = Drag the handle to move the toolbar
menu-paste = Paste
diff --git a/thinkterm-i18n/i18n/fr-FR.ftl b/thinkterm-i18n/i18n/fr-FR.ftl
index 06e42294..d6e9b9eb 100644
--- a/thinkterm-i18n/i18n/fr-FR.ftl
+++ b/thinkterm-i18n/i18n/fr-FR.ftl
@@ -323,7 +323,10 @@ menu-clear-recording-overlay = Effacer les masques d’enregistrement
recording-overlay-done = Terminé (Échap)
recording-overlay-delete = Supprimer la sélection
recording-overlay-clear = Tout effacer
-recording-overlay-hint = Glisser sur une zone vide pour ajouter · Déplacer un masque · Redimensionner par le coin inférieur droit
+recording-overlay-hint = Glisser sur une zone vide pour ajouter · Déplacer un masque · Redimensionner par un bord ou un coin · Flèches pour ajuster · { $undo } pour annuler
+recording-overlay-follow-background = Suivre le fond du terminal
+recording-overlay-pick = Prélever une couleur dans le terminal
+recording-overlay-picking = Cliquez sur une cellule pour reprendre son fond · Échap pour annuler
recording-overlay-limit = Limite de 64 masques · Supprimez un masque pour en ajouter un
recording-overlay-drag-toolbar = Déplacer la barre par sa poignée
menu-paste = Coller
diff --git a/thinkterm-i18n/i18n/ja-JP.ftl b/thinkterm-i18n/i18n/ja-JP.ftl
index 163a671b..6b60a1d6 100644
--- a/thinkterm-i18n/i18n/ja-JP.ftl
+++ b/thinkterm-i18n/i18n/ja-JP.ftl
@@ -314,7 +314,10 @@ menu-clear-recording-overlay = 録画用のマスクを消去
recording-overlay-done = 完了(Esc)
recording-overlay-delete = 選択を削除
recording-overlay-clear = すべて消去
-recording-overlay-hint = 空白をドラッグして追加 · マスクをドラッグして移動 · 右下をドラッグしてサイズ変更
+recording-overlay-hint = 空白をドラッグして追加 · マスクをドラッグして移動 · 辺や角をドラッグしてサイズ変更 · 矢印キーで微調整 · { $undo } で取り消し
+recording-overlay-follow-background = ターミナルの背景に合わせる
+recording-overlay-pick = ターミナルから色を拾う
+recording-overlay-picking = セルをクリックしてその背景色を使う · Esc でキャンセル
recording-overlay-limit = 上限の64個に達しました · 追加するにはマスクを削除してください
recording-overlay-drag-toolbar = ハンドルをドラッグしてツールバーを移動
menu-paste = ペースト
diff --git a/thinkterm-i18n/i18n/zh-CN.ftl b/thinkterm-i18n/i18n/zh-CN.ftl
index 9cd40a95..1e0d624a 100644
--- a/thinkterm-i18n/i18n/zh-CN.ftl
+++ b/thinkterm-i18n/i18n/zh-CN.ftl
@@ -314,7 +314,10 @@ menu-clear-recording-overlay = 清除录屏遮挡
recording-overlay-done = 完成(Esc)
recording-overlay-delete = 删除选中
recording-overlay-clear = 全部清除
-recording-overlay-hint = 拖动空白处添加 · 拖动遮挡块移动 · 拖动右下角调整大小
+recording-overlay-hint = 拖动空白处添加 · 拖动遮挡块移动 · 拖动边或角调整大小 · 方向键微调 · { $undo } 撤销
+recording-overlay-follow-background = 跟随终端背景
+recording-overlay-pick = 从终端里取色
+recording-overlay-picking = 点终端里的一格,用它的底色 · Esc 取消
recording-overlay-limit = 已达到 64 块上限 · 删除一块后可继续添加
recording-overlay-drag-toolbar = 拖动把手移动工具条
menu-paste = 粘贴
diff --git a/wezterm-gui/src/termwindow/render/mod.rs b/wezterm-gui/src/termwindow/render/mod.rs
index b8e1879e..283964cf 100644
--- a/wezterm-gui/src/termwindow/render/mod.rs
+++ b/wezterm-gui/src/termwindow/render/mod.rs
@@ -1384,6 +1384,43 @@ fn resolve_fg_color_attr(
config: &ConfigHandle,
style: &config::TextStyle,
) -> LinearRgba {
+ resolve_fg_color_attr_srgb(attrs, fg, palette, config, style).to_linear()
+}
+
+/// A cell's colours as the line renderer draws them, before blinking: its
+/// foreground resolved as text is -- bold brightening, a font rule's colour
+/// -- and the two swapped where the cell's reverse attribute and the
+/// screen's reverse video disagree. The flag says the background is the
+/// pane's own ground.
+pub(crate) fn cell_fg_bg(
+ attrs: &CellAttributes,
+ palette: &ColorPalette,
+ config: &ConfigHandle,
+ style: &config::TextStyle,
+ reverse_video: bool,
+) -> (
+ wezterm_term::color::SrgbaTuple,
+ wezterm_term::color::SrgbaTuple,
+ bool,
+) {
+ let bg_is_default = attrs.background() == ColorAttribute::Default;
+ let bg = palette.resolve_bg(attrs.background());
+ let fg = resolve_fg_color_attr_srgb(attrs, attrs.foreground(), palette, config, style);
+ // Check the line reverse_video flag and flip.
+ if attrs.reverse() == !reverse_video {
+ (bg, fg, false)
+ } else {
+ (fg, bg, bg_is_default)
+ }
+}
+
+fn resolve_fg_color_attr_srgb(
+ attrs: &CellAttributes,
+ fg: ColorAttribute,
+ palette: &ColorPalette,
+ config: &ConfigHandle,
+ style: &config::TextStyle,
+) -> wezterm_term::color::SrgbaTuple {
match fg {
wezterm_term::color::ColorAttribute::Default => {
if let Some(fg) = style.foreground {
@@ -1408,7 +1445,6 @@ fn resolve_fg_color_attr(
}
_ => palette.resolve_fg(fg),
}
- .to_linear()
}
fn update_next_frame_time(storage: &mut Option<Instant>, next_due: Option<Instant>) {
diff --git a/wezterm-gui/src/termwindow/render/paint.rs b/wezterm-gui/src/termwindow/render/paint.rs
index 2dfb8d79..5b3cfc8a 100644
--- a/wezterm-gui/src/termwindow/render/paint.rs
+++ b/wezterm-gui/src/termwindow/render/paint.rs
@@ -163,11 +163,14 @@ pub(crate) struct PreviewGeometryKey {
dpi: usize,
}
+// The pane, its grid in the preview, the clip, its masks, and the ground a
+// mask that follows the pane's background takes there.
type PreviewRecordingRegion = (
mux::pane::PaneId,
RectF,
RectF,
crate::termwindow::ui::recording_overlay::PaneRecordingLayer,
+ LinearRgba,
);
/// One card's thumbnail, kept between frames.
@@ -2244,9 +2247,9 @@ impl crate::TermWindow {
mask_regions,
..
} = partial;
- for (_, grid, clip, masks) in &mask_regions {
- let rects = masks.preview_rects(*grid, *clip);
- self.occlude_recording_masks(&mut heap, &rects)?;
+ for (_, grid, clip, masks, ground) in &mask_regions {
+ let rects = masks.preview_rects(*grid, *clip, *ground);
+ self.occlude_recording_masks(&mut heap, &rects, None)?;
}
// Each rebuild slice reads the live layer. Never publish a recording
// whose masks changed while the earlier slices were being authored.
@@ -2677,7 +2680,7 @@ impl crate::TermWindow {
let grid_height = pane_height * (1.0 - nav_fraction);
if mask_regions
.last()
- .is_none_or(|(id, _, _, _)| *id != pane.pane_id)
+ .is_none_or(|(id, _, _, _, _)| *id != pane.pane_id)
{
let masks = pane.recording_layer.clone();
mask_regions.push((
@@ -2685,6 +2688,7 @@ impl crate::TermWindow {
euclid::rect(pane_rect.min_x(), grid_top, pane_width, grid_height),
pane_bounds,
masks,
+ palette.resolve_bg(ColorAttribute::Default).to_linear(),
));
}
@@ -4222,7 +4226,7 @@ impl crate::TermWindow {
}
self.paint_pane(&pos, &mut layers).context("paint_pane")?;
if let Ok(frame) = self.pane_mask_frame(&pos) {
- self.paint_pane_recording_masks(pos.pane.pane_id(), frame, &mut layers)?;
+ self.paint_pane_recording_masks(&pos, frame, &mut layers)?;
}
}
diff --git a/wezterm-gui/src/termwindow/render/screen_line.rs b/wezterm-gui/src/termwindow/render/screen_line.rs
index dd3f3a19..11129dab 100644
--- a/wezterm-gui/src/termwindow/render/screen_line.rs
+++ b/wezterm-gui/src/termwindow/render/screen_line.rs
@@ -1,6 +1,6 @@
use crate::quad::{QuadTrait, TripleLayerQuadAllocator, TripleLayerQuadAllocatorTrait};
use crate::termwindow::render::{
- resolve_fg_color_attr, same_hyperlink, update_next_frame_time, ClusterStyleCache,
+ cell_fg_bg, resolve_fg_color_attr, same_hyperlink, update_next_frame_time, ClusterStyleCache,
ComputeCellFgBgParams, ComputeCellFgBgResult, LineToElementParams, LineToElementShape,
RenderScreenLineParams, RenderScreenLineResult,
};
@@ -790,26 +790,16 @@ impl crate::TermWindow {
&params.render_metrics,
)?
.texture_coords();
- let bg_is_default = attrs.background() == ColorAttribute::Default;
- let bg_color = params.palette.resolve_bg(attrs.background()).to_linear();
-
- let fg_color = resolve_fg_color_attr(
- &attrs,
- attrs.foreground(),
- &params.palette,
- &params.config,
- style,
- );
let (fg_color, bg_color, bg_is_default) = {
- let mut fg = fg_color;
- let mut bg = bg_color;
- let mut bg_default = bg_is_default;
-
- // Check the line reverse_video flag and flip.
- if attrs.reverse() == !params.reverse_video {
- std::mem::swap(&mut fg, &mut bg);
- bg_default = false;
- }
+ let (fg, bg, bg_default) = cell_fg_bg(
+ &attrs,
+ &params.palette,
+ &params.config,
+ style,
+ params.reverse_video,
+ );
+ let mut fg = fg.to_linear();
+ let bg = bg.to_linear();
// Check for blink, and if this is the "not-visible"
// part of blinking then set fg = bg. This is a cheap
diff --git a/wezterm-gui/src/termwindow/ui/icons.rs b/wezterm-gui/src/termwindow/ui/icons.rs
index 8e1989f0..742a4277 100644
--- a/wezterm-gui/src/termwindow/ui/icons.rs
+++ b/wezterm-gui/src/termwindow/ui/icons.rs
@@ -71,6 +71,7 @@ pub enum SvgIcon {
Pencil,
Pin,
PinOff,
+ Pipette,
Plus,
Puzzle,
RotateCcw,
@@ -287,6 +288,7 @@ impl SvgIcon {
Self::Package => include_bytes!("../../../../third_party/lucide/icons/package.svg"),
Self::Pin => include_bytes!("../../../../third_party/lucide/icons/pin.svg"),
Self::PinOff => include_bytes!("../../../../third_party/lucide/icons/pin-off.svg"),
+ Self::Pipette => include_bytes!("../../../../third_party/lucide/icons/pipette.svg"),
Self::Unlink2 => include_bytes!("../../../../third_party/lucide/icons/unlink-2.svg"),
Self::Plus => include_bytes!("../../../../third_party/lucide/icons/plus.svg"),
Self::RotateCcw => {
@@ -790,6 +792,7 @@ mod tests {
SvgIcon::PanelRightOpen,
SvgIcon::Pin,
SvgIcon::PinOff,
+ SvgIcon::Pipette,
SvgIcon::Plus,
SvgIcon::Unlink2,
SvgIcon::RotateCcw,
diff --git a/wezterm-gui/src/termwindow/ui/recording_overlay.rs b/wezterm-gui/src/termwindow/ui/recording_overlay.rs
index b09a95b9..39782a77 100644
--- a/wezterm-gui/src/termwindow/ui/recording_overlay.rs
+++ b/wezterm-gui/src/termwindow/ui/recording_overlay.rs
@@ -1,21 +1,51 @@
//! Pane-owned recording masks, shared by terminal and preview rendering.
//! normal input passes through unless the user explicitly enters edit mode.
-use crate::quad::TripleLayerQuadAllocator;
+use crate::quad::{QuadTrait, TripleLayerQuadAllocator};
use crate::ui::{DrawContext, SvgIcon};
use crate::utilsprites::RenderMetrics;
+use config::{ConfigHandle, HsbTransform, TextStyle};
+use fluent_bundle::FluentArgs;
use mux::pane::PaneId;
-use std::collections::HashMap;
+use mux::tab::PositionedPane;
+use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, LazyLock, Mutex};
+use termwiz::cell::CellAttributes;
+use wezterm_term::color::ColorPalette;
+use wezterm_term::StableRowIndex;
use window::color::LinearRgba;
-use window::{KeyCode, KeyEvent, MouseCursor, MouseEvent, MouseEventKind, MousePress, WindowOps};
+use window::{
+ KeyCode, KeyEvent, Modifiers, MouseCursor, MouseEvent, MouseEventKind, MousePress, WindowOps,
+};
const MAX_MASKS: usize = 64;
const MIN_SIZE: f32 = 8.0;
-const HANDLE_SIZE: f32 = 16.0;
+// A selected mask's edges can be caught this far outside it and this far
+// inside, and its handles are drawn this big. UI units, like everything here:
+// 2x backing pixels (see `ui_scale_for_dpi`).
+const HANDLE_REACH: f32 = 12.0;
+const HANDLE_INWARD: f32 = 8.0;
+const HANDLE_SIZE: f32 = 14.0;
+// Edits that can be undone; kept only while editing.
+const UNDO_LIMIT: usize = 100;
+// What an arrow key moves the selection by, and with Shift: a point, or ten.
+const NUDGE: f32 = 2.0;
+const NUDGE_FAR: f32 = 20.0;
// Above content transitions, tooltips and the command palette (currently 11).
const OVERLAY_ZINDEX: i8 = 20;
+// The floating toolbar, before it is squeezed to fit.
+const BAR_HEIGHT: f32 = 80.0;
+const BAR_PAD: f32 = 16.0;
+const GRIP_WIDTH: f32 = 20.0;
+const SWATCH: f32 = 30.0;
+const SWATCH_GAP: f32 = 10.0;
+const DIVIDER_GAP: f32 = 16.0;
+const ICON: f32 = 28.0;
+const ICON_GAP: f32 = 10.0;
+const BUTTON_HEIGHT: f32 = 56.0;
+const BUTTON_PAD: f32 = 18.0;
+
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Rect {
x: f32,
@@ -24,6 +54,53 @@ struct Rect {
h: f32,
}
+/// What a mask is filled with. `Background` is the pane's own ground, so a
+/// mask over a prompt stays out of sight when the theme changes.
+#[derive(Clone, Copy, Debug, PartialEq)]
+enum MaskColor {
+ Background,
+ Srgb(u8, u8, u8),
+}
+
+impl Default for MaskColor {
+ fn default() -> Self {
+ BLACK
+ }
+}
+
+impl MaskColor {
+ /// Always opaque: a mask is drawn over the text it hides, so a ground
+ /// with any transparency in it would let that text through.
+ fn linear(self, ground: LinearRgba) -> LinearRgba {
+ match self {
+ Self::Background => LinearRgba::with_components(ground.0, ground.1, ground.2, 1.0),
+ Self::Srgb(r, g, b) => LinearRgba::with_srgba(r, g, b, 0xff),
+ }
+ }
+}
+
+const BLACK: MaskColor = MaskColor::Srgb(0, 0, 0);
+
+// The toolbar's colours, in order. The pipette takes any other.
+const SWATCHES: [MaskColor; 10] = [
+ MaskColor::Background,
+ BLACK,
+ MaskColor::Srgb(0x63, 0x63, 0x66),
+ MaskColor::Srgb(0xf2, 0xf2, 0xf7),
+ MaskColor::Srgb(0xff, 0x45, 0x3a),
+ MaskColor::Srgb(0xff, 0x9f, 0x0a),
+ MaskColor::Srgb(0xe5, 0xb4, 0x00),
+ MaskColor::Srgb(0x30, 0xd1, 0x58),
+ MaskColor::Srgb(0x0a, 0x84, 0xff),
+ MaskColor::Srgb(0xbf, 0x5a, 0xf2),
+];
+
+#[derive(Clone, Copy, Debug, PartialEq)]
+struct Mask {
+ rect: Rect,
+ color: MaskColor,
+}
+
// GUI-local state follows a pane between windows; no terminal or mux data changes.
// Rectangles are normalized to the pane frame, so previews reuse the same geometry.
#[derive(Clone, Debug, Default)]
@@ -31,7 +108,7 @@ pub(crate) struct PaneRecordingLayer(Arc<Mutex<PaneRecordingState>>);
#[derive(Debug, Default)]
struct PaneRecordingState {
- masks: Vec<Rect>,
+ masks: Vec<Mask>,
// The actual text grid, relative to the editable pane frame. This includes
// the renderer's padding, nav bar and effective per-pane font metrics.
grid: Option<Rect>,
@@ -53,11 +130,14 @@ impl PaneRecordingLayer {
self.0.lock().unwrap().revision
}
+ /// The masks over a preview of this pane, `Background` being the
+ /// preview's own `ground`.
pub(crate) fn preview_rects(
&self,
target_grid: window::RectF,
clip: window::RectF,
- ) -> Vec<window::RectF> {
+ ground: LinearRgba,
+ ) -> Vec<(window::RectF, LinearRgba)> {
let state = self.0.lock().unwrap();
if state.masks.is_empty() {
return Vec::new();
@@ -65,24 +145,29 @@ impl PaneRecordingLayer {
let Some(grid) = state.grid.filter(|grid| grid.w > 0.0 && grid.h > 0.0) else {
// A source pane that has not painted since its masks were created
// has no trustworthy grid mapping yet. Keep its preview covered.
- return vec![clip];
+ return vec![(clip, BLACK.linear(ground))];
};
state
.masks
.iter()
.filter_map(|mask| {
- Rect {
- x: (mask.x - grid.x) / grid.w,
- y: (mask.y - grid.y) / grid.h,
- w: mask.w / grid.w,
- h: mask.h / grid.h,
+ let rect = Rect {
+ x: (mask.rect.x - grid.x) / grid.w,
+ y: (mask.rect.y - grid.y) / grid.h,
+ w: mask.rect.w / grid.w,
+ h: mask.rect.h / grid.h,
}
.in_frame(target_grid)
- .intersection(&clip)
+ .intersection(&clip)?;
+ Some((rect, mask.color.linear(ground)))
})
.collect()
}
+ fn grid(&self) -> Option<Rect> {
+ self.0.lock().unwrap().grid
+ }
+
fn set_grid(&self, frame: window::RectF, grid: window::RectF) -> bool {
let grid = Rect {
x: (grid.min_x() - frame.min_x()) / frame.width().max(1.0),
@@ -125,7 +210,7 @@ pub(crate) fn pane_layer(pane_id: PaneId) -> PaneRecordingLayer {
.clone()
}
-fn save_pane_masks(pane_id: PaneId, masks: Vec<Rect>) {
+fn save_pane_masks(pane_id: PaneId, masks: Vec<Mask>) {
let layer = pane_layer(pane_id);
let mut state = layer.0.lock().unwrap();
if state.masks != masks {
@@ -134,6 +219,43 @@ fn save_pane_masks(pane_id: PaneId, masks: Vec<Rect>) {
}
}
+/// The colour a cell's background is drawn in, as a mask colour: the pane's
+/// own ground for a cell that has none of its own. Resolved as the line
+/// renderer resolves it -- reverse video, bold brightening and all -- so the
+/// pipette takes what is on screen.
+fn cell_mask_color(
+ attrs: &CellAttributes,
+ palette: &ColorPalette,
+ config: &ConfigHandle,
+ style: &TextStyle,
+ reverse_video: bool,
+) -> MaskColor {
+ let (_, drawn, own_ground) =
+ crate::termwindow::render::cell_fg_bg(attrs, palette, config, style, reverse_video);
+ if own_ground {
+ return MaskColor::Background;
+ }
+ let (r, g, b, _) = drawn.to_srgb_u8();
+ MaskColor::Srgb(r, g, b)
+}
+
+/// ⌘Z on macOS; Ctrl+Z elsewhere, where Super belongs to the window manager.
+fn undo_modifiers() -> Modifiers {
+ if cfg!(target_os = "macos") {
+ Modifiers::SUPER
+ } else {
+ Modifiers::CTRL
+ }
+}
+
+fn undo_shortcut() -> &'static str {
+ if cfg!(target_os = "macos") {
+ "⌘Z"
+ } else {
+ "Ctrl+Z"
+ }
+}
+
impl Rect {
fn normalized(self, size: (f32, f32)) -> Self {
Self {
@@ -174,16 +296,60 @@ impl Rect {
}
}
+ fn grown(self, by: f32) -> Self {
+ Self {
+ x: self.x - by,
+ y: self.y - by,
+ w: self.w + by * 2.0,
+ h: self.h + by * 2.0,
+ }
+ }
+
fn contains(self, p: (f32, f32)) -> bool {
p.0 >= self.x && p.0 <= self.x + self.w && p.1 >= self.y && p.1 <= self.y + self.h
}
- fn handle(self) -> Self {
+ /// The edges a press at `p` catches on this mask, once it is selected: a
+ /// band along each edge from HANDLE_REACH outside it to HANDLE_INWARD
+ /// inside, but never more than a quarter of the mask, so that even a
+ /// small one keeps a middle to be dragged by.
+ fn handle_at(self, p: (f32, f32)) -> Option<Handle> {
+ fn side(p: f32, start: f32, len: f32) -> Option<i8> {
+ let inward = HANDLE_INWARD.min(len / 4.0);
+ if p < start - HANDLE_REACH || p > start + len + HANDLE_REACH {
+ None
+ } else if p <= start + inward {
+ Some(-1)
+ } else if p >= start + len - inward {
+ Some(1)
+ } else {
+ Some(0)
+ }
+ }
+ let handle = Handle(side(p.0, self.x, self.w)?, side(p.1, self.y, self.h)?);
+ (handle != Handle(0, 0)).then_some(handle)
+ }
+
+ /// This rectangle with the edges `handle` names moved by `delta`, kept
+ /// inside `bounds` and no smaller than MIN_SIZE.
+ fn resized(self, handle: Handle, delta: (f32, f32), bounds: (f32, f32)) -> Self {
+ let (mut left, mut top) = (self.x, self.y);
+ let (mut right, mut bottom) = (self.x + self.w, self.y + self.h);
+ match handle.0 {
+ -1 => left = (left + delta.0).min(right - MIN_SIZE).max(0.0),
+ 1 => right = (right + delta.0).max(left + MIN_SIZE).min(bounds.0),
+ _ => {}
+ }
+ match handle.1 {
+ -1 => top = (top + delta.1).min(bottom - MIN_SIZE).max(0.0),
+ 1 => bottom = (bottom + delta.1).max(top + MIN_SIZE).min(bounds.1),
+ _ => {}
+ }
Self {
- x: self.x + self.w - HANDLE_SIZE.min(self.w),
- y: self.y + self.h - HANDLE_SIZE.min(self.h),
- w: HANDLE_SIZE.min(self.w),
- h: HANDLE_SIZE.min(self.h),
+ x: left,
+ y: top,
+ w: (right - left).max(0.0),
+ h: (bottom - top).max(0.0),
}
}
@@ -197,11 +363,27 @@ impl Rect {
}
}
+/// The edges a resize moves: -1 the left or top one, 1 the right or bottom
+/// one, 0 neither.
+#[derive(Clone, Copy, Debug, PartialEq)]
+struct Handle(i8, i8);
+
+impl Handle {
+ fn cursor(self) -> MouseCursor {
+ match self {
+ Handle(0, _) => MouseCursor::SizeUpDown,
+ Handle(_, 0) => MouseCursor::SizeLeftRight,
+ Handle(x, y) if x == y => MouseCursor::SizeNorthWestSouthEast,
+ _ => MouseCursor::SizeNorthEastSouthWest,
+ }
+ }
+}
+
#[derive(Clone, Copy, Debug)]
enum DragKind {
Create,
Move(usize, Rect),
- Resize(usize, Rect),
+ Resize(usize, Rect, Handle),
}
#[derive(Clone, Copy, Debug)]
@@ -211,21 +393,91 @@ struct Drag {
preview: Rect,
}
+#[derive(Clone, Copy, Debug, PartialEq)]
+enum Control {
+ Swatch(usize),
+ Pipette,
+ Delete,
+ Clear,
+ Done,
+}
+
+/// The toolbar laid out from its own top-left corner, in UI points.
+struct ToolbarLayout {
+ controls: Vec<(Control, Rect)>,
+ grip: Rect,
+ dividers: [f32; 2],
+ width: f32,
+}
+
+/// `labels` are the widths of the Delete, Clear and Done labels. Delete and
+/// Clear show theirs only `with_labels`; without, they are icons.
+fn toolbar_layout(labels: [f32; 3], with_labels: bool) -> ToolbarLayout {
+ let centred = |x: f32, w: f32, h: f32| Rect {
+ x,
+ y: (BAR_HEIGHT - h) / 2.0,
+ w,
+ h,
+ };
+ let mut controls = Vec::with_capacity(SWATCHES.len() + 4);
+ let mut x = BAR_PAD;
+ let grip = centred(x, GRIP_WIDTH, 32.0);
+ x += GRIP_WIDTH + ICON_GAP;
+ for index in 0..SWATCHES.len() {
+ controls.push((Control::Swatch(index), centred(x, SWATCH, SWATCH)));
+ x += SWATCH + SWATCH_GAP;
+ }
+ x += DIVIDER_GAP - SWATCH_GAP;
+ let first = x;
+ x += 1.0 + DIVIDER_GAP;
+ controls.push((Control::Pipette, centred(x, BUTTON_HEIGHT, BUTTON_HEIGHT)));
+ x += BUTTON_HEIGHT + DIVIDER_GAP;
+ let second = x;
+ x += 1.0 + DIVIDER_GAP;
+ for (control, label) in [(Control::Delete, labels[0]), (Control::Clear, labels[1])] {
+ let label = if with_labels { ICON_GAP + label } else { 0.0 };
+ let w = BUTTON_PAD * 2.0 + ICON + label;
+ controls.push((control, centred(x, w, BUTTON_HEIGHT)));
+ x += w + 8.0;
+ }
+ x += 8.0;
+ let w = BUTTON_PAD * 2.0 + ICON + ICON_GAP + labels[2];
+ controls.push((Control::Done, centred(x, w, BUTTON_HEIGHT)));
+ x += w + BAR_PAD;
+ ToolbarLayout {
+ controls,
+ grip,
+ dividers: [first, second],
+ width: x,
+ }
+}
+
#[derive(Default)]
pub(crate) struct RecordingOverlay {
// UI coordinates, independent of terminal font size and backing DPI.
- masks: Vec<Rect>,
+ masks: Vec<Mask>,
pane_id: Option<PaneId>,
canvas_size: (f32, f32),
pub(crate) editing: bool,
selected: Option<usize>,
drag: Option<Drag>,
- buttons: [Option<Rect>; 3],
+ // What a new mask is filled with: the colour chosen last.
+ color: MaskColor,
+ // The masks as they were before each edit, newest last.
+ history: VecDeque<Vec<Mask>>,
+ // The mask the last edit nudged, while that is what it did: holding an
+ // arrow key down is one step to undo, not one per repeat.
+ nudging: Option<usize>,
+ // The pipette is out: the next press in the pane takes the colour there.
+ picking: bool,
+ // While picking, where the pointer is and the colour under it.
+ pick_preview: Option<((f32, f32), MaskColor)>,
+ controls: Vec<(Control, Rect)>,
toolbar: Option<Rect>,
toolbar_position: Option<(f32, f32)>,
toolbar_drag: Option<((f32, f32), Rect)>,
- hovered_button: Option<usize>,
- pressed_button: Option<usize>,
+ hovered: Option<Control>,
+ pressed: Option<Control>,
finish_key: Option<KeyCode>,
}
@@ -235,6 +487,8 @@ impl RecordingOverlay {
self.pane_id = Some(pane_id);
self.canvas_size = size;
self.selected = None;
+ self.history = VecDeque::new();
+ self.nudging = None;
self.masks = pane_masks(pane_id)
.map(|masks| {
masks
@@ -243,11 +497,14 @@ impl RecordingOverlay {
.unwrap()
.masks
.iter()
- .map(|r| Rect {
- x: r.x * size.0,
- y: r.y * size.1,
- w: r.w * size.0,
- h: r.h * size.1,
+ .map(|mask| Mask {
+ rect: Rect {
+ x: mask.rect.x * size.0,
+ y: mask.rect.y * size.1,
+ w: mask.rect.w * size.0,
+ h: mask.rect.h * size.1,
+ },
+ color: mask.color,
})
.collect()
})
@@ -260,7 +517,10 @@ impl RecordingOverlay {
pane_id,
self.masks
.iter()
- .map(|r| r.normalized(self.canvas_size))
+ .map(|mask| Mask {
+ rect: mask.rect.normalized(self.canvas_size),
+ ..*mask
+ })
.collect(),
);
}
@@ -274,6 +534,9 @@ impl RecordingOverlay {
}
pub(crate) fn clear(&mut self) {
+ if !self.masks.is_empty() {
+ self.remember();
+ }
self.masks.clear();
self.selected = None;
self.drag = None;
@@ -282,28 +545,64 @@ impl RecordingOverlay {
pub(crate) fn cancel_interaction(&mut self) {
self.drag = None;
- self.pressed_button = None;
+ self.pressed = None;
self.finish_key = None;
self.toolbar_drag = None;
- self.hovered_button = None;
+ self.hovered = None;
+ self.picking = false;
+ self.pick_preview = None;
}
fn finish(&mut self) {
self.editing = false;
self.cancel_interaction();
- self.buttons = [None; 3];
+ self.controls = Vec::new();
self.toolbar = None;
+ self.history = VecDeque::new();
+ self.nudging = None;
}
- fn button_enabled(&self, index: usize) -> bool {
- match index {
- 0 => true,
- 1 => self.selected.is_some(),
- 2 => !self.is_empty(),
- _ => false,
+ fn control_enabled(&self, control: Control) -> bool {
+ match control {
+ Control::Delete => self.selected.is_some(),
+ Control::Clear => !self.is_empty(),
+ _ => true,
}
}
+ fn activate(&mut self, control: Control) {
+ match control {
+ Control::Swatch(index) => {
+ self.picking = false;
+ self.pick_preview = None;
+ self.apply_color(SWATCHES[index]);
+ }
+ Control::Pipette => {
+ self.picking = !self.picking;
+ self.pick_preview = None;
+ }
+ Control::Delete | Control::Clear => {
+ // Whatever the pipette was out for, this is something else.
+ self.picking = false;
+ self.pick_preview = None;
+ if control == Control::Delete {
+ self.delete_selected();
+ } else {
+ self.clear();
+ }
+ }
+ Control::Done => self.finish(),
+ }
+ }
+
+ /// The colour the toolbar shows as current: the selection's, or else the
+ /// one the next mask gets.
+ fn current_color(&self) -> MaskColor {
+ self.selected
+ .and_then(|index| self.masks.get(index))
+ .map_or(self.color, |mask| mask.color)
+ }
+
fn toolbar_rect(&self, size: (f32, f32), bounds: (f32, f32)) -> Rect {
let w = size.0.min((bounds.0 - 32.0).max(0.0));
let h = size.1.min(bounds.1);
@@ -325,37 +624,114 @@ impl RecordingOverlay {
}
}
+ /// Keep the masks as they are before an edit, for undo to return to. Only
+ /// while editing, and only the last UNDO_LIMIT edits.
+ fn remember(&mut self) {
+ self.nudging = None;
+ if !self.editing {
+ return;
+ }
+ if self.history.len() == UNDO_LIMIT {
+ self.history.pop_front();
+ }
+ self.history.push_back(self.masks.clone());
+ }
+
+ fn undo(&mut self) {
+ self.drag = None;
+ self.nudging = None;
+ if let Some(masks) = self.history.pop_back() {
+ self.masks = masks;
+ self.selected = self.selected.filter(|&index| index < self.masks.len());
+ self.save();
+ }
+ }
+
fn delete_selected(&mut self) {
self.drag = None;
if let Some(index) = self.selected.take() {
+ self.remember();
self.masks.remove(index);
self.save();
}
}
+ /// The selection takes `color`, and so does every mask drawn after it.
+ fn apply_color(&mut self, color: MaskColor) {
+ self.color = color;
+ if let Some(index) = self.selected {
+ if self.masks[index].color != color {
+ self.remember();
+ self.masks[index].color = color;
+ self.save();
+ }
+ }
+ }
+
+ fn nudge(&mut self, dx: f32, dy: f32) {
+ let Some(index) = self.selected.filter(|_| self.drag.is_none()) else {
+ return;
+ };
+ let bounds = self.canvas_size;
+ let rect = self.masks[index].rect.fitted(bounds);
+ let moved = Rect {
+ x: rect.x + dx,
+ y: rect.y + dy,
+ ..rect
+ }
+ .fitted(bounds);
+ if moved != rect {
+ if self.nudging != Some(index) {
+ self.remember();
+ self.nudging = Some(index);
+ }
+ self.masks[index].rect = moved;
+ self.save();
+ }
+ }
+
fn hit(&self, point: (f32, f32), bounds: (f32, f32)) -> Option<usize> {
self.masks
.iter()
- .rposition(|rect| rect.fitted(bounds).contains(point))
+ .rposition(|mask| mask.rect.fitted(bounds).contains(point))
+ }
+
+ /// What a press at `point` takes hold of: the selection's edges first,
+ /// even where they stick out over another mask; then the topmost mask
+ /// under it, by an edge or else as a whole.
+ fn grab_at(&self, point: (f32, f32), bounds: (f32, f32)) -> Option<DragKind> {
+ let selected = self.selected.and_then(|index| {
+ let rect = self.masks[index].rect.fitted(bounds);
+ rect.handle_at(point)
+ .map(|handle| DragKind::Resize(index, rect, handle))
+ });
+ selected.or_else(|| {
+ let index = self.hit(point, bounds)?;
+ let rect = self.masks[index].rect.fitted(bounds);
+ Some(match rect.handle_at(point) {
+ Some(handle) => DragKind::Resize(index, rect, handle),
+ None => DragKind::Move(index, rect),
+ })
+ })
}
fn begin(&mut self, point: (f32, f32), bounds: (f32, f32)) {
- self.selected = self.hit(point, bounds);
- let kind = match self.selected {
- Some(index) => {
- let rect = self.masks[index].fitted(bounds);
- if rect.handle().contains(point) {
- DragKind::Resize(index, rect)
- } else {
- DragKind::Move(index, rect)
- }
- }
+ self.nudging = None;
+ let kind = match self.grab_at(point, bounds) {
+ Some(kind) => kind,
None if self.masks.len() < MAX_MASKS => DragKind::Create,
- None => return,
+ None => {
+ self.selected = None;
+ return;
+ }
+ };
+ self.selected = match kind {
+ DragKind::Move(index, _) | DragKind::Resize(index, _, _) => Some(index),
+ DragKind::Create => None,
};
let preview = match kind {
DragKind::Create => Rect::between(point, point),
- DragKind::Move(_, rect) | DragKind::Resize(_, rect) => rect,
+ DragKind::Move(_, rect) | DragKind::Resize(_, rect, _) => rect,
};
self.drag = Some(Drag {
kind,
@@ -369,23 +745,16 @@ impl RecordingOverlay {
return;
};
let point = (point.0.clamp(0.0, bounds.0), point.1.clamp(0.0, bounds.1));
+ let delta = (point.0 - drag.anchor.0, point.1 - drag.anchor.1);
drag.preview = match drag.kind {
DragKind::Create => Rect::between(drag.anchor, point),
DragKind::Move(_, original) => Rect {
- x: original.x + point.0 - drag.anchor.0,
- y: original.y + point.1 - drag.anchor.1,
+ x: original.x + delta.0,
+ y: original.y + delta.1,
..original
}
.fitted(bounds),
- DragKind::Resize(_, original) => Rect {
- w: (original.w + point.0 - drag.anchor.0)
- .max(MIN_SIZE)
- .min(bounds.0 - original.x),
- h: (original.h + point.1 - drag.anchor.1)
- .max(MIN_SIZE)
- .min(bounds.1 - original.y),
- ..original
- },
+ DragKind::Resize(_, original, handle) => original.resized(handle, delta, bounds),
};
}
@@ -398,11 +767,19 @@ impl RecordingOverlay {
}
match drag.kind {
DragKind::Create => {
+ self.remember();
self.selected = Some(self.masks.len());
- self.masks.push(drag.preview);
+ self.masks.push(Mask {
+ rect: drag.preview,
+ color: self.color,
+ });
}
- DragKind::Move(index, _) | DragKind::Resize(index, _) => {
- self.masks[index] = drag.preview
+ // A press that selects without moving changes nothing.
+ DragKind::Move(_, original) | DragKind::Resize(_, original, _)
+ if drag.preview == original => {}
+ DragKind::Move(index, _) | DragKind::Resize(index, _, _) => {
+ self.remember();
+ self.masks[index].rect = drag.preview;
}
}
self.save();
@@ -411,20 +788,98 @@ impl RecordingOverlay {
fn displayed(&self, index: usize, bounds: (f32, f32)) -> Rect {
if let Some(drag) = self.drag {
match drag.kind {
- DragKind::Move(i, _) | DragKind::Resize(i, _) if i == index => return drag.preview,
+ DragKind::Move(i, _) | DragKind::Resize(i, _, _) if i == index => {
+ return drag.preview
+ }
_ => {}
}
}
- self.masks[index].fitted(bounds)
+ self.masks[index].rect.fitted(bounds)
+ }
+
+ /// The pointer over the pane while editing.
+ fn cursor_at(&self, point: (f32, f32), bounds: (f32, f32)) -> MouseCursor {
+ match self.drag.map(|drag| drag.kind) {
+ Some(DragKind::Resize(_, _, handle)) => return handle.cursor(),
+ Some(DragKind::Move(..)) => return MouseCursor::Hand,
+ Some(DragKind::Create) => return MouseCursor::Arrow,
+ None => {}
+ }
+ match self.grab_at(point, bounds) {
+ Some(DragKind::Resize(_, _, handle)) => handle.cursor(),
+ Some(_) => MouseCursor::Hand,
+ None => MouseCursor::Arrow,
+ }
}
}
impl crate::TermWindow {
- fn recording_pane_frame(&self, pane_id: PaneId) -> Option<window::RectF> {
+ fn recording_pane(&self, pane_id: PaneId) -> Option<PositionedPane> {
self.get_panes_to_render()
- .iter()
+ .into_iter()
.find(|pos| pos.pane.pane_id() == pane_id)
- .and_then(|pos| self.pane_mask_frame(pos).ok())
+ }
+
+ fn recording_pane_frame(&self, pane_id: PaneId) -> Option<window::RectF> {
+ self.recording_pane(pane_id)
+ .and_then(|pos| self.pane_mask_frame(&pos).ok())
+ }
+
+ /// What `Background` is over this pane -- its ground as painted -- and
+ /// the dimming an inactive pane gets, so that its masks dim with it.
+ fn recording_mask_ground(&self, pos: &PositionedPane) -> (LinearRgba, Option<HsbTransform>) {
+ let background = pos.pane.palette().background;
+ let ground = self
+ .dark_terminal_ground()
+ .unwrap_or_else(|| background.to_linear());
+ let hsv =
+ (!pos.is_active).then(|| self.config.inactive_pane_hsb_for_background(background));
+ (ground, hsv)
+ }
+
+ /// The colour a mask needs to disappear into the cell under `coords`
+ /// (window pixels) of the pane at `pos`, whose mask frame is `frame`.
+ /// Padding outside the grid is the pane's own ground.
+ fn recording_color_at(
+ &self,
+ pos: &PositionedPane,
+ frame: window::RectF,
+ coords: (f32, f32),
+ ) -> Option<MaskColor> {
+ let pane_id = pos.pane.pane_id();
+ let grid = pane_masks(pane_id)?.grid()?.in_frame(frame);
+ let dims = pos.pane.get_dimensions();
+ if dims.cols == 0 || dims.viewport_rows == 0 || grid.width() <= 0.0 || grid.height() <= 0.0
+ {
+ return Some(MaskColor::Background);
+ }
+ let viewport = self.get_viewport(pane_id);
+ let scrolled = match viewport {
+ Some(_) => self.drawn_viewport_px(&pos.pane),
+ None => 0.0,
+ };
+ let col = ((coords.0 - grid.min_x()) / (grid.width() / dims.cols as f32)).floor();
+ let row = ((coords.1 - grid.min_y() + scrolled)
+ / (grid.height() / dims.viewport_rows as f32))
+ .floor();
+ // Scrolled part of the way through a row, one more is drawn below.
+ let rows = dims.viewport_rows + usize::from(scrolled > 0.0);
+ if col < 0.0 || row < 0.0 || col >= dims.cols as f32 || row >= rows as f32 {
+ return Some(MaskColor::Background);
+ }
+ let stable = viewport.unwrap_or(dims.physical_top) + row as StableRowIndex;
+ // Asked for a row it does not have, a pane answers with another one.
+ let (first, lines) = pos.pane.get_lines(stable..stable + 1);
+ let line = lines.first().filter(|_| first == stable);
+ let palette = pos.pane.palette();
+ Some(match line.and_then(|line| line.get_cell(col as usize)) {
+ Some(cell) => {
+ let attrs = cell.attrs();
+ let style = self.fonts.match_style(&self.config, attrs);
+ cell_mask_color(attrs, &palette, &self.config, style, dims.reverse_video)
+ }
+ None => MaskColor::Background,
+ })
}
pub(crate) fn active_pane_recording_masks_empty(&self) -> bool {
@@ -458,11 +913,12 @@ impl crate::TermWindow {
pub(crate) fn occlude_recording_masks(
&self,
heap: &mut crate::quad::HeapQuadAllocator,
- rects: &[window::RectF],
+ masks: &[(window::RectF, LinearRgba)],
+ hsv: Option<HsbTransform>,
) -> anyhow::Result<()> {
- let clips: Vec<_> = rects
+ let clips: Vec<_> = masks
.iter()
- .map(|rect| {
+ .map(|(rect, _)| {
crate::quad::QuadClipRect::from_top_left_pixels(
rect.min_x(),
rect.min_y(),
@@ -474,13 +930,9 @@ impl crate::TermWindow {
.collect();
heap.occlude(&clips);
let mut layers = TripleLayerQuadAllocator::Heap(heap);
- for rect in rects {
- self.filled_rectangle(
- &mut layers,
- 2,
- *rect,
- LinearRgba::with_components(0.0, 0.0, 0.0, 1.0),
- )?;
+ for (rect, color) in masks {
+ self.filled_rectangle(&mut layers, 2, *rect, *color)?
+ .set_hsv(hsv);
}
Ok(())
}
@@ -488,58 +940,50 @@ impl crate::TermWindow {
// This runs inside the terminal world, so a recorded transition includes it.
pub(crate) fn paint_pane_recording_masks(
&self,
- pane_id: PaneId,
+ pos: &PositionedPane,
frame: window::RectF,
layers: &mut TripleLayerQuadAllocator,
) -> anyhow::Result<()> {
- let black = LinearRgba::with_components(0.0, 0.0, 0.0, 1.0);
+ let pane_id = pos.pane.pane_id();
+ let editing =
+ self.recording_overlay.editing && self.recording_overlay.pane_id == Some(pane_id);
+ let layer = match pane_masks(pane_id) {
+ Some(layer) if editing || !layer.is_empty() => layer,
+ _ => return Ok(()),
+ };
+ let (ground, hsv) = self.recording_mask_ground(pos);
if let TripleLayerQuadAllocator::Heap(heap) = layers {
- let rects = pane_masks(pane_id)
- .map(|masks| {
- masks
- .0
- .lock()
- .unwrap()
- .masks
- .iter()
- .filter_map(|mask| mask.in_frame(frame).intersection(&frame))
- .collect::<Vec<_>>()
+ let masks = layer
+ .0
+ .lock()
+ .unwrap()
+ .masks
+ .iter()
+ .filter_map(|mask| {
+ let rect = mask.rect.in_frame(frame).intersection(&frame)?;
+ Some((rect, mask.color.linear(ground)))
})
- .unwrap_or_default();
- self.occlude_recording_masks(heap, &rects)?;
- return Ok(());
+ .collect::<Vec<_>>();
+ return self.occlude_recording_masks(heap, &masks, hsv);
}
- let editing =
- self.recording_overlay.editing && self.recording_overlay.pane_id == Some(pane_id);
if !editing {
- return self.paint_saved_recording_masks(pane_id, frame, frame, layers, 1.0);
+ for mask in layer.0.lock().unwrap().masks.iter() {
+ if let Some(rect) = mask.rect.in_frame(frame).intersection(&frame) {
+ self.filled_rectangle(layers, 2, rect, mask.color.linear(ground))?
+ .set_hsv(hsv);
+ }
+ }
+ return Ok(());
}
let state = &self.recording_overlay;
- let accent = self.chrome().accent;
- for index in 0..state.masks.len() {
- let rect = state
- .displayed(index, state.canvas_size)
- .normalized(state.canvas_size)
- .in_frame(frame);
- self.filled_rectangle(layers, 2, rect, black)?;
- if state.selected == Some(index) {
- self.fill_rounded_rectangle_with_border(
- layers,
- 2,
- rect,
- black,
- accent,
- 0.0,
- self.ui_f32(1.0),
- )?;
- let side = self.ui_f32(6.0).min(rect.width()).min(rect.height());
- self.fill_rounded_rectangle(
- layers,
- 2,
- euclid::rect(rect.max_x() - side, rect.max_y() - side, side, side),
- accent,
- self.ui_f32(2.0),
- )?;
+ let px = self.ui_f32(1.0).max(0.01);
+ let in_pane = |rect: Rect| rect.normalized(state.canvas_size).in_frame(frame);
+ for (index, mask) in state.masks.iter().enumerate() {
+ let rect = in_pane(state.displayed(index, state.canvas_size));
+ self.filled_rectangle(layers, 2, rect, mask.color.linear(ground))?
+ .set_hsv(hsv);
+ if state.selected != Some(index) {
+ self.paint_mask_outline(layers, rect, px)?;
}
}
if let Some(Drag {
@@ -548,47 +992,100 @@ impl crate::TermWindow {
..
}) = state.drag
{
- self.filled_rectangle(
- layers,
- 2,
- preview.normalized(state.canvas_size).in_frame(frame),
- black,
- )?;
+ if preview.w > 0.0 && preview.h > 0.0 {
+ let rect = in_pane(preview);
+ self.filled_rectangle(layers, 2, rect, state.color.linear(ground))?
+ .set_hsv(hsv);
+ self.paint_mask_outline(layers, rect, px)?;
+ }
+ }
+ if let Some(index) = state.selected {
+ let rect = in_pane(state.displayed(index, state.canvas_size));
+ self.paint_mask_selection(layers, rect, px)?;
}
Ok(())
}
- pub(crate) fn paint_saved_recording_masks(
+ /// A mask's edge while editing, in two tones so that it shows on any
+ /// ground: light just inside the mask, dark just outside it.
+ fn paint_mask_outline(
&self,
- pane_id: PaneId,
- frame: window::RectF,
- clip: window::RectF,
layers: &mut TripleLayerQuadAllocator,
- opacity: f32,
+ rect: window::RectF,
+ px: f32,
) -> anyhow::Result<()> {
- if let Some(masks) = pane_masks(pane_id) {
- self.paint_recording_mask_layer(&masks, frame, clip, layers, opacity)?;
+ let light = LinearRgba::with_components(1.0, 1.0, 1.0, 0.7);
+ let dark = LinearRgba::with_components(0.0, 0.0, 0.0, 0.5);
+ let line = 2.0 * px;
+ self.stroke_rectangle(layers, rect, line, light)?;
+ self.stroke_rectangle(layers, rect.inflate(line, line), line, dark)
+ }
+
+ /// The selected mask: an accent ring just outside it, and a handle on
+ /// each corner and, where there is room, in the middle of each edge.
+ fn paint_mask_selection(
+ &self,
+ layers: &mut TripleLayerQuadAllocator,
+ rect: window::RectF,
+ px: f32,
+ ) -> anyhow::Result<()> {
+ let accent = self.chrome().accent;
+ let ring = 4.0 * px;
+ self.stroke_rectangle(layers, rect.inflate(ring, ring), ring, accent)?;
+ let size = HANDLE_SIZE * px;
+ let white = LinearRgba::with_components(1.0, 1.0, 1.0, 1.0);
+ for (fx, fy) in [
+ (0.0, 0.0),
+ (0.5, 0.0),
+ (1.0, 0.0),
+ (1.0, 0.5),
+ (1.0, 1.0),
+ (0.5, 1.0),
+ (0.0, 1.0),
+ (0.0, 0.5),
+ ] {
+ if (fx == 0.5 && rect.width() < size * 3.0) || (fy == 0.5 && rect.height() < size * 3.0)
+ {
+ continue;
+ }
+ let x = rect.min_x() + rect.width() * fx - size / 2.0;
+ let y = rect.min_y() + rect.height() * fy - size / 2.0;
+ self.fill_rounded_rectangle_with_border(
+ layers,
+ 2,
+ euclid::rect(x, y, size, size),
+ white,
+ accent,
+ 3.0 * px,
+ 3.0 * px,
+ )?;
}
Ok(())
}
- pub(crate) fn paint_recording_mask_layer(
+ fn stroke_rectangle(
&self,
- masks: &PaneRecordingLayer,
- frame: window::RectF,
- clip: window::RectF,
layers: &mut TripleLayerQuadAllocator,
- opacity: f32,
+ rect: window::RectF,
+ width: f32,
+ color: LinearRgba,
) -> anyhow::Result<()> {
- for rect in masks.0.lock().unwrap().masks.iter() {
- if let Some(rect) = rect.in_frame(frame).intersection(&clip) {
- self.filled_rectangle(
- layers,
- 2,
- rect,
- LinearRgba::with_components(0.0, 0.0, 0.0, opacity),
- )?;
- }
+ let w = width.min(rect.width() / 2.0).min(rect.height() / 2.0);
+ if w <= 0.0 {
+ return Ok(());
+ }
+ for edge in [
+ euclid::rect(rect.min_x(), rect.min_y(), rect.width(), w),
+ euclid::rect(rect.min_x(), rect.max_y() - w, rect.width(), w),
+ euclid::rect(rect.min_x(), rect.min_y() + w, w, rect.height() - w * 2.0),
+ euclid::rect(
+ rect.max_x() - w,
+ rect.min_y() + w,
+ w,
+ rect.height() - w * 2.0,
+ ),
+ ] {
+ self.filled_rectangle(layers, 2, edge, color)?;
}
Ok(())
}
@@ -600,6 +1097,9 @@ impl crate::TermWindow {
let Some(frame) = self.recording_pane_frame(pane.pane_id()) else {
return;
};
+ // The layer records where the pane's grid is from its next paint on,
+ // which is what the pipette reads cells through.
+ pane_layer(pane.pane_id());
let scale = self.ui_f32(1.0).max(0.01);
self.recording_overlay.bind(
pane.pane_id(),
@@ -607,7 +1107,7 @@ impl crate::TermWindow {
);
self.recording_overlay.editing = true;
self.recording_overlay.drag = None;
- self.recording_overlay.pressed_button = None;
+ self.recording_overlay.pressed = None;
self.context_menu = None;
self.dragging = None;
self.current_mouse_capture = None;
@@ -632,14 +1132,37 @@ impl crate::TermWindow {
return false;
}
if key.key_is_down {
- match key.key {
+ let mods = key.modifiers
+ & (Modifiers::CTRL | Modifiers::SHIFT | Modifiers::ALT | Modifiers::SUPER);
+ let step = if mods.contains(Modifiers::SHIFT) {
+ NUDGE_FAR
+ } else {
+ NUDGE
+ };
+ let overlay = &mut self.recording_overlay;
+ match &key.key {
+ // Esc puts the pipette down before it ends editing.
+ KeyCode::Char('\u{1b}') if overlay.picking => {
+ overlay.picking = false;
+ overlay.pick_preview = None;
+ }
KeyCode::Char('\u{1b}') | KeyCode::Char('\r') => {
- self.recording_overlay.finish();
- self.recording_overlay.finish_key = Some(key.key.clone());
+ overlay.finish();
+ overlay.finish_key = Some(key.key.clone());
}
KeyCode::Char('\u{7f}') | KeyCode::Char('\u{8}') => {
- self.recording_overlay.delete_selected();
+ overlay.delete_selected();
+ }
+ KeyCode::Char(c)
+ if mods == undo_modifiers()
+ && (c.eq_ignore_ascii_case(&'z') || *c == '\u{1a}') =>
+ {
+ overlay.undo();
}
+ KeyCode::LeftArrow => overlay.nudge(-step, 0.0),
+ KeyCode::RightArrow => overlay.nudge(step, 0.0),
+ KeyCode::UpArrow => overlay.nudge(0.0, -step),
+ KeyCode::DownArrow => overlay.nudge(0.0, step),
_ => {}
}
crate::frontend::front_end().invalidate_all_windows();
@@ -661,46 +1184,64 @@ impl crate::TermWindow {
self.dimensions.pixel_width as f32 / scale,
self.dimensions.pixel_height as f32 / scale,
);
+ let coords = (event.coords.x as f32, event.coords.y as f32);
let point = (
- (event.coords.x as f32 / scale).clamp(0.0, bounds.0),
- (event.coords.y as f32 / scale).clamp(0.0, bounds.1),
+ (coords.0 / scale).clamp(0.0, bounds.0),
+ (coords.1 / scale).clamp(0.0, bounds.1),
);
- let frame = self
+ let pane = self
.recording_overlay
.pane_id
- .and_then(|id| self.recording_pane_frame(id));
- let Some(frame) = frame else {
+ .and_then(|id| self.recording_pane(id))
+ .and_then(|pos| Some((self.pane_mask_frame(&pos).ok()?, pos)));
+ let Some((frame, pos)) = pane else {
self.recording_overlay.finish();
context.invalidate();
return true;
};
let canvas = self.recording_overlay.canvas_size;
let local = (
- (event.coords.x as f32 - frame.min_x()) / frame.width().max(1.0) * canvas.0,
- (event.coords.y as f32 - frame.min_y()) / frame.height().max(1.0) * canvas.1,
+ (coords.0 - frame.min_x()) / frame.width().max(1.0) * canvas.0,
+ (coords.1 - frame.min_y()) / frame.height().max(1.0) * canvas.1,
);
let inside = local.0 >= 0.0 && local.1 >= 0.0 && local.0 <= canvas.0 && local.1 <= canvas.1;
let local = (local.0.clamp(0.0, canvas.0), local.1.clamp(0.0, canvas.1));
- let button = self
+ let control = self
.recording_overlay
- .buttons
+ .controls
.iter()
- .position(|rect| rect.is_some_and(|r| r.contains(point)));
+ .find(|(_, rect)| rect.contains(point))
+ .map(|(control, _)| *control);
let over_toolbar = self
.recording_overlay
.toolbar
.is_some_and(|rect| rect.contains(point));
- self.recording_overlay.hovered_button = button;
+ self.recording_overlay.hovered = control;
+ if self.recording_overlay.picking {
+ let color = if inside && !over_toolbar {
+ self.recording_color_at(&pos, frame, coords)
+ } else {
+ None
+ };
+ self.recording_overlay.pick_preview = color.map(|color| (point, color));
+ }
match event.kind {
MouseEventKind::Press(MousePress::Left) => {
if over_toolbar {
- if let Some(button) = button {
- if self.recording_overlay.button_enabled(button) {
- self.recording_overlay.pressed_button = Some(button);
+ if let Some(control) = control {
+ if self.recording_overlay.control_enabled(control) {
+ self.recording_overlay.pressed = Some(control);
}
} else if let Some(rect) = self.recording_overlay.toolbar {
self.recording_overlay.toolbar_drag = Some((point, rect));
}
+ } else if self.recording_overlay.picking {
+ // A press in the pane takes the colour under it; one
+ // anywhere else just puts the pipette down.
+ if let Some((_, color)) = self.recording_overlay.pick_preview.take() {
+ self.recording_overlay.apply_color(color);
+ }
+ self.recording_overlay.picking = false;
} else if inside {
self.recording_overlay.begin(local, canvas);
}
@@ -713,14 +1254,9 @@ impl crate::TermWindow {
if self.recording_overlay.toolbar_drag.is_some() {
self.recording_overlay.move_toolbar(point, bounds);
self.recording_overlay.toolbar_drag = None;
- } else if let Some(pressed) = self.recording_overlay.pressed_button.take() {
- if button == Some(pressed) {
- match pressed {
- 0 => self.recording_overlay.finish(),
- 1 => self.recording_overlay.delete_selected(),
- 2 => self.recording_overlay.clear(),
- _ => {}
- }
+ } else if let Some(pressed) = self.recording_overlay.pressed.take() {
+ if control == Some(pressed) {
+ self.recording_overlay.activate(pressed);
}
} else {
self.recording_overlay.update_drag(local, canvas);
@@ -729,25 +1265,17 @@ impl crate::TermWindow {
}
_ => {}
}
- let cursor = if !self.recording_overlay.editing {
+ let overlay = &self.recording_overlay;
+ let cursor = if !overlay.editing {
MouseCursor::Arrow
- } else if over_toolbar {
- if button.is_none_or(|i| self.recording_overlay.button_enabled(i)) {
+ } else if over_toolbar || overlay.toolbar_drag.is_some() {
+ if control.is_none_or(|control| overlay.control_enabled(control)) {
MouseCursor::Hand
} else {
MouseCursor::Arrow
}
- } else if let Some(index) = self.recording_overlay.hit(local, canvas).filter(|_| inside) {
- if self
- .recording_overlay
- .displayed(index, canvas)
- .handle()
- .contains(local)
- {
- MouseCursor::SizeNorthWestSouthEast
- } else {
- MouseCursor::Hand
- }
+ } else if overlay.drag.is_some() || (inside && !overlay.picking) {
+ overlay.cursor_at(local, canvas)
} else {
MouseCursor::Arrow
};
@@ -763,15 +1291,16 @@ impl crate::TermWindow {
if !self.recording_overlay.editing {
return Ok(());
}
- if self
+ let pos = self
.recording_overlay
.pane_id
- .and_then(|id| self.recording_pane_frame(id))
- .is_none()
- {
+ .and_then(|id| self.recording_pane(id))
+ .filter(|pos| self.pane_mask_frame(pos).is_ok());
+ let Some(pos) = pos else {
self.recording_overlay.finish();
return Ok(());
- }
+ };
+ let (ground, _) = self.recording_mask_ground(&pos);
let scale = self.ui_f32(1.0).max(0.01);
let bounds = (
self.dimensions.pixel_width as f32 / scale,
@@ -781,6 +1310,7 @@ impl crate::TermWindow {
let layer = gl.layer_for_zindex(OVERLAY_ZINDEX)?;
let mut layers = layer.quad_allocator();
let black = LinearRgba::with_components(0.0, 0.0, 0.0, 1.0);
+ let white = LinearRgba::with_components(1.0, 1.0, 1.0, 1.0);
let size = (crate::native_settings::home_font_size(&crate::native_settings::load_shared())
* 0.8)
.max(10.0);
@@ -790,138 +1320,278 @@ impl crate::TermWindow {
let palette = self.chrome();
let text_height = metrics.cell_size.height as f32 / scale;
let labels = [
- crate::i18n::tr("recording-overlay-done"),
crate::i18n::tr("recording-overlay-delete"),
crate::i18n::tr("recording-overlay-clear"),
+ crate::i18n::tr("recording-overlay-done"),
];
let widths = labels
.each_ref()
- .map(|label| (ctx.measure_text_width(&font, label) / scale + 36.0).max(136.0));
- let natural_width = widths.iter().sum::<f32>() + 64.0;
+ .map(|label| ctx.measure_text_width(&font, label) / scale);
+ // Labels go first when the window is narrow, then everything shrinks.
+ let available = (bounds.0 - 32.0).max(0.0);
+ let mut layout = toolbar_layout(widths, true);
+ let with_labels = layout.width <= available;
+ if !with_labels {
+ layout = toolbar_layout(widths, false);
+ }
+ let squeeze = (available / layout.width).min(1.0);
let bar = self
.recording_overlay
- .toolbar_rect((natural_width, text_height + 96.0), bounds);
- let compression = (bar.w / natural_width).min(1.0);
+ .toolbar_rect((layout.width * squeeze, BAR_HEIGHT * squeeze), bounds);
self.recording_overlay.toolbar = Some(bar);
+ let place = |rect: Rect| Rect {
+ x: bar.x + rect.x * squeeze,
+ y: bar.y + rect.y * squeeze,
+ w: rect.w * squeeze,
+ h: rect.h * squeeze,
+ };
+ let controls = layout
+ .controls
+ .iter()
+ .map(|(control, rect)| (*control, place(*rect)))
+ .collect::<Vec<_>>();
- // Small, bounded rounded quads: no blurred framebuffer or animation loop.
- for (spread, offset, alpha) in [(12.0, 8.0, 0.05), (6.0, 5.0, 0.10), (2.0, 3.0, 0.18)] {
- let shadow = Rect {
- x: bar.x - spread,
- y: bar.y - spread + offset,
- w: bar.w + spread * 2.0,
- h: bar.h + spread * 2.0,
- };
- self.fill_rounded_rectangle(
- &mut layers,
- 2,
- shadow.pixels(scale),
- black.mul_alpha(alpha),
- (34.0 + spread) * scale,
- )?;
- }
- self.fill_rounded_rectangle_with_border(
+ // The command palette's surface: soft shadows from the atlas under a
+ // bordered fill.
+ let radius = bar.h / 2.0;
+ ctx.draw_elevated_surface(
&mut layers,
2,
bar.pixels(scale),
palette.control_bg,
palette.control_border,
- 34.0 * scale,
- scale.max(1.0),
+ black,
+ radius * scale,
)?;
- // The whole gap around the buttons is draggable, not just this visual grip.
- self.fill_rounded_rectangle(
- &mut layers,
- 2,
- Rect {
- x: bar.x + (bar.w - 48.0) / 2.0,
- y: bar.y + 12.0,
- w: 48.0,
- h: 5.0,
- }
- .pixels(scale),
- palette.muted_text.mul_alpha(0.45),
- 2.5 * scale,
- )?;
- let mut x = bar.x + 20.0 * compression;
- let mut buttons = [None; 3];
- for i in [1, 2, 0] {
- let width = widths[i] * compression;
- let rect = Rect {
- x,
- y: bar.y + 28.0,
- w: width,
- h: bar.h - 42.0,
+ // The whole gap around the controls is draggable, not just this grip.
+ let grip = place(layout.grip);
+ let dot = 5.0 * squeeze;
+ for (column, row) in [(0, 0), (1, 0), (0, 1), (1, 1), (0, 2), (1, 2)] {
+ let dot_rect = Rect {
+ x: grip.x + (grip.w - dot * 3.0) / 2.0 + column as f32 * dot * 2.0,
+ y: grip.y + (grip.h - dot * 5.0) / 2.0 + row as f32 * dot * 2.0,
+ w: dot,
+ h: dot,
};
- let enabled = self.recording_overlay.button_enabled(i);
- let hovered = self.recording_overlay.hovered_button == Some(i) && enabled;
- let pressed = self.recording_overlay.pressed_button == Some(i) && hovered;
- let bg = if pressed {
+ self.fill_rounded_rectangle(
+ &mut layers,
+ 2,
+ dot_rect.pixels(scale),
+ palette.muted_text.mul_alpha(0.6),
+ dot / 2.0 * scale,
+ )?;
+ }
+ for x in layout.dividers {
+ let divider = place(Rect {
+ x,
+ y: (BAR_HEIGHT - 36.0) / 2.0,
+ w: 2.0,
+ h: 36.0,
+ });
+ self.filled_rectangle(
+ &mut layers,
+ 2,
+ divider.pixels(scale),
+ palette.control_border,
+ )?;
+ }
+
+ let overlay = &self.recording_overlay;
+ let current = overlay.current_color();
+ let edge = palette.text.mul_alpha(0.18);
+ let icon = ICON * squeeze;
+ for (control, rect) in &controls {
+ let enabled = overlay.control_enabled(*control);
+ let hovered = enabled && overlay.hovered == Some(*control);
+ let pressed = hovered && overlay.pressed == Some(*control);
+ let button_bg = if pressed {
palette.control_pressed_bg
} else {
palette.control_hover_bg
};
- if hovered {
- self.fill_rounded_rectangle(&mut layers, 2, rect.pixels(scale), bg, 18.0 * scale)?;
- }
- let center_x = x + width / 2.0;
- let icon = match i {
- 0 => SvgIcon::Check,
- 1 => SvgIcon::Trash2,
- _ => SvgIcon::RotateCcw,
- };
- let tint = if enabled {
- palette.text
- } else {
- palette.muted_text.mul_alpha(0.4)
- };
- let icon_color = if i == 0 { palette.on_accent } else { tint };
- if i == 0 {
- self.fill_rounded_rectangle(
- &mut layers,
- 2,
- Rect {
- x: center_x - 23.0,
- y: rect.y + 2.0,
- w: 46.0,
- h: 46.0,
+ let icon_y = (rect.y + (rect.h - icon) / 2.0) * scale;
+ let text_y = (rect.y + (rect.h - text_height) / 2.0) * scale;
+ match *control {
+ Control::Swatch(index) => {
+ let color = SWATCHES[index];
+ let ring = rect.grown(6.0 * squeeze);
+ if color == current {
+ self.fill_rounded_rectangle_with_border(
+ &mut layers,
+ 2,
+ ring.pixels(scale),
+ palette.control_bg,
+ palette.accent,
+ ring.w / 2.0 * scale,
+ 3.0 * squeeze * scale,
+ )?;
+ } else if hovered {
+ self.fill_rounded_rectangle(
+ &mut layers,
+ 2,
+ ring.pixels(scale),
+ button_bg,
+ ring.w / 2.0 * scale,
+ )?;
+ }
+ // The pane's own ground is meant to blend in, so it gets
+ // the stronger edge.
+ let (border, width) = match color {
+ MaskColor::Background => (palette.muted_text, 3.0),
+ _ => (edge, 2.0),
+ };
+ self.fill_rounded_rectangle_with_border(
+ &mut layers,
+ 2,
+ rect.pixels(scale),
+ color.linear(ground),
+ border,
+ rect.w / 2.0 * scale,
+ width * squeeze * scale,
+ )?;
+ }
+ Control::Pipette => {
+ if overlay.picking || hovered {
+ let bg = if overlay.picking {
+ palette.control_pressed_bg
+ } else {
+ button_bg
+ };
+ self.fill_rounded_rectangle(
+ &mut layers,
+ 2,
+ rect.pixels(scale),
+ bg,
+ 16.0 * squeeze * scale,
+ )?;
}
- .pixels(scale),
- palette.accent,
- 23.0 * scale,
- )?;
+ let tint = if overlay.picking {
+ palette.accent
+ } else {
+ palette.text
+ };
+ ctx.draw_svg_icon(
+ &mut layers,
+ SvgIcon::Pipette,
+ (rect.x + (rect.w - icon) / 2.0) * scale,
+ icon_y,
+ icon * scale,
+ tint,
+ )?;
+ if !SWATCHES.contains(&current) {
+ // A colour the pipette took, which no swatch shows.
+ let chip = Rect {
+ x: rect.x + rect.w - 20.0 * squeeze,
+ y: rect.y + rect.h - 20.0 * squeeze,
+ w: 16.0 * squeeze,
+ h: 16.0 * squeeze,
+ };
+ self.fill_rounded_rectangle_with_border(
+ &mut layers,
+ 2,
+ chip.pixels(scale),
+ current.linear(ground),
+ palette.accent,
+ chip.w / 2.0 * scale,
+ 3.0 * squeeze * scale,
+ )?;
+ }
+ }
+ Control::Delete | Control::Clear => {
+ if hovered {
+ self.fill_rounded_rectangle(
+ &mut layers,
+ 2,
+ rect.pixels(scale),
+ button_bg,
+ rect.h / 2.0 * scale,
+ )?;
+ }
+ let tint = if enabled {
+ palette.text
+ } else {
+ palette.muted_text.mul_alpha(0.4)
+ };
+ let (svg, label) = match control {
+ Control::Delete => (SvgIcon::Trash2, &labels[0]),
+ _ => (SvgIcon::RotateCcw, &labels[1]),
+ };
+ let x = rect.x + BUTTON_PAD * squeeze;
+ ctx.draw_svg_icon(&mut layers, svg, x * scale, icon_y, icon * scale, tint)?;
+ if with_labels {
+ let text_x = x + icon + ICON_GAP * squeeze;
+ let max_width = (rect.x + rect.w - text_x).max(0.0) * scale;
+ ctx.draw_text_on_layer(
+ &mut layers,
+ 2,
+ &font,
+ text_x * scale,
+ text_y,
+ label,
+ tint,
+ max_width,
+ )?;
+ }
+ }
+ Control::Done => {
+ let fill = if pressed {
+ palette.accent.mul_alpha(0.8)
+ } else {
+ palette.accent
+ };
+ self.fill_rounded_rectangle(
+ &mut layers,
+ 2,
+ rect.pixels(scale),
+ fill,
+ rect.h / 2.0 * scale,
+ )?;
+ let x = rect.x + BUTTON_PAD * squeeze;
+ ctx.draw_svg_icon(
+ &mut layers,
+ SvgIcon::Check,
+ x * scale,
+ icon_y,
+ icon * scale,
+ palette.on_accent,
+ )?;
+ let text_x = x + icon + ICON_GAP * squeeze;
+ let max_width = (rect.x + rect.w - text_x).max(0.0) * scale;
+ ctx.draw_text_on_layer(
+ &mut layers,
+ 2,
+ &font,
+ text_x * scale,
+ text_y,
+ &labels[2],
+ palette.on_accent,
+ max_width,
+ )?;
+ }
}
- ctx.draw_svg_icon(
- &mut layers,
- icon,
- (center_x - 16.0) * scale,
- (rect.y + 9.0) * scale,
- 32.0 * scale,
- icon_color,
- )?;
- let max_width = (width - 12.0).max(0.0) * scale;
- let label_width = ctx.measure_text_width(&font, &labels[i]).min(max_width);
- ctx.draw_text_on_layer(
- &mut layers,
- 2,
- &font,
- center_x * scale - label_width / 2.0,
- (rect.y + 56.0) * scale,
- &labels[i],
- tint,
- max_width,
- )?;
- buttons[i] = Some(rect);
- x += width + 12.0 * compression;
}
- let hint_key = if self.recording_overlay.masks.len() >= MAX_MASKS {
- "recording-overlay-limit"
- } else if self.recording_overlay.toolbar_drag.is_some() {
- "recording-overlay-drag-toolbar"
+
+ let hint = if overlay.picking {
+ crate::i18n::tr("recording-overlay-picking")
+ } else if overlay.masks.len() >= MAX_MASKS {
+ crate::i18n::tr("recording-overlay-limit")
+ } else if overlay.toolbar_drag.is_some() {
+ crate::i18n::tr("recording-overlay-drag-toolbar")
} else {
- "recording-overlay-hint"
+ match overlay.hovered {
+ Some(Control::Swatch(index)) if SWATCHES[index] == MaskColor::Background => {
+ crate::i18n::tr("recording-overlay-follow-background")
+ }
+ Some(Control::Pipette) => crate::i18n::tr("recording-overlay-pick"),
+ Some(Control::Delete) if !with_labels => labels[0].clone(),
+ Some(Control::Clear) if !with_labels => labels[1].clone(),
+ _ => {
+ let mut args = FluentArgs::new();
+ args.set("undo", undo_shortcut());
+ crate::i18n::tr_args("recording-overlay-hint", &args)
+ }
+ }
};
- let hint = crate::i18n::tr(hint_key);
let max_width = (bounds.0 - 48.0).max(0.0) * scale;
let hint_width = ctx.measure_text_width(&font, &hint).min(max_width);
let hint_x = ((bar.x + bar.w / 2.0) * scale - hint_width / 2.0)
@@ -950,7 +1620,36 @@ impl crate::TermWindow {
max_width,
)?;
}
- self.recording_overlay.buttons = buttons;
+
+ // Beside the pointer, the colour a press would take.
+ if let Some((point, color)) = overlay.pick_preview {
+ let chip = Rect {
+ x: point.0 + 28.0,
+ y: point.1 - 68.0,
+ w: 44.0,
+ h: 44.0,
+ }
+ .fitted(bounds);
+ ctx.draw_shadow(
+ &mut layers,
+ 2,
+ chip.pixels(scale),
+ chip.w / 2.0 * scale,
+ ctx.px(4.0),
+ ctx.px(2.0),
+ black.mul_alpha(0.35),
+ )?;
+ self.fill_rounded_rectangle_with_border(
+ &mut layers,
+ 2,
+ chip.pixels(scale),
+ color.linear(ground),
+ white,
+ chip.w / 2.0 * scale,
+ 4.0 * scale,
+ )?;
+ }
+ self.recording_overlay.controls = controls;
Ok(())
}
}
@@ -958,12 +1657,23 @@ impl crate::TermWindow {
#[cfg(test)]
mod tests {
use super::*;
+ use wezterm_term::color::ColorAttribute;
+
+ const CANVAS: (f32, f32) = (500.0, 400.0);
+
+ fn mask(x: f32, y: f32, w: f32, h: f32) -> Mask {
+ Mask {
+ rect: Rect { x, y, w, h },
+ color: BLACK,
+ }
+ }
fn with_mask() -> RecordingOverlay {
let mut state = RecordingOverlay::default();
state.editing = true;
- state.begin((180.0, 160.0), (500.0, 400.0));
- state.update_drag((100.0, 80.0), (500.0, 400.0));
+ state.canvas_size = CANVAS;
+ state.begin((180.0, 160.0), CANVAS);
+ state.update_drag((100.0, 80.0), CANVAS);
state.commit_drag();
state
}
@@ -971,15 +1681,7 @@ mod tests {
#[test]
fn reverse_drag_creates_mask_and_finish_keeps_it() {
let mut state = with_mask();
- assert_eq!(
- state.masks,
- vec![Rect {
- x: 100.0,
- y: 80.0,
- w: 80.0,
- h: 80.0
- }]
- );
+ assert_eq!(state.masks, vec![mask(100.0, 80.0, 80.0, 80.0)]);
state.finish();
assert!(!state.owns_keyboard());
assert_eq!(state.masks.len(), 1);
@@ -988,11 +1690,11 @@ mod tests {
#[test]
fn moving_and_resizing_stay_inside_window() {
let mut state = with_mask();
- state.begin((110.0, 90.0), (500.0, 400.0));
- state.update_drag((900.0, 900.0), (500.0, 400.0));
+ state.begin((110.0, 90.0), CANVAS);
+ state.update_drag((900.0, 900.0), CANVAS);
state.commit_drag();
assert_eq!(
- state.masks[0],
+ state.masks[0].rect,
Rect {
x: 420.0,
y: 320.0,
@@ -1000,24 +1702,81 @@ mod tests {
h: 80.0
}
);
- state.begin((495.0, 395.0), (500.0, 400.0));
- state.update_drag((0.0, 0.0), (500.0, 400.0));
+ state.begin((495.0, 395.0), CANVAS);
+ state.update_drag((0.0, 0.0), CANVAS);
+ state.commit_drag();
+ assert_eq!(state.masks[0].rect.w, MIN_SIZE);
+ assert_eq!(state.masks[0].rect.h, MIN_SIZE);
+ }
+
+ #[test]
+ fn edges_and_corners_resize_and_a_small_mask_still_moves() {
+ let mut state = with_mask();
+ // The left edge, caught just outside the mask.
+ state.begin((97.0, 120.0), CANVAS);
+ state.update_drag((60.0, 300.0), CANVAS);
+ state.commit_drag();
+ // The edge follows the pointer, caught 3 points out.
+ assert_eq!(
+ state.masks[0].rect,
+ Rect {
+ x: 63.0,
+ y: 80.0,
+ w: 117.0,
+ h: 80.0
+ }
+ );
+ // The top-right corner.
+ state.begin((181.0, 79.0), CANVAS);
+ state.update_drag((200.0, 40.0), CANVAS);
state.commit_drag();
- assert_eq!(state.masks[0].w, MIN_SIZE);
- assert_eq!(state.masks[0].h, MIN_SIZE);
+ assert_eq!(
+ state.masks[0].rect,
+ Rect {
+ x: 63.0,
+ y: 41.0,
+ w: 136.0,
+ h: 119.0
+ }
+ );
+ // A mask smaller than the handles still moves from its middle...
+ state.masks.push(mask(300.0, 300.0, 10.0, 10.0));
+ state.selected = Some(1);
+ state.begin((305.0, 305.0), CANVAS);
+ assert!(matches!(state.drag.unwrap().kind, DragKind::Move(1, _)));
+ state.update_drag((325.0, 315.0), CANVAS);
+ state.commit_drag();
+ assert_eq!(state.masks[1].rect, mask(320.0, 310.0, 10.0, 10.0).rect);
+ // ...and resizes from just outside it.
+ state.begin((332.0, 315.0), CANVAS);
+ assert!(matches!(
+ state.drag.unwrap().kind,
+ DragKind::Resize(1, _, Handle(1, 0))
+ ));
+ assert_eq!(
+ state.cursor_at((332.0, 315.0), CANVAS),
+ MouseCursor::SizeLeftRight
+ );
}
#[test]
fn cancelled_drag_and_click_do_not_change_masks() {
let mut state = with_mask();
let original = state.masks.clone();
- state.begin((110.0, 90.0), (500.0, 400.0));
- state.update_drag((300.0, 200.0), (500.0, 400.0));
+ let history = state.history.len();
+ state.begin((110.0, 90.0), CANVAS);
+ state.update_drag((300.0, 200.0), CANVAS);
state.finish();
assert_eq!(state.masks, original);
- state.begin((10.0, 10.0), (500.0, 400.0));
+ state.editing = true;
+ state.begin((10.0, 10.0), CANVAS);
+ state.commit_drag();
+ assert_eq!(state.masks, original);
+ // Selecting without moving is not an edit either.
+ state.begin((110.0, 90.0), CANVAS);
state.commit_drag();
assert_eq!(state.masks, original);
+ assert!(state.history.len() <= history);
}
#[test]
@@ -1025,21 +1784,204 @@ mod tests {
let mut state = with_mask();
state.delete_selected();
assert!(state.is_empty());
- state.masks = vec![
- Rect {
- x: 100.0,
- y: 100.0,
- w: 10.0,
- h: 10.0
- };
- MAX_MASKS
- ];
- state.begin((1.0, 1.0), (500.0, 400.0));
+ state.masks = vec![mask(100.0, 100.0, 10.0, 10.0); MAX_MASKS];
+ state.begin((1.0, 1.0), CANVAS);
assert!(state.drag.is_none());
state.clear();
assert!(state.is_empty());
}
+ #[test]
+ fn undo_steps_back_through_edits_and_is_bounded() {
+ let mut state = with_mask();
+ let created = state.masks.clone();
+ state.begin((120.0, 100.0), CANVAS);
+ state.update_drag((220.0, 100.0), CANVAS);
+ state.commit_drag();
+ state.apply_color(SWATCHES[4]);
+ state.clear();
+ assert!(state.is_empty());
+ state.undo(); // the clear
+ assert_eq!(state.masks.len(), 1);
+ assert_eq!(state.masks[0].color, SWATCHES[4]);
+ state.undo(); // the colour
+ assert_eq!(state.masks[0].color, BLACK);
+ state.undo(); // the move
+ assert_eq!(state.masks, created);
+ state.undo(); // the mask itself
+ assert!(state.is_empty());
+ state.undo(); // nothing left to undo
+ assert!(state.is_empty());
+ for _ in 0..UNDO_LIMIT + 20 {
+ state.remember();
+ }
+ assert_eq!(state.history.len(), UNDO_LIMIT);
+ state.finish();
+ assert!(state.history.is_empty());
+ // Outside editing nothing is kept.
+ state.remember();
+ assert!(state.history.is_empty());
+ }
+
+ #[test]
+ fn nudges_move_the_selection_inside_the_pane() {
+ let mut state = with_mask();
+ state.nudge(1.0, 0.0);
+ assert_eq!(state.masks[0].rect.x, 101.0);
+ state.nudge(0.0, -1000.0);
+ assert_eq!(state.masks[0].rect.y, 0.0);
+ state.undo();
+ assert_eq!(state.masks[0].rect.y, 80.0);
+ let before = state.masks.clone();
+ state.selected = None;
+ state.nudge(10.0, 10.0);
+ assert_eq!(state.masks, before);
+ }
+
+ #[test]
+ fn a_run_of_nudges_is_one_step_to_undo() {
+ let mut state = with_mask();
+ let before = state.masks.clone();
+ let steps = state.history.len();
+ for _ in 0..50 {
+ state.nudge(NUDGE, 0.0);
+ }
+ assert_eq!(state.history.len(), steps + 1);
+ assert_eq!(state.masks[0].rect.x, 100.0 + 50.0 * NUDGE);
+ // Any other edit ends the run.
+ state.apply_color(SWATCHES[4]);
+ state.nudge(NUDGE, 0.0);
+ assert_eq!(state.history.len(), steps + 3);
+ state.undo();
+ state.undo();
+ state.undo();
+ assert_eq!(state.masks, before);
+ }
+
+ #[test]
+ fn an_unselected_mask_resizes_from_its_edges_too() {
+ let mut state = with_mask();
+ state.selected = None;
+ // Inside its lower-right corner.
+ state.begin((176.0, 156.0), CANVAS);
+ assert!(matches!(
+ state.drag.unwrap().kind,
+ DragKind::Resize(0, _, Handle(1, 1))
+ ));
+ assert_eq!(state.selected, Some(0));
+ state.update_drag((196.0, 176.0), CANVAS);
+ state.commit_drag();
+ assert_eq!(state.masks[0].rect, mask(100.0, 80.0, 100.0, 100.0).rect);
+ // Just outside a mask that is not selected is empty space.
+ state.selected = None;
+ state.begin((205.0, 120.0), CANVAS);
+ assert!(matches!(state.drag.unwrap().kind, DragKind::Create));
+ }
+
+ #[test]
+ fn a_translucent_ground_still_hides_what_is_under_it() {
+ let ground = LinearRgba::with_components(0.2, 0.3, 0.4, 0.6);
+ assert_eq!(
+ MaskColor::Background.linear(ground),
+ LinearRgba::with_components(0.2, 0.3, 0.4, 1.0)
+ );
+ assert_eq!(SWATCHES[4].linear(ground).3, 1.0);
+ }
+
+ #[test]
+ fn colours_go_to_the_selection_and_to_new_masks() {
+ let mut state = with_mask();
+ state.apply_color(MaskColor::Background);
+ assert_eq!(state.masks[0].color, MaskColor::Background);
+ state.selected = None;
+ state.apply_color(SWATCHES[7]);
+ assert_eq!(state.masks[0].color, MaskColor::Background);
+ state.begin((300.0, 300.0), CANVAS);
+ state.update_drag((340.0, 340.0), CANVAS);
+ state.commit_drag();
+ assert_eq!(state.masks[1].color, SWATCHES[7]);
+ assert_eq!(state.current_color(), SWATCHES[7]);
+ // The pipette is a toggle, and a swatch puts it down.
+ state.activate(Control::Pipette);
+ assert!(state.picking);
+ state.activate(Control::Swatch(1));
+ assert!(!state.picking);
+ assert_eq!(state.masks[1].color, BLACK);
+ }
+
+ fn srgb(color: wezterm_term::color::SrgbaTuple) -> MaskColor {
+ let (r, g, b, _) = color.to_srgb_u8();
+ MaskColor::Srgb(r, g, b)
+ }
+
+ #[test]
+ fn cells_become_mask_colours() {
+ let palette = ColorPalette::default();
+ let config = ConfigHandle::default_config();
+ let style = TextStyle::default();
+ let color = |attrs: &CellAttributes, reverse_video| {
+ cell_mask_color(attrs, &palette, &config, &style, reverse_video)
+ };
+ let mut attrs = CellAttributes::default();
+ assert_eq!(color(&attrs, false), MaskColor::Background);
+ attrs.set_background(ColorAttribute::PaletteIndex(2));
+ assert_eq!(
+ color(&attrs, false),
+ srgb(palette.resolve_bg(ColorAttribute::PaletteIndex(2)))
+ );
+ // Reverse video draws the foreground behind the text.
+ attrs.set_reverse(true);
+ assert_eq!(
+ color(&attrs, false),
+ srgb(palette.resolve_fg(ColorAttribute::Default))
+ );
+ }
+
+ #[test]
+ fn the_pipette_picks_what_the_renderer_draws() {
+ let palette = ColorPalette::default();
+ let config = ConfigHandle::default_config();
+ let style = TextStyle::default();
+ // A bold reversed red cell is drawn in bright red, entry 9.
+ let mut attrs = CellAttributes::default();
+ attrs.set_foreground(ColorAttribute::PaletteIndex(1));
+ attrs.set_intensity(wezterm_term::Intensity::Bold);
+ attrs.set_reverse(true);
+ assert_eq!(
+ cell_mask_color(&attrs, &palette, &config, &style, false),
+ srgb(palette.resolve_fg(ColorAttribute::PaletteIndex(9)))
+ );
+ // The whole screen reversed: a plain cell is drawn in the foreground,
+ // and a reversed one is back on the pane's own ground.
+ let plain = CellAttributes::default();
+ assert_eq!(
+ cell_mask_color(&plain, &palette, &config, &style, true),
+ srgb(palette.resolve_fg(ColorAttribute::Default))
+ );
+ let mut reversed = CellAttributes::default();
+ reversed.set_reverse(true);
+ assert_eq!(
+ cell_mask_color(&reversed, &palette, &config, &style, true),
+ MaskColor::Background
+ );
+ }
+
+ #[test]
+ fn toolbar_layout_keeps_controls_apart_and_drops_labels_first() {
+ let full = toolbar_layout([60.0, 50.0, 70.0], true);
+ let compact = toolbar_layout([60.0, 50.0, 70.0], false);
+ assert!(compact.width < full.width);
+ for layout in [&full, &compact] {
+ assert_eq!(layout.controls.len(), SWATCHES.len() + 4);
+ let mut right = layout.grip.x + layout.grip.w;
+ for (_, rect) in &layout.controls {
+ assert!(rect.x >= right && rect.x + rect.w <= layout.width);
+ assert!(rect.y >= 0.0 && rect.y + rect.h <= BAR_HEIGHT);
+ right = rect.x + rect.w;
+ }
+ }
+ }
+
#[test]
fn smaller_window_keeps_masks_editable_without_changing_saved_geometry() {
let state = with_mask();
@@ -1053,7 +1995,7 @@ mod tests {
h: 50.0
}
);
- assert_eq!(state.displayed(0, (500.0, 400.0)), state.masks[0]);
+ assert_eq!(state.displayed(0, CANVAS), state.masks[0].rect);
}
#[test]
@@ -1061,24 +2003,16 @@ mod tests {
let a = usize::MAX - 10;
let b = usize::MAX - 11;
let mut editor = RecordingOverlay::default();
- editor.bind(a, (500.0, 400.0));
+ editor.bind(a, CANVAS);
editor.begin((100.0, 80.0), editor.canvas_size);
editor.update_drag((200.0, 160.0), editor.canvas_size);
editor.commit_drag();
- editor.bind(b, (500.0, 400.0));
+ editor.bind(b, CANVAS);
assert!(editor.is_empty());
editor.clear();
let mut other_window = RecordingOverlay::default();
other_window.bind(a, (1000.0, 800.0));
- assert_eq!(
- other_window.masks,
- vec![Rect {
- x: 200.0,
- y: 160.0,
- w: 200.0,
- h: 160.0
- }]
- );
+ assert_eq!(other_window.masks, vec![mask(200.0, 160.0, 200.0, 160.0)]);
forget_pane(a);
forget_pane(b);
}
@@ -1088,20 +2022,15 @@ mod tests {
let id = usize::MAX - 12;
let cached = pane_layer(id);
assert!(cached.is_empty());
- let rect = Rect {
- x: 0.2,
- y: 0.3,
- w: 0.4,
- h: 0.1,
- };
- save_pane_masks(id, vec![rect]);
- assert_eq!(cached.0.lock().unwrap().masks, vec![rect]);
+ let saved = mask(0.2, 0.3, 0.4, 0.1);
+ save_pane_masks(id, vec![saved]);
+ assert_eq!(cached.0.lock().unwrap().masks, vec![saved]);
save_pane_masks(id, vec![]);
assert!(cached.is_empty());
- save_pane_masks(id, vec![rect]);
+ save_pane_masks(id, vec![saved]);
forget_pane(id);
assert!(pane_masks(id).is_none());
- assert_eq!(cached.0.lock().unwrap().masks, vec![rect]);
+ assert_eq!(cached.0.lock().unwrap().masks, vec![saved]);
}
#[test]
@@ -1145,17 +2074,33 @@ mod tests {
fn focus_loss_cancels_interaction_without_removing_masks() {
let mut state = with_mask();
let original = state.masks.clone();
- state.begin((110.0, 90.0), (500.0, 400.0));
- state.update_drag((300.0, 200.0), (500.0, 400.0));
- state.pressed_button = Some(2);
+ state.begin((110.0, 90.0), CANVAS);
+ state.update_drag((300.0, 200.0), CANVAS);
+ state.pressed = Some(Control::Clear);
state.finish_key = Some(KeyCode::Char('\u{1b}'));
+ state.picking = true;
state.cancel_interaction();
state.commit_drag();
assert_eq!(state.masks, original);
assert!(state.drag.is_none());
- assert!(state.pressed_button.is_none());
+ assert!(state.pressed.is_none());
assert!(state.finish_key.is_none());
+ assert!(!state.picking);
+ }
+
+ fn preview_rects(
+ layer: &PaneRecordingLayer,
+ target: window::RectF,
+ clip: window::RectF,
+ ) -> Vec<window::RectF> {
+ let ground = LinearRgba::with_components(0.1, 0.1, 0.1, 1.0);
+ layer
+ .preview_rects(target, clip, ground)
+ .into_iter()
+ .map(|(rect, _)| rect)
+ .collect()
}
+
#[test]
fn preview_masks_follow_text_grid_padding_splits_and_pane_font_metrics() {
for (frame, grid, cell) in [
@@ -1179,17 +2124,17 @@ mod tests {
),
] {
let layer = PaneRecordingLayer::default();
- let mask = Rect {
+ let rect = Rect {
x: grid.min_x() - frame.min_x(),
y: grid.min_y() - frame.min_y(),
w: cell.0 * 4.0,
h: cell.1,
}
.normalized((frame.width(), frame.height()));
- layer.0.lock().unwrap().masks = vec![mask];
+ layer.0.lock().unwrap().masks = vec![Mask { rect, color: BLACK }];
layer.set_grid(frame, grid);
let target = euclid::rect(25.0, 80.0, grid.width() / 4.0, grid.height() / 4.0);
- let result = layer.preview_rects(target, euclid::rect(0.0, 0.0, 1000.0, 1000.0));
+ let result = preview_rects(&layer, target, euclid::rect(0.0, 0.0, 1000.0, 1000.0));
assert_eq!(result.len(), 1);
for (got, wanted) in [
(result[0].min_x(), target.min_x()),
@@ -1202,18 +2147,39 @@ mod tests {
}
}
+ #[test]
+ fn preview_masks_take_their_colours_and_the_preview_ground() {
+ let layer = PaneRecordingLayer::default();
+ let frame = euclid::rect(0.0, 0.0, 100.0, 100.0);
+ let ground = LinearRgba::with_components(0.2, 0.3, 0.4, 1.0);
+ layer.0.lock().unwrap().masks = vec![
+ Mask {
+ rect: Rect {
+ x: 0.1,
+ y: 0.1,
+ w: 0.2,
+ h: 0.2,
+ },
+ color: MaskColor::Background,
+ },
+ mask(0.5, 0.5, 0.2, 0.2),
+ ];
+ layer.set_grid(frame, frame);
+ let colours: Vec<_> = layer
+ .preview_rects(frame, frame, ground)
+ .into_iter()
+ .map(|(_, colour)| colour)
+ .collect();
+ assert_eq!(colours, vec![ground, BLACK.linear(ground)]);
+ }
+
#[test]
fn missing_grid_only_covers_masked_panes_and_geometry_changes_invalidate() {
let layer = PaneRecordingLayer::default();
let frame = euclid::rect(0.0, 0.0, 100.0, 100.0);
- assert!(layer.preview_rects(frame, frame).is_empty());
- layer.0.lock().unwrap().masks.push(Rect {
- x: 0.1,
- y: 0.1,
- w: 0.2,
- h: 0.2,
- });
- assert_eq!(layer.preview_rects(frame, frame), vec![frame]);
+ assert!(preview_rects(&layer, frame, frame).is_empty());
+ layer.0.lock().unwrap().masks.push(mask(0.1, 0.1, 0.2, 0.2));
+ assert_eq!(preview_rects(&layer, frame, frame), vec![frame]);
layer.set_grid(frame, euclid::rect(10.0, 10.0, 80.0, 80.0));
let first = layer.revision();
layer.set_grid(frame, euclid::rect(10.0, 10.0, 80.0, 80.0));
@@ -1228,13 +2194,8 @@ mod tests {
TerminalPreviewPaneSnapshot, TerminalPreviewSnapshot,
};
let id = usize::MAX - 111;
- let mask = Rect {
- x: 0.1,
- y: 0.1,
- w: 0.2,
- h: 0.2,
- };
- save_pane_masks(id, vec![mask]);
+ let saved = mask(0.1, 0.1, 0.2, 0.2);
+ save_pane_masks(id, vec![saved]);
let snapshot = TerminalPreviewSnapshot {
tab_size: Default::default(),
splits: Vec::new(),
@@ -1258,7 +2219,7 @@ mod tests {
};
let first = snapshot.recording_revision();
assert!(first > 0); // zero-sized/off-card panes participate in both key checks
- save_pane_masks(id, vec![mask, mask]);
+ save_pane_masks(id, vec![saved, saved]);
let edited = snapshot.recording_revision();
assert!(edited > first); // retires both a cached picture and its old partial
forget_pane(id);
@@ -1275,9 +2236,7 @@ mod tests {
);
let frame = euclid::rect(0.0, 0.0, 100.0, 100.0);
assert_eq!(
- snapshot.panes[0]
- .recording_layer
- .preview_rects(frame, frame),
+ preview_rects(&snapshot.panes[0].recording_layer, frame, frame),
vec![frame]
);
}
@@ -1288,16 +2247,17 @@ mod tests {
let grid = euclid::rect(112.0, 60.0, 1200.0, 960.0);
// A visible cell near the right edge; the snapshot also contains
// source columns/rows outside the current window's clipped viewport.
- layer.0.lock().unwrap().masks = vec![Rect {
+ let rect = Rect {
x: 12.0 + 60.0 * 12.0,
y: 40.0 + 20.0 * 24.0,
w: 12.0,
h: 24.0,
}
- .normalized((frame.width(), frame.height()))];
+ .normalized((frame.width(), frame.height()));
+ layer.0.lock().unwrap().masks = vec![Mask { rect, color: BLACK }];
layer.set_grid(frame, grid);
let preview_grid = euclid::rect(20.0, 30.0, 300.0, 240.0);
- let result = layer.preview_rects(preview_grid, preview_grid);
+ let result = preview_rects(&layer, preview_grid, preview_grid);
assert_eq!(result.len(), 1);
for (got, wanted) in [
(result[0].min_x(), 200.0),