Screenshot editor: annotate, blur, pixelate, redact and crop before saving
CI / test (push) Canceled after 12m58s

Every screenshot now opens in an editor after KDE Spectacle's annotation mode:
Arrow, Line, Rectangle, Ellipse, Pen, Highlighter, Text, Number markers, Blur,
Pixelate, Redact and Crop, 8 colours, 3 sizes, undo/redo. Enter saves and
copies, Ctrl+C copies only, Esc discards.

- Drawn by the compositor (shot_editor.mbt), keyboard owned by it while up.
- Annotations are drawn into the picture's pixels at full size (annotate.mbt),
  so what is shown is what is saved. Blur = 6x6 block average + 3 box-blur
  passes; redact is opaque; both touch only their box (pixel tests).
- The picture goes to the renderer as <=2 MB strips: one 8 MB image-pixels
  call exhausts wasmtime's hostcall fuel and killed the wasm host.
- Frame callbacks are held while the overlay/editor freeze the screen, so apps
  behind stop drawing (0% CPU vs 52% with a full-screen animating app).
- wasmhost: error buffer 16 KB so trap causes aren't cut off; image-pixels
  limit 1024 -> 16384 a side (both hosts).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hh3TaFJh7nhCxAjV4Q
This commit is contained in:
Claude committed 2026-11-26 05:37:03 +00:00
1 parent 76a23f7816
commit 77fcc4db1c
14 files changed
+2127 -67

No files matched your search

