garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /programs/src/bin/launcher.rs

raw

//! The home screen: the Store, Settings, the Terminal, Files, Fien, Kees and every installed
//! app (`/apps` on the system disk), as tiles; tapping one starts it. It
//! reads `/apps` again each time it comes to the front, so a newly
//! installed app is there. Built from a view: the tiles are a grid as wide
//! as the window allows, which scrolls when it is taller than the window.

#![no_std]
#![no_main]

extern crate alloc;

use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

use pinnacle_gfx::Rect;
use pinnacle_rt::fs;
use pinnacle_ui::icons::{self, Icons};
use pinnacle_ui::{App, Event, theme};
use pinnacle_view::{IntoView, Screen, Signal, View, css, el, signal};

/// The tiles before the installed apps: the Store, Settings, the Terminal,
/// Files, Fien, Kees.
const SYSTEM: usize = 6;

#[derive(Clone, PartialEq)]
struct Tile {
    name: String,
    title: String,
}

/// The Store, Settings, the Terminal, Files, Fien and Kees, then the installed
/// apps by title.
fn apps() -> Vec<Tile> {
    let mut installed: Vec<Tile> = fs::list("system:/apps")
        .unwrap_or_default()
        .into_iter()
        .filter_map(|entry| {
            let name = String::from(entry.strip_suffix('/')?);
            let manifest = fs::read_string(&format!("system:/apps/{name}/manifest")).unwrap_or_default();
            let title = fs::field(&manifest, "title")
                .map(String::from)
                .unwrap_or_else(|| theme::display_name(&name));
            Some(Tile { name, title })
        })
        .collect();
    installed.sort_by(|a, b| a.title.cmp(&b.title));
    let system = [
        ("store", "Store"),
        ("settings", "Settings"),
        ("terminal", "Terminal"),
        ("filer", "Files"),
        ("fien", "Fien"),
        ("kees", "Kees"),
    ];
    let mut all: Vec<Tile> = system
        .iter()
        .map(|(name, title)| Tile {
            name: (*name).into(),
            title: (*title).into(),
        })
        .collect();
    all.extend(installed);
    all
}

/// A press sinks the icon a little into its place.
fn rules() -> pinnacle_view::Rules {
    css! {
        ".home-tile" {
            "display": "flex";
            "flex-direction": "column";
            "align-items": "center";
            "gap": "6px";
            "padding": "0";
            "background": "none";
            "border": "0";
            "color": "var(--m-text)";
            "min-width": "0";
        }
        ".home-tile__plate" {
            "width": "90px";
            "aspect-ratio": "1";
            "display": "flex";
        }
        ".home-tile__plate > canvas" {
            "flex": "1";
            "display": "block";
            "min-width": "0";
        }
        ".home-tile:active > .home-tile__plate" { "padding": "5px"; }
        ".home-tile__title" {
            "font-size": "12px";
            "white-space": "nowrap";
            "text-align": "center";
        }
    }
}

/// The heading and the count over the tiles.
fn page(tiles: Signal<Vec<Tile>>, tapped: Signal<Option<String>>) -> View {
    let count = {
        let tiles = tiles.clone();
        move || {
            pinnacle_locale::tf(
                "{} apps installed",
                &[&format!("{}", tiles.get().len().saturating_sub(SYSTEM))],
            )
        }
    };
    let grid = move || {
        let mut grid = el("div").attr(
            "style",
            "display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); \
             gap: 28px 12px",
        );
        for t in tiles.get() {
            let tapped = tapped.clone();
            let name = t.name.clone();
            grid = grid.child(
                el("button")
                    .class("home-tile")
                    .attr("type", "button")
                    .attr("data-name", t.title.clone())
                    .on("click", move |_| tapped.set(Some(name.clone())))
                    .child(
                        el("div")
                            .class("home-tile__plate")
                            .child(el("canvas").attr("data-name", icons::canvas_name(&t.name, &t.title))),
                    )
                    .child(
                        el("div")
                            .class("home-tile__title")
                            .child(String::from(pinnacle_locale::tr(&t.title))),
                    ),
            );
        }
        grid.into_view()
    };
    el("main")
        .attr(
            "style",
            "max-width: 960px; margin: 0 auto; padding: 44px 18px 48px; \
             display: flex; flex-direction: column",
        )
        .child(
            el("div")
                .attr(
                    "style",
                    "display: flex; align-items: flex-end; gap: 8px; \
                     font-size: 30px; color: var(--m-text-strong)",
                )
                .child(
                    el("canvas")
                        .attr("data-name", icons::canvas_name(icons::MARK, ""))
                        .attr(
                            "style",
                            "width: 30px; height: 34px; display: block; margin-bottom: 4px",
                        ),
                )
                .child("Pinnacle"),
        )
        .child(
            el("div")
                .attr(
                    "style",
                    "font-size: 12px; color: var(--m-text-dim); margin-bottom: 24px",
                )
                .child(count),
        )
        .child(grid)
        .into_view()
}

/// Draws the screen, each icon into its canvas.
fn draw(screen: &mut Screen, app: &mut App, icons: &mut Icons, all: bool) -> Option<Rect> {
    screen.draw_with(&mut app.canvas(), all, &mut |name, canvas, rect| {
        icons.draw_canvas(canvas, rect, name)
    })
}

#[unsafe(no_mangle)]
fn main(_argument: u64) -> u64 {
    let mut app = App::new("Home");
    let mut screen = Screen::new(app.width, app.height).with_rules(&rules());
    let tiles = signal(apps());
    let tapped: Signal<Option<String>> = signal(None);
    let mut icons = Icons::new();
    screen.mount(page(tiles.clone(), tapped.clone()));
    draw(&mut screen, &mut app, &mut icons, true);
    app.present_all();
    pinnacle_rt::println!("launcher: {} apps", tiles.get_untracked().len() - SYSTEM);
    screen.announce("launcher: tile ");
    loop {
        match app.next_event(None) {
            Event::Input(e) => {
                screen.input(&e);
                if let Some(name) = tapped.get_untracked() {
                    tapped.set(None);
                    app.launch(&name);
                }
                if let Some(area) = draw(&mut screen, &mut app, &mut icons, false) {
                    app.present(area);
                }
            }
            Event::Shown(true) | Event::Resized => {
                let now = apps();
                if now != tiles.get_untracked() {
                    tiles.set(now);
                }
                screen.resize(app.width, app.height);
                draw(&mut screen, &mut app, &mut icons, true);
                app.present_all();
                pinnacle_rt::println!("launcher: {} apps", tiles.get_untracked().len() - SYSTEM);
                screen.announce("launcher: tile ");
            }
            // The language or the text size changed: the tiles again.
            Event::Preferences => {
                screen.restyle();
                screen.mount(page(tiles.clone(), tapped.clone()));
                draw(&mut screen, &mut app, &mut icons, true);
                app.present_all();
                screen.announce("launcher: tile ");
            }
            Event::Closed => return 0,
            _ => {}
        }
    }
}