JFLib

plat/screen.rs#

Source: lib/plat/screen.rs

screen -- the monitors, the colour of any pixel on the screen, pictures of the screen

  monitors() -> Vec<Monitor>        every monitor: id, name, rectangle and work area (desktop
                                    units), dpi, refresh rate, the primary one first; when the
                                    set or one of them changes, E_SETTINGS with SET_MONITORS
  monitor_at(x, y) -> Option<Monitor>   the monitor holding a desktop point, else the primary
  pick_available() -> bool          the user can pick a colour outside the app's windows here
  pick_start() -> Result<(), ScreenError>   the user clicks a pixel anywhere on the screen (Esc
                                    or a right click cancels); the answer is ScreenEvent::Color
  capture_available() -> bool       pictures of the screen can be taken here
  capture_start(rect) -> Result<(), ScreenError>   a picture of the whole desktop (None) or
                                    of a rectangle (desktop units); needs `screen.capture` in
                                    the manifest (else Denied, "PERMDENY screen.capture"); the
                                    answer is ScreenEvent::Shot
  screen_take() -> Vec<ScreenEvent> the answers since the last call (each one calls wake())
  pick_test_point(Some((x, y)))     (test aid) the next pick_start answers the pixel at the
                                    desktop point x, y without asking the user (Windows,
                                    X11, macOS; the portal and the browser ignore it)

One pick and one capture at a time (else Busy).  Picking needs no right: the user chooses
the one pixel the app gets, like a file in the file dialog.  Call pick_start and
capture_start from the UI thread: their answers are made there (Windows, X11) or on a thread
of their own (the portal), and arrive through the event loop.

Windows (win_screen.rs): EnumDisplayMonitors + GetMonitorInfoW (device name, work area,
primary), GetDpiForMonitor (shcore), EnumDisplaySettingsW (refresh), WM_DISPLAYCHANGE and a
changed work area -> SET_MONITORS; capture: BitBlt (CAPTUREBLT) from the screen DC into a DIB
section; pick: a topmost window over the virtual desktop showing a frozen picture of it with
a magnifier at the pointer.  Linux (linux_screen.rs): X11 RandR 1.5 (GetMonitors, the CRTC's
mode for the refresh rate, _NET_WORKAREA, RRScreenChangeNotify), GetImage of the root window,
a pointer grab for the pick; Wayland wl_output (+ zxdg_output_v1 for logical position and
size; no work area, no primary: the first output); in Wayland sessions and in Flatpak the
screenshot portal (Screenshot, PickColor: the desktop asks the user).  Browser: the host's
window.screen (getScreenDetails when the page may), the EyeDropper API (Chromium) for the
pick, no capture.  Android: the host's DisplayManager displays, no pick, no capture.  macOS
(apple_mac_screen.rs): NSScreen's screens; the pick through the system's loupe
(NSColorSampler, no right needed); pictures through ScreenCaptureKit (macOS 15.2 and later,
before CGWindowListCreateImage), which need the user's screen recording right for the program
(without it: Denied, and the system's dialog once).  iOS: none of the three.
  • struct Monitor one monitor

    • id: u64 -- stable while the monitor stays connected (Windows: a hash of its device name, X11 the RandR monitor's name atom, Wayland the output's global name, browser / Android an index)
    • name: String -- the system's name ("\\.\DISPLAY1", "HDMI-1", "rdp-0"); "" when unknown
    • rect: [i32; 4] -- x, y, w, h in desktop units: physical pixels on Windows, X11 and Android; the compositor's logical units on Wayland; CSS pixels in the browser
    • work: [i32; 4] -- the part windows should use (without taskbars, docks, panels); = rect where unknown
    • dpi: u32 -- 96 = 100 %
    • refresh_mhz: u32 -- refresh rate in mHz (60000 = 60 Hz), 0 = unknown
    • primary: bool -- the primary monitor (exactly one in a non-empty list)
  • enum ScreenError why a pick or a capture did not happen

    • Unavailable -- not possible here (browser without EyeDropper, Android, no X server / portal)
    • Denied -- the app's permissions lack screen.capture, or the system / the user refused
    • Cancelled -- the user cancelled (Esc, a right click, the portal's dialog dismissed)
    • Busy -- a pick (or capture) is still running
    • Failed(String) -- the system failed: what it said
  • struct Shot a picture of the screen

    • rect: [i32; 4] -- where it was taken: x, y, w, h in desktop units ([0, 0, w, h] when the system did not say: the portal)
    • w: i32 -- size in pixels
    • h: i32
    • px: Vec<u8> -- w x h pixels B, G, R, x (rows of w * 4 bytes); empty when file holds the picture
    • file: Option<(String, Vec<u8>)> -- the picture as the system wrote it (the screenshot portal's PNG): its name and bytes
  • enum ScreenEvent an answer of pick_start / capture_start

    • Color(Result<u32, ScreenError>) -- the picked colour 0xRRGGBB
    • Shot(Result<Shot, ScreenError>) -- the picture
  • fn screen_take() -> Vec<ScreenEvent> the answers since the last call, oldest first

  • fn monitors() -> Vec<Monitor> every monitor, the primary one first (empty when nothing is known: Linux before the first window, headless runs)

  • fn monitor_at(x: i32, y: i32) -> Option<Monitor> the monitor holding the desktop point x, y, else the primary one

  • fn pick_available() -> bool whether the user can pick a colour anywhere on the screen here

  • fn pick_start() -> Result<(), ScreenError> the user picks a pixel anywhere on the screen; the answer comes as ScreenEvent::Color

  • fn pick_test_point(p: Option<(i32, i32)>) (test aid) the next pick_start answers the pixel at the desktop point x, y at once, without asking the user (Windows, X11, macOS; the portal and the browser ignore it); None: ask again. With a manifest only where screen.capture allows pictures of the screen

  • fn capture_available() -> bool whether pictures of the screen can be taken here (the permission is checked when taking)

  • fn capture_start(rect: Option<[i32; 4]>) -> Result<(), ScreenError> a picture of the whole desktop (None) or of the rectangle x, y, w, h (desktop units); the answer comes as ScreenEvent::Shot. The screenshot portal takes the whole screen (or what the user chooses in its dialog) whatever rect says.

  • fn shot_pixel(s: &Shot, x: i32, y: i32) -> Option<u32> the pixel 0xRRGGBB at x, y of a shot's B, G, R, x rows (None outside)

Source: docs/API.md in the JFLib repository.