+106 -55
View File
@@ -12,9 +12,10 @@
/// offers the same four modes.
///
/// The picture is what was on screen when the shortcut was pressed (the overlay is never in it).
/// It is saved as ~/Pictures/Screenshots/Screenshot_YYYYMMDD_HHMMSS.png (XDG_PICTURES_DIR when
/// It opens in the screenshot editor (shot_editor.mbt) for annotating, cropping and blurring;
/// from there it is saved as ~/Pictures/Screenshots/Screenshot_YYYYMMDD_HHMMSS.png (XDG_PICTURES_DIR when
/// user-dirs.dirs names one), put on the clipboard as image/png, and a notification with a
/// thumbnail confirms it for a few seconds.
/// thumbnail confirms it for a few seconds. Esc in the editor discards it.
///| evdev codes (keys on the overlay; the shortcuts themselves are in the shortcut table,
/// compositor/shell/shortcuts.mbt, and can be rebound in Settings > Shortcuts).
@@ -255,7 +256,7 @@ fn ChandraCompositor::active_window(self : ChandraCompositor) -> UInt64? {
///| Take a screenshot in `mode`: at once, or by opening the overlay (Region, or a window pick
/// when no window is under the pointer).
pub fn ChandraCompositor::take_screenshot(self : ChandraCompositor, mode : ShotMode) -> Unit {
if self.shot is Some(_) {
if self.shot is Some(_) || self.shot_edit is Some(_) {
return
}
let frame = match self.freeze_screen() {
@@ -304,6 +305,8 @@ fn ChandraCompositor::open_overlay(self : ChandraCompositor, frame : @renderer.P
let prev_focus = self.kbd_win
self.leave_pointer()
self.set_kbd_focus(None)
// Clients stop drawing while the screen is frozen (they are sent their frame callbacks after).
self.server.set_hold_frames(true)
self.shot = Some({
frame,
pick,
@@ -326,31 +329,52 @@ fn ChandraCompositor::open_overlay(self : ChandraCompositor, frame : @renderer.P
///| Take the overlay down: give the keyboard back and show what clients drew meanwhile.
fn ChandraCompositor::close_overlay(self : ChandraCompositor) -> Unit {
self.close_overlay_to(false)
}
///| Give the keyboard back to `prev` (the window that had it before a screenshot), or to the
/// active window when that one has gone.
fn ChandraCompositor::restore_focus(self : ChandraCompositor, prev : UInt64?) -> Unit {
match prev {
Some(w) =>
if self.windows.contains(w) {
self.set_kbd_focus(Some(w))
} else {
match self.active_window() {
Some(a) => self.set_kbd_focus(Some(a))
None => ()
}
}
None => ()
}
}
///| Show what clients drew while the screen was frozen (under the overlay or the editor), and let
/// them draw again.
fn ChandraCompositor::thaw_windows(self : ChandraCompositor) -> Unit {
for wid, snap in self.shot_frozen {
match self.windows.get(wid) {
Some(win) => {
release_snapshot(win.surface)
win.surface = Some(snap)
self.thumb_stale(wid)
}
None => release_snapshot(Some(snap))
}
}
self.shot_frozen.clear()
self.server.set_hold_frames(false)
}
///| Take the overlay down. `to_editor`: the editor opens next, keeping the keyboard and the
/// frozen screen behind it; else give the keyboard back and show what clients drew meanwhile.
fn ChandraCompositor::close_overlay_to(self : ChandraCompositor, to_editor : Bool) -> Unit {
match self.shot {
Some(o) => {
self.shot = None
for wid, snap in self.shot_frozen {
match self.windows.get(wid) {
Some(win) => {
release_snapshot(win.surface)
win.surface = Some(snap)
self.thumb_stale(wid)
}
None => release_snapshot(Some(snap))
}
}
self.shot_frozen.clear()
match o.prev_focus {
Some(w) =>
if self.windows.contains(w) {
self.set_kbd_focus(Some(w))
} else {
match self.active_window() {
Some(a) => self.set_kbd_focus(Some(a))
None => ()
}
}
None => ()
if !to_editor {
self.thaw_windows()
self.restore_focus(o.prev_focus)
}
self.damage.mark_full()
}
@@ -373,52 +397,61 @@ fn ShotOverlay::window_at(self : ShotOverlay, x : Int, y : Int) -> @scene.Rect?
None
}
///| Crop `r` out of `frame`, save it, put it on the clipboard and confirm with a notification.
///| Crop `r` out of `frame` and open it in the screenshot editor (or start recording `r`, when
/// the overlay was picking what to record).
fn ChandraCompositor::finish_screenshot(self : ChandraCompositor, frame : @renderer.Pixmap, r : @scene.Rect) -> Unit {
let record = match self.shot {
Some(o) => o.record
None => false
let (record, prev_focus) = match self.shot {
Some(o) => (o.record, o.prev_focus)
None => (false, self.kbd_win)
}
self.close_overlay()
if record {
self.close_overlay()
self.start_recording(r)
return
}
self.close_overlay_to(true)
let x0 = if r.x < 0 { 0 } else { r.x }
let y0 = if r.y < 0 { 0 } else { r.y }
let x1 = if r.x + r.w > frame.width { frame.width } else { r.x + r.w }
let y1 = if r.y + r.h > frame.height { frame.height } else { r.y + r.h }
if x1 <= x0 || y1 <= y0 {
self.thaw_windows()
self.restore_focus(prev_focus)
return
}
let w = x1 - x0
let h = y1 - y0
// `shot` is what the thumbnail is made from: the picture, or a small copy from the host.
let (shot, png) = match self.frame_shots {
Some(host) => {
let crop = @scene.Rect::new(x0, y0, w, h)
let (tw, th) = thumb_size(w, h, 96, 60)
((host.pixels)(crop, tw, th), (host.png)(crop))
}
let crop = @scene.Rect::new(x0, y0, w, h)
// The picture, opaque (the framebuffer's alpha means nothing in a saved picture). When the
// host holds the frozen screen it hands over this part of it, and encodes it itself when it is
// saved unchanged.
let (pic, host_png) = match self.frame_shots {
Some(host) => ((host.pixels)(crop, w, h), Some(fn() { (host.png)(crop) }))
None => {
let data = FixedArray::make(w * h * 4, b'\x00')
for y = 0; y < h; y = y + 1 {
FixedArray::blit_to(frame.data, data, len=w * 4, src_offset=((y0 + y) * frame.width + x0) * 4, dst_offset=y * w * 4)
}
// Fully opaque: the framebuffer's alpha means nothing in a saved picture.
for i = 3; i < data.length(); i = i + 4 {
data[i] = b'\xff'
}
let shot : @renderer.Pixmap = { width: w, height: h, data }
(shot, @renderer.encode_png_compressed(shot))
({ width: w, height: h, data }, None)
}
}
self.open_editor(pic, prev_focus, host_png)
}
///| Put `png` (the picture `pic`) on the clipboard, save it when `save`, and confirm with a
/// notification.
fn ChandraCompositor::deliver_screenshot(self : ChandraCompositor, pic : @renderer.Pixmap, png : Bytes, save : Bool) -> Unit {
self.server.set_own_selection(["image/png"], png)
let dir = if self.shot_dir.length() > 0 { self.shot_dir } else { pictures_dir() + "/Screenshots" }
let path = save_png(dir, png)
let path = if save { save_png(dir, png) } else { "" }
self.last_shot = path
let (thumb, tw, th) = thumbnail_ops(shot, 96, 60)
let (title, detail) = if path.length() > 0 {
let (thumb, tw, th) = thumbnail_ops(pic, 96, 60)
let (title, detail) = if !save {
("Screenshot copied to clipboard", "Not saved")
} else if path.length() > 0 {
("Screenshot saved", base_name(path) + " · copied to clipboard")
} else {
("Screenshot copied to clipboard", "Couldn't save it to " + dir)
@@ -557,22 +590,30 @@ fn ChandraCompositor::screenshot_input(self : ChandraCompositor, ev : @nested.In
Key(code, pressed) => self.screenshot_key(code, pressed)
Modifiers(_, _, _, _) => false
PointerMove(x, y) =>
match self.shot {
Some(o) => {
match (self.shot, self.shot_edit) {
(Some(o), _) => {
self.ptr_x = x
self.ptr_y = y
self.overlay_motion(o, x.to_int(), y.to_int())
true
}
None => false
(None, Some(e)) => {
self.editor_input(e, ev)
true
}
(None, None) => false
}
PointerButton(button, pressed) =>
match self.shot {
Some(o) => {
match (self.shot, self.shot_edit) {
(Some(o), _) => {
self.overlay_button(o, button, pressed, time_ms)
true
}
None =>
(None, Some(e)) => {
self.editor_input(e, ev)
true
}
(None, None) =>
if pressed && button == 272U && self.recording_click(self.ptr_x.to_int(), self.ptr_y.to_int()) {
self.rec_press = true
true
@@ -583,14 +624,15 @@ fn ChandraCompositor::screenshot_input(self : ChandraCompositor, ev : @nested.In
false
}
}
Scroll(_, _, _) => self.shot is Some(_)
Scroll(_, _, _) => self.shot is Some(_) || self.shot_edit is Some(_)
}
}
///| A key, for the overlay.
fn ChandraCompositor::screenshot_key(self : ChandraCompositor, code : UInt, pressed : Bool) -> Bool {
let modal = self.shot is Some(_) || self.shot_edit is Some(_)
if code == key_lsuper || code == key_rsuper {
if self.shot is Some(_) {
if modal {
self.super_solo = false
return true
}
@@ -602,16 +644,21 @@ fn ChandraCompositor::screenshot_key(self : ChandraCompositor, code : UInt, pres
self.shot_swallow.retain(c => c != code)
return true
}
return self.shot is Some(_)
return modal
}
match self.shot {
Some(o) => {
match (self.shot, self.shot_edit) {
(Some(o), _) => {
self.shot_swallow.push(code)
self.overlay_key(o, code, (self.mods & mod_shift) != 0U, (self.mods & mod_ctrl) != 0U)
true
}
(None, Some(e)) => {
self.shot_swallow.push(code)
self.editor_key(e, code)
true
}
// The shortcuts that start screenshots and recordings are in the shortcut table (shortcuts.mbt).
None => false
(None, None) => false
}
}
@@ -828,6 +875,10 @@ fn ChandraCompositor::screenshot_ops(self : ChandraCompositor) -> Array[@scene.D
Some(o) => self.overlay_ops(o, ops)
None => ()
}
match self.shot_edit {
Some(e) => self.editor_ops(e, ops)
None => ()
}
match self.shot_toast {
Some(t) =>
if @sys.monotonic_ms() >= t.until {