ui_app

source of pkg::alloy::ui::languages

1// Languages and directions (decisions/F14.md, F14a): the app's2// language chosen from the system's preferred ones as iOS chooses, the3// direction it is written in, and each element's direction, its own4// (`directed`), else its parent's, else the Ui's, resolved as layout goes5// down.67import std::string;8import std::vector;9import std::option;10import std::languages;11import pkg::alloy::window;1213import ui;14import store;15import tree;1617// The direction the language is written in.18pub fn direction_of_language(tag: &[u8]) -> LayoutDirection {19    return if (20        writes_right_to_left(&tag)21    ) LayoutDirection::RightToLeft else LayoutDirection::LeftToRight;22}2324// Ways a language the system wants matches one the app has, tried in25// turn: the same tag, the app's cut from it (`en` for `en-GB`), the same26// language (`en-GB` for `en-US`).27type Match = enum {28    Same,29    Shorter,30    Language,31};3233const MATCHES: [Match : 3] = [Match::Same, Match::Shorter, Match::Language];3435// The app's language for the system's preferred ones: the first the app36// has, by the nearest match, else the app's first.37pub fn chosen_language(app: &[&[u8]], system: &[String]) -> String {38    for (system) |&wanted| {39        for (MATCHES) |how| {40            for (app) |&have| {41                if (matches(&have, &wanted.view(), how)) {42                    return String::from(&have);43                }44            }45        }46    }47    if (app.length() > 0) {48        return String::from(&app[0]);49    }50    return String::from(&"en");51}5253fn matches(have: &[u8], wanted: &[u8], how: Match) -> bool => match (how) {54    ::Same same_tag(&have, &wanted);55    ::Shorter have.length() < wanted.length()56        && is_dash(wanted[have.length()])57        && same_tag(&have, &wanted[..have.length()]);58    ::Language same_tag(&primary(&have), &primary(&wanted));59};6061// Whether the tags are the same, whatever their letters' case and `_` or62// `-`.63fn same_tag(a: &[u8], b: &[u8]) -> bool {64    if (a.length() != b.length()) {65        return false;66    }67    for (a, b) |x, y| {68        if (lower(x) != lower(y) && !(is_dash(x) && is_dash(y))) {69            return false;70        }71    }72    return true;73}7475fn is_dash(c: u8) -> bool => c == '-' || c == '_';7677fn lower(c: u8) -> u8 => if (c >= 'A' && c <= 'Z') c + 32 else c;7879// The tag's language, before its first subtag.80fn primary(tag: &[u8]) -> &[u8] {81    for (tag, [..tag.length()]) |c, k| {82        if (is_dash(c)) {83            return &tag[..k];84        }85    }86    return &tag;87}8889// Chooses the app's language from the system's preferred ones.90pub fn choose_language(self ui: &var Ui, app: &[&[u8]], system: &[String]) {91    ui.chosen = chosen_language(&app, &system);92    ui.redirect();93}9495// The language and the direction again from what the app set and what96// was chosen; everything laid out again when either changed.97pub fn redirect(self ui: &var Ui) {98    var spoken = copy ui.chosen;99    if (ui.language_set.peek(&ui) is ::Some |&tag|) {100        spoken = copy tag;101    }102    var flow = direction_of_language(&spoken.view());103    if (ui.direction_set.peek(&ui) is ::Some |d|) {104        flow = d;105    }106    if (spoken != ui.spoken || flow != ui.flow) {107        ui.spoken = move spoken;108        ui.flow = flow;109        ui.invalidate_all();110        ui.access_stale = true;111    }112}113114// The language the Ui's content is in and the way it reads, as `redirect`115// resolved them.116exp fn spoken_language(self ui: &Ui) -> &String => &ui.spoken;117118exp fn reading_flow(self ui: &Ui) -> LayoutDirection => ui.flow;119120// The direction the node reads in: its own, else its parent's, else the121// Ui's. A node turning the other way has all under it laid out and122// painted again.123pub fn resolve_direction(self ui: &var Ui, index: u32) -> LayoutDirection {124    var direction = ui.flow;125    if (ui.node(index).directed is ::Some |d|) {126        direction = d;127    } else if (ui.node(index).parent is ::Some |p|) {128        direction = ui.node(p).direction;129    }130    if (ui.node(index).direction != direction) {131        const node = &var ui.node_var(index);132        node.direction = direction;133        node.needs_layout = true;134        node.needs_paint = true;135        ui.mark_below(index);136    }137    return direction;138}