JFLib

ui/md_view.rs#

Source: lib/ui/md_view.rs

md_view -- ui.markdown_view / ui.html_view: a read-only document view over an MdDoc
(md_doc.rs) for help pages, README-like content, release notes and chat output

  ui.markdown_view(id, &mut MdViewState, text, MV_*) -> MdEv   Markdown; parsed again only
       when the text changes (the state keeps a hash of it)
  ui.html_view(id, &mut MdViewState, html, MV_*) -> MdEv        HTML (the rich-text reader)
  ui.doc_view(id, &mut MdViewState, MV_*) -> MdEv                the document set before
       (st.set_markdown / set_html / set_doc)
  ui.markdown_fit(&mut MdViewState, text, w_px, MV_*) -> DIP     how high a view w_px wide must be to
       show the whole text without scrolling: for a short text among other widgets (a question
       in a form); every block is laid out now; give next_size(-1, h) to markdown_view
  MdEv { ev, link, hover, field, submit, click }   ev: EV_FOCUS, EV_HOVER, EV_ACT (a link was
       activated); link: the activated link's target (a click on it, Enter on the focused
       one); hover: the target of the link under the pointer; field: a form field the user
       changed (typed, toggled, chose) or a button without a submit / reset action he pressed;
       submit: a form was sent (MdSubmit: the form, its action and method, the name / value
       pairs, the button); click: the element of an HTML document a click on the text hit (the
       inline element around the character under the pointer, else the block's, the document's
       root beside the blocks; none after a drag; Enter on a link: its element; a field's
       widget takes its own clicks: field, submit).  The view fills the place it gets (natural
       480 x 320 DIP; next_size(-1, -1) fills).
  MdViewState::new() / base (where relative image paths start, e.g. "app://help/") / label
       (the name for screen readers; "" = "Dokument") / doc() / selection() /
       set_selection(sel) / select_all() / selected_text() / selected_html() /
       scroll_to_block(i) / scroll_to_end() (the end, also while the document grows: chat
       output; scrolling to the end of a long document does the same until the user scrolls
       away) / first_visible() -> (block, px above the view) / focused_link()
  Changes of the document (HTML: its elements, md_dom.rs -- page scripts): st.doc_mut() ->
       &mut MdDoc; the view takes the changes every frame (st.update(): now): the blocks
       derived again (MdDoc::update), the layouts and heights of the blocks that stay kept,
       the view on the block it showed, the selection and the focused link with their blocks,
       the fields' values by their elements (what the user or the app gave a field stays;
       the others follow the document's values), a focused field's widget moved with it
  Images from the app: st.images = MI_LOCAL (default: VFS names and paths, other addresses
       show their alt text), MI_ASK (as MI_LOCAL; http:, https: and data: addresses are asked
       from the app) or MI_APP (every image is asked: nothing is read by the view -- pages
       from the network).  st.image_requests() -> the addresses the view waits for (each
       once; relative ones after base, the rest as written); st.set_image(addr, Some(bytes) |
       None) answers (decoded at once by the image decoders: SVG or img_core; None or bytes
       it cannot decode: the alt text) -- in a frame before the view, or the app asks for one
       (invalidate) when the bytes come outside.  Until then a frame with the alt text stands
       in (at the width / height the HTML gives, else the frame's size); the image's block is
       laid out again when its bytes come, the view stays where it is.
  Forms (HTML: md_doc.rs "Forms"): the fields are the library's widgets in the text flow --
       text boxes (password fields: dots, nothing to copy or read out), text areas, check
       boxes, radios, drop-down lists, buttons -- with the style's look, high contrast, text
       scale and mirroring.  Their values live in the state: field_text(i) / field_checked(i)
       / field_selected(i), set_field_text / set_field_checked / set_field_selected,
       form_data(form, button) (the pairs a browser sends), reset_form(form),
       focused_field(); a new document starts with its first values (a changed one keeps
       them, above).  Keys: Tab / Shift+Tab
       go through links and fields in reading order (a group of radios is one stop: the
       checked one; Up / Down and Left / Right -- as the radios lie in their paragraph --
       choose inside it), Space toggles, Enter in a text field, check
       box or radio sends its form (with its first submit button, none when that is
       disabled), Enter / Space on a button presses it, Esc in a text field returns to the
       view, PgUp / PgDn that a field does not take scroll the view.  The fields follow the
       window's mirroring (as every widget), not the direction of their paragraph.  A
       document without fields reads as before.
  Flags: MV_NO_FRAME (no card, a smaller padding), MV_OPEN_LINKS (an activated http, https
       or mailto link is also opened with open_url: the app's policy applies; never without
       the user's click or Enter; links of other schemes and relative ones only reach the
       app), MV_RTL / MV_LTR (the document's direction; default: that of
       its first strong character, else the window's)

Layout: the blocks in a column; block quotes indent with a bar, lists with their markers in
front (bullets by depth: disc, ring, square; numbers; task boxes), headings 2 / 1.5 / 1.25 /
1 / 0.875 / 0.85 times the body text in bold (h1 and h2 underlined), code blocks in the
monospace font on a tinted box with the code editor's highlighting (Highlighter::for_name of
the fence's language, ui.code_theme colours), tables with column widths from their cells
(wrapped to fit the column), images at their size (DIP; wider than the column: scaled down;
SVGs through img_svg, sharp at every size), rules.  Boxes of CSS (MdDoc::boxes) at their
place among the indents: margins and padding (growing with the text), borders, a background
under their blocks; a margin CSS gives at a box's top or bottom takes the place of the gap
between blocks; on a background the text without a colour of its own takes one that reads on
it (fields there get the theme's background behind them); high contrast draws no
backgrounds and the borders in the text's colour; left and right stay where they are in a
right-to-left document.  Text wraps at text::segment's line
breaks (clusters inside a word too long).  A paragraph's direction comes from its first
strong character: Hebrew and Arabic run right to left (their runs shaped and reordered by
text_shape); indents and markers follow the document's direction.  Only the blocks in view
are laid out (cached per width, dpi, text scale and fonts) and drawn; scrolling is anchored at
the first visible block (vlist.rs Rows), so blocks laid out for the first time never move
what is shown.
Mouse: a drag selects (the view scrolls at its edges), Shift+click extends, a double click
selects a word; a click on a link activates it.  Keys (focused): Up / Down scroll by a line,
PgUp / PgDn and Space / Shift+Space by a view, Home / End to the ends, Tab / Shift+Tab
through the links and fields (the focused link is framed, a field gets the focus; scrolled
into view; past the last one the focus moves on), Enter activates the focused link, Esc
drops it, Ctrl+A selects all,
Ctrl+C / Ctrl+Insert copy the selection as text (list markers, cells by tabs) and HTML.
Images: names of the VFS ("app://", "data://" ...) and paths (relative ones after base) are
read on first view and kept in the state -- of the OS's files only regular ones up to 32 MB,
never a network or device path ("//server/share", "\\.\..."); http(s) addresses are never
fetched by the view (MI_LOCAL: the alt text shows in a frame instead; MI_ASK / MI_APP: the
app fetches them).
Records: K_BOX id (label: the label, value: the scroll position in DIP); per visible block
K_HEADING (value: the level), K_TEXT (paragraphs, code), K_LISTITEM (the first block of a list
item, value: its number), K_CHECKBOX (a task's box, checked); a table's rows in view: K_TABLE
around them (grid: the table's rows and columns), per row K_ROW (label: its cells' texts) and
per cell K_CELL (a header row's: K_COLHEADER; cell: row and column); K_LINK per
link, K_IMAGE (label: the alt text) per image; per visible field the widget's record
(K_TEXTBOX, K_CHECKBOX, K_RADIO, K_COMBO, K_BUTTON: label the field's name -- its <label>,
aria-label, title, placeholder -- or a button's caption; text boxes and lists take it from a
K_TEXT record without area along their top edge).  Ids: id (focus, keys), part_id(id, 1)
the scroll bar, md_field_id(id, i) the fields.
/// markdown_view flags: no card around the document
MV_NO_FRAME: u32 = 1
/// an activated link is also opened (open_url) when it is http, https or mailto
MV_OPEN_LINKS: u32 = 2
/// the document runs right to left (indents and markers on the right)
MV_RTL: u32 = 4
/// the document runs left to right (also in a mirrored window)
MV_LTR: u32 = 8
/// MdViewState::images: VFS names and paths are read, other addresses show their alt text
MI_LOCAL: u8 = 0
/// as MI_LOCAL, but http:, https: and data: addresses are asked from the app
MI_ASK: u8 = 1
/// every image is asked from the app (the view reads no file)
MI_APP: u8 = 2
  • struct MdEv what a document view reports (see the module header)

    • ev: u32 -- EV_FOCUS, EV_HOVER, EV_ACT (a link was activated)
    • link: Option<String> -- the target of the link activated this frame
    • hover: Option<String> -- the target of the link under the pointer
    • field: Option<usize> -- a field the user changed this frame, or a button (MF_BUTTON) he pressed (index of doc().fields())
    • submit: Option<MdSubmit> -- a form sent this frame (Enter in a field, a submit button)
    • click: Option<MdNode> -- the element a click hit this frame, or the link Enter activated (see the module header)
  • struct MdSubmit a form being sent: what a browser would send (MdViewState::form_data)

    • form: usize -- the form (index of doc().forms()), its action (as written) and method ("get" / "post")
    • action: String
    • method: String
    • fields: Vec<(String, String)> -- the name / value pairs in document order
    • button: Option<usize> -- the submit button that sent it (None: Enter in a form without one)
  • fn md_field_id(id: u32, i: usize) -> u32 the widget id of field i of the view id (the drop-down lists' rows take ids of their own)

  • struct MdViewState the state of a document view (see the module header)

    • base: String -- where relative image paths start ("app://help/"; "" = as given)
    • label: String -- the name for screen readers ("" = "Dokument")
    • images: u8 -- where the images come from: MI_LOCAL (default), MI_ASK, MI_APP (see the module header)

impl MdViewState

  • fn new() -> MdViewState an empty document
  • fn doc(&self) -> &MdDoc the document shown
  • fn doc_mut(&mut self) -> &mut MdDoc the document to change (an HTML document's elements, md_dom.rs): the view takes the changes at its next frame (or update()) and keeps its place, selection, focus and the values the user gave the fields that stay
  • fn update(&mut self) -> bool take the document's changes now (MdDoc::update; the view does it every frame) -> there were changes
  • fn set_markdown(&mut self, s: &str) show Markdown (parsed now unless it is the text shown already)
  • fn set_html(&mut self, s: &str) show HTML (parsed now unless it is the text shown already)
  • fn set_doc(&mut self, doc: MdDoc) show a document
  • fn selection(&self) -> TextSel the selection (TextPos: block, byte of its text)
  • fn set_selection(&mut self, sel: TextSel) select a range (clamped to the document)
  • fn select_all(&mut self) select the whole document
  • fn selected_text(&self) -> String the selection as text (list markers, cells by tabs, images as their alt texts)
  • fn selected_html(&self) -> String the selection as HTML
  • fn scroll_to_block(&mut self, i: usize) show block i at the top
  • fn scroll_to_end(&mut self) show the end, also while the document grows (until the user scrolls away)
  • fn first_visible(&self) -> (usize, i32) the first visible block and how many px of it are above the view
  • fn image_requests(&mut self) -> Vec<String> the image addresses the view waits for since the last call (MI_ASK / MI_APP; each one once): the app fetches them and answers with set_image
  • fn set_image(&mut self, addr: &str, data: Option<&[u8]>) -> bool the bytes of an image address (None: it could not be had): decoded now, shown from the next frame on (the alt text when they are no picture) -> an image of the document waited for it. An address not asked yet is kept for the images that need it later.
  • fn field_text(&self, i: usize) -> &str field i's text (text fields, text areas, hidden fields; "" for the other kinds)
  • fn field_checked(&self, i: usize) -> bool field i is checked (check boxes, radios)
  • fn field_selected(&self, i: usize) -> i32 the option chosen in field i (drop-down lists; -1: none)
  • fn set_field_text(&mut self, i: usize, s: &str) set field i's text (text fields, text areas, hidden fields; changes of the document keep it, as the user's input)
  • fn set_field_checked(&mut self, i: usize, on: bool) check field i or not (a radio: the others of its group go off)
  • fn set_field_selected(&mut self, i: usize, k: i32) choose option k of field i (a drop-down list; -1: none)
  • fn form_data(&self, form: usize, button: Option<usize>) -> Vec<(String, String)> the name / value pairs form form sends with the submit button button (as browsers build them: fields with a name that are not disabled; check boxes and radios when checked; a list's chosen value; of the buttons only button)
  • fn reset_form(&mut self, form: usize) the fields of form form get their first values back
  • fn focused_field(&self) -> Option<usize> the field with the keyboard focus (an index of doc().fields())

impl Ui

  • fn markdown_view(&mut self, id: u32, st: &mut MdViewState, text: &str, flags: u32) -> MdEv ui_markdown_view(id, &mut st, text, MV_*) -> MdEv (see the module header of md_view.rs)
  • fn markdown_fit(&mut self, st: &mut MdViewState, text: &str, w_px: i32, flags: u32) -> i32 ui_markdown_fit(&mut st, text, w_px, MV_*) -> the height in DIP a view w_px wide needs for the whole text (see the module header)
  • fn html_view(&mut self, id: u32, st: &mut MdViewState, html: &str, flags: u32) -> MdEv ui_html_view(id, &mut st, html, MV_*) -> MdEv: the HTML counterpart of markdown_view
  • fn doc_view(&mut self, id: u32, st: &mut MdViewState, flags: u32) -> MdEv ui_doc_view(id, &mut st, MV_*) -> MdEv: the document set in st (see the module header)

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