Files
chandra/compositor/host/emoji_picker.mbt
T
nandiandClaude Opus 5.5 e30d9cc71f
Deploy to the handset / build-and-deploy (push) Successful in 2m20s
CI / test (push) Failing after 2m56s
Touch: fingers reach apps as wl_touch, so a thumb scrolls
Every touch was turned into a mouse drag in the seat code, which selects text
instead of scrolling. The seat now reports raw touches and the compositor
routes them: to the app as wl_touch on its own content, and to the pointer
(tap, drag, hold for right click, as before) everywhere else and for apps that
don't listen to touches. The cursor is hidden while fingers are in use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 22:22:14 -08:00

637 lines
21 KiB
MoonBit

///| The emoji picker, like Windows' Win+.: Super+. (the "emoji-picker" shortcut) opens a panel
/// with a search field, category tabs, a grid of emoji and a skin tone choice. The chosen emoji
/// goes where the user was typing.
///
/// Drawn by the compositor, like the screenshot editor: it needs the keyboard while it is up,
/// colour emoji pictures (read from Noto Color Emoji on the host, @raster.emoji_image) and the
/// text-input protocol to type into the app, none of which a panel module has.
///
/// Keyboard: while the picker is up every key is its own (type to search, arrows move, Enter
/// inserts, Shift+Enter inserts and stays open, Tab / Shift+Tab change tabs, Ctrl+Left/Right the
/// skin tone, Esc closes); only modifier keys still reach the app, so its modifier state stays
/// right. Unlike the editor, the app keeps its keyboard focus (no wl_keyboard.leave), the way an
/// input method's popup works, so its text field stays active to receive the emoji.
///
/// Inserting: when the focused window has an enabled text input (zwp_text_input_v3), the emoji is
/// typed in with commit_string. Otherwise it is put on the clipboard and a notification says so
/// and why ("paste it with Ctrl+V"); Chandra doesn't press Ctrl+V for the app, which would do
/// something else in a terminal.
///
/// Recent emoji and the skin tone are kept in state.edn (`:emoji`).
///| Sizes: a grid cell, the emoji in it, the padding, the search field and tab row heights.
let ep_cell : Int = 40
///|
let ep_emoji : Int = 28
///|
let ep_pad : Int = 12
///|
let ep_bar : Int = 34
///|
let ep_cols : Int = 9
///|
let ep_rows : Int = 6
///| What a click on the picker hits.
priv enum EmojiHit {
Cell(Int)
Tab(Int)
Tone(Int)
Panel
} derive(Eq)
///| The picker while it is up.
pub(all) struct EmojiUi {
/// None when there is no colour emoji font: `error` says why.
picker : @emoji.Picker?
error : String
/// The panel on screen.
mut rect : @scene.Rect
/// The cell under the pointer.
mut hover : Int?
}
///| Whether the picker is up.
pub fn ChandraCompositor::emoji_picker_open(self : ChandraCompositor) -> Bool {
self.emoji_ui is Some(_)
}
///| The picker's state (tests).
pub fn ChandraCompositor::emoji_picker(self : ChandraCompositor) -> @emoji.Picker? {
match self.emoji_ui {
Some(ui) => ui.picker
None => None
}
}
///| The message the picker shows instead of emoji ("" when it has emoji).
pub fn ChandraCompositor::emoji_picker_error(self : ChandraCompositor) -> String {
match self.emoji_ui {
Some(ui) => ui.error
None => ""
}
}
///| Which emoji the font can draw, asked once per session.
fn ChandraCompositor::emoji_coverage(self : ChandraCompositor) -> Array[Bool] {
match self.emoji_cover {
Some(c) => c
None => {
let c = @raster.emoji_has(@emoji.all().map(e => e.text))
let missing = c.filter(b => !b).length()
if missing > 0 {
@sys.log("emoji: the font has no picture for " + missing.to_string() + " of " + c.length().to_string() + " emoji (newer than the font); they are not listed")
}
self.emoji_cover = Some(c)
c
}
}
}
///| Super+.: open the picker, or close it.
pub fn ChandraCompositor::toggle_emoji_picker(self : ChandraCompositor) -> Unit {
if self.emoji_ui is Some(_) {
self.close_emoji_picker()
return
}
if self.shell.inspecting() || self.auth is Some(_) || self.shot is Some(_) || self.shot_edit is Some(_) {
return
}
// The launcher's search has the keyboard otherwise; the emoji are for the app.
if self.shell.launcher_open() {
self.shell.toggle_launcher()
self.overview_sync()
}
let ui : EmojiUi = match @raster.emoji_font() {
Err(why) => { picker: None, error: why, rect: @scene.Rect::new(0, 0, 0, 0), hover: None }
Ok(_) => {
let cover = self.emoji_coverage()
let (recent, tone) = match self.emoji_saved {
Some(st) => st
None => {
let st = self.shell.load_emoji()
self.emoji_saved = Some(st)
st
}
}
let p = @emoji.Picker::new(recent, tone, has=i => cover[i], cols=ep_cols, rows=ep_rows)
{ picker: Some(p), error: "", rect: @scene.Rect::new(0, 0, 0, 0), hover: None }
}
}
self.emoji_ui = Some(ui)
self.emoji_place(ui)
self.damage.mark_full()
}
///| Keep the picker's recents and skin tone for the next time, and in state.edn when they changed.
fn ChandraCompositor::emoji_remember(self : ChandraCompositor, p : @emoji.Picker) -> Unit {
let now = (p.recents.copy(), p.tone)
match self.emoji_saved {
Some((r, t)) if r == now.0 && t == now.1 => ()
_ => {
self.emoji_saved = Some(now)
self.shell.save_emoji(now.0, now.1)
}
}
}
///| Take the picker down (keeping its recents and skin tone).
pub fn ChandraCompositor::close_emoji_picker(self : ChandraCompositor) -> Unit {
match self.emoji_ui {
Some(ui) => {
match ui.picker {
Some(p) => self.emoji_remember(p)
None => ()
}
self.emoji_ui = None
self.damage.mark_full()
}
None => ()
}
}
///| The panel's size.
fn ChandraCompositor::emoji_size(self : ChandraCompositor, ui : EmojiUi) -> (Int, Int) {
let w = ep_pad * 2 + ep_cols * ep_cell
match ui.picker {
Some(_) => (w, ep_pad + ep_bar + 8 + ep_bar + 8 + ep_rows * ep_cell + 8 + 32 + ep_pad)
None => (w, 150 + self.theme.font_size * 3)
}
}
///| Put the panel under the caret of the focused text field when the app says where it is,
/// else in the middle of the focused window, else of the screen; always on screen.
fn ChandraCompositor::emoji_place(self : ChandraCompositor, ui : EmojiUi) -> Unit {
let (w, h) = self.emoji_size(ui)
let sw = self.output_w
let sh = self.output_h
let mut x = (sw - w) / 2
let mut y = (sh - h) / 2
match self.kbd_win {
Some(wid) =>
match self.windows.get(wid) {
Some(win) =>
match self.server.text_input_cursor(wid) {
Some((cx, cy, _, ch)) => {
let (ox, oy) = win.surface_origin()
x = ox + cx
y = oy + cy + ch + 6
let bottom_edge = if self.osk_visible {
self.osk_rect().y
} else {
sh - self.theme.panel_height
}
if y + h > bottom_edge {
y = oy + cy - h - 6 // no room under the caret: above it
}
}
None => {
let r = win.client_rect()
x = r.x + (r.w - w) / 2
y = r.y + (r.h - h) / 2
}
}
None => ()
}
None => ()
}
x = x.min((sw - w - 8).max(8)).max(8)
let max_y = sh - self.osk_bottom_inset() - h - 8
y = y.min(max_y.max(8)).max(8)
ui.rect = @scene.Rect::new(x, y, w, h)
}
///| The search field, the tabs, the grid and the skin tones, on screen.
fn emoji_search_rect(r : @scene.Rect) -> @scene.Rect {
@scene.Rect::new(r.x + ep_pad, r.y + ep_pad, r.w - ep_pad * 2, ep_bar)
}
///|
fn emoji_tab_rect(r : @scene.Rect, t : Int) -> @scene.Rect {
let tw = (r.w - ep_pad * 2) / 10
@scene.Rect::new(r.x + ep_pad + t * tw, r.y + ep_pad + ep_bar + 8, tw, ep_bar)
}
///|
fn emoji_grid_rect(r : @scene.Rect) -> @scene.Rect {
@scene.Rect::new(r.x + ep_pad, r.y + ep_pad + ep_bar * 2 + 16, ep_cols * ep_cell, ep_rows * ep_cell)
}
///|
fn emoji_tone_rect(r : @scene.Rect, t : Int) -> @scene.Rect {
let g = emoji_grid_rect(r)
let s = 28
@scene.Rect::new(r.x + r.w - ep_pad - (6 - t) * (s + 2), g.y + g.h + 10, s, s)
}
///| What is at (x, y).
fn ChandraCompositor::emoji_hit(self : ChandraCompositor, ui : EmojiUi, x : Int, y : Int) -> EmojiHit? {
let r = ui.rect
if !r.contains(x, y) {
return None
}
match ui.picker {
None => Some(Panel)
Some(p) => {
let g = emoji_grid_rect(r)
if g.contains(x, y) {
let i = (p.scroll + (y - g.y) / ep_cell) * ep_cols + (x - g.x) / ep_cell
return if i < p.items().length() { Some(Cell(i)) } else { Some(Panel) }
}
for t = 0; t < 10; t = t + 1 {
if emoji_tab_rect(r, t).contains(x, y) {
return Some(Tab(t))
}
}
for t = 0; t < 6; t = t + 1 {
if emoji_tone_rect(r, t).contains(x, y) {
return Some(Tone(t))
}
}
Some(Panel)
}
}
}
///| evdev codes the picker reads (Enter and the arrows are in screenshot.mbt / switcher.mbt).
///|
let key_tab : UInt = 15
///|
let key_pgup : UInt = 104
///|
let key_pgdn : UInt = 109
///|
let key_home : UInt = 102
///|
let key_end : UInt = 107
///| Input while the picker is up. True when it was the picker's.
fn ChandraCompositor::emoji_input(self : ChandraCompositor, ev : @nested.InputEvent, time_ms : UInt) -> Bool {
match ev {
Key(code, false) =>
// Releases of keys the picker took (also after it closed).
if self.emoji_swallow.contains(code) {
self.emoji_swallow.retain(c => c != code)
return true
}
_ => ()
}
let ui = match self.emoji_ui {
Some(ui) => ui
None => return false
}
match ev {
Touch(_) => false
// Other releases (the '.' of Super+., a key held since before) take the usual path.
Key(_, false) => false
Key(code, true) => {
if code == key_lsuper || code == key_rsuper {
// Super never reaches clients anyway; a Super tap doesn't open the launcher over the picker.
self.super_solo = false
return true
}
if @shell.is_modifier_key(code) {
return false // the app keeps its modifier state right
}
self.emoji_swallow.push(code)
if self.shell.shortcuts.lookup({ mods: self.held_mods(), key: code }) == Some(EmojiPicker) {
self.close_emoji_picker() // Super+. again
} else {
self.emoji_key(ui, code)
}
true
}
Modifiers(_, _, _, _) => false
PointerMove(x, y) => {
let hit = self.emoji_hit(ui, x.to_int(), y.to_int())
let hover = match hit {
Some(Cell(i)) => Some(i)
_ => None
}
if hover != ui.hover {
ui.hover = hover
self.damage.mark_full()
}
if hit is Some(_) {
self.ptr_x = x
self.ptr_y = y
self.leave_pointer()
true
} else {
false
}
}
PointerButton(button, pressed) => {
let hit = self.emoji_hit(ui, self.ptr_x.to_int(), self.ptr_y.to_int())
match hit {
None => {
// A click elsewhere closes the picker and goes on as usual.
if pressed {
self.close_emoji_picker()
}
false
}
Some(h) => {
if pressed && button == 272U {
self.emoji_click(ui, h, (self.mods & mod_shift) != 0U, time_ms)
}
true
}
}
}
Scroll(axis, v, discrete) =>
match self.emoji_hit(ui, self.ptr_x.to_int(), self.ptr_y.to_int()) {
None => false
Some(_) => {
match ui.picker {
Some(p) if axis == 0U => {
let rows = if discrete != 0 { discrete } else if v > 0.0 { 1 } else if v < 0.0 { -1 } else { 0 }
p.scroll_by(rows)
self.damage.mark_full()
}
_ => ()
}
true
}
}
}
}
///| A key press on the picker.
fn ChandraCompositor::emoji_key(self : ChandraCompositor, ui : EmojiUi, code : UInt) -> Unit {
self.damage.mark_full()
if code == key_esc {
self.close_emoji_picker()
return
}
let p = match ui.picker {
Some(p) => p
None => return // only Esc (or a click) does anything on the error panel
}
let shift = (self.mods & mod_shift) != 0U
let ctrl = (self.mods & mod_ctrl) != 0U
let key : @emoji.Key? = if code == key_enter || code == key_kp_enter {
Some(Choose)
} else if code == key_backspace {
Some(Backspace)
} else if code == key_tab {
Some(if shift { PrevTab } else { NextTab })
} else if code == key_left {
Some(if ctrl { PrevTone } else { Left })
} else if code == key_right {
Some(if ctrl { NextTone } else { Right })
} else if code == key_up {
Some(Up)
} else if code == key_down {
Some(Down)
} else if code == key_pgup {
Some(PageUp)
} else if code == key_pgdn {
Some(PageDown)
} else if code == key_home {
Some(Home)
} else if code == key_end {
Some(End)
} else if ctrl || (self.held_mods() & @shell.mod_alt) != 0U {
None
} else {
let mods = self.mods | self.mods_latched | self.mods_locked
match self.auth_keymap().char_for(code, mods, self.mods_group) {
Some(c) if c.to_int() >= 32 => Some(Type(c))
_ => None
}
}
match key {
Some(k) =>
match p.key(k) {
Some(text) => ignore(self.emoji_insert(text, shift))
None => ()
}
None => ()
}
}
///| A click on the picker.
fn ChandraCompositor::emoji_click(self : ChandraCompositor, ui : EmojiUi, h : EmojiHit, keep_open : Bool, _time_ms : UInt) -> Unit {
self.damage.mark_full()
match ui.picker {
None => ()
Some(p) =>
match h {
Cell(i) => {
p.select(i)
match p.key(Choose) {
Some(text) => ignore(self.emoji_insert(text, keep_open))
None => ()
}
}
Tab(t) => p.set_tab(t)
Tone(t) => p.set_tone(t)
Panel => ()
}
}
}
///| Mime types the emoji is offered as on the clipboard.
let text_mimes : Array[String] = ["text/plain;charset=utf-8", "text/plain", "UTF8_STRING", "TEXT", "STRING"]
///| How an emoji reached the app, for tests and the log.
pub(all) enum EmojiDelivery {
/// Typed into the focused text field (text-input-v3).
Typed
/// Put on the clipboard: (why it couldn't be typed).
Copied(String)
} derive(Eq, Show, Debug)
///| Put `text` where the user was typing: into the focused window's text field when it has
/// one active, else on the clipboard with a notification saying so. Closes the picker unless
/// `keep_open`. Saves the recents.
pub fn ChandraCompositor::emoji_insert(self : ChandraCompositor, text : String, keep_open : Bool) -> EmojiDelivery {
let target = self.kbd_win
let delivery = match target {
Some(w) if self.server.text_input_commit(w, text) => Typed
Some(w) => {
let app = match self.windows.get(w) {
Some(win) if win.title.length() > 0 => win.title
_ => "The focused window"
}
Copied(app + " has no text field taking typed text (text-input-v3)")
}
None => Copied("No window has the keyboard")
}
match delivery {
Typed => ()
Copied(why) => {
self.server.set_own_selection(text_mimes, @utf8.encode(text))
@sys.log("emoji: copied to the clipboard: " + why)
let img = @raster.emoji_image(text, 48)
self.shot_toast = Some({
title: "Emoji copied: paste it with Ctrl+V",
detail: why,
until: @sys.monotonic_ms() + 6000UL,
thumb: if img != 0U { [@scene.DrawOp::Image(@scene.Rect::new(0, 0, 48, 48), img)] } else { [] },
tw: 48,
th: 48,
})
}
}
match self.emoji_ui {
Some(ui) =>
match ui.picker {
Some(p) => {
p.used(text) // (already done when it came from the picker's keys or a click)
self.emoji_remember(p)
}
None => ()
}
None => ()
}
if !keep_open || delivery != Typed {
self.close_emoji_picker()
}
self.damage.mark_full()
delivery
}
///| An emoji picture at `r` (centred, `px` square), or a box with "?" when the font has none.
fn ChandraCompositor::emoji_pic(self : ChandraCompositor, text : String, r : @scene.Rect, px : Int, ops : Array[@scene.DrawOp]) -> Unit {
let x = r.x + (r.w - px) / 2
let y = r.y + (r.h - px) / 2
let h = @raster.emoji_image(text, px)
if h != 0U {
ops.push(@scene.DrawOp::Image(@scene.Rect::new(x, y, px, px), h))
} else {
let p = self.theme.palette
ops.push(@scene.DrawOp::StrokeRect(@scene.Rect::new(x + 2, y + 2, px - 4, px - 4), p.foreground_muted, 1, 3))
ops.push(@scene.DrawOp::Text(x + px / 2, y + px / 2, "?", self.theme.font_size, p.foreground_muted, @scene.TextAlign::Center, false))
}
}
///| The picker's draw ops.
fn ChandraCompositor::emoji_ops(self : ChandraCompositor) -> Array[@scene.DrawOp] {
let ops : Array[@scene.DrawOp] = []
let ui = match self.emoji_ui {
Some(ui) => ui
None => return ops
}
let (w, h) = self.emoji_size(ui)
if ui.rect.w != w || ui.rect.h != h || ui.rect.x + w > self.output_w || ui.rect.y + h > self.output_h {
self.emoji_place(ui)
}
let r = ui.rect
let pal = self.theme.palette
let fs = self.theme.font_size
let rad = self.theme.radius + 4
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(r.x + 2, r.y + 4, r.w, r.h), @scene.Rgba::new(0, 0, 0, 70), rad))
// Opaque: whatever is behind would show through the grid.
let bg = pal.surface_popup
ops.push(@scene.DrawOp::FillRect(r, { r: bg.r, g: bg.g, b: bg.b, a: b'\xff' }, rad))
ops.push(@scene.DrawOp::StrokeRect(r, pal.border, 1, rad))
let p = match ui.picker {
Some(p) => p
None => {
// No colour emoji font: say so, and what to install.
ops.push(@scene.DrawOp::Text(r.x + ep_pad, r.y + ep_pad + fs, "Emoji need a colour emoji font", fs + 1, @scene.Rgba::from_hex("#f87171"), @scene.TextAlign::Left, true))
let mut y = r.y + ep_pad + fs * 3
for line in wrap_text(ui.error, fs - 1, false, false, r.w - ep_pad * 2) {
ops.push(@scene.DrawOp::Text(r.x + ep_pad, y, line, fs - 1, pal.foreground, @scene.TextAlign::Left, false))
y = y + fs + 5
}
ops.push(@scene.DrawOp::Text(r.x + r.w - ep_pad, r.y + r.h - ep_pad - fs / 2, "Esc: close", fs - 2, pal.foreground_muted, @scene.TextAlign::Right, false))
return ops
}
}
// The search field.
let s = emoji_search_rect(r)
ops.push(@scene.DrawOp::FillRect(s, pal.surface, self.theme.radius))
ops.push(@scene.DrawOp::StrokeRect(s, pal.accent, 1, self.theme.radius))
let tx = s.x + 10
if p.query.length() == 0 {
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(tx, s.y + 8, 1, s.h - 16), pal.foreground, 0))
ops.push(@scene.DrawOp::Text(tx + 4, s.y + s.h / 2, "Type to search emoji", fs, pal.foreground_muted, @scene.TextAlign::Left, false))
} else {
ops.push(@scene.DrawOp::Text(tx, s.y + s.h / 2, p.query, fs, pal.foreground, @scene.TextAlign::Left, false))
let cw = @renderer.text_width(p.query, fs, false, false)
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(tx + cw + 1, s.y + 8, 1, s.h - 16), pal.foreground, 0))
}
// The tabs (dimmed while searching: results come from every tab).
for t = 0; t < 10; t = t + 1 {
let tr = emoji_tab_rect(r, t)
let on = t == p.tab && p.query.length() == 0
if on {
ops.push(@scene.DrawOp::FillRect(tr, pal.surface_hover, self.theme.radius))
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(tr.x + 8, tr.y + tr.h - 3, tr.w - 16, 3), pal.accent, 1))
} else if tr.contains(self.ptr_x.to_int(), self.ptr_y.to_int()) {
ops.push(@scene.DrawOp::FillRect(tr, pal.surface_hover, self.theme.radius))
}
self.emoji_pic(@emoji.tab_icons[t], tr, 20, ops)
}
// The grid.
let g = emoji_grid_rect(r)
let items = p.items()
if items.length() == 0 {
let msg = if p.query.length() > 0 {
"No emoji match “" + p.query + "”"
} else if p.tab == 0 {
"Emoji you use show up here"
} else {
"The emoji font has none of these"
}
ops.push(@scene.DrawOp::Text(g.x + g.w / 2, g.y + g.h / 2, msg, fs, pal.foreground_muted, @scene.TextAlign::Center, false))
}
ops.push(@scene.DrawOp::PushClip(g))
let first = p.scroll * ep_cols
let last = (first + ep_cols * ep_rows).min(items.length())
for i = first; i < last; i = i + 1 {
let k = i - first
let cell = @scene.Rect::new(g.x + k % ep_cols * ep_cell, g.y + k / ep_cols * ep_cell, ep_cell, ep_cell)
if i == p.sel {
ops.push(@scene.DrawOp::FillRect(cell, pal.accent, self.theme.radius))
} else if ui.hover == Some(i) {
ops.push(@scene.DrawOp::FillRect(cell, pal.surface_hover, self.theme.radius))
}
self.emoji_pic(items[i].text, cell, ep_emoji, ops)
}
ops.push(@scene.DrawOp::PopClip)
// A scroll bar when there is more than one screenful.
let total_rows = (items.length() + ep_cols - 1) / ep_cols
if total_rows > ep_rows {
let bar_h = (g.h * ep_rows / total_rows).max(16)
let bar_y = g.y + (g.h - bar_h) * p.scroll / (total_rows - ep_rows)
ops.push(@scene.DrawOp::FillRect(@scene.Rect::new(r.x + r.w - 7, bar_y, 3, bar_h), pal.foreground_muted, 1))
}
// The selected emoji's name, and the skin tones.
// (The skin tone shows on the swatches beside it.)
let name = match p.selected() {
Some(it) => @emoji.all()[it.index].name
None => ""
}
let ty = g.y + g.h + 24
let tone0 = emoji_tone_rect(r, 0)
ops.push(@scene.DrawOp::PushClip(@scene.Rect::new(r.x + ep_pad, ty - fs, tone0.x - r.x - ep_pad - 6, fs * 2)))
ops.push(@scene.DrawOp::Text(r.x + ep_pad, ty, name, fs, pal.foreground, @scene.TextAlign::Left, false))
ops.push(@scene.DrawOp::PopClip)
for t = 0; t < 6; t = t + 1 {
let tr = emoji_tone_rect(r, t)
if t == p.tone {
ops.push(@scene.DrawOp::StrokeRect(tr, pal.accent, 2, self.theme.radius))
}
self.emoji_pic(@emoji.tone_swatches[t], tr, 20, ops)
}
ops
}