VApp Class

Apps › Core

#include <vortex/vapp.h>

Public Types

using ScanStatusHandler = std::function<void (const std::string &, const std::string &)>

Android only: App hook that hears about the camera behind a Scanner, installed with set_scan_status_handler().

status Means
"scanning" The camera is live
"permission" The Scanner needs camera access that isn't granted yet
"granted" The user allowed camera access
"denied" The user refused camera access
"error:" The camera failed to start, with the reason after the colon

path names the Scanner, and is "" for "granted" and "denied", since the app may have no Scanner on screen while the user answers.

using ListObserver = std::function<void (const int &)>

Hook that receives a list's new entries each time set_list() sets them, installed with set_list_observer().

using ListActionHandler = std::function<void (const std::string &, const std::string &, const std::string &)>

App hook that handles a click on a list action button, installed with set_list_action_handler().

It receives the list's key, the row's ListEntry::id ("" for a button outside the rows), and the action from the button's intent, like "remove".

Public Members

static const double kNarrowBreakpoint = 600.0
static const double kMediumBreakpoint = 900.0
static const double kScrollHideThreshold = 30.0
static const double kMobileIconScale = 1.5
static const double kMobileFontScale = 1.25
static const double kMobileMetricScale = 1.25

Public Functions

VApp()
VApp(const VApp &)
VApp & operator=(const VApp &)
VPlatform & platform()
void set_root_style(const std::string & path)
void set_back_handler(BackHandler handler)
void bind_text(const std::string & path, const std::string & text)
std::string bound_text(const std::string & path)
void bind_media_source(const std::string & path, const std::string & source)
std::string bound_media_source(const std::string & path)
void bind_href(const std::string & path, const std::string & url)
void bind_qr_data(const std::string & path, const std::string & data)
void bind_height(const std::string & path, double height)
double bound_height(const std::string & path)
void bind_visible(const std::string & path, bool visible)
bool is_hidden(const std::string & path)
void bind_click(const std::string & path, std::function<void ()> handler)
void toggle_state(const std::string & path, const std::string & state)
void set_editor_text(const std::string & path, std::string text)
std::string editor_text(const std::string & path)
void push_editor_text(const std::string & path, const std::string & text)
void set_editor_input_handler(const std::string & path, std::function<void (const std::string &)> handler)
bool checkbox_checked(const std::string & path)
void set_checkbox_checked(const std::string & path, bool checked)
void set_combo_options(const std::string & path, int options)
std::string combo_selected(const std::string & path)
void set_combo_selected(const std::string & path, const std::string & id)
void set_combo_input_handler(const std::string & path, std::function<void (const std::string &)> handler)
void set_scan_result_handler(const std::string & path, std::function<void (const std::string &)> handler)
void set_scan_status_handler(ScanStatusHandler handler)
void request_camera_permission()
void http(const HttpRequest & request, HttpCallback callback)
void pick_and_upload(const std::string & method, const std::string & accept, const std::string & url, const int & headers, const std::string & field_name, UploadCallback callback)
void pick_path(const PickRequest & request, PickCallback callback)
void open_url(const std::string & url)
void navigate_url(const std::string & url)
void copy_text(const std::string & text)
void save_text(const std::string & suggested_name, const std::string & mime_type, const std::string & content, SaveTextCallback callback)
void run_process(const int & argv, ProcessLineCallback on_line, ProcessExitCallback on_exit)
void start_purchase(const std::string & product_id, const std::string & plan_id, const std::string & account_id, PurchaseCallback callback)
void query_purchases(QueryPurchasesCallback callback)
void after(int milliseconds, std::function<void ()> callback)
void post(std::function<void ()> callback)
void set_env(const std::string & key, std::string value)
std::string env(const std::string & key)
void store(const std::string & key, const std::string & value)
const std::string & active_theme()
const int & themes()
const VTheme * active_theme_data()
bool is_narrow()
bool is_medium()
bool is_mobile()
const std::string & app_version()
void set_list(const std::string & key, int entries)
void set_list_observer(const std::string & key, ListObserver observer)
void set_list_action_handler(const std::string & key, ListActionHandler handler)
void set_list_action_handler(ListActionHandler handler)
int & engine()
bool init(const std::string & themes_path, const std::string & styles_path, const std::string & theme_id, const int & app_style_dirs = {})
bool set_active_theme(const std::string & id)
std::string add_theme_json(const std::string & json)
bool remove_theme(const std::string & id)
void set_theme_changed_handler(std::function<void (const std::string &)> handler)
void set_http_handler(HttpHandler handler)
void set_upload_handler(UploadHandler handler)
void set_pick_handler(PickHandler handler)
void set_open_url_handler(OpenUrlHandler handler)
void set_navigate_handler(OpenUrlHandler handler)
bool navigate_back()
bool back_captures()
void set_copy_text_handler(CopyTextHandler handler)
void set_save_text_handler(SaveTextHandler handler)
void set_run_process_handler(RunProcessHandler handler)
void set_purchase_handler(PurchaseHandler handler)
void set_query_purchases_handler(QueryPurchasesHandler handler)
void set_timer_handler(TimerHandler handler)
void set_post_handler(PostHandler handler)
void set_render_handler(std::function<void ()> handler)
void request_render()
void set_store_handler(std::function<void (const std::string &, const std::string &)> handler)
bool has_editor_value(const std::string & path)
void set_camera_permission_handler(std::function<void ()> handler)
void dispatch(const std::string & path, const std::string & event)
bool set_viewport_width(double width)
bool set_viewport_height(double height)
double viewport_height()
bool set_maximized(bool maximized)
bool on_scroll(double delta)
bool reveal_controls()
bool controls_hidden()
void set_platform(const std::string & name)
void set_app_version(std::string version)
void set_mobile_icon_scale(double scale)
double mobile_icon_scale()
void set_mobile_font_scale(double scale)
double mobile_font_scale()
void set_mobile_metric_scale(double scale)
double mobile_metric_scale()
RenderNode build_render_tree()
std::string render_tree_json()
int render_patches()
std::string render_patches_json()

