etyOS

Nirvana: writing applets

Events

What each component sends when it is used, and the helpers that read an event's value.

An event has the id of the node it happened on, a name, and sometimes a value. Events arrive in the order they happened, one at a time; each is followed by a render.

By component

ComponentEventValueRead it with
Button, Key, MenuItem, Tab, AppRow, GalleryItem, CalendarDayclicknone
Button with .opens_menu()clickwhere to open the menuevent.point()
Row with an id, SidebarItemclicknone
Row, SidebarItem with .context()contextthe pointer’s positionevent.point()
SidebarItem with .action(…)click on <id>.actionnone
SidebarItem with .reorder(…)reorderbefore:<id> or after:<id>event.text()
Switchchangeon or offevent.flag()
Sliderinputthe number, while draggingevent.number()
TextFieldinputthe text, at every editevent.text()
submitthe text, on Enterevent.text()
Segmented, PictureChoiceselectthe indexevent.index()
Shortcutrecordrecording started or stoppedevent.flag()
changethe binding ("Super+Shift+A"), "" when clearedevent.text()
Sheet with an iddismissnone
Menudismissnone
Tabclick on <id>.closenone (the close button, or a middle click)
PathBarselectthe crumb’s indexevent.index()
clicknone (the empty part of the bar)
submit, dismissthe typed path; given upevent.text()
DataTableselectthe rowevent.selection()
activatethe row’s id (double click, Enter)event.text()
contextthe row and the pointevent.target(), event.point()
click on <id>.sort:<column>none
Terminaldatawhat was typedevent.text()
resizethe size in cellsevent.grid()
changethe title the program setevent.text()
MediaViewkeynext, previous, endedevent.text()
RegionSelectselectthe rectangleevent.rectangle()
Wizardclick on <id>.next, <id>.backnone

FileView

The view does not keep the selection: the applet does, and the view reports what was done.

EventMeaningRead it with
selectA click or a key changed the selection: the item and how (one, toggle, range, all, none)event.selection()
activateDouble click or Enterevent.text() (the item’s id)
contextRight click, on an item or on the backgroundevent.target(), event.point()
dropThe selection was dropped on a folder, a sidebar item or a breadcrumbevent.target(), event.flag_named("copy")
dropFiles came in from another programevent.paths()
dropIcons were moved (free placement)event.moved()
keyA shortcut by name: copy, cut, paste, delete, rename, new-folder, refresh, back, …event.text()
submit, dismissThe inline rename fieldevent.text()

The helpers

All of them return None (or an empty list) when the value is not what they expect, so an event of another kind never panics an applet.

HelperReturnsFor
event.is(name)boolTelling submit from input, context from click
event.text()Option<&str>Text fields, bindings, ids, key names
event.flag()Option<bool>Switches
event.number()Option<f64>Sliders
event.index()Option<usize>Segmented controls
event.point()Option<(f64, f64)>Where to open a menu, in window coordinates
event.target()Option<&str>The item a context or drop is about
event.selection()Option<(Option<&str>, &str)>The item and the mode of a select
event.paths()Vec<String>Files dragged in
event.moved()Vec<(String, i32, i32)>Items moved to new places
event.flag_named(name)boolA named boolean, such as copy
event.rectangle()Option<(i64, i64, i64, i64)>x, y, width, height
event.grid()Option<(u16, u16)>Columns and rows of a terminal

A pattern for event

Match on the id; within a family, on the name.

fn event(&mut self, event: &Event, commands: &mut Commands) {
    let id = event.node_id.as_str();

    // Families first: ids that carry what they are about.
    if let Some(address) = id.strip_prefix("device:") {
        return self.select_device(address);
    }

    match id {
        "search" => self.query = event.text().unwrap_or_default().to_owned(),
        "list" if event.is("context") => self.menu = event.target().map(str::to_owned).zip(event.point()),
        "list" if event.is("activate") => self.open(event.text()),
        "menu" => self.menu = None,                       // dismissed
        "@host" if event.is("open") => self.open(event.text()),
        "done" => commands.close_window(),
        _ => {}
    }
}

Something wrong or missing on this page? Say so in the tracker (choose “Site and docs”).

Esc