///| Screenshots, after KDE Spectacle. /// /// Super+Shift+S (or Super+Shift+Print) freezes the screen and puts a selection overlay over it, /// like Spectacle's "Rectangular Region": drag out a rectangle (its size is shown under it and a /// magnifier follows the pointer while drawing), drag inside it to move it, drag its handles to /// resize it, or just click a window to select that window. Enter or a double-click takes the /// picture, Escape cancels; F takes the whole screen and W the active window instead, the arrow /// keys nudge the selection (Shift+arrows resize it). /// /// The other Spectacle shortcuts (these are the defaults) take a picture at once: Print the full screen, Super+Print the /// active window, Super+Ctrl+Print the window under the pointer. The screenshot panel module /// 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 /// user-dirs.dirs names one), put on the clipboard as image/png, and a notification with a /// thumbnail confirms it for a few seconds. ///| 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). let key_enter : UInt = 28 ///| let key_kp_enter : UInt = 96 ///| let key_f : UInt = 33 ///| let key_w : UInt = 17 ///| let key_c : UInt = 46 ///| let key_p : UInt = 25 ///| xkb modifier bits (wl_keyboard.modifiers depressed mask). let mod_shift : UInt = 1 ///| let mod_ctrl : UInt = 4 ///| What a screenshot takes. pub(all) enum ShotMode { /// Draw a rectangle on the overlay (or click a window). Region FullScreen ActiveWindow /// The window under the pointer; when there is none there (the request came from the panel), /// the overlay asks for a click on one. WindowUnderCursor } derive(Eq, Debug) ///| The mode with number `i` (the screenshot capability's numbering), if any. pub fn ShotMode::from_int(i : Int) -> ShotMode? { match i { 0 => Some(Region) 1 => Some(FullScreen) 2 => Some(ActiveWindow) 3 => Some(WindowUnderCursor) _ => None } } ///| The selection overlay while it is up. pub(all) struct ShotOverlay { /// The frozen screen. When the host takes screenshots (`frame_shots`) its pixels stay there /// and `data` is empty. frame : @renderer.Pixmap /// Window-pick mode: a click takes the window under it, there is no rectangle. pick : Bool /// Picking what to record (recorder.mbt) rather than what to take a picture of. mut record : Bool /// Outer rectangles (decorations included) of the windows on screen, top-most first. wins : Array[@scene.Rect] /// The active window's rectangle, for W. active : @scene.Rect? mut sel : @scene.Rect? /// 0 nothing, 1 drawing a new rectangle, 2 moving it, 3 + h resizing by handle h. mut drag : Int mut ax : Int mut ay : Int mut orig : @scene.Rect mut moved : Bool mut last_press : UInt mut last_px : Int mut last_py : Int /// The window that had the keyboard before, given it back afterwards. prev_focus : UInt64? } ///| Screenshots taken by the host that draws the screen, so the screen's pixels never cross into /// the compositor: `snapshot` freezes the screen there, `png` crops the frozen screen to PNG /// bytes, and `pixels` reads a rectangle of it scaled to a size (magnifier, thumbnail). pub(all) struct FrameShots { snapshot : () -> Bool png : (@scene.Rect) -> Bytes pixels : (@scene.Rect, Int, Int) -> @renderer.Pixmap } ///| The confirmation shown after a screenshot. pub(all) struct ShotToast { title : String detail : String until : UInt64 thumb : Array[@scene.DrawOp] tw : Int th : Int } ///| Whether the selection overlay is up. pub fn ChandraCompositor::screenshot_active(self : ChandraCompositor) -> Bool { self.shot is Some(_) } ///| The current selection on the overlay. pub fn ChandraCompositor::screenshot_selection(self : ChandraCompositor) -> @scene.Rect? { match self.shot { Some(o) => o.sel None => None } } ///| The path of the last screenshot saved ("" when none was, or saving failed). pub fn ChandraCompositor::last_screenshot(self : ChandraCompositor) -> String { self.last_shot } ///| Whether the confirmation is on screen. pub fn ChandraCompositor::screenshot_toast_shown(self : ChandraCompositor) -> Bool { self.shot_toast is Some(_) } ///| A panel module asked for a screenshot: take it once a frame without the module's popup has /// been drawn, so the popup isn't in the picture. pub fn ChandraCompositor::request_screenshot(self : ChandraCompositor, mode : ShotMode) -> Unit { self.shot_request = Some((mode, self.frames_built)) } ///| What the screenshot capability asked for: 0-3 a screenshot in that mode (ShotMode::from_int), /// 10-12 start (or stop) recording a region, the screen or a window, 20 stop recording, 30/31 /// hide/show the pointer in recordings. pub fn ChandraCompositor::screenshot_request(self : ChandraCompositor, code : Int) -> Unit { match code { 0 | 1 | 2 | 3 => match ShotMode::from_int(code) { Some(m) => self.request_screenshot(m) None => () } 10 => self.rec_request = Some((ShotMode::Region, self.frames_built)) 11 => self.rec_request = Some((ShotMode::FullScreen, self.frames_built)) 12 => self.rec_request = Some((ShotMode::WindowUnderCursor, self.frames_built)) 20 => self.stop_recording() 30 => self.set_record_pointer(false) 31 => self.set_record_pointer(true) _ => () } } ///| Take a requested screenshot when its frame has been drawn (called from `step`). fn ChandraCompositor::screenshot_tick(self : ChandraCompositor) -> Unit { match self.shot_request { Some((mode, at)) => if self.frames_built > at { self.shot_request = None self.take_screenshot(mode) } None => () } match self.rec_request { Some((mode, at)) => if self.frames_built > at { self.rec_request = None self.toggle_recording(mode) } None => () } } ///| What is on screen now, as pixels: the host framebuffer (wasm compositor) or the last frame /// the software renderer drew. A copy, so later frames don't change it. fn ChandraCompositor::grab_screen(self : ChandraCompositor) -> @renderer.Pixmap? { let src = match self.frame_source { Some(f) => f() None => match self.last_frame { Some(p) => Some(p) None => Some(self.compose_frame()) } } match src { Some(p) => if p.width <= 0 || p.height <= 0 || p.data.length() < p.width * p.height * 4 { None } else { let data = FixedArray::make(p.width * p.height * 4, b'\x00') FixedArray::blit_to(p.data, data, len=data.length()) Some({ width: p.width, height: p.height, data }) } None => None } } ///| Freeze what is on screen for a screenshot: on the host when it takes them (an empty pixmap /// of the screen's size here), else a copy of the pixels. fn ChandraCompositor::freeze_screen(self : ChandraCompositor) -> @renderer.Pixmap? { match self.frame_shots { Some(host) => if (host.snapshot)() { Some({ width: self.output_w, height: self.output_h, data: FixedArray::make(0, b'\x00') }) } else { None } None => self.grab_screen() } } ///| A window's rectangle on screen with its title bar and borders (Spectacle's default). fn ChandraCompositor::outer_rect(self : ChandraCompositor, wid : UInt64) -> @scene.Rect? { match self.windows.get(wid) { Some(win) => { let g = win.geometry let r = if win.fullscreen { g } else { @scene.Rect::new(g.x - 2, g.y - 32, g.w + 4, g.h + 34) } Some(self.clip_to_screen(r)) } None => None } } ///| fn ChandraCompositor::clip_to_screen(self : ChandraCompositor, r : @scene.Rect) -> @scene.Rect { 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 > self.output_w { self.output_w } else { r.x + r.w } let y1 = if r.y + r.h > self.output_h { self.output_h } else { r.y + r.h } @scene.Rect::new(x0, y0, x1 - x0, y1 - y0) } ///| The focused window, if one is on screen. fn ChandraCompositor::active_window(self : ChandraCompositor) -> UInt64? { for wid in self.wm.stacking_order() { match self.windows.get(wid) { Some(w) => if w.focused { return Some(wid) } None => () } } None } ///| 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(_) { return } let frame = match self.freeze_screen() { Some(f) => f None => return } let full = @scene.Rect::new(0, 0, frame.width, frame.height) match mode { FullScreen => self.finish_screenshot(frame, full) ActiveWindow => match self.active_window().bind(w => self.outer_rect(w)) { Some(r) => self.finish_screenshot(frame, r) None => self.finish_screenshot(frame, full) } WindowUnderCursor => match self.window_at(self.ptr_x.to_int(), self.ptr_y.to_int()) { Some((wid, _)) => match self.outer_rect(wid) { Some(r) => self.finish_screenshot(frame, r) None => self.open_overlay(frame, true) } None => self.open_overlay(frame, true) } Region => self.open_overlay(frame, false) } } ///| Put the selection overlay up over the frozen `frame`. fn ChandraCompositor::open_overlay(self : ChandraCompositor, frame : @renderer.Pixmap, pick : Bool) -> Unit { let wins : Array[@scene.Rect] = [] let order = self.wm.stacking_order() let mut i = order.length() - 1 while i >= 0 { match self.outer_rect(order[i]) { Some(r) => if r.w > 0 && r.h > 0 { wins.push(r) } None => () } i = i - 1 } // Client menus would be drawn over the overlay: close them (they are in the picture). let mut j = self.popups.length() - 1 while j >= 0 { self.server.popup_done(self.popups[j].id) j = j - 1 } let prev_focus = self.kbd_win self.leave_pointer() self.set_kbd_focus(None) self.shot = Some({ frame, pick, record: false, wins, active: self.active_window().bind(w => self.outer_rect(w)), sel: None, drag: 0, ax: 0, ay: 0, orig: @scene.Rect::new(0, 0, 0, 0), moved: false, last_press: 0U, last_px: -100, last_py: -100, prev_focus, }) self.damage.mark_full() } ///| Take the overlay down: give the keyboard back and show what clients drew meanwhile. fn ChandraCompositor::close_overlay(self : ChandraCompositor) -> 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 => () } self.damage.mark_full() } None => () } } ///| Cancel the overlay without taking a picture. pub fn ChandraCompositor::cancel_screenshot(self : ChandraCompositor) -> Unit { self.close_overlay() } ///| The top-most window rectangle under (x, y) on the overlay. fn ShotOverlay::window_at(self : ShotOverlay, x : Int, y : Int) -> @scene.Rect? { for r in self.wins { if x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h { return Some(r) } } None } ///| Crop `r` out of `frame`, save it, put it on the clipboard and confirm with a notification. fn ChandraCompositor::finish_screenshot(self : ChandraCompositor, frame : @renderer.Pixmap, r : @scene.Rect) -> Unit { let record = match self.shot { Some(o) => o.record None => false } self.close_overlay() if record { self.start_recording(r) return } 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 { 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)) } 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)) } } 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) self.last_shot = path let (thumb, tw, th) = thumbnail_ops(shot, 96, 60) let (title, detail) = if path.length() > 0 { ("Screenshot saved", base_name(path) + " · copied to clipboard") } else { ("Screenshot copied to clipboard", "Couldn't save it to " + dir) } self.shot_toast = Some({ title, detail, until: @sys.monotonic_ms() + 5000UL, thumb, tw, th }) self.damage.mark_full() } ///| The last part of a path. fn base_name(path : String) -> String { match path.rev_find("/") { Some(i) => path[i + 1:].to_string() None => path } } ///| A folder named in user-dirs.dirs (`key`: XDG_PICTURES_DIR, XDG_VIDEOS_DIR), if it names one. fn user_dir(key : String) -> String? { let home = @sys.getenv("HOME") let cfg = match @sys.getenv("XDG_CONFIG_HOME") { "" => home + "/.config" c => c } let text = @utf8.decode_lossy(@sys.read_file_bytes(cfg + "/user-dirs.dirs")) let prefix = key + "=" for line in text.split("\n") { let l = line.trim(chars=" \t\r").to_string() if l.has_prefix(prefix) { let v = l[prefix.length():].to_string().trim(chars="\"").to_string() let v = if v.has_prefix("$HOME") { home + v["$HOME".length():].to_string() } else { v } if v.length() > 0 && v != home && v != home + "/" { return Some(v) } } } None } ///| The user's pictures folder: XDG_PICTURES_DIR from user-dirs.dirs, else ~/Pictures. fn pictures_dir() -> String { match user_dir("XDG_PICTURES_DIR") { Some(d) => d None => @sys.getenv("HOME") + "/Pictures" } } ///| Two digits. fn pad2(n : Int) -> String { if n < 10 { "0" + n.to_string() } else { n.to_string() } } ///| Local time as YYYYMMDD_HHMMSS. fn timestamp() -> String { @sys.wall_time_field(0).to_string() + pad2(@sys.wall_time_field(1)) + pad2(@sys.wall_time_field(2)) + "_" + pad2(@sys.wall_time_field(3)) + pad2(@sys.wall_time_field(4)) + pad2(@sys.wall_time_field(5)) } ///| Save `png` in `dir` (created if missing) as Screenshot_YYYYMMDD_HHMMSS.png, with -2, -3, ... /// when that name is taken. The path, or "" when it couldn't be written. fn save_png(dir : String, png : Bytes) -> String { ignore(@sys.mkdir_p(dir)) let base = dir + "/Screenshot_" + timestamp() let mut path = base + ".png" let mut n = 2 while @sys.read_file_bytes(path).length() > 0 && n < 100 { path = base + "-" + n.to_string() + ".png" n = n + 1 } if @sys.write_file_bytes(path, png) == 0 { path } else { "" } } ///| `w` x `h` fitted into `max_w` x `max_h`, keeping the aspect ratio, never enlarging. fn thumb_size(w : Int, h : Int, max_w : Int, max_h : Int) -> (Int, Int) { let (tw, th) = if w * max_h > h * max_w { (max_w, (h * max_w / w).max(1)) } else { ((w * max_h / h).max(1), max_h) } (tw.min(w), th.min(h)) } ///| A small picture of `p` as draw ops (runs of one colour per row become one rectangle), /// fitted into `max_w` x `max_h`, and its size. fn thumbnail_ops(p : @renderer.Pixmap, max_w : Int, max_h : Int) -> (Array[@scene.DrawOp], Int, Int) { let ops : Array[@scene.DrawOp] = [] if p.width <= 0 || p.height <= 0 { return (ops, 0, 0) } let (tw, th) = thumb_size(p.width, p.height, max_w, max_h) for ty = 0; ty < th; ty = ty + 1 { let sy = ty * p.height / th let mut run_x = 0 let mut run_c : @scene.Rgba? = None for tx = 0; tx <= tw; tx = tx + 1 { let c = if tx < tw { let sx = tx * p.width / tw let o = (sy * p.width + sx) * 4 // Quantise a little so near-identical neighbours share a run. Some( @scene.Rgba::new( p.data[o].to_int() & 0xFC, p.data[o + 1].to_int() & 0xFC, p.data[o + 2].to_int() & 0xFC, 255, ), ) } else { None } if c != run_c { match run_c { Some(rc) => ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(run_x, ty, tx - run_x, 1), rc, 0)) None => () } run_x = tx run_c = c } } } (ops, tw, th) } ///| Move every op of `ops` by (dx, dy). fn offset_ops(ops : Array[@scene.DrawOp], dx : Int, dy : Int) -> Array[@scene.DrawOp] { ops.map(op => match op { FillRect(r, c, rad) => @scene.DrawOp::FillRect(@scene.Rect::new(r.x + dx, r.y + dy, r.w, r.h), c, rad) other => other }) } ///| Handle input for screenshots: the shortcuts, and everything while the overlay is up. /// True when the event was used (it doesn't go on to the shell or clients). fn ChandraCompositor::screenshot_input(self : ChandraCompositor, ev : @nested.InputEvent, time_ms : UInt) -> Bool { match ev { Key(code, pressed) => self.screenshot_key(code, pressed) Modifiers(_, _, _, _) => false PointerMove(x, y) => match self.shot { Some(o) => { self.ptr_x = x self.ptr_y = y self.overlay_motion(o, x.to_int(), y.to_int()) true } None => false } PointerButton(button, pressed) => match self.shot { Some(o) => { self.overlay_button(o, button, pressed, time_ms) true } None => if pressed && button == 272U && self.recording_click(self.ptr_x.to_int(), self.ptr_y.to_int()) { self.rec_press = true true } else if !pressed && self.rec_press { self.rec_press = false true } else { false } } Scroll(_, _, _) => self.shot is Some(_) } } ///| A key, for the overlay. fn ChandraCompositor::screenshot_key(self : ChandraCompositor, code : UInt, pressed : Bool) -> Bool { if code == key_lsuper || code == key_rsuper { if self.shot is Some(_) { self.super_solo = false return true } return false } if !pressed { // Releases of keys we took must not reach a client either. if self.shot_swallow.contains(code) { self.shot_swallow.retain(c => c != code) return true } return self.shot is Some(_) } match self.shot { Some(o) => { self.shot_swallow.push(code) self.overlay_key(o, code, (self.mods & mod_shift) != 0U, (self.mods & mod_ctrl) != 0U) true } // The shortcuts that start screenshots and recordings are in the shortcut table (shortcuts.mbt). None => false } } ///| A key press on the overlay. fn ChandraCompositor::overlay_key(self : ChandraCompositor, o : ShotOverlay, code : UInt, shift : Bool, ctrl : Bool) -> Unit { let full = @scene.Rect::new(0, 0, o.frame.width, o.frame.height) if code == key_esc { self.close_overlay() } else if code == key_enter || code == key_kp_enter || (code == key_c && ctrl) { if o.pick { return } self.finish_screenshot(o.frame, o.sel.unwrap_or(full)) } else if code == key_p && o.record { self.set_record_pointer(!self.rec_pointer) } else if code == key_f && !o.pick { self.finish_screenshot(o.frame, full) } else if code == key_w && !o.pick { self.finish_screenshot(o.frame, o.active.unwrap_or(full)) } else if code == key_left || code == key_right || code == key_up || code == key_down { match o.sel { Some(r) => { let step = if ctrl { 10 } else { 1 } let dx = if code == key_left { -step } else if code == key_right { step } else { 0 } let dy = if code == key_up { -step } else if code == key_down { step } else { 0 } let nr = if shift { @scene.Rect::new(r.x, r.y, (r.w + dx).max(1), (r.h + dy).max(1)) } else { @scene.Rect::new(r.x + dx, r.y + dy, r.w, r.h) } o.sel = Some(self.keep_on_screen(nr)) self.damage.mark_full() } None => () } } } ///| `r` moved (not shrunk) back onto the screen where it can be, then clipped. fn ChandraCompositor::keep_on_screen(self : ChandraCompositor, r : @scene.Rect) -> @scene.Rect { let x = if r.x + r.w > self.output_w { self.output_w - r.w } else { r.x } let y = if r.y + r.h > self.output_h { self.output_h - r.h } else { r.y } self.clip_to_screen(@scene.Rect::new(if x < 0 { 0 } else { x }, if y < 0 { 0 } else { y }, r.w, r.h)) } ///| Selection handle `h` (0 NW, 1 N, 2 NE, 3 E, 4 SE, 5 S, 6 SW, 7 W) of `r`: its centre. fn handle_pos(r : @scene.Rect, h : Int) -> (Int, Int) { let cx = r.x + r.w / 2 let cy = r.y + r.h / 2 let x1 = r.x + r.w let y1 = r.y + r.h match h { 0 => (r.x, r.y) 1 => (cx, r.y) 2 => (x1, r.y) 3 => (x1, cy) 4 => (x1, y1) 5 => (cx, y1) 6 => (r.x, y1) _ => (r.x, cy) } } ///| The handle of `r` under (x, y), or -1. fn handle_at(r : @scene.Rect, x : Int, y : Int) -> Int { for h = 0; h < 8; h = h + 1 { let (hx, hy) = handle_pos(r, h) if (x - hx).abs() <= 8 && (y - hy).abs() <= 8 { return h } } -1 } ///| The rectangle with corners (x0, y0) and (x1, y1), either way round, on screen. fn ChandraCompositor::rect_between(self : ChandraCompositor, x0 : Int, y0 : Int, x1 : Int, y1 : Int) -> @scene.Rect { let l = x0.min(x1) let t = y0.min(y1) self.clip_to_screen(@scene.Rect::new(l, t, (x0 - x1).abs().max(1), (y0 - y1).abs().max(1))) } ///| The pointer moved on the overlay. fn ChandraCompositor::overlay_motion(self : ChandraCompositor, o : ShotOverlay, x : Int, y : Int) -> Unit { if o.drag != 0 && ((x - o.ax).abs() > 2 || (y - o.ay).abs() > 2) { o.moved = true } if o.drag == 1 && o.moved { o.sel = Some(self.rect_between(o.ax, o.ay, x, y)) } else if o.drag == 2 { let r = o.orig o.sel = Some(self.keep_on_screen(@scene.Rect::new(r.x + x - o.ax, r.y + y - o.ay, r.w, r.h))) } else if o.drag >= 3 { let r = o.orig let mut l = r.x let mut t = r.y let mut rr = r.x + r.w let mut b = r.y + r.h let dx = x - o.ax let dy = y - o.ay match o.drag - 3 { 0 => { l = l + dx; t = t + dy } 1 => t = t + dy 2 => { rr = rr + dx; t = t + dy } 3 => rr = rr + dx 4 => { rr = rr + dx; b = b + dy } 5 => b = b + dy 6 => { l = l + dx; b = b + dy } _ => l = l + dx } o.sel = Some(self.rect_between(l, t, rr, b)) } self.damage.mark_full() } ///| A button on the overlay: left draws, moves or resizes the selection (a click selects the /// window under it, a double-click inside takes the picture); right clears the selection, or /// cancels when there is none. fn ChandraCompositor::overlay_button(self : ChandraCompositor, o : ShotOverlay, button : UInt, pressed : Bool, time_ms : UInt) -> Unit { let x = self.ptr_x.to_int() let y = self.ptr_y.to_int() let full = @scene.Rect::new(0, 0, o.frame.width, o.frame.height) if button == 273U { if pressed && o.drag == 0 { if o.sel is Some(_) && !o.pick { o.sel = None self.damage.mark_full() } else { self.close_overlay() } } return } if button != 272U { return } if pressed { if o.pick { return } let double = time_ms - o.last_press < 400U && (x - o.last_px).abs() <= 4 && (y - o.last_py).abs() <= 4 o.last_press = time_ms o.last_px = x o.last_py = y match o.sel { Some(r) => { let inside = x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h if double && inside { self.finish_screenshot(o.frame, r) return } let h = handle_at(r, x, y) o.drag = if h >= 0 { 3 + h } else if inside { 2 } else { 1 } } None => o.drag = 1 } o.ax = x o.ay = y o.orig = o.sel.unwrap_or(@scene.Rect::new(x, y, 0, 0)) o.moved = false } else { if o.pick { self.finish_screenshot(o.frame, o.window_at(x, y).unwrap_or(full)) return } if o.drag == 1 && !o.moved { // A click: select the window under it (nothing on bare desktop). o.sel = o.window_at(x, y) } o.drag = 0 self.damage.mark_full() } } ///| Cursor shape over the overlay: crosshair, or move / resize over the selection. fn ChandraCompositor::overlay_cursor(self : ChandraCompositor, o : ShotOverlay) -> UInt { if o.pick { return 8U // crosshair } let x = self.ptr_x.to_int() let y = self.ptr_y.to_int() let h = if o.drag >= 3 { o.drag - 3 } else if o.drag == 0 { match o.sel { Some(r) => handle_at(r, x, y) None => -1 } } else { -1 } match h { 0 | 4 => 29U // nwse-resize 2 | 6 => 28U // nesw-resize 1 | 5 => 27U // ns-resize 3 | 7 => 26U // ew-resize _ => if o.drag == 2 { 13U // move } else { match o.sel { Some(r) => if o.drag == 0 && x >= r.x && x < r.x + r.w && y >= r.y && y < r.y + r.h { 13U } else { 8U } None => 8U } } } } ///| Draw ops for the overlay and the notification, on top of everything else. fn ChandraCompositor::screenshot_ops(self : ChandraCompositor) -> Array[@scene.DrawOp] { let ops : Array[@scene.DrawOp] = [] self.recording_ops(ops) match self.shot { Some(o) => self.overlay_ops(o, ops) None => () } match self.shot_toast { Some(t) => if @sys.monotonic_ms() >= t.until { self.shot_toast = None } else { self.toast_ops(t, ops) } None => () } ops } ///| A rounded card with lines of text, centred on screen. fn ChandraCompositor::help_card(self : ChandraCompositor, lines : Array[String], ops : Array[@scene.DrawOp]) -> Unit { let p = self.theme.palette let fs = self.theme.font_size let line_h = fs + 12 let mut w = 0 for i, l in lines { w = w.max(@renderer.text_width(l, if i == 0 { fs + 2 } else { fs }, i == 0, false)) } w = w + 48 let h = lines.length() * line_h + 24 let x = (self.output_w - w) / 2 let y = (self.output_h - h) / 2 let r = self.theme.radius + 4 ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(x, y, w, h), p.surface_popup, r)) ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(x, y, w, h), p.border, 1, r)) for i, l in lines { let ly = y + 12 + i * line_h + line_h / 2 if i == 0 { ops.push(@scene.DrawOp::Text(x + w / 2, ly, l, fs + 2, p.foreground, @scene.TextAlign::Center, true)) } else { ops.push(@scene.DrawOp::Text(x + w / 2, ly, l, fs, p.foreground_muted, @scene.TextAlign::Center, false)) } } } ///| fn ChandraCompositor::overlay_ops(self : ChandraCompositor, o : ShotOverlay, ops : Array[@scene.DrawOp]) -> Unit { let p = self.theme.palette let sw = self.output_w let sh = self.output_h let dim = @scene.Rgba::new(0, 0, 0, 120) let accent = p.accent let x = self.ptr_x.to_int() let y = self.ptr_y.to_int() match o.sel { None => { ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(0, 0, sw, sh), dim, 0)) // The window a click would take. match o.window_at(x, y) { Some(r) => if o.drag == 0 || !o.moved { ops.push(@scene.DrawOp::FillRect(r, @scene.Rgba::new(accent.r.to_int(), accent.g.to_int(), accent.b.to_int(), 40), 0)) ops.push(@scene.DrawOp::StrokeRect(r, accent, 2, 0)) } None => () } if o.drag == 0 { let pointer = "P: pointer " + (if self.rec_pointer { "shown" } else { "hidden" }) + " in the video" let lines = if o.pick && o.record { ["Record a window", "Click a window to record it", pointer, "Esc: cancel"] } else if o.pick { ["Window under cursor", "Click a window to take a screenshot of it", "Esc: cancel"] } else if o.record { [ "Record a region", "Drag to select a region, or click a window", "Enter or double-click: start recording", "F: full screen W: active window", pointer, "Esc or right-click: cancel", ] } else { [ "Rectangular region", "Drag to select a region, or click a window", "Enter or double-click: take the screenshot", "F: full screen W: active window", "Arrow keys: move Shift+arrows: resize", "Esc or right-click: cancel", ] } self.help_card(lines, ops) } } Some(r) => { // Dim around the selection, outline it, then its handles and size. ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(0, 0, sw, r.y), dim, 0)) ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(0, r.y + r.h, sw, sh - r.y - r.h), dim, 0)) ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(0, r.y, r.x, r.h), dim, 0)) ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(r.x + r.w, r.y, sw - r.x - r.w, r.h), dim, 0)) ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(r.x - 1, r.y - 1, r.w + 2, r.h + 2), accent, 2, 0)) if o.drag != 2 { for h = 0; h < 8; h = h + 1 { let (hx, hy) = handle_pos(r, h) ops.push(@scene.DrawOp::Circle(hx, hy, 5, accent, true)) ops.push(@scene.DrawOp::Circle(hx, hy, 5, p.accent_foreground, false)) } } let fs = self.theme.font_size let label = r.w.to_string() + " × " + r.h.to_string() let hint = if o.record { "Enter: start recording Esc: cancel" } else { "Enter: take screenshot Esc: cancel" } let show_hint = o.drag == 0 let lw = @renderer.text_width(if show_hint { hint } else { label }, fs, false, false) + 24 let lh = if show_hint { fs * 2 + 22 } else { fs + 14 } let lx = (r.x + r.w / 2 - lw / 2).clamp(min=4, max=(sw - lw - 4).max(4)) let ly = if r.y + r.h + 10 + lh <= sh { r.y + r.h + 10 } else if r.y - 10 - lh >= 0 { r.y - 10 - lh } else { r.y + 10 } ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(lx, ly, lw, lh), p.surface_popup, self.theme.radius)) ops.push(@scene.DrawOp::Text(lx + lw / 2, ly + (fs + 14) / 2, label, fs, p.foreground, @scene.TextAlign::Center, true)) if show_hint { ops.push(@scene.DrawOp::Text(lx + lw / 2, ly + fs + 6 + (fs + 14) / 2, hint, fs - 2, p.foreground_muted, @scene.TextAlign::Center, false)) } } } if (o.drag == 1 && o.moved) || o.drag >= 3 { self.magnifier_ops(o, x, y, ops) } } ///| A magnified view of the frozen screen around (x, y), the pixel under the pointer framed. fn ChandraCompositor::magnifier_ops(self : ChandraCompositor, o : ShotOverlay, x : Int, y : Int, ops : Array[@scene.DrawOp]) -> Unit { let n = 15 let z = 8 let size = n * z let mx = if x + 24 + size <= self.output_w { x + 24 } else { x - 24 - size } let my = if y + 24 + size <= self.output_h { y + 24 } else { y - 24 - size } let p = self.theme.palette ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(mx - 2, my - 2, size + 4, size + 4), p.border, 2)) // The pixels around (x, y), from the host when it holds the frozen screen. let (src, ox, oy) = match self.frame_shots { Some(host) => ((host.pixels)(@scene.Rect::new(x - n / 2, y - n / 2, n, n), n, n), 0, 0) None => (o.frame, x - n / 2, y - n / 2) } for j = 0; j < n; j = j + 1 { for i = 0; i < n; i = i + 1 { let c = match src.pixel(ox + i, oy + j) { Some(c) => @scene.Rgba::new(c.r.to_int(), c.g.to_int(), c.b.to_int(), 255) None => @scene.Rgba::new(0, 0, 0, 255) } ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(mx + i * z, my + j * z, z, z), c, 0)) } } ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(mx + n / 2 * z - 1, my + n / 2 * z - 1, z + 2, z + 2), p.accent, 2, 0)) ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(mx - 1, my - 1, size + 2, size + 2), p.accent, 1, 2)) } ///| The notification card: thumbnail, title, file name. fn ChandraCompositor::toast_ops(self : ChandraCompositor, t : ShotToast, ops : Array[@scene.DrawOp]) -> Unit { let p = self.theme.palette let fs = self.theme.font_size let pad = 12 let text_w = @renderer.text_width(t.title, fs, true, false).max(@renderer.text_width(t.detail, fs - 2, false, false)) let w = (pad * 3 + 96 + text_w).min(self.output_w - 32) let h = 60 + pad * 2 let x = self.output_w - w - 16 let y = self.output_h - self.theme.panel_height - h - 16 let r = self.theme.radius + 4 ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(x, y, w, h), p.surface_popup, r)) ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(x, y, w, h), p.border, 1, r)) let tx = x + pad + (96 - t.tw) / 2 let ty = y + pad + (60 - t.th) / 2 ops.append(offset_ops(t.thumb, tx, ty)) ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(tx - 1, ty - 1, t.tw + 2, t.th + 2), p.border, 1, 0)) let cx = x + pad * 2 + 96 ops.push(@scene.DrawOp::PushClip(@scene.Rect::new(cx, y, x + w - pad - cx, h))) ops.push(@scene.DrawOp::Text(cx, y + h / 2 - fs / 2 - 3, t.title, fs, p.foreground, @scene.TextAlign::Left, true)) ops.push(@scene.DrawOp::Text(cx, y + h / 2 + fs / 2 + 3, t.detail, fs - 2, p.foreground_muted, @scene.TextAlign::Left, false)) ops.push(@scene.DrawOp::PopClip) }