///| The emoji picker's state and keys, without drawing (compositor/host/emoji_picker.mbt draws it /// and feeds it keys and clicks). /// /// What is shown: the search results when something is typed, else the chosen tab (Recent, or /// one category). The selection moves over a grid of `cols` columns; `scroll` is the first row /// shown, kept so the selection stays in view. ///| The most recently used emoji kept. pub let max_recent : Int = 40 ///| What the picker shows in one cell: the emoji's index in `all()` and the text it inserts. pub(all) struct Item { index : Int text : String } derive(Eq, Show) ///| What a key asks the picker to do. pub(all) enum Key { /// A typed character goes into the search. Type(Char) Backspace Left Right Up Down PageUp PageDown Home End NextTab PrevTab /// Ctrl+Right / Ctrl+Left: the next / previous skin tone. NextTone PrevTone /// Enter: insert the selected emoji (and close; with Shift, stay open). Choose } derive(Eq, Show) ///| The picker. pub(all) struct Picker { mut query : String /// 0 Recent, 1-9 the categories (`tab_names`). mut tab : Int /// The selected cell among `items()`. mut sel : Int /// The first grid row shown. mut scroll : Int /// 0 default, 1-5 light to dark. mut tone : Int /// Emoji inserted lately, newest first, as inserted (skin tone included). recents : Array[String] mut cols : Int mut rows : Int /// Whether an emoji (index into `all()`) can be shown; emoji the font can't draw are left out. has : (Int) -> Bool /// What `items()` returned last, until the query, tab or tone changes. priv mut cache : Array[Item]? } ///| A picker on `recents` (newest first) and skin tone `tone`. It opens on Recent when there are /// recent emoji, else on Smileys. pub fn Picker::new( recents : Array[String], tone : Int, has? : (Int) -> Bool = _ => true, cols? : Int = 9, rows? : Int = 6, ) -> Picker { let p : Picker = { query: "", tab: 0, sel: 0, scroll: 0, tone: if tone >= 0 && tone <= 5 { tone } else { 0 }, recents: [], cols, rows, has, cache: None, } for r in recents { if p.recents.length() < max_recent && find(r) is Some(_) && !p.recents.contains(r) { p.recents.push(r) } } p.tab = if p.shown_recents().length() > 0 { 0 } else { 1 } p } ///| The recent emoji the font can draw. fn Picker::shown_recents(self : Picker) -> Array[Item] { let out : Array[Item] = [] for r in self.recents { match find(r) { Some((i, _)) if (self.has)(i) => out.push({ index: i, text: r }) _ => () } } out } ///| What is shown, in order. pub fn Picker::items(self : Picker) -> Array[Item] { match self.cache { Some(c) => return c None => () } let es = all() let out = if self.query.length() > 0 { search(self.query, keep=self.has).map(i => { index: i, text: es[i].toned(self.tone) }) } else if self.tab == 0 { self.shown_recents() } else { let a : Array[Item] = [] for i, e in es { if e.tab == self.tab && (self.has)(i) { a.push({ index: i, text: e.toned(self.tone) }) } } a } self.cache = Some(out) out } ///| The selected item, if anything is shown. pub fn Picker::selected(self : Picker) -> Item? { let items = self.items() if self.sel >= 0 && self.sel < items.length() { Some(items[self.sel]) } else { None } } ///| Something changed what is shown: start again from the first item. fn Picker::reset(self : Picker) -> Unit { self.cache = None self.sel = 0 self.scroll = 0 } ///| Show tab `t` (and clear the search). pub fn Picker::set_tab(self : Picker, t : Int) -> Unit { self.tab = (t % 10 + 10) % 10 self.query = "" self.reset() } ///| Use skin tone `t` from now on (0-5). pub fn Picker::set_tone(self : Picker, t : Int) -> Unit { self.tone = (t % 6 + 6) % 6 let sel = self.sel let scroll = self.scroll self.cache = None self.sel = sel self.scroll = scroll } ///| Select item `i` (clamped) and scroll it into view. pub fn Picker::select(self : Picker, i : Int) -> Unit { let n = self.items().length() self.sel = if n == 0 { 0 } else { i.max(0).min(n - 1) } let row = self.sel / self.cols if row < self.scroll { self.scroll = row } else if row >= self.scroll + self.rows { self.scroll = row - self.rows + 1 } } ///| Scroll by `rows` grid rows (the wheel), keeping the selection where it is. pub fn Picker::scroll_by(self : Picker, rows : Int) -> Unit { let total = (self.items().length() + self.cols - 1) / self.cols self.scroll = (self.scroll + rows).min(total - self.rows).max(0) } ///| Note that `text` was inserted: it goes first in the recent list. pub fn Picker::used(self : Picker, text : String) -> Unit { self.recents.retain(r => r != text) self.recents.insert(0, text) while self.recents.length() > max_recent { ignore(self.recents.pop()) } if self.query.length() == 0 && self.tab == 0 { // The Recent tab changed under the selection. self.cache = None } } ///| Do what `key` asks. Returns the emoji to insert for `Choose` (None when nothing is shown). pub fn Picker::key(self : Picker, key : Key) -> String? { match key { Type(c) => { self.query = self.query + c.to_string() self.reset() } Backspace => if self.query.length() > 0 { let cs = self.query.to_array() ignore(cs.pop()) self.query = String::from_array(cs) self.reset() } Left => self.select(self.sel - 1) Right => self.select(self.sel + 1) Up => if self.sel >= self.cols { self.select(self.sel - self.cols) } Down => { let n = self.items().length() if self.sel + self.cols < n { self.select(self.sel + self.cols) } else if self.sel / self.cols < (n - 1) / self.cols { // The last row is shorter: go to its last item. self.select(n - 1) } } PageUp => self.select(self.sel - self.cols * self.rows) PageDown => self.select(self.sel + self.cols * self.rows) Home => self.select(0) End => self.select(self.items().length() - 1) NextTab => self.set_tab(self.tab + 1) PrevTab => self.set_tab(self.tab - 1) NextTone => self.set_tone(self.tone + 1) PrevTone => self.set_tone(self.tone - 1) Choose => match self.selected() { Some(it) => { self.used(it.text) return Some(it.text) } None => () } } None }