///| Dock-style quick launcher: a row of app tiles that grow under the pointer, /// click to start. Pure: the component stub passes in the app list and tokens. ///| Most tiles shown; the catalog is ordered defaults, recents, then the rest. pub let max_tiles : Int = 8 ///| Space between tile slots. let gap : Int = 6 ///| Side padding of the whole dock. let pad : Int = 6 ///| Tile edge when not hovered, for a widget `h` pixels tall. fn base_size(h : Int) -> Int { let s = h - 14 if s < 16 { 16 } else if s > 32 { 32 } else { s } } ///| Distance between tile centres. fn pitch(h : Int) -> Int { base_size(h) + gap } ///| Preferred widget width for `n` tiles. pub fn preferred_width(n : Int) -> Int { let n = if n > max_tiles { max_tiles } else { n } pad * 2 + n * pitch(34) - gap } ///| Index of the tile slot under widget-local `x`, or -1 (outside the tile row / padding / gap). pub fn tile_at(n : Int, h : Int, x : Int, y : Int) -> Int { let n = if n > max_tiles { max_tiles } else { n } if y < 0 || y >= h || x < pad { return -1 } let rel = x - pad let i = rel / pitch(h) if i >= n { return -1 } // The gap between slots belongs to the nearer tile so the cursor never flickers off it. i } ///| Edge length of tile `i` given the hovered tile (-1 for none): the hovered tile /// grows most, its neighbours half as much. pub fn tile_size(i : Int, hover : Int, h : Int) -> Int { let base = base_size(h) let room = h - 4 - base let extra = if room > 8 { 8 } else if room < 0 { 0 } else { room } if hover < 0 { return base } let d = if i > hover { i - hover } else { hover - i } if d == 0 { base + extra } else if d == 1 { base + extra / 2 } else { base } } ///| Tile colours: fixed saturated hues picked from the app id so an app keeps its colour. let hues : Array[(Int, Int, Int)] = [ (0xE0, 0x5A, 0x4F), (0xE8, 0x92, 0x3A), (0xD9, 0xB5, 0x2E), (0x4F, 0xAE, 0x6B), (0x2F, 0xA8, 0xA0), (0x3D, 0x8B, 0xE0), (0x7C, 0x5C, 0xD6), (0xC4, 0x55, 0xA8), ] ///| fn hash(s : String) -> Int { let mut h = 5381 for c in s { h = (h * 33 + c.to_int()) & 0x7FFFFFF } h } ///| Colour of an app's tile. pub fn tile_color(id : String) -> @types.Color { let (r, g, b) = hues[hash(id) % hues.length()] @ui.color(r, g, b, 255) } ///| Draw the dock into `size`. `hover` is the hovered tile index or -1. pub fn render( size : @types.Size, t : @theme.Tokens, apps : Array[@types.AppEntry], hover : Int, ) -> Array[@types.DrawOp] { let w = size.w.reinterpret_as_int() let h = size.h.reinterpret_as_int() let r = t.radius.reinterpret_as_int() let n = if apps.length() > max_tiles { max_tiles } else { apps.length() } let ops : Array[@types.DrawOp] = [ @ui.fill_rect(@ui.rect(0, 0, w, h), t.palette.surface, radius=r + 4), ] if n == 0 { ops.push( @ui.text(w / 2, h / 2, "No apps", t.font_size.reinterpret_as_int() - 2, t.palette.foreground_muted, align=CENTER), ) return ops } let white = @ui.color(255, 255, 255, 255) for i = 0; i < n; i = i + 1 { let s = tile_size(i, hover, h) let cx = pad + i * pitch(h) + base_size(h) / 2 let x = cx - s / 2 let y = (h - s) / 2 let c = tile_color(apps[i].id) let fill = if i == hover { @ui.lerp_color(c, white, 1, 5) } else { c } ops.push(@ui.fill_rect(@ui.rect(x, y, s, s), fill, radius=s / 4)) ops.push( @ui.text(cx, h / 2, apps[i].glyph, s * 11 / 20, white, align=CENTER, bold=true), ) } ops } ///| Name shown for tile `i` (used for the hover tooltip popup). pub fn tile_name(apps : Array[@types.AppEntry], i : Int) -> String { if i >= 0 && i < apps.length() && i < max_tiles { apps[i].name } else { "" } } ///| Pointer events -> (new hover, redraw?, app id to launch). Pure so it can be tested. pub fn handle( apps : Array[@types.AppEntry], hover : Int, h : Int, event : @types.InputEvent, ) -> (Int, Bool, String?) { match event { PointerMove(p) => { let i = tile_at(apps.length(), h, p.x, p.y) (i, i != hover, None) } PointerLeave => (-1, hover != -1, None) PointerDown(p) => { let i = tile_at(apps.length(), h, p.x, p.y) if i >= 0 { (i, true, Some(apps[i].id)) } else { (hover, false, None) } } _ => (hover, false, None) } }