Detailed Description

App's handle on the Vortex core, which each platform's runner passes to build_app().

An app's interface comes from its .lyr styles, and VApp fills it in at runtime: it binds live values onto the styles' nodes, reads back what the user typed or picked, and reaches platform services like HTTP.

Paths

Most members take a node's path: the root style's name, then the name of each child down to the node, joined by /. A row that a list repeats takes its entry's id as its name, so a node inside it has a path like "App/Notes/n1/Title".

App << Vortex/App:                  // "App"
    Status << Vortex/Text:          // "App/Status"
        Text: "Ready"

    Save << Vortex/Button:          // "App/Save"
        Label << Vortex/Text:       // "App/Save/Label"
            Text: "Save"

Binds

A bind replaces a value that a node's style sets, like its Text, and lasts until the app changes it. Binds show on screen at the next render. The platform renders after each event that it delivers, like a click, and after each callback that it runs for a service, like an http() reply, so app code never asks for one:

void Vortex::build_app(VApp& app)
{
    app.set_root_style("App");

    app.bind_click("App/Save", [&app]
    {
        app.bind_text("App/Status", "Saved");
    });
}

Inputs

Four node types take the user's input. For each, the app can read the value, set it, and install a handler that runs when the user changes it:

Node Read Set On change
Editor editor_text() push_editor_text() set_editor_input_handler()
Check Box checkbox_checked() set_checkbox_checked() bind_click()
Combo Box combo_selected() set_combo_selected() set_combo_input_handler()
Scanner set_scan_result_handler()

Setting a value from code doesn't run the change handler. When the handler runs, the read call already returns the new value. A path holds one handler of each kind, so installing another replaces it.

Platform services

Some members reach a service that the platform provides, like http() for networking or open_url() for the system browser. Each platform installs the services that it supports, so the same app code runs on all of them. Calling a service that isn't installed, like run_process() on web or any service in a test, is safe: most report a failure or cancel through their callback right away, so app code never waits on a reply that won't come. Each member's doc says what it does without its service.

Callbacks run on the UI thread, which is the only thread that may use VApp. A worker thread hands its results to post() instead.

