Cursors, drag and drop, clipboard images, dirty regions#
ui/cursor.rs#
Source: lib/ui/cursor.rs
cursor -- custom pointer cursors drawn by the app, at the dpi of the window
[src/ui/ui_cursor.s]
cursor_new(f, w, h, hot) -> handle 1..CUR_USER_MAX (0 = no slot left)
a cursor of w x h DIP with the hot spot hot = x | y << 16 (DIP); f(g, dpi) draws it
at `dpi` into g (FMT_PARGB, transparent; physical px: ceil(w * dpi / 96) x
ceil(h * dpi / 96)) -- called again for every new dpi
cursor_custom(handle) the cursor that hit boxes with CUR_CUSTOM show (their
next_flags(CUR_CUSTOM << HB_CURSOR_SHIFT)); set it each
frame before the widget; 0 = the crosshair
cursor_render(handle, dpi) -> Some(surface) the cursor drawn into a new surface
(FMT_PARGB; tests, previews)
cursor_current() -> handle the handle CUR_CUSTOM stands for (0 = none)
cursor_replace(handle, f, w, h, hot) -> bool (E3) a new drawing for a live handle (the
platform's cursor is made again when it is shown next)
cursor_free(handle) (E3) the slot is free again (cursor_new may hand it out); a
hit box showing it gets the crosshair
cursor_next(shape) (E3) the cursor of the next hit box: any CUR_* -- the system
shapes CUR_X0 .. CUR_X_END (not-allowed, grab, the resize
shapes ...) that do not fit into the 4 flag bits, CUR_CUSTOM
cursor_at(x, y) -> shape (ctx.rs) the cursor at a point (tests, an engine's own hover)
Internal: cursor_resolve(shape) -> the plat shape for a hit box cursor (CUR_CUSTOM: the
custom cursor made for the current window's dpi with plat::cursor_create, rebuilt when
that dpi changes; the crosshair when there is none or the platform has no custom
cursors, e.g. headless runs).
type CursorDraw = Rc<dyn Fn(&mut Gfx, i32)>a cursor's draw function: f(g, dpi) draws into the cursor surface
impl Ui
fn cursor_new(&mut self, f: CursorDraw, w: u32, h: u32, hot: u32) -> u32ui_cursor_new(f, w, h, hot) -> handle (0 = none)fn cursor_custom(&mut self, h: u32)ui_cursor_custom(handle): the cursor of CUR_CUSTOM hit boxesfn cursor_current(&self) -> u32ui_cursor_current -> handlefn cursor_next(&mut self, shape: u32)(E3) the cursor of the next hit box (canvas, button, hit_rect ...): any CUR_* shape -- also those beyond CUR_CUSTOM's 4 bits (CUR_NOT_ALLOWED, CUR_GRAB, CUR_SIZE_NE ...), a custom cursor through CUR_CUSTOM; it wins over the hit box's flags. A platform without a shape shows plat::cursor_fallback'sfn cursor_replace(&mut self, handle: u32, f: CursorDraw, w: u32, h: u32, hot: u32) -> bool(E3) a new drawing for a live handle (f, w x h DIP, hot spot x | y << 16): the platform's cursor is made again at the next show -> false for a handle that is not livefn cursor_free(&mut self, handle: u32)(E3) free a handle of cursor_new: its platform cursor goes, the slot may be handed out again; when it was the custom cursor, CUR_CUSTOM shows the crosshair until another is setfn cursor_render(&mut self, h: u32, dpi: i32) -> Option<Surface>ui_cursor_render(handle, dpi) -> the cursor drawn at dpi into a new surface
ui/drop.rs#
Source: lib/ui/drop.rs
drop -- (K1) dropped files and other content for apps, the clipboard format probe, the
headless image clipboard; the command lines of later starts (AF_SINGLE_INSTANCE)
[src/ui/ui_drop.s]
open_take() -> Vec<Request> (UIC_OPEN, arg = their count) the working folder and
arguments of every later start of the app since the last
call; Request::paths() gives the files, which the app may
read and write (fs.user); the main window comes forward
drop_count() -> n files of the last drop (UIC_DROP, arg = window id; or
DS_DROP of a drop target, dnd.rs)
drop_path(i) -> Option<&str> path i; valid until the next drop arrives
drop_list() -> &[String] every path of the last drop
drop_data() -> &DragData everything the last drop carried: paths, text, URLs, HTML,
an image file (PNG, or BMP from a DIB), the app's own kind
and bytes; drop_types() -> its DT_* kinds, drop_text()
drop_pos() -> (x, y) drop position in DIP of the window it happened in
drop_pos_px() -> (x, y) the same in px of that window, exact (as last_rect and a
canvas's UiInput: x - rect[0] is the point in the canvas)
drop_window() -> id that window's app id (0 = main window)
clip_has(fmt) -> bool the clipboard offers CLIP_TEXT / CLIP_PNG / CLIP_IMAGE
(headless runs: the in-process clipboard)
Which kinds a window takes outside its drop targets: drop_accept (dnd.rs, default DT_FILES).
Internal:
drop_receive(ev) -> taken E_DROP: the platform's pending drop (runtime.rs) onto the
drop target at the point (true; also when no effect the
source allows is taken there: refused) or for UIC_DROP (false)
drop_inject(data, allowed) script "drop": the content of the next E_DROP and the effects
its source allows (0: not said)
hclip_img_set(fmt, data) headless CLIP_PNG (bytes) / CLIP_IMAGE (a CLIPIMG
record: png then bmp, see clipimg.rs): PNG + BMP copies
hclip_img_get(fmt) CLIP_PNG: the PNG; CLIP_IMAGE: the PNG, else the BMP
hclip_img_clear() hclip_img_has(fmt)
impl Ui
fn drop_count(&self) -> u32ui_drop_count: files of the last dropfn drop_path(&self, i: u32) -> Option<&str>ui_drop_path(i): path i of the last drop (None when out of range)fn open_take(&mut self) -> Vec<plat::instance::Request>AF_SINGLE_INSTANCE: the command lines of later starts that arrived (UIC_OPEN), oldest first; each call takes themfn drop_list(&self) -> &[String]ui_drop_list: every path of the last dropfn drop_pos(&self) -> (i32, i32)ui_drop_pos: drop position in DIP of the window it happened in (rounded to whole DIP: at 150 % three pixels share two DIP; drop_pos_px is exact)fn drop_pos_px(&self) -> (i32, i32)drop position in px of the window it happened in, exact: logical likelast_rectand a canvas's \UiInput::rect\\(mirrored in a right-to-left window), so \(x - rect[0], y - rect[1])\\is the point in a canvas, in the canvas's own pixels -- a drop onto a page an engine draws at the display's resolutionfn drop_window(&self) -> u32ui_drop_window: the app id of the window of the last drop (0 = main window)fn drop_data(&self) -> &DragDataeverything the last drop carried (paths, text, URLs, HTML, image, the app's own kind and bytes; effects = the effect it was dropped with)fn drop_types(&self) -> u32the DT_* kinds of the last dropfn drop_text(&self) -> Option<&str>the text of the last drop (None: it had none)fn clip_has(&self, fmt: u32) -> boolui_clip_has(fmt): the clipboard offers fmt (cheap: no data is transferred)
ui/dnd.rs#
Source: lib/ui/dnd.rs
dnd -- drag and drop: widgets as drag sources and drop targets, the hover of a drag over a
window, drops of other programs onto widgets, dragging out of the app
Sources (the app's own drags):
drag_source(id, ev) -> DS_* after the widget's behave (ev = its EV_*): DS_START when it
starts being dragged (EV_DRAG; hand the content to drag_set
in the same pass, else nothing is dragged), DS_ACTIVE while
it is dragged (and while the answer of its drop onto a target
of the app waits: drop_defer), DS_END once when the drag ended
drag_set(data) what is dragged (DragData: text, files, URLs, HTML, a PNG,
the app's own kind DT_USER0 << k with bytes, a label shown
next to the pointer, the effects the source allows)
drag_out(data) -> started (left button held) hand data to the platform at once
drag_effect() -> DE_* how the app's last drag ended (DE_COPY / DE_MOVE /
DE_LINK; 0 = cancelled or refused); UIC_DRAG_END says it too
dragging() -> bool one of the app's drags runs
Targets:
drop_target(id, types) -> DS_* the widget at last_rect takes drags of these DT_* kinds:
DS_HOVER while one is over it (the style highlights it),
DS_DROP in the pass of the drop (drop_data(), drop_effect():
one of the target's effects that the source allows; none in
common: refused, no DS_DROP)
drop_effect_set(effects) the DE_* effects the target of the last drop_target call
takes in this pass (default DE_ALL): the platform answers a
drag over it with one of them (the system's pointer shows
copy, move or link), 0 refuses the drag there (no drop, no
highlight); a drag of the app picks from them too
drop_accept(types) what the windows take outside every drop target (default
DT_FILES); such drops come as UIC_DROP (drop.rs, as before)
drag_over() -> (types, x, y) the drag over the live window: its kinds and position (DIP);
(0, 0, 0) when none; drag_over_px() the position in px of
the window, exact (as drop_pos_px, drop.rs)
drag_allowed() -> DE_* the effects its source allows (DE_ALL where the platform does
not say); after a drop its source's, from E_DROP to the end
of its passes and while drop_defer waits; 0 when none
drop_defer() -> bool (in the pass of a drop: DS_DROP or UIC_DROP) the drop's answer
to its source waits for drop_finish (at most DROP_DEFER_MS),
also for the app's own drag (its DS_END waits for it); false:
the platform answered at the drop already
drop_finish(effect) -> bool the answer: the DE_* effect it was taken with, 0 = not taken
An internal drag stays in the app while the pointer stays in its window: the drop target under
the pointer is highlighted, a label follows the pointer, Esc cancels, a release over a matching
target drops there (else nothing happens). The target is the innermost drop target at the
point (the smallest of the topmost layer); targets under a popup or a modal layer do not count.
When the pointer leaves the window with content other programs know (DT_STD), the platform
takes over (plat::drag_start: DoDragDrop, Xdnd, wl_data_device.start_drag); dropped back onto
the app the drop carries the app's own content again (its kind and bytes). Headless runs keep
what would have left the app for the scripts (`dragout expect`, `dragout end`).
Other programs' drags: E_DRAG (hover: the targets highlight), E_DROP (onto the drop target at
the point, else UIC_DROP when drop_accept takes it). The platform asks the published targets
synchronously (uiw_drag_pw, win.rs) to answer the source with an effect: the effects the target
under the point took in the last pass (drop_effect_set), DE_ALL for drop_accept's window --
IDropTarget::DragOver's pdwEffect, XdndStatus' action, wl_data_offer.set_actions, macOS'
NSDragOperation (plat::drag_query). The answer follows the app's pass: an effect set for the
drag's point in one pass answers the moves until the next pass. The effects the source allows
come with E_DRAG (forgotten when it leaves or the drag ends) and with the drop (plat::
drop_allowed; without them those of the hover before it, else every effect): the drop's effect
is one the target takes that the source allows -- the platform's, else the first of copy,
move, link -- and a target that takes none of them refuses the drop as the platform refuses
such a drag over it. An empty set of a source's effects means it does not say: every effect
(DE_ALL), for the app's own drags (DragData::effects 0) as for another program's (E_DRAG,
drop_allowed, plat::drag_decide) -- files are never moved.
The answer of a drop to its source (the effect, or that it was not taken) goes after the
passes of E_DROP: the effect it was delivered with -- unless the app called drop_defer in them
and answers later with drop_finish (a web page decides asynchronously whether it took a drop).
Where the platform allows a later answer (plat::drop_hold: X11, Wayland, Windows with a source
in async mode) it then reaches the source; elsewhere drop_defer says false and the source got
the effect at the drop. A deferred drop without drop_finish is answered after DROP_DEFER_MS as
it was delivered (a UIWARN); so is one a newer drop overtakes. Headless runs keep the answer
for the scripts (`drop expect`).
The app's own drag dropped onto one of its targets is answered the same way: its source's end
(DS_END, drag_effect, UIC_DRAG_END) comes after the drop's passes with the effect it was
delivered with, or -- when the target called drop_defer -- with drop_finish's effect (0: not
taken), DS_ACTIVE until then; a new drag of the app overtakes it (answered as delivered).
Positions stay logical in mirrored (RTL) windows: E_DRAG / E_DROP points are mirrored on the
way in like every pointer event; the label next to the pointer is drawn through the mirror.
impl Ui
fn drag_source(&mut self, id: u32, ev: u32) -> u32drag_source(id, ev) -> DS_START / DS_ACTIVE / DS_END / 0 for the widgetidwhose behave returned ev: DS_START when a drag of it begins (call drag_set in this pass), DS_ACTIVE while its content is dragged, DS_END once afterwards (drag_effect())fn drag_set(&mut self, data: DragData)drag_set(data): the content of the drag that starts (after DS_START, same pass)fn drag_out(&mut self, data: DragData) -> booldrag_out(data) -> started: (the left button held) the platform drags data out of the app right away (DoDragDrop, Xdnd, wl_data_device.start_drag); UIC_DRAG_END at the endfn drag_effect(&self) -> u32the effect of the app's last drag: DE_COPY / DE_MOVE / DE_LINK, 0 = cancelled / refusedfn dragging(&self) -> boolone of the app's drags runs (inside the app or carried by the platform; not once it was dropped, also while the drop's answer waits)fn drop_target(&mut self, id: u32, types: u32) -> u32drop_target(id, types) -> DS_HOVER / DS_DROP / 0: the widget at last_rect takes drags of these DT_* kinds (DT_USER0 << k: the app's own). DS_HOVER while such a drag is over it (the style draws the highlight), DS_DROP in the pass of the drop (drop_data(), drop_effect(): one of the target's effects that the source allows; none in common: the drop is refused, no DS_DROP)fn drop_effect_set(&mut self, effects: u32)drop_effect_set(effects): the DE_* effects (DE_COPY | DE_MOVE | DE_LINK) the drop target of the last drop_target call takes in this pass; DE_ALL until it is called. The platform answers a drag of another program over the target with one of them (the modifier keys choose, else copy, move, link in this order; files are never moved): the system's pointer shows that effect and the drop carries it (drop_effect()). 0 refuses the drag there: no drop, no highlight, the pointer shows "no" -- a page that takes drops only in some places; so is a drag that allows none of them (a link target, a source that only copies or moves). A drag of the app inside its window picks from them the same way (a plain drag moves when both allow it). Call it after drop_target in every pass the effect should hold, typically while drop_target says DS_HOVER, for the point of drag_over_px: the platform's answers follow the passes, one pass late while the drag movesfn drop_accept(&mut self, types: u32)drop_accept(types): the DT_* kinds a window takes outside every drop target (default DT_FILES); those drops arrive as UIC_DROPfn drag_over(&self) -> (u32, i32, i32)drag_over() -> (DT_* kinds, x, y DIP): the drag over the live window, (0, 0, 0) = nonefn drag_over_px(&self) -> (u32, i32, i32)drag_over_px() -> (DT_* kinds, x, y px): drag_over with the position in px of the live window, exact (logical likelast_rect; minus a canvas'sUiInput::rect: the point in the canvas's own pixels); (0, 0, 0) = nonefn drag_allowed(&self) -> u32drag_allowed() -> the DE_* effects (DE_COPY | DE_MOVE | DE_LINK) the source of the drag over the live window allows: another program's as its platform says -- Windows the effects of its DoDragDrop, X11 the source's XdndActionList with the action it asks for (DE_ALL without a list), Wayland its source_actions (copy and move: Wayland has no link), macOS the drag's operation mask (narrowed by the modifier keys, as AppKit hands it on) -- the app's own drag its DragData::allowed(); DE_ALL where the platform does not say. After a drop, the effects its source allows -- as the platform gives them with the drop (Windows Drop's pdwEffect, X11 the list with the last XdndPosition's action, Wayland source_actions, macOS the operation mask), else as the hover before it said, else DE_ALL (the browser, Android); the app's own drop its DragData::allowed() -- from its E_DROP (the app's own: the release) to the end of the passes it starts (DS_DROP, App::command's UIC_DROP) and, when the app called drop_defer, until its answer went (drop_finish, DROP_DEFER_MS, a newer drop): in every window over which no drag is, and drop_finish answers within them. 0 otherwise: no drag over the window, no drop in its passes or waiting. A page that decides an effect (a web page's effectAllowed and dropEffect) picks from these, then drop_effect_set; the platform never moves another program's files whatever this saysfn drop_defer(&mut self) -> booldrop_defer() -> waits: (in the pass of a drop: while drop_target says DS_DROP, or in App::command's UIC_DROP) the drop's answer to its source -- the effect it was taken with, or that it was not taken -- waits for drop_finish, at most DROP_DEFER_MS; then it goes as the drop was delivered (drop_effect()) with a UIWARN. For a target that learns later whether it took the drop (a web page's drop handler in another process). The app's own drag waits too: its source sees DS_ACTIVE until the answer, then DS_END with drag_effect() and UIC_DRAG_END as drop_finish says. false: the answer went already (the platform answers at the drop: macOS, the browser, Android, Windows with a source whose data object has no async mode; outside the pass of a drop): drop_finish has nothing to do then. What the source sees, per platform: docs/GUIDE.md section 7, "Drag and drop"fn drop_finish(&mut self, effect: u32) -> booldrop_finish(effect) -> answered: the answer of the drop that waits (drop_defer; or, in the pass of a drop, instead of the effect it was delivered with): the DE_* effect it was taken with, 0 = not taken. An effect the source does not allow (drag_allowed says them until then) becomes the first it does (copy, move, link): taken stays taken. false: no drop waits (none was deferred, it was answered, or its DROP_DEFER_MS were up)fn drop_effect(&self) -> u32the effect of the last drop (DE_*): one its target takes (drop_effect_set; outside the targets every one) that its source allows (drag_allowed in its passes), files never moved -- the platform's choice, else the first of copy, move, link. A drop with none in common is refused (no DS_DROP, no UIC_DROP; its source is answered "not taken")
ui/clipimg.rs#
Source: lib/ui/clipimg.rs
clipimg -- (K1) images on the clipboard [src/ui/ui_clipimg.s]
clip_set_image(sf) -> 0 / -1 sf (FMT_ARGB straight, FMT_PARGB or FMT_XRGB) as
CLIP_IMAGE: PNG (deflate level 1) + a 32-bit BMP
(BITMAPV5HEADER, BI_BITFIELDS, straight alpha)
clip_get_image() -> Ok(sf) / Err the clipboard image (CLIP_IMAGE: PNG, else the DIB /
BMP) decoded into a new FMT_ARGB surface (straight,
like paint.net layers); ERR_FAIL (-1) = none
clip_bmp_from_sf(sf) -> BMP file the BMP of clip_set_image
A CLIP_IMAGE clipboard record (the asm's CLIPIMG: PNG and BMP pointers) travels as bytes:
u64 PNG length (LE), the PNG, u64 BMP length, the BMP (clipimg_record / clipimg_parse);
plat::clip_set(CLIP_IMAGE, ..) takes this record.
fn clipimg_record(png: Option<&[u8]>, bmp: Option<&[u8]>) -> Vec<u8>the CLIP_IMAGE record: u64 PNG length, PNG, u64 BMP length, BMPfn clipimg_parse(r: &[u8]) -> Option<(Option<&[u8]>, Option<&[u8]>)>the parts of a CLIP_IMAGE record (an empty part = none); None when it is malformedfn clip_bmp_from_sf(sf: &Surface) -> Option<Vec<u8>>clip_bmp_from_sf(sf) -> the BMP file of clip_set_image (None: an empty / huge surface or another format)
impl Ui
fn clip_set_image(&mut self, sf: &Surface) -> i32ui_clip_set_image(sf) -> 0 / -1: sf as CLIP_IMAGE (PNG level 1 + BMP)fn clip_get_image(&mut self) -> Result<Surface, i32>ui_clip_get_image -> the clipboard image in a new FMT_ARGB surface, or ERR_* (-1 = no image)
ui/dirty.rs#
Source: lib/ui/dirty.rs
dirty -- dirty regions: only redraw and present what changed [src/ui/ui_dirty.s]
dirty_compute(w, h) -> n number of dirty rectangles (Ui::drects: x, y, w, h,
clipped to w x h), -1 = everything. Hashes the
recorded commands of every layer into 32 x 32 px cells
and compares with the previous frame.
dirty_force_full() the next dirty_compute reports "everything"
dirty_free() forget the live window's cells (a closed window)
canvas_damage(id, x, y, w, h) (U1) explicit damage: the part x, y, w, h (px,
relative to the canvas) of canvas `id` is redrawn with
the next render even when its command did not change
dmg_canvas_seen(id) canvas() remembers its rectangle and window
A command's hash covers all of its content (colours, coordinates, text, a custom draw
function's identity and version) and the clip it is drawn in, so any visible change
marks its cells. The same pass fills the content grids of the scroll views
(scrollcopy.rs): dirty cells whose pixels a scrolled view still shows elsewhere are copied
there before the rectangles are drawn.
While a window's frames go through the GPU (gpu_out.rs, W::split) the pass also keeps the hash
of every cell after each layer (W::through: what changed in which layer) and notes the big
images of the base layer and which cells they end up on top of (W::top, W::imgs). A canvas whose
view comes from tiles (canvas.rs, W::canv) is noted the same way; its hash stands only for where
it lies: what it shows there is the GPU's.
impl Ui
fn dirty_force_full(&mut self)dirty_force_full: the next frame redraws and presents everythingfn canvas_damage(&mut self, id: u32, x: i32, y: i32, w: i32, h: i32)ui_canvas_damage(id, x, y, w, h): redraw part of a canvas with the next render (px relative to the canvas, cut to it): the canvas of the window being built (when it has one of the id); else (outside the passes: App::command, a task) the canvas of that id in every window