Screenshot editor: annotate, blur, pixelate, redact and crop before saving
CI / test (push) Canceled after 12m58s
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:
14 files changed
+2127
-67
No files matched your search
@@ -0,0 +1,864 @@
|
||||
///| The screenshot editor, after KDE Spectacle's annotation mode. Every screenshot opens here
|
||||
/// once it is taken (screenshot.mbt `finish_screenshot`): the picture fills the screen, with the
|
||||
/// tools above it and the colours, sizes, Undo/Redo and Discard / Copy / Save below.
|
||||
///
|
||||
/// Tools: Arrow, Line, Rectangle, Ellipse, Pen, Highlighter, Text, Number (numbered markers),
|
||||
/// Blur, Pixelate, Redact and Crop (annotate.mbt draws them). Drag to draw; Text and Number take
|
||||
/// a click (type, then Enter or a click elsewhere). Keys: Enter or Ctrl+S saves the picture (where
|
||||
/// screenshots always go) and copies it, Ctrl+C only copies it, Esc discards it; Ctrl+Z undoes,
|
||||
/// Ctrl+Shift+Z or Ctrl+Y redoes; A L R E P H T N B X D C pick a tool, 1-8 a colour, [ and ]
|
||||
/// the size.
|
||||
///
|
||||
/// The editor is drawn by the compositor, on top of everything, and has the keyboard while it is
|
||||
/// up (the window that had it gets it back). Windows behind it stay as they were: what clients
|
||||
/// draw meanwhile is shown when it closes, and their frame callbacks wait until then, so they
|
||||
/// stop drawing and cost nothing. Each finished annotation is drawn once into a full
|
||||
/// size copy of the picture, which is shown as one image, so idle costs nothing and a frame
|
||||
/// draws only the annotation being made.
|
||||
|
||||
///| What a click on the editor's bars does.
|
||||
priv enum EditHit {
|
||||
PickTool(Tool)
|
||||
PickColor(Int)
|
||||
PickSize(Int)
|
||||
Undo
|
||||
Redo
|
||||
Discard
|
||||
CopyOnly
|
||||
Save
|
||||
} derive(Eq)
|
||||
|
||||
///| The colours on offer.
|
||||
let edit_colors : Array[@scene.Rgba] = [
|
||||
@scene.Rgba::from_hex("#e53935"),
|
||||
@scene.Rgba::from_hex("#fb8c00"),
|
||||
@scene.Rgba::from_hex("#fdd835"),
|
||||
@scene.Rgba::from_hex("#43a047"),
|
||||
@scene.Rgba::from_hex("#1e88e5"),
|
||||
@scene.Rgba::from_hex("#8e24aa"),
|
||||
@scene.Rgba::from_hex("#000000"),
|
||||
@scene.Rgba::from_hex("#ffffff"),
|
||||
]
|
||||
|
||||
///| Stroke widths: thin, medium, thick.
|
||||
let edit_widths : Array[Int] = [2, 4, 8]
|
||||
|
||||
///| The tools in toolbar order: label and key (evdev code).
|
||||
let edit_tools : Array[(Tool, String, UInt)] = [
|
||||
(Tool::Arrow, "Arrow", 30U),
|
||||
(Tool::Line, "Line", 38U),
|
||||
(Tool::Rectangle, "Rectangle", 19U),
|
||||
(Tool::Ellipse, "Ellipse", 18U),
|
||||
(Tool::Pen, "Pen", 25U),
|
||||
(Tool::Highlighter, "Highlighter", 35U),
|
||||
(Tool::Text, "Text", 20U),
|
||||
(Tool::Number, "Number", 49U),
|
||||
(Tool::Blur, "Blur", 48U),
|
||||
(Tool::Pixelate, "Pixelate", 45U),
|
||||
(Tool::Redact, "Redact", 32U),
|
||||
(Tool::Crop, "Crop", 46U),
|
||||
]
|
||||
|
||||
///| evdev codes the editor uses beyond the tool letters.
|
||||
let key_s : UInt = 31
|
||||
|
||||
///|
|
||||
let key_z : UInt = 44
|
||||
|
||||
///|
|
||||
let key_y : UInt = 21
|
||||
|
||||
///|
|
||||
let key_lbrace : UInt = 26
|
||||
|
||||
///|
|
||||
let key_rbrace : UInt = 27
|
||||
|
||||
///| The editor while it is up.
|
||||
pub(all) struct ShotEditor {
|
||||
/// The picture as taken (opaque).
|
||||
base : @renderer.Pixmap
|
||||
/// `base` with every annotation in `anns` drawn in (crops aside): what is shown and saved.
|
||||
mut rendered : @renderer.Pixmap
|
||||
anns : Array[Annotation]
|
||||
/// Undone annotations, the last undone last.
|
||||
redo : Array[Annotation]
|
||||
mut tool : Tool
|
||||
mut color : Int
|
||||
mut size : Int
|
||||
/// The annotation being drawn (or typed, when `typing`).
|
||||
mut cur : Annotation?
|
||||
mut typing : Bool
|
||||
/// The window that had the keyboard before, given it back afterwards.
|
||||
prev_focus : UInt64?
|
||||
/// The host's PNG of the picture as taken (the host keeps the frozen screen), used when
|
||||
/// nothing was changed; None encodes `rendered`.
|
||||
host_png : (() -> Bytes)?
|
||||
/// Where the picture is drawn and at what scale (picture pixels to screen pixels).
|
||||
mut view : @scene.Rect
|
||||
mut scale : Double
|
||||
/// What the renderer holds of the picture: its size, rows per image and how many images.
|
||||
mut shown : (Int, Int, Int, Int)
|
||||
priv hits : Array[(@scene.Rect, EditHit)]
|
||||
}
|
||||
|
||||
///| The annotations made so far (undone ones not included).
|
||||
pub fn ShotEditor::annotations(self : ShotEditor) -> Array[Annotation] {
|
||||
self.anns
|
||||
}
|
||||
|
||||
///| The picture with the annotations so far, before cropping.
|
||||
pub fn ShotEditor::picture(self : ShotEditor) -> @renderer.Pixmap {
|
||||
self.rendered
|
||||
}
|
||||
|
||||
///| The tool in use.
|
||||
pub fn ShotEditor::current_tool(self : ShotEditor) -> Tool {
|
||||
self.tool
|
||||
}
|
||||
|
||||
///| What would be saved now.
|
||||
pub fn ShotEditor::export(self : ShotEditor) -> @renderer.Pixmap {
|
||||
crop_pixmap(self.rendered, final_crop(self.rendered.width, self.rendered.height, self.anns))
|
||||
}
|
||||
|
||||
///| Where the picture is on screen, and its scale.
|
||||
pub fn ShotEditor::view(self : ShotEditor) -> (@scene.Rect, Double) {
|
||||
(self.view, self.scale)
|
||||
}
|
||||
|
||||
///| Whether the editor is up.
|
||||
pub fn ChandraCompositor::screenshot_editing(self : ChandraCompositor) -> Bool {
|
||||
self.shot_edit is Some(_)
|
||||
}
|
||||
|
||||
///| The editor, while it is up.
|
||||
pub fn ChandraCompositor::screenshot_editor(self : ChandraCompositor) -> ShotEditor? {
|
||||
self.shot_edit
|
||||
}
|
||||
|
||||
///| The most bytes sent to the renderer in one image: the wasm host copies a call's data under a
|
||||
/// fixed budget (wasmtime's hostcall fuel), which a whole 1920 x 1080 picture (8 MB) runs out.
|
||||
let strip_bytes : Int = 2 * 1024 * 1024
|
||||
|
||||
///| Show the picture in the editor: `rendered`, scaled to the size it is drawn at, sent to
|
||||
/// whatever draws the screen as images of a few rows each (`strip_bytes`), drawn one under the
|
||||
/// other at 1:1 so they meet without seams.
|
||||
fn ChandraCompositor::editor_upload(self : ChandraCompositor, e : ShotEditor) -> Unit {
|
||||
let vw = e.view.w
|
||||
let vh = e.view.h
|
||||
let p = if vw == e.rendered.width && vh == e.rendered.height {
|
||||
e.rendered
|
||||
} else {
|
||||
@renderer.scale_pixmap(e.rendered, vw, vh)
|
||||
}
|
||||
let rows = (strip_bytes / (vw * 4)).max(1)
|
||||
let n = (vh + rows - 1) / rows
|
||||
let imgs = self.shot_edit_images
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
let y0 = i * rows
|
||||
let rh = rows.min(vh - y0)
|
||||
let reuse = if i < imgs.length() { imgs[i] } else { 0U }
|
||||
let strip = FixedArray::make(vw * rh * 4, b'\x00')
|
||||
FixedArray::blit_to(p.data, strip, len=vw * rh * 4, src_offset=y0 * vw * 4)
|
||||
let px = Bytes::from_fixedarray(strip)
|
||||
let h = @raster.image_pixels(vw, rh, px, reuse)
|
||||
if h == 0U {
|
||||
// image-pixels refuses only sizes over 16384 pixels a side, which no screen has.
|
||||
@sys.log("screenshot editor: the renderer refused a " + vw.to_string() + "x" + rh.to_string() + " image")
|
||||
self.shot_toast = Some({
|
||||
title: "Screenshot editor can't show this picture",
|
||||
detail: "the renderer refused a " + vw.to_string() + "x" + rh.to_string() + " image",
|
||||
until: @sys.monotonic_ms() + 8000UL,
|
||||
thumb: [],
|
||||
tw: 0,
|
||||
th: 0,
|
||||
})
|
||||
}
|
||||
if i < imgs.length() {
|
||||
imgs[i] = h
|
||||
} else {
|
||||
imgs.push(h)
|
||||
}
|
||||
}
|
||||
e.shown = (vw, vh, rows, n)
|
||||
self.damage.mark_full()
|
||||
}
|
||||
|
||||
///| Open the editor on `pic` (an opaque picture just taken). `prev_focus`: the window to give
|
||||
/// the keyboard back to.
|
||||
fn ChandraCompositor::open_editor(
|
||||
self : ChandraCompositor,
|
||||
pic : @renderer.Pixmap,
|
||||
prev_focus : UInt64?,
|
||||
host_png : (() -> Bytes)?,
|
||||
) -> Unit {
|
||||
// Client menus would be drawn over the editor.
|
||||
let mut j = self.popups.length() - 1
|
||||
while j >= 0 {
|
||||
self.server.popup_done(self.popups[j].id)
|
||||
j = j - 1
|
||||
}
|
||||
self.leave_pointer()
|
||||
self.set_kbd_focus(None)
|
||||
self.server.set_hold_frames(true)
|
||||
let e : ShotEditor = {
|
||||
base: pic,
|
||||
rendered: copy_pixmap(pic),
|
||||
anns: [],
|
||||
redo: [],
|
||||
tool: self.shot_edit_tool,
|
||||
color: self.shot_edit_color,
|
||||
size: self.shot_edit_size,
|
||||
cur: None,
|
||||
typing: false,
|
||||
prev_focus,
|
||||
host_png,
|
||||
view: @scene.Rect::new(0, 0, 0, 0),
|
||||
scale: 1.0,
|
||||
shown: (0, 0, 1, 0),
|
||||
hits: [],
|
||||
}
|
||||
self.shot_edit = Some(e)
|
||||
self.editor_layout(e)
|
||||
self.editor_upload(e)
|
||||
}
|
||||
|
||||
///| Take the editor down and give the keyboard back.
|
||||
fn ChandraCompositor::close_editor(self : ChandraCompositor) -> Unit {
|
||||
match self.shot_edit {
|
||||
Some(e) => {
|
||||
self.shot_edit = None
|
||||
// Remember the tool, colour and size for the next screenshot.
|
||||
self.shot_edit_tool = e.tool
|
||||
self.shot_edit_color = e.color
|
||||
self.shot_edit_size = e.size
|
||||
// Let the renderer drop the picture's memory (the handles are kept for the next one).
|
||||
let imgs = self.shot_edit_images
|
||||
for i = 0; i < imgs.length(); i = i + 1 {
|
||||
if imgs[i] != 0U {
|
||||
imgs[i] = @raster.image_pixels(1, 1, b"\x00\x00\x00\x00", imgs[i])
|
||||
}
|
||||
}
|
||||
self.thaw_windows()
|
||||
self.restore_focus(e.prev_focus)
|
||||
self.damage.mark_full()
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///| Discard the picture (Esc).
|
||||
pub fn ChandraCompositor::discard_screenshot(self : ChandraCompositor) -> Unit {
|
||||
self.close_editor()
|
||||
}
|
||||
|
||||
///| Copy the picture to the clipboard and, when `save`, save it; confirm with a notification.
|
||||
fn ChandraCompositor::editor_finish(self : ChandraCompositor, e : ShotEditor, save : Bool) -> Unit {
|
||||
self.commit_typing(e)
|
||||
let (pic, png) = match e.host_png {
|
||||
Some(f) if e.anns.length() == 0 => (e.base, f())
|
||||
_ => {
|
||||
let p = e.export()
|
||||
(p, @renderer.encode_png_compressed(p))
|
||||
}
|
||||
}
|
||||
self.close_editor()
|
||||
self.deliver_screenshot(pic, png, save)
|
||||
}
|
||||
|
||||
///| A point on screen as a picture pixel, kept on the picture.
|
||||
fn ShotEditor::to_pic(self : ShotEditor, x : Int, y : Int) -> (Int, Int) {
|
||||
let px = ((x - self.view.x).to_double() / self.scale).floor().to_int()
|
||||
let py = ((y - self.view.y).to_double() / self.scale).floor().to_int()
|
||||
(px.clamp(min=0, max=self.base.width - 1), py.clamp(min=0, max=self.base.height - 1))
|
||||
}
|
||||
|
||||
///| A picture position on screen.
|
||||
fn ShotEditor::sx(self : ShotEditor, x : Double) -> Int {
|
||||
(self.view.x.to_double() + x * self.scale).round().to_int()
|
||||
}
|
||||
|
||||
///|
|
||||
fn ShotEditor::sy(self : ShotEditor, y : Double) -> Int {
|
||||
(self.view.y.to_double() + y * self.scale).round().to_int()
|
||||
}
|
||||
|
||||
///| A picture rectangle on screen.
|
||||
fn ShotEditor::srect(self : ShotEditor, r : @scene.Rect) -> @scene.Rect {
|
||||
let x0 = self.sx(r.x.to_double())
|
||||
let y0 = self.sy(r.y.to_double())
|
||||
let x1 = self.sx((r.x + r.w).to_double())
|
||||
let y1 = self.sy((r.y + r.h).to_double())
|
||||
@scene.Rect::new(x0, y0, x1 - x0, y1 - y0)
|
||||
}
|
||||
|
||||
///| The toolbar's buttons' height, and the gap around the bars.
|
||||
let bar_h : Int = 32
|
||||
|
||||
///|
|
||||
let bar_gap : Int = 12
|
||||
|
||||
///| Lay out the bars and the picture for the screen's size (`hits` and `view`).
|
||||
fn ChandraCompositor::editor_layout(self : ChandraCompositor, e : ShotEditor) -> Unit {
|
||||
let sw = self.output_w
|
||||
let sh = self.output_h
|
||||
let fs = self.theme.font_size
|
||||
e.hits.clear()
|
||||
// Top: the tools, centred.
|
||||
let pad = 12
|
||||
let widths = edit_tools.map(t => @renderer.text_width(t.1, fs, false, false) + pad * 2)
|
||||
let total = widths.fold(init=0, (a, w) => a + w) + 4 * (widths.length() - 1)
|
||||
let mut x = ((sw - total) / 2).max(4)
|
||||
for i, t in edit_tools {
|
||||
e.hits.push((@scene.Rect::new(x, bar_gap, widths[i], bar_h), EditHit::PickTool(t.0)))
|
||||
x = x + widths[i] + 4
|
||||
}
|
||||
// Bottom left: colours, sizes, Undo, Redo.
|
||||
let by = sh - bar_gap - bar_h
|
||||
let mut bx = bar_gap
|
||||
for i = 0; i < edit_colors.length(); i = i + 1 {
|
||||
e.hits.push((@scene.Rect::new(bx, by, bar_h, bar_h), EditHit::PickColor(i)))
|
||||
bx = bx + bar_h + 2
|
||||
}
|
||||
bx = bx + 14
|
||||
for i = 0; i < edit_widths.length(); i = i + 1 {
|
||||
e.hits.push((@scene.Rect::new(bx, by, bar_h, bar_h), EditHit::PickSize(i)))
|
||||
bx = bx + bar_h + 2
|
||||
}
|
||||
bx = bx + 14
|
||||
for b in [("Undo", EditHit::Undo), ("Redo", EditHit::Redo)] {
|
||||
let w = @renderer.text_width(b.0, fs, false, false) + pad * 2
|
||||
e.hits.push((@scene.Rect::new(bx, by, w, bar_h), b.1))
|
||||
bx = bx + w + 4
|
||||
}
|
||||
// Bottom right: Discard, Copy, Save.
|
||||
let mut rx = sw - bar_gap
|
||||
for b in [("Save", EditHit::Save), ("Copy", EditHit::CopyOnly), ("Discard", EditHit::Discard)] {
|
||||
let w = @renderer.text_width(b.0, fs, true, false) + pad * 2 + 8
|
||||
rx = rx - w
|
||||
e.hits.push((@scene.Rect::new(rx, by, w, bar_h), b.1))
|
||||
rx = rx - 6
|
||||
}
|
||||
// The picture: as big as fits between the bars, never enlarged.
|
||||
let top = bar_gap + bar_h + fs + 20
|
||||
let bottom = by - 12
|
||||
let aw = (sw - 2 * bar_gap).max(1)
|
||||
let ah = (bottom - top).max(1)
|
||||
let pw = e.base.width
|
||||
let ph = e.base.height
|
||||
let scale = (aw.to_double() / pw.to_double()).min(ah.to_double() / ph.to_double()).min(1.0)
|
||||
let vw = (pw.to_double() * scale).round().to_int().max(1)
|
||||
let vh = (ph.to_double() * scale).round().to_int().max(1)
|
||||
e.view = @scene.Rect::new((sw - vw) / 2, top + (ah - vh) / 2, vw, vh)
|
||||
e.scale = vw.to_double() / pw.to_double()
|
||||
}
|
||||
|
||||
///| The button under (x, y).
|
||||
fn ShotEditor::hit_at(self : ShotEditor, x : Int, y : Int) -> EditHit? {
|
||||
for h in self.hits {
|
||||
if h.0.contains(x, y) {
|
||||
return Some(h.1)
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
///| A new annotation with the current tool, colour and size at `pt`.
|
||||
fn ShotEditor::start(self : ShotEditor, pt : (Int, Int), number : Int) -> Annotation {
|
||||
let color = if self.tool == Redact { edit_colors[self.color].with_alpha(255) } else { edit_colors[self.color] }
|
||||
{ tool: self.tool, color, width: edit_widths[self.size], points: [pt, pt], text: "", number }
|
||||
}
|
||||
|
||||
///| The next number marker's number.
|
||||
fn ShotEditor::next_number(self : ShotEditor) -> Int {
|
||||
self.anns.filter(a => a.tool == Number).length() + 1
|
||||
}
|
||||
|
||||
///| Add `a` to the picture.
|
||||
fn ChandraCompositor::editor_commit(self : ChandraCompositor, e : ShotEditor, a : Annotation) -> Unit {
|
||||
apply_annotation(e.rendered, a)
|
||||
e.anns.push(a)
|
||||
e.redo.clear()
|
||||
self.editor_upload(e)
|
||||
}
|
||||
|
||||
///| Redraw the picture from the one taken (after an undo).
|
||||
fn ChandraCompositor::editor_rebuild(self : ChandraCompositor, e : ShotEditor) -> Unit {
|
||||
let p = copy_pixmap(e.base)
|
||||
for a in e.anns {
|
||||
apply_annotation(p, a)
|
||||
}
|
||||
e.rendered = p
|
||||
self.editor_upload(e)
|
||||
}
|
||||
|
||||
///| Undo the last annotation.
|
||||
pub fn ChandraCompositor::editor_undo(self : ChandraCompositor) -> Unit {
|
||||
match self.shot_edit {
|
||||
Some(e) => {
|
||||
e.cur = None
|
||||
e.typing = false
|
||||
match e.anns.pop() {
|
||||
Some(a) => {
|
||||
e.redo.push(a)
|
||||
self.editor_rebuild(e)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///| Redo the last undone annotation.
|
||||
pub fn ChandraCompositor::editor_redo(self : ChandraCompositor) -> Unit {
|
||||
match self.shot_edit {
|
||||
Some(e) => {
|
||||
self.commit_typing(e)
|
||||
match e.redo.pop() {
|
||||
Some(a) => {
|
||||
apply_annotation(e.rendered, a)
|
||||
e.anns.push(a)
|
||||
self.editor_upload(e)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///| Finish the text being typed (dropped when empty).
|
||||
fn ChandraCompositor::commit_typing(self : ChandraCompositor, e : ShotEditor) -> Unit {
|
||||
if !e.typing {
|
||||
return
|
||||
}
|
||||
e.typing = false
|
||||
match e.cur {
|
||||
Some(a) => {
|
||||
e.cur = None
|
||||
if a.text.length() > 0 {
|
||||
self.editor_commit(e, a)
|
||||
} else {
|
||||
self.damage.mark_full()
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///| Pick a tool (ends any text being typed).
|
||||
fn ChandraCompositor::editor_pick_tool(self : ChandraCompositor, e : ShotEditor, t : Tool) -> Unit {
|
||||
self.commit_typing(e)
|
||||
e.tool = t
|
||||
self.damage.mark_full()
|
||||
}
|
||||
|
||||
///| Do what a bar button does.
|
||||
fn ChandraCompositor::editor_hit(self : ChandraCompositor, e : ShotEditor, h : EditHit) -> Unit {
|
||||
match h {
|
||||
PickTool(t) => self.editor_pick_tool(e, t)
|
||||
PickColor(i) => {
|
||||
e.color = i
|
||||
self.damage.mark_full()
|
||||
}
|
||||
PickSize(i) => {
|
||||
e.size = i
|
||||
self.damage.mark_full()
|
||||
}
|
||||
Undo => self.editor_undo()
|
||||
Redo => self.editor_redo()
|
||||
Discard => self.close_editor()
|
||||
CopyOnly => self.editor_finish(e, false)
|
||||
Save => self.editor_finish(e, true)
|
||||
}
|
||||
}
|
||||
|
||||
///| Input while the editor is up: it takes everything.
|
||||
fn ChandraCompositor::editor_input(self : ChandraCompositor, e : ShotEditor, ev : @nested.InputEvent) -> Unit {
|
||||
match ev {
|
||||
PointerMove(x, y) => {
|
||||
self.ptr_x = x
|
||||
self.ptr_y = y
|
||||
if e.typing {
|
||||
return
|
||||
}
|
||||
match e.cur {
|
||||
Some(a) => {
|
||||
let pt = e.to_pic(x.to_int(), y.to_int())
|
||||
if a.tool == Pen || a.tool == Highlighter {
|
||||
if a.points[a.points.length() - 1] != pt {
|
||||
// The first two points start out the same: the press.
|
||||
if a.points.length() == 2 && a.points[0] == a.points[1] {
|
||||
a.points[1] = pt
|
||||
} else {
|
||||
a.points.push(pt)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
a.points[a.points.length() - 1] = pt
|
||||
}
|
||||
self.damage.mark_full()
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
PointerButton(button, pressed) => {
|
||||
let x = self.ptr_x.to_int()
|
||||
let y = self.ptr_y.to_int()
|
||||
if button == 273U {
|
||||
// Right button: drop what is being drawn.
|
||||
if pressed && e.cur is Some(_) {
|
||||
e.cur = None
|
||||
e.typing = false
|
||||
self.damage.mark_full()
|
||||
}
|
||||
return
|
||||
}
|
||||
if button != 272U {
|
||||
return
|
||||
}
|
||||
if pressed {
|
||||
match e.hit_at(x, y) {
|
||||
Some(h) => {
|
||||
self.editor_hit(e, h)
|
||||
return
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
if e.typing {
|
||||
// A click elsewhere finishes the text.
|
||||
self.commit_typing(e)
|
||||
return
|
||||
}
|
||||
let v = e.view
|
||||
if x < v.x - 8 || y < v.y - 8 || x >= v.x + v.w + 8 || y >= v.y + v.h + 8 {
|
||||
return
|
||||
}
|
||||
let pt = e.to_pic(x, y)
|
||||
match e.tool {
|
||||
Text => {
|
||||
e.cur = Some({ ..e.start(pt, 0), points: [pt] })
|
||||
e.typing = true
|
||||
}
|
||||
Number => self.editor_commit(e, { ..e.start(pt, e.next_number()), points: [pt] })
|
||||
_ => e.cur = Some(e.start(pt, 0))
|
||||
}
|
||||
self.damage.mark_full()
|
||||
} else {
|
||||
if e.typing {
|
||||
return
|
||||
}
|
||||
match e.cur {
|
||||
Some(a) => {
|
||||
e.cur = None
|
||||
if annotation_ready(a) {
|
||||
self.editor_commit(e, a)
|
||||
} else {
|
||||
self.damage.mark_full()
|
||||
}
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
///| Whether a drawn annotation is big enough to keep (a click without a drag isn't a shape).
|
||||
fn annotation_ready(a : Annotation) -> Bool {
|
||||
match a.tool {
|
||||
Pen | Highlighter => true
|
||||
Arrow | Line => {
|
||||
let (x0, y0) = a.points[0]
|
||||
let (x1, y1) = a.points[a.points.length() - 1]
|
||||
(x1 - x0).abs() + (y1 - y0).abs() >= 3
|
||||
}
|
||||
Text => a.text.length() > 0
|
||||
Number => true
|
||||
_ => {
|
||||
let r = a.rect()
|
||||
r.w >= 2 && r.h >= 2
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| A key press while the editor is up.
|
||||
fn ChandraCompositor::editor_key(self : ChandraCompositor, e : ShotEditor, code : UInt) -> Unit {
|
||||
let ctrl = (self.mods & mod_ctrl) != 0U
|
||||
let shift = (self.mods & mod_shift) != 0U
|
||||
if e.typing {
|
||||
match e.cur {
|
||||
Some(a) =>
|
||||
if code == key_esc {
|
||||
e.cur = None
|
||||
e.typing = false
|
||||
} else if code == key_enter || code == key_kp_enter {
|
||||
self.commit_typing(e)
|
||||
} else if code == key_backspace {
|
||||
// Drop the last character.
|
||||
let chars : Array[Char] = []
|
||||
for ch in a.text {
|
||||
chars.push(ch)
|
||||
}
|
||||
ignore(chars.pop())
|
||||
let buf = StringBuilder::new()
|
||||
for ch in chars {
|
||||
buf.write_char(ch)
|
||||
}
|
||||
a.text = buf.to_string()
|
||||
} else if !ctrl {
|
||||
let mods = self.mods | self.mods_latched | self.mods_locked
|
||||
match self.auth_keymap().char_for(code, mods, self.mods_group) {
|
||||
Some(ch) => if ch.to_int() >= 32 && ch.to_int() != 127 { a.text = a.text + ch.to_string() }
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
None => e.typing = false
|
||||
}
|
||||
self.damage.mark_full()
|
||||
return
|
||||
}
|
||||
if code == key_esc {
|
||||
if e.cur is Some(_) {
|
||||
e.cur = None
|
||||
self.damage.mark_full()
|
||||
} else {
|
||||
self.close_editor()
|
||||
}
|
||||
} else if code == key_enter || code == key_kp_enter || (ctrl && code == key_s) {
|
||||
self.editor_finish(e, true)
|
||||
} else if ctrl && code == key_c {
|
||||
self.editor_finish(e, false)
|
||||
} else if ctrl && code == key_z && !shift {
|
||||
self.editor_undo()
|
||||
} else if ctrl && ((code == key_z && shift) || code == key_y) {
|
||||
self.editor_redo()
|
||||
} else if ctrl {
|
||||
()
|
||||
} else if code >= 2U && code < 2U + edit_colors.length().reinterpret_as_uint() {
|
||||
e.color = (code - 2U).reinterpret_as_int()
|
||||
self.damage.mark_full()
|
||||
} else if code == key_lbrace {
|
||||
e.size = (e.size - 1).max(0)
|
||||
self.damage.mark_full()
|
||||
} else if code == key_rbrace {
|
||||
e.size = (e.size + 1).min(edit_widths.length() - 1)
|
||||
self.damage.mark_full()
|
||||
} else {
|
||||
for t in edit_tools {
|
||||
if t.2 == code {
|
||||
self.editor_pick_tool(e, t.0)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Cursor shape over the editor: a hand over buttons, an I-beam for text, else a crosshair
|
||||
/// over the picture.
|
||||
fn ChandraCompositor::editor_cursor(self : ChandraCompositor, e : ShotEditor) -> UInt {
|
||||
let x = self.ptr_x.to_int()
|
||||
let y = self.ptr_y.to_int()
|
||||
if e.hit_at(x, y) is Some(_) {
|
||||
return 4U // pointer
|
||||
}
|
||||
if e.view.contains(x, y) {
|
||||
return if e.tool == Text { 9U } else { 8U } // text / crosshair
|
||||
}
|
||||
1U
|
||||
}
|
||||
|
||||
///| A dashed outline of `r` (crop and region tools while they are drawn).
|
||||
fn dashed_rect(r : @scene.Rect, c : @scene.Rgba, ops : Array[@scene.DrawOp]) -> Unit {
|
||||
let dash = 6
|
||||
let mut x = r.x
|
||||
while x < r.x + r.w {
|
||||
let l = dash.min(r.x + r.w - x)
|
||||
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(x, r.y, l, 1), c, 0))
|
||||
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(x, r.y + r.h - 1, l, 1), c, 0))
|
||||
x = x + dash * 2
|
||||
}
|
||||
let mut y = r.y
|
||||
while y < r.y + r.h {
|
||||
let l = dash.min(r.y + r.h - y)
|
||||
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(r.x, y, 1, l), c, 0))
|
||||
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(r.x + r.w - 1, y, 1, l), c, 0))
|
||||
y = y + dash * 2
|
||||
}
|
||||
}
|
||||
|
||||
///| Draw ops showing annotation `a` (being drawn) over the picture: the same shapes `apply_annotation`
|
||||
/// will put in, at the picture's scale.
|
||||
fn ChandraCompositor::annotation_preview(self : ChandraCompositor, e : ShotEditor, a : Annotation, ops : Array[@scene.DrawOp]) -> Unit {
|
||||
let s = e.scale
|
||||
let lw = (a.width.to_double() * s).round().to_int().max(1)
|
||||
let pt = fn(p : (Int, Int)) { (e.sx(p.0.to_double() + 0.5), e.sy(p.1.to_double() + 0.5)) }
|
||||
let line = fn(p : (Int, Int), q : (Int, Int), c : @scene.Rgba, w : Int) {
|
||||
let (x0, y0) = pt(p)
|
||||
let (x1, y1) = pt(q)
|
||||
ops.push(@scene.DrawOp::Line(x0, y0, x1, y1, c, w))
|
||||
}
|
||||
let poly = fn(pts : Array[(Int, Int)], c : @scene.Rgba, w : Int) {
|
||||
if pts.length() == 1 {
|
||||
let (x, y) = pt(pts[0])
|
||||
ops.push(@scene.DrawOp::Circle(x, y, (w / 2).max(1), c, true))
|
||||
}
|
||||
for i = 1; i < pts.length(); i = i + 1 {
|
||||
line(pts[i - 1], pts[i], c, w)
|
||||
}
|
||||
}
|
||||
let first = a.points[0]
|
||||
let last = a.points[a.points.length() - 1]
|
||||
match a.tool {
|
||||
Line => line(first, last, a.color, lw)
|
||||
Arrow => {
|
||||
let (fx, fy) = centre(first)
|
||||
let (lx, ly) = centre(last)
|
||||
let (tip, b1, b2, shaft) = arrow_head(fx, fy, lx, ly, a.width)
|
||||
let (x0, y0) = pt(first)
|
||||
ops.push(@scene.DrawOp::Line(x0, y0, e.sx(shaft.0), e.sy(shaft.1), a.color, lw))
|
||||
// The head as a fan of lines from the tip to its base.
|
||||
let n = 12
|
||||
for i = 0; i <= n; i = i + 1 {
|
||||
let t = i.to_double() / n.to_double()
|
||||
let bx = b1.0 + (b2.0 - b1.0) * t
|
||||
let by = b1.1 + (b2.1 - b1.1) * t
|
||||
ops.push(@scene.DrawOp::Line(e.sx(tip.0), e.sy(tip.1), e.sx(bx), e.sy(by), a.color, 2))
|
||||
}
|
||||
}
|
||||
Rectangle => {
|
||||
let r = a.rect()
|
||||
poly([(r.x, r.y), (r.x + r.w, r.y), (r.x + r.w, r.y + r.h), (r.x, r.y + r.h), (r.x, r.y)], a.color, lw)
|
||||
}
|
||||
Ellipse => poly(ellipse_points(a.rect()), a.color, lw)
|
||||
Pen => poly(a.points, a.color, lw)
|
||||
Highlighter =>
|
||||
poly(a.points, a.color.with_alpha(highlighter_alpha), (highlighter_half(a.width) * 2.0 * s).round().to_int().max(1))
|
||||
Text => {
|
||||
let (x, y) = (e.sx(first.0.to_double()), e.sy(first.1.to_double()))
|
||||
let size = (annotation_text_size(a.width).to_double() * s).round().to_int().max(6)
|
||||
let tw = @renderer.text_width(a.text, size, false, false)
|
||||
ops.push(@scene.DrawOp::Text(x, y, a.text, size, a.color, @scene.TextAlign::Left, false))
|
||||
// The caret, and a dashed box around what is typed.
|
||||
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(x + tw + 1, y - size / 2 - 2, 2, size + 4), a.color, 0))
|
||||
dashed_rect(@scene.Rect::new(x - 4, y - size / 2 - 6, tw + 12, size + 12), self.theme.palette.accent, ops)
|
||||
}
|
||||
Number => ()
|
||||
Redact => ops.push(@scene.DrawOp::FillRect(e.srect(a.rect()), a.color.with_alpha(255), 0))
|
||||
Blur | Pixelate => {
|
||||
let r = e.srect(a.rect())
|
||||
ops.push(@scene.DrawOp::FillRect(r, @scene.Rgba::new(128, 128, 128, 140), 0))
|
||||
dashed_rect(r, self.theme.palette.accent, ops)
|
||||
if r.w > 60 && r.h > 24 {
|
||||
ops.push(@scene.DrawOp::Text(r.x + r.w / 2, r.y + r.h / 2, if a.tool == Blur { "Blur" } else { "Pixelate" }, self.theme.font_size, @scene.Rgba::new(255, 255, 255, 255), @scene.TextAlign::Center, true))
|
||||
}
|
||||
}
|
||||
Crop => ()
|
||||
}
|
||||
}
|
||||
|
||||
///| The editor's draw ops.
|
||||
fn ChandraCompositor::editor_ops(self : ChandraCompositor, e : ShotEditor, ops : Array[@scene.DrawOp]) -> Unit {
|
||||
self.editor_layout(e)
|
||||
let p = self.theme.palette
|
||||
let fs = self.theme.font_size
|
||||
let sw = self.output_w
|
||||
let sh = self.output_h
|
||||
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(0, 0, sw, sh), @scene.Rgba::new(28, 28, 32, 255), 0))
|
||||
// The picture.
|
||||
let v = e.view
|
||||
ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(v.x - 1, v.y - 1, v.w + 2, v.h + 2), @scene.Rgba::new(255, 255, 255, 40), 1, 0))
|
||||
if e.shown.0 != v.w || e.shown.1 != v.h {
|
||||
self.editor_upload(e) // the screen changed size
|
||||
}
|
||||
let (_, _, rows, n) = e.shown
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
let h = self.shot_edit_images[i]
|
||||
if h != 0U {
|
||||
ops.push(@scene.DrawOp::Image(@scene.Rect::new(v.x, v.y + i * rows, v.w, rows.min(v.h - i * rows)), h))
|
||||
}
|
||||
}
|
||||
// The crop: the one being drawn, else the last one made.
|
||||
let crop = match e.cur {
|
||||
Some(a) if a.tool == Crop => Some(a.rect())
|
||||
_ => {
|
||||
let r = final_crop(e.base.width, e.base.height, e.anns)
|
||||
if r.w == e.base.width && r.h == e.base.height { None } else { Some(r) }
|
||||
}
|
||||
}
|
||||
match crop {
|
||||
Some(r) => {
|
||||
let c = e.srect(r)
|
||||
let dim = @scene.Rgba::new(0, 0, 0, 150)
|
||||
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(v.x, v.y, v.w, c.y - v.y), dim, 0))
|
||||
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(v.x, c.y + c.h, v.w, v.y + v.h - c.y - c.h), dim, 0))
|
||||
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(v.x, c.y, c.x - v.x, c.h), dim, 0))
|
||||
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(c.x + c.w, c.y, v.x + v.w - c.x - c.w, c.h), dim, 0))
|
||||
dashed_rect(c, @scene.Rgba::new(255, 255, 255, 230), ops)
|
||||
ops.push(@scene.DrawOp::Text(c.x + 4, c.y - fs / 2 - 6, r.w.to_string() + " × " + r.h.to_string(), fs - 2, @scene.Rgba::new(255, 255, 255, 230), @scene.TextAlign::Left, false))
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
match e.cur {
|
||||
Some(a) => {
|
||||
ops.push(@scene.DrawOp::PushClip(v))
|
||||
self.annotation_preview(e, a, ops)
|
||||
ops.push(@scene.DrawOp::PopClip)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
// The bars.
|
||||
let hint = "Enter: save and copy Ctrl+C: copy Esc: discard Ctrl+Z / Ctrl+Shift+Z: undo / redo " +
|
||||
"A L R E P H T N B X D C: tools 1-8: colour [ ]: size"
|
||||
ops.push(@scene.DrawOp::Text(sw / 2, bar_gap + bar_h + 6 + fs / 2, hint, fs - 2, @scene.Rgba::new(200, 200, 210, 255), @scene.TextAlign::Center, false))
|
||||
for h in e.hits {
|
||||
let r = h.0
|
||||
let hover = r.contains(self.ptr_x.to_int(), self.ptr_y.to_int())
|
||||
match h.1 {
|
||||
PickTool(t) => {
|
||||
let on = t == e.tool
|
||||
let label = edit_tools.filter(x => x.0 == t)[0].1
|
||||
ops.push(@scene.DrawOp::FillRect(r, if on { p.accent } else if hover { p.surface_hover } else { p.surface_popup }, self.theme.radius))
|
||||
ops.push(@scene.DrawOp::Text(r.x + r.w / 2, r.y + r.h / 2, label, fs, if on { p.accent_foreground } else { p.foreground }, @scene.TextAlign::Center, on))
|
||||
}
|
||||
PickColor(i) => {
|
||||
if i == e.color {
|
||||
ops.push(@scene.DrawOp::FillRect(r, p.surface_popup, self.theme.radius))
|
||||
ops.push(@scene.DrawOp::StrokeRect(r, p.accent, 2, self.theme.radius))
|
||||
}
|
||||
ops.push(@scene.DrawOp::Circle(r.x + r.w / 2, r.y + r.h / 2, 10, edit_colors[i], true))
|
||||
ops.push(@scene.DrawOp::Circle(r.x + r.w / 2, r.y + r.h / 2, 10, @scene.Rgba::new(255, 255, 255, 90), false))
|
||||
}
|
||||
PickSize(i) => {
|
||||
ops.push(@scene.DrawOp::FillRect(r, if i == e.size { p.accent } else if hover { p.surface_hover } else { p.surface_popup }, self.theme.radius))
|
||||
let fg = if i == e.size { p.accent_foreground } else { p.foreground }
|
||||
ops.push(@scene.DrawOp::Circle(r.x + r.w / 2, r.y + r.h / 2, edit_widths[i] / 2 + 2, fg, true))
|
||||
}
|
||||
Undo | Redo | Discard | CopyOnly => {
|
||||
let label = match h.1 {
|
||||
Undo => "Undo"
|
||||
Redo => "Redo"
|
||||
Discard => "Discard"
|
||||
_ => "Copy"
|
||||
}
|
||||
let enabled = match h.1 {
|
||||
Undo => e.anns.length() > 0
|
||||
Redo => e.redo.length() > 0
|
||||
_ => true
|
||||
}
|
||||
ops.push(@scene.DrawOp::FillRect(r, if hover && enabled { p.surface_hover } else { p.surface_popup }, self.theme.radius))
|
||||
ops.push(@scene.DrawOp::Text(r.x + r.w / 2, r.y + r.h / 2, label, fs, if enabled { p.foreground } else { p.foreground_muted }, @scene.TextAlign::Center, false))
|
||||
}
|
||||
Save => {
|
||||
ops.push(@scene.DrawOp::FillRect(r, p.accent, self.theme.radius))
|
||||
ops.push(@scene.DrawOp::Text(r.x + r.w / 2, r.y + r.h / 2, "Save", fs, p.accent_foreground, @scene.TextAlign::Center, true))
|
||||
}
|
||||
}
|
||||
}
|
||||
// The picture's size (as it will be saved) in the middle of the bottom bar.
|
||||
let out = final_crop(e.base.width, e.base.height, e.anns)
|
||||
ops.push(@scene.DrawOp::Text(sw / 2, sh - bar_gap - bar_h / 2, out.w.to_string() + " × " + out.h.to_string(), fs - 2, @scene.Rgba::new(200, 200, 210, 255), @scene.TextAlign::Center, false))
|
||||
}
|
||||
Reference in new issue
Block a user