Member Variable Documentation

static const double kNarrowBreakpoint = 600.0

Widest window (px) in the Narrow state, for layouts like a phone's.

static const double kMediumBreakpoint = 900.0

Widest window (px) in the Medium state, which starts above kNarrowBreakpoint, like an unfolded foldable phone with room for one pane but not two.

static const double kScrollHideThreshold = 30.0

Distance (px) that the user scrolls down to hide the controls, like the titlebar at the bottom of a Narrow window, or up to show them again (see VPlatform::on_scroll()).

static const double kMobileIconScale = 1.5

static const double kMobileFontScale = 1.25

static const double kMobileMetricScale = 1.25

Member Function Documentation

VApp()

Creates an empty app. Each platform's runner creates one at startup and passes it to build_app().

VApp(const VApp &)

Not copyable, since the platform and the app's handlers keep references to it. Pass it by reference, like build_app() does.

VApp & operator=(const VApp &)

No description available.

VPlatform & platform()

Returns the platform contract, the methods that platform code calls to start the app, deliver the user's input, and read back what to draw.

App code doesn't need it.

void set_root_style(const std::string & path)

Sets the style that the UI is built from, like "App".

It starts every node's path, and nothing renders until it names a loaded style. An app that uses the window shell keeps the name App, since the titlebar links to App/Icon and App/Name.

void set_back_handler(BackHandler handler)

Android only: Installs the handler that decides what the system back button does (see BackHandler).

Without one, back leaves the app.

void bind_text(const std::string & path, const std::string & text)

Binds the text of the node at path, in place of its style's Text.

Text, Button, Markdown, and Host nodes show it as their content, and an Editor shows it as its starting value. To change what an Editor shows later, use push_editor_text().

An empty text shows no text. It doesn't bring back the style's Text.

std::string bound_text(const std::string & path)

Returns the text bound to the node at path, or "" when none is.

It doesn't read the style's Text.

void bind_media_source(const std::string & path, const std::string & source)

Binds what the Image or Video node at path shows, in place of its style's Source, like a profile picture's URL.

source is a URL or a file path. An empty source removes the bind, so the node shows its style's Source again, like a placeholder.

std::string bound_media_source(const std::string & path)

Returns the source bound to the node at path, or "" when none is.

void bind_href(const std::string & path, const std::string & url)

Static-web only: Binds the URL that the Link node at path links to, in place of its style's Href.

An empty url removes the bind, so the link goes back to its style's Href.

void bind_qr_data(const std::string & path, const std::string & data)

Binds the data that the QR node at path encodes, in place of its style's Data, like a URL to share.

An empty data removes the bind, so the node shows its style's Data again.

void bind_height(const std::string & path, double height)

Binds the height of the node at path (px), in place of its style's Size.Height, like a logo preview at a height that the user types.

The height is used as is: the mobile scale doesn't enlarge it, and it wins over a Size.Height Viewport fraction. A height of 0 or less removes the bind.

double bound_height(const std::string & path)

Returns the height bound to the node at path (px), or 0 when none is.

void bind_visible(const std::string & path, bool visible)

Shows or hides the node at path, along with everything under it.

A hidden node leaves the render tree, so it takes no space. Showing a node only undoes a hide from this call, so a node that its style hides, like with Visible: 0, stays hidden.

bool is_hidden(const std::string & path)

Returns whether bind_visible() hid the node at path.

A node that its style hides reads false.

void bind_click(const std::string & path, std::function<void ()> handler)

Binds the handler that runs when the user clicks the node at path.

Buttons and Check Boxes send clicks, and so does the scrim beside an open side sheet (see ResolvedStyle::overlay). A button with a list: or window: intent runs that instead (see ResolvedStyle::intent).

void toggle_state(const std::string & path, const std::string & state)

Turns state on for the node at path if it's off, or off if it's on.

A state is a name that a style can give its own values to, like Selected here:

Tab << Vortex/Button:
    Fill: Transparent @Selected: Theme/Tertiary    // Tertiary while on

The node's descendants resolve with the state too. Pass the name without its @. Since nothing reads a state back, an app that needs a state on or off, rather than flipped, keeps track of it.

void set_editor_text(const std::string & path, std::string text)

Sets the value that editor_text() returns for the Editor at path, without changing what the field shows.

Call it with the same text as bind_text() when giving a field its starting value, so that a submit without edits reads that value. To change what the field shows, use push_editor_text().

std::string editor_text(const std::string & path)

Returns the value of the Editor at path: what the user last typed, or what set_editor_text() or push_editor_text() last set.

Returns "" before any of those, even when bind_text() gave the field a starting value.

void push_editor_text(const std::string & path, const std::string & text)

Sets what the Editor at path shows, like clearing a field after a submit, and what editor_text() returns.

The field changes at the next render. Unlike a new bind_text(), it also works when the bound text doesn't change, like when clearing a field whose starting value is already "".

void set_editor_input_handler(const std::string & path, std::function<void (const std::string &)> handler)

Installs the handler that runs each time the user edits the Editor at path, with the field's new text, like for search as you type.

bool checkbox_checked(const std::string & path)

Returns whether the Check Box at path is checked, which is whether its Checked state is on.

A Check Box starts unchecked.

void set_checkbox_checked(const std::string & path, bool checked)

Checks or unchecks the Check Box at path, like when resetting a form.

void set_combo_options(const std::string & path, int options)

Sets the options that the Combo Box at path offers, in order (see ComboOption).

The selection stays when the new options include it, and otherwise moves to the first option ("" when options is empty). That way combo_selected() always names an option that the box offers.

std::string combo_selected(const std::string & path)

Returns the id of the option selected in the Combo Box at path, or "" when the box has no options.

void set_combo_selected(const std::string & path, const std::string & id)

Selects the option with id in the Combo Box at path, like when filling a form from saved values.

An id that isn't among the box's options is ignored, so set the options first.

void set_combo_input_handler(const std::string & path, std::function<void (const std::string &)> handler)

Installs the handler that runs each time the user picks a different option in the Combo Box at path, with the option's id.

void set_scan_result_handler(const std::string & path, std::function<void (const std::string &)> handler)

Android only: Installs the handler that runs each time the Scanner at path reads a QR code, with the text that the code holds.

void set_scan_status_handler(ScanStatusHandler handler)

Android only: Installs the handler that hears about the camera behind every Scanner, like when the user refuses access (see ScanStatusHandler).

void request_camera_permission()

Android only: Shows the system's camera permission prompt, like from a "Grant camera access" button.

Vortex shows the prompt once on its own, the first time a Scanner needs access, so this is for asking again. The answer arrives as a "granted" or "denied" status (see ScanStatusHandler). Elsewhere, it does nothing.

void http(const HttpRequest & request, HttpCallback callback)

Sends an HTTP request without blocking, then calls callback with the reply (see HttpResponse).

Without the platform's HTTP service, callback runs right away with a status of 0.

void pick_and_upload(const std::string & method, const std::string & accept, const std::string & url, const int & headers, const std::string & field_name, UploadCallback callback)

Opens the platform's file picker, uploads the chosen file to url as a multipart form, then calls callback with the result (see UploadResult).

The file's bytes never reach the app, only the server's reply does. Without the platform's upload service, callback runs right away with a status of 0.

  • method is the HTTP verb, like "POST" or "PUT".
  • accept limits the picker to MIME types, like "image/png,image/jpeg".
  • headers are sent with the upload, like an Authorization header.
  • field_name is the form field that carries the file, like "file".

void pick_path(const PickRequest & request, PickCallback callback)

Desktop only: Opens a file dialog (see PickRequest), then calls callback with the absolute path that the user picked, or "" when they canceled.

Elsewhere, callback runs right away with "", like a cancel.

void open_url(const std::string & url)

Opens url outside the app: in the system browser, or in a new tab on web.

Browsers block a new tab outside a click handler, so for a URL that arrives later, like in an http() reply, use navigate_url(). Without the platform's browser service, it does nothing.

void copy_text(const std::string & text)

Puts text on the system clipboard, like a link to share.

On web, it only works from a click handler on a secure page (HTTPS or localhost), since browsers only allow clipboard writes there. Without the platform's clipboard service, it does nothing.

void save_text(const std::string & suggested_name, const std::string & mime_type, const std::string & content, SaveTextCallback callback)

Saves content as a text file, then calls callback with whether it was saved.

Desktop and Android show a save dialog, while web downloads the file directly (see SaveTextCallback). Without the platform's save service, callback runs right away with false, like a cancel.

  • suggested_name is the file name to start with, like "notes.json".
  • mime_type is the content's type, like "application/json".

void run_process(const int & argv, ProcessLineCallback on_line, ProcessExitCallback on_exit)

Desktop only: Runs another program, like a command-line tool that ships with the app, and streams its output back.

argv[0] is the program. A path runs as given, while a bare name is looked for next to the app's executable first, then on the PATH. on_line runs once for each line of output, and on_exit runs once at the end. Elsewhere, on_exit runs right away with -1, like for a program that failed to start.

void start_purchase(const std::string & product_id, const std::string & plan_id, const std::string & account_id, PurchaseCallback callback)

Android only: Shows Google Play's purchase sheet, then calls callback with the result (see PurchaseResult).

Desktop and web apps take payments some other way, like on a website. There, callback runs right away with an error.

  • product_id and plan_id name what to buy in the store's catalog, like a subscription and its "monthly-3" plan.
  • account_id is an opaque ID for the user's account, which the store uses to detect fraud.

void query_purchases(QueryPurchasesCallback callback)

Android only: Calls callback with the purchases that the user owns but that the app's server hasn't verified.

A purchase can go through at the store but never reach the server, like when the app closes mid-purchase, and the store refunds a purchase that isn't confirmed within a few days. So an app calls this at launch and sends each token to its server again. Elsewhere, callback runs right away with an empty list.

void after(int milliseconds, std::function<void ()> callback)

Runs callback once after milliseconds, like to wait for the user to stop typing before a search.

A timer can't be canceled, so a callback that may no longer be wanted checks for itself, like by comparing a counter that each newer timer bumps. Without the platform's timer service, callback never runs.

void post(std::function<void ()> callback)

Desktop only: Runs callback on the UI thread, and is safe to call from any thread, like to hand over a worker thread's result.

Elsewhere, callback never runs.

void set_env(const std::string & key, std::string value)

Sets the value that env() returns for key, until the app closes.

Platforms call it at startup, before build_app(). To keep a value for later runs, use store().

std::string env(const std::string & key)

Returns the value under key, or "" when there is none.

Before build_app() runs, the platform passes in the values that the app saved with store() on its last run, and some of its own, like "base_url" on web (the page's origin, like "https://example.com"). Values from set_env() and store() read back right away.

void store(const std::string & key, const std::string & value)

Saves value under key, where env() reads it now and on later runs, like a sign-in token.

The platform keeps it unencrypted, in QSettings on desktop, local storage on web, and SharedPreferences on Android. An empty value removes the key. Without the platform's storage service, the value lasts until the app closes.

const std::string & active_theme()

Returns the active theme's id, its name and publisher, like "Dark (huntrsoftwarellc)", or "" before the platform loads the themes.

const int & themes()

Returns the loaded themes, keyed by id, like for a theme picker.

const VTheme * active_theme_data()

Returns the active theme, or nullptr before the platform loads the themes.

bool is_narrow()

Returns whether the window is at most kNarrowBreakpoint wide, which turns on the Narrow state.

Styles give the state values of their own, like Layout.Direction: "Row" @Narrow: "Column", so this is for choices that app code makes.

bool is_medium()

Returns whether the window is wider than kNarrowBreakpoint and at most kMediumBreakpoint wide, which turns on the Medium state.

bool is_mobile()

Returns whether the app runs on Android, which turns on the Mobile state.

const std::string & app_version()

Returns the app's version, like "1.2.0", or "" in a build without one, like a development build.

The platform sets it at startup, like from DesktopAppConfig::appVersion on desktop. The settings' Version section shows it, and update checks compare it with the newest release.

void set_list(const std::string & key, int entries)

Sets the entries of the list with key, replacing its old ones (see ListEntry).

A list is a node with Bind: "List:<key>". Its first child is the row template, which the core repeats once for each entry, and an optional second child is a separator that goes between rows:

Notes << Vortex/Box:
    Bind: "List:notes"

    Row << Vortex/Text:          // One per entry, like "App/Notes/n1"
        Bind: "Field:title"      // The entry's "title", like "Groceries"

    Divider << Vortex/Box:       // Between rows, not after the last
        Size.Height: 1

The first row has the First state on and the last has Last, so a template can restyle the ends, like rounding only the outer corners. A separator can't read the entries' fields. The rows change on screen at the next render, and the key's observer runs right away (see set_list_observer()).

void set_list_observer(const std::string & key, ListObserver observer)

Installs the handler that runs each time set_list() sets the list with key, with the new entries.

It's for reacting to a list that platform code fills. For example, the theme directories dialog shows its empty-state text until the platform lists a folder. It doesn't run when installed. A key holds one handler, so installing another replaces it.

void set_list_action_handler(const std::string & key, ListActionHandler handler)

Installs the handler that runs when the user clicks a list action button for the list with key.

A list action button is a button with Intent: "list:<action>" inside a row template. A button outside the rows, like an Add button, names its list too. The comments show what the handler receives:

Notes << Vortex/Box:
    Bind: "List:notes"

    Row << Vortex/Box:
        Remove << Vortex/Button:
            Intent: "list:remove"    // ("notes", "n1", "remove")

Add << Vortex/Button:
    Intent: "list:add:notes"         // ("notes", "", "add")

A key holds one handler, so installing another replaces it. It wins over the handler that the other overload installs, whichever was installed first.

void set_list_action_handler(ListActionHandler handler)

Installs the handler that runs when the user clicks a list action button for a list that has no handler of its own.

Platform code installs it for the lists that it fills, like the theme directories, so app code gives each of its lists a handler of its own with the overload above.

int & engine()

Replaced by platform().engine().

bool init(const std::string & themes_path, const std::string & styles_path, const std::string & theme_id, const int & app_style_dirs = {})

Replaced by platform().init().

bool set_active_theme(const std::string & id)

std::string add_theme_json(const std::string & json)

bool remove_theme(const std::string & id)

void set_theme_changed_handler(std::function<void (const std::string &)> handler)

void set_http_handler(HttpHandler handler)

void set_upload_handler(UploadHandler handler)

void set_pick_handler(PickHandler handler)

void set_open_url_handler(OpenUrlHandler handler)

void set_navigate_handler(OpenUrlHandler handler)

bool back_captures()

void set_copy_text_handler(CopyTextHandler handler)

void set_save_text_handler(SaveTextHandler handler)

void set_run_process_handler(RunProcessHandler handler)

void set_purchase_handler(PurchaseHandler handler)

void set_query_purchases_handler(QueryPurchasesHandler handler)

void set_timer_handler(TimerHandler handler)

void set_post_handler(PostHandler handler)

void set_render_handler(std::function<void ()> handler)

void request_render()

void set_store_handler(std::function<void (const std::string &, const std::string &)> handler)

bool has_editor_value(const std::string & path)

void set_camera_permission_handler(std::function<void ()> handler)

void dispatch(const std::string & path, const std::string & event)

Replaced by platform().dispatch().

bool set_viewport_width(double width)

bool set_viewport_height(double height)

double viewport_height()

bool set_maximized(bool maximized)

bool on_scroll(double delta)

Replaced by platform().on_scroll().

bool reveal_controls()

bool controls_hidden()

void set_platform(const std::string & name)

Replaced by platform().set_name().

void set_app_version(std::string version)

void set_mobile_icon_scale(double scale)

double mobile_icon_scale()

void set_mobile_font_scale(double scale)

double mobile_font_scale()

void set_mobile_metric_scale(double scale)

double mobile_metric_scale()

RenderNode build_render_tree()

std::string render_tree_json()

int render_patches()

std::string render_patches_json()

› Documentation
Appearance
Theme
—