Files

773 lines
30 KiB
Ziran

#import "apps_ui"
#import "std/text_tools"
// Shared cards and native widget menus; account, installation and release
// verification remain with the trusted Apps service provider.
#import "std/json_scan"
#import "std/c_string"
#import "std/vec"
#import "std/option"
#import "std/text_buffer"
#import "kryon/app_context"
#import "kryon/app_protocol"
#import "kryon/text_align"
#import "kryon/control_props"
#import "kryon/drawing_props"
#import "kryon/session"
#import "kryon/geometry"
#import "kryon/tree"
#import "kryon/input_protocol"
#import "kryon/widget_palette"
Box :: #import "kryon/box";
Button :: #import "kryon/button";
Carousel :: #import "kryon/carousel";
ContextMenu :: #import "kryon/context_menu";
Image :: #import "kryon/image";
Joystick :: #import "kryon/joystick";
LoadingIndicator :: #import "kryon/loading_indicator";
Modal :: #import "kryon/modal";
Scroll :: #import "kryon/scroll";
Text :: #import "kryon/text";
TextField :: #import "kryon/text_field";
#import "appearance"
Rules :: #import "protocol";
#import, file "words.zi";
using Image.ImageFit;
using Text.Wrap;
using TextAlign;
using ButtonTone;
using ButtonEmphasis;
using IconPlacement;
using ContentAlignment;
APP_ELLIPSIS :: "S3rr:M12 5H12.01M12 12H12.01M12 19H12.01"
APP_REORDER :: "S2rr:M8 5H8.01M16 5H16.01M8 12H8.01M16 12H16.01M8 19H8.01M16 19H16.01"
APP_DOWNLOAD :: "S1.6rr:M12 3V15M7 10L12 15L17 10M5 20H19"
// What a host lets an app reach, named by its catalog's "permissions".
APP_PERMISSION_ACCOUNT :: 1
APP_PERMISSION_FRIENDS :: 2
APP_PERMISSION_AUDIO :: 4
APP_PERMISSION_MEDIA :: 8
AppDownloadItem :: struct {
title: [256]u8
bytes: s64
dependency: bool
next: *AppDownloadItem
}
AppCard :: struct {
id: [128]u8
title: [256]u8
cover: Rules.AppCover
// Present while the person's own title replaces the app's title.
original_title: [256]u8
// The offered icon the person chose; empty shows the app's own icon.
icon_choice: [64]u8
preview: [512]u8
icon: [512]u8
icon_shape: [16384]u8
installed: bool
// Whether the app's package is on this device; one that is not shows
// the download symbol instead of its options, and says so while it
// downloads.
on_device: bool
downloading: bool
// How much of the download has arrived in percent, or -1 while unknown.
progress: s32
download_failed: bool
// APP_PERMISSION_* bits for what the app may reach once it is here.
permissions: u32
favorite: bool
reorderable: bool
launcher: s32
selected: bool
disabled: bool
bytes: s64
downloads: *AppDownloadItem
download_count: s32
actions: u64
current_version: [64]u8
available: Rules.AppVersion
update_available: bool
update_seen: bool
history: [3]Rules.AppVersion
history_count: s32
}
cards: Vec(AppCard);
empty_target: [128]u8;
title: [256]u8;
locale: [16]u8;
can_back: bool;
icon_mode: bool;
cover_mode: bool;
cover_grid: bool;
cover_state: Carousel.CarouselState;
// The launcher transition advances with rendered frames; no game clock or
// optional host time capability is required by icon/list-only controllers.
cover_time: float64;
list_mode: bool;
hide_sizes: bool;
has_reorder_handles: bool;
section_break: s32;
has_section_break: bool;
presentation_columns: s32;
tile_class: s32;
action_class: s32;
muted_class: s32;
image_class: s32;
cover_logo_width: float32;
cover_logo_top: float32;
// The host's revision of the catalog parsed into these cards, or 0. Parsing
// the whole catalog every frame cost a phone most of each frame.
catalog_revision: s64;
scroll_offset: s32;
request: s64;
status: [32]u8;
action_taken: bool;
menu_id: [128]u8;
menu_x: float32;
menu_y: float32;
dialog_id: [128]u8;
// 1 update confirmation, 2 retained version chooser, 3 rollback confirmation,
// 4 the app's properties, 5 downloading an app not on this device.
dialog: s32;
// Whether Download was pressed in the open download dialog; frames drawn
// since, for the bar of a download whose size is not known.
download_started: bool;
download_installing: bool;
download_frames: s32;
download_fresh: bool;
last_download_request: s64;
// Console games use the same artwork title as standalone cover collections.
title_id: [128]u8;
title_state: Carousel.CarouselState;
title_fresh: bool;
ConsoleDownload :: struct {
id: [128]u8
installing: bool
}
console_downloads: Vec(ConsoleDownload);
dialog_target: Rules.AppVersion;
dialog_message: [2048]u8;
AppIconChoice :: struct {
id: [64]u8
shape: [16384]u8
image: [512]u8
}
// The properties being edited, kept until Save or Cancel.
properties_title: [256]u8;
properties_icon: [64]u8;
properties_cursor: s32;
properties_anchor: s32;
properties_scroll_x: s32;
properties_focused: bool;
icon_choices: Vec(AppIconChoice);
icon_scroll: s32;
AppsBool :: (text: string, at: s32) -> bool {
return at >= 0 && MatchLiteral(text, Space(text, at), "true")
}
AppsNumber :: (text: string, at: s32) -> float64 {
parsed := ReadNumber(text, at)
return ifx parsed.valid then parsed.value else 0.0
}
AppsArrayAt :: (text: string, array: s32, index: s32) -> s32 {
at := Space(text, array + 1)
for previous: cast(s32)0..index - 1 { at = Space(text, ValueEnd(text, at, 0) + 1) }
return at
}
AppsOptionalText :: (text: string, at: s32, key: string, output: []u8) -> bool {
return FieldAt(text, at, key) < 0 || Rules.AppsReadText(text, at, key, output)
}
AppsVersionRead :: (text: string, at: s32, version: *Rules.AppVersion) -> bool {
return Rules.AppsReadText(text, at, "version", version.version[:]) &&
Rules.AppVersionValid(TextUntilNul(version.version[:])) &&
Rules.AppsReadText(text, at, "sha256", version.sha256[:]) &&
Rules.AppDigestValid(TextUntilNul(version.sha256[:]))
}
AppsVersionEqual :: (left: *Rules.AppVersion, right: *Rules.AppVersion) -> bool {
return TextUntilNul(left.version[:]) == TextUntilNul(right.version[:]) &&
TextUntilNul(left.sha256[:]) == TextUntilNul(right.sha256[:])
}
AppsSupported :: (card: *AppCard, operation: s32) -> bool {
return (card.actions & (cast(u64)1 << cast(u64)operation)) != cast(u64)0
}
AppsReadCatalog :: () -> bool {
text: string
if catalog_revision > 0 {
bytes: Vec(u8)
BuilderPrint(*bytes, "{\"v\":1,\"op\":\"catalog\",\"revision\":%}", catalog_revision)
text = AppService("apps", TextView(VecSlice(bytes, 0, bytes.count)))
VecFree(bytes)
// The cards already hold this revision.
if AppsBool(text, FieldAt(text, 0, "unchanged")) { return true }
} else {
text = AppService("apps", "{\"v\":1,\"op\":\"catalog\"}")
}
catalog_revision = 0
version := ReadNumber(text, FieldAt(text, 0, "v"))
if !version.valid || version.value != 1.0 || !Rules.AppsReadText(text, 0, "title", title[:]) ||
!Rules.AppsReadText(text, 0, "locale", locale[:]) { return false }
can_back = AppsBool(text, FieldAt(text, 0, "can_back"))
presentation := FieldAt(text, 0, "presentation")
mode: [16]u8
if !AppsOptionalText(text, presentation, "mode", mode[:]) { return false }
list_mode = TextUntilNul(mode[:]) == "list"
icon_mode = list_mode || TextUntilNul(mode[:]) == "icon"
cover_grid = TextUntilNul(mode[:]) == "covers"
hide_sizes = AppsBool(text, FieldAt(text, presentation, "hide_sizes"))
requested_download: [128]u8
if !AppsOptionalText(text, presentation, "download_id", requested_download[:]) { return false }
download_request := cast(s64)AppsNumber(text, FieldAt(text, presentation, "download_request"))
has_section_break = FieldAt(text, presentation, "section_break") >= 0
section_break = cast(s32)AppsNumber(text, FieldAt(text, presentation, "section_break"))
presentation_columns = cast(s32)AppsNumber(text, FieldAt(text, presentation, "columns"))
tile_class = cast(s32)AppsNumber(text, FieldAt(text, presentation, "tile_class"))
action_class = cast(s32)AppsNumber(text, FieldAt(text, presentation, "action_class"))
muted_class = cast(s32)AppsNumber(text, FieldAt(text, presentation, "muted_class"))
image_class = cast(s32)AppsNumber(text, FieldAt(text, presentation, "image_class"))
cover_logo_width = cast(float32)AppsNumber(text, FieldAt(text, presentation, "cover_logo_width"))
cover_logo_top = cast(float32)AppsNumber(text, FieldAt(text, presentation, "cover_logo_top"))
children := FieldAt(text, 0, "children")
count := ArrayCount(text, children)
if count < 0 || count > 4096 { return false }
for index: cast(s64)0..cards.count - 1 { AppsDownloadItemsClear(*cards[index]) }
VecClear(cards)
has_reorder_handles = false
cover_mode = false
at := Space(text, children + 1)
for index: cast(s32)0..count - 1 {
card: AppCard
if !Rules.AppsReadText(text, at, "id", card.id[:]) || !Rules.AppIdValid(TextUntilNul(card.id[:])) ||
!Rules.AppsReadText(text, at, "title", card.title[:]) ||
!AppsOptionalText(text, at, "preview", card.preview[:]) ||
!AppsOptionalText(text, at, "icon", card.icon[:]) ||
!AppsOptionalText(text, at, "icon_shape", card.icon_shape[:]) ||
!AppsOptionalText(text, at, "original_title", card.original_title[:]) ||
!AppsOptionalText(text, at, "icon_choice", card.icon_choice[:]) ||
!AppsOptionalText(text, at, "current_version", card.current_version[:]) { return false }
cover := FieldAt(text, at, "cover")
if cover >= 0 {
if !Rules.AppsReadText(text, cover, "portrait", card.cover.portrait[:]) ||
!AppsOptionalText(text, cover, "landscape", card.cover.landscape[:]) ||
!AppsOptionalText(text, cover, "logo", card.cover.logo[:]) ||
!AppsOptionalText(text, cover, "caption", card.cover.caption[:]) ||
!AppsOptionalText(text, cover, "action", card.cover.action[:]) { return false }
cover_mode = true
}
if TextUntilNul(card.preview[:]) != "" && !Rules.AppPreviewPathValid(TextUntilNul(card.preview[:])) { return false }
if TextUntilNul(card.icon[:]) != "" && !Rules.AppPreviewPathValid(TextUntilNul(card.icon[:])) { return false }
if TextUntilNul(card.current_version[:]) == "" { unused AppsOptionalText(text, at, "version", card.current_version[:]) }
card.installed = AppsBool(text, FieldAt(text, at, "installed"))
// Hosts from before these fields only list apps on the device.
card.on_device = FieldAt(text, at, "on_device") < 0 || AppsBool(text, FieldAt(text, at, "on_device"))
card.downloading = !card.on_device && AppsBool(text, FieldAt(text, at, "downloading"))
card.progress = -1
progress := FieldAt(text, at, "progress")
if card.downloading && progress >= 0 {
value := AppsNumber(text, progress)
if value >= 0.0 && value <= 100.0 { card.progress = cast(s32)value }
}
card.download_failed = !card.on_device && !card.downloading && AppsBool(text, FieldAt(text, at, "download_failed"))
// Names a newer host adds and this screen does not know are left out.
permissions := FieldAt(text, at, "permissions")
permission_count := ifx permissions < 0 then 0 else ArrayCount(text, permissions)
if permission_count < 0 || permission_count > 16 { return false }
for entry: cast(s32)0..permission_count - 1 {
name: [32]u8
if ReadStringText(text, AppsArrayAt(text, permissions, entry), name[:]) < 0 { return false }
card.permissions = card.permissions | AppsPermission(TextUntilNul(name[:]))
}
card.favorite = AppsBool(text, FieldAt(text, at, "favorite"))
card.reorderable = AppsBool(text, FieldAt(text, at, "reorderable"))
if card.reorderable { has_reorder_handles = true }
card.launcher = cast(s32)AppsNumber(text, FieldAt(text, at, "launcher"))
card.selected = AppsBool(text, FieldAt(text, at, "selected"))
card.disabled = AppsBool(text, FieldAt(text, at, "disabled"))
card.bytes = cast(s64)AppsNumber(text, FieldAt(text, at, "bytes"))
actions := FieldAt(text, at, "actions")
if actions < 0 {
for operation: cast(s32)2..8 { card.actions = card.actions | (cast(u64)1 << cast(u64)operation) }
} else {
action_count := ArrayCount(text, actions)
if action_count < 0 || action_count > 14 { return false }
for action: cast(s32)0..action_count - 1 {
name: [32]u8
if ReadStringText(text, AppsArrayAt(text, actions, action), name[:]) < 0 { return false }
operation := Rules.AppsOperation(TextUntilNul(name[:]))
if operation > 0 { card.actions = card.actions | (cast(u64)1 << cast(u64)operation) }
}
}
if AppsBool(text, FieldAt(text, at, "update_available")) &&
AppsOptionalText(text, at, "available_version", card.available.version[:]) &&
AppsOptionalText(text, at, "available_sha256", card.available.sha256[:]) &&
Rules.AppVersionValid(TextUntilNul(card.available.version[:])) && Rules.AppDigestValid(TextUntilNul(card.available.sha256[:])) {
card.update_available = AppsSupported(*card, Rules.APPS_UPDATE)
card.update_seen = AppsBool(text, FieldAt(text, at, "update_seen"))
}
history := FieldAt(text, at, "version_history")
history_count := ifx history < 0 then 0 else ArrayCount(text, history)
if history_count < 0 || history_count > 3 { return false }
for entry: cast(s32)0..history_count - 1 {
retained: Rules.AppVersion
if !AppsVersionRead(text, AppsArrayAt(text, history, entry), *retained) { return false }
duplicate: bool
for previous: cast(s32)0..card.history_count - 1 {
if AppsVersionEqual(*retained, *card.history[previous]) { duplicate = true }
}
if !duplicate { card.history[card.history_count] = retained; card.history_count += 1 }
}
for previous: cast(s64)0..cards.count - 1 {
if TextUntilNul(cards[previous].id[:]) == TextUntilNul(card.id[:]) { return false }
}
downloads := FieldAt(text, at, "downloads")
if downloads >= 0 {
download_count := ArrayCount(text, downloads)
if download_count < 0 || download_count > 32 { return false }
last: *AppDownloadItem
for index: cast(s32)0..download_count - 1 {
row := ElementAt(text, downloads, index)
item := New(AppDownloadItem)
if item == null { AppsDownloadItemsClear(*card); return false }
if last == null { card.downloads = item }
else { last.next = item }
last = item
if !Rules.AppsReadText(text, row, "title", item.title[:]) { AppsDownloadItemsClear(*card); return false }
item.bytes = cast(s64)AppsNumber(text, FieldAt(text, row, "bytes"))
item.dependency = AppsBool(text, FieldAt(text, row, "dependency"))
}
card.download_count = cast(s32)download_count
}
if !VecPush(cards, card) { AppsDownloadItemsClear(*card); return false }
at = Space(text, ValueEnd(text, at, 0))
if index + 1 < count { at = Space(text, at + 1) }
}
// A native navigation shortcut can ask for this same download dialog.
// Consume each request once so Cancel and Hide keep working.
if download_request > last_download_request && requested_download[0] != 0 {
target := AppsFind(requested_download)
if target != null {
// Console shelves keep their own selection; a requested add-on such
// as a game's editor uses the named install page, then opens.
if !cover_grid {
for index: cast(s32)0..cast(s32)cards.count - 1 {
if TextUntilNul(cards[index].id[:]) == TextUntilNul(target.id[:]) { cover_state.selected = index; cover_state.position = cast(float32)index }
}
}
if (!target.on_device || !target.installed) && AppsSupported(target, Rules.APPS_INSTALL) { AppsDownloadOpen(target) }
last_download_request = download_request
}
}
// Hosts without revisions are asked for the whole catalog every frame.
revision := ReadNumber(text, FieldAt(text, 0, "revision"))
if revision.valid && revision.value >= 1.0 && revision.value <= 2147483647.0 &&
cast(float64)cast(s64)revision.value == revision.value {
catalog_revision = cast(s64)revision.value
}
return true
}
AppsResponse :: (text: string) {
unused ReadStringText(text, FieldAt(text, 0, "status"), status[:])
request = cast(s64)AppsNumber(text, FieldAt(text, 0, "request"))
}
AppsAct :: (operation: string, target: [128]u8, version: Rules.AppVersion = Rules.AppVersion.{},
properties: bool = false) {
if action_taken { return }
action_taken = true
bytes: Vec(u8)
BuilderPrint(*bytes, "{\"v\":1,\"op\":")
Rules.AppsWriteQuoted(*bytes, operation)
if TextUntilNul(target[:]) != "" {
BuilderPrint(*bytes, ",\"id\":")
Rules.AppsWriteQuoted(*bytes, TextUntilNul(target[:]))
}
if Rules.AppsVersionOperation(Rules.AppsOperation(operation)) {
BuilderPrint(*bytes, ",\"version\":")
Rules.AppsWriteQuoted(*bytes, TextUntilNul(version.version[:]))
BuilderPrint(*bytes, ",\"sha256\":")
Rules.AppsWriteQuoted(*bytes, TextUntilNul(version.sha256[:]))
}
if properties {
BuilderPrint(*bytes, ",\"title\":")
Rules.AppsWriteQuoted(*bytes, TextUntilNul(properties_title[:]))
BuilderPrint(*bytes, ",\"icon\":")
Rules.AppsWriteQuoted(*bytes, TextUntilNul(properties_icon[:]))
}
BuilderPrint(*bytes, "}")
AppsResponse(AppService("apps", TextView(VecSlice(bytes, 0, bytes.count))))
VecFree(bytes)
}
AppsFind :: (id: [128]u8) -> *AppCard {
for index: cast(s64)0..cards.count - 1 {
if TextUntilNul(cards[index].id[:]) == TextUntilNul(id[:]) { return *cards[index] }
}
return null
}
AppsMenuItem :: (items: []ContextMenu.ContextMenuItem, count: *s32, card: *AppCard, operation: s32,
language: string, label: string, disabled: bool = false) {
if !AppsSupported(card, operation) { return }
items[count.*] = ContextMenu.ContextMenuItem.{.id = operation, .label = AppsLabel(language, label), .disabled = disabled}
count.* += 1
}
AppsMenu :: (session: Session, scale: float32, language: string) {
AppsMenuView(session, scale, language)
}
AppsActionProps :: (key: u64, card: *AppCard, action_class: s32, bounds: Rectangle,
language: string, cover_grid: bool) -> Button.Props {
props := Button.Props.{.key = key, .label = AppsLabel(language, "options"),
.icon_shape = APP_ELLIPSIS, .icon_only = true, .class_name = action_class,
.emphasis = cast(ButtonEmphasis)ButtonEmphasisGhost, .bounds = bounds,
.disabled = card.disabled, .fit_content = true}
if cover_grid {
props.emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft
props.circle = true
}
if card.selected { props.tone = cast(ButtonTone)ButtonToneAccent }
return props
}
// The icons the host offers for this app: its own first, then the rest.
AppsIconsRead :: (id: [128]u8) {
VecClear(icon_choices)
bytes: Vec(u8)
BuilderPrint(*bytes, "{\"v\":1,\"op\":\"icons\",\"id\":")
Rules.AppsWriteQuoted(*bytes, TextUntilNul(id[:]))
BuilderPrint(*bytes, "}")
text := AppService("apps", TextView(VecSlice(bytes, 0, bytes.count)))
VecFree(bytes)
icons := FieldAt(text, 0, "icons")
count := ArrayCount(text, icons)
if count <= 0 || count > 64 { return }
for index: cast(s32)0..count - 1 {
at := AppsArrayAt(text, icons, index)
choice: AppIconChoice
if !Rules.AppsReadText(text, at, "id", choice.id[:]) || !Rules.AppIconChoiceValid(TextUntilNul(choice.id[:])) ||
!AppsOptionalText(text, at, "shape", choice.shape[:]) || !AppsOptionalText(text, at, "image", choice.image[:]) ||
(TextUntilNul(choice.image[:]) != "" && !Rules.AppPreviewPathValid(TextUntilNul(choice.image[:]))) ||
(TextUntilNul(choice.shape[:]) == "" && TextUntilNul(choice.image[:]) == "") || !VecPush(icon_choices, choice) {
VecClear(icon_choices)
return
}
}
}
AppsPropertiesOpen :: (card: *AppCard) {
AppsIconsRead(card.id)
properties_title = card.title
properties_icon = card.icon_choice
properties_cursor = cast(s32)TextUntilNul(properties_title[:]).count
properties_anchor = properties_cursor
properties_scroll_x = 0
properties_focused = false
icon_scroll = 0
dialog_id = card.id
dialog = 4
}
AppsPropertiesClose :: () {
dialog = 0
download_fresh = false
properties_focused = false
VecClear(icon_choices)
}
// Replace bytes start..end of a zero-terminated buffer, as a text field edits.
AppsReplaceText :: (buffer: []u8, start: s32, end: s32, replacement: string) {
length := cast(s32)TextUntilNul(buffer).count
new_length := length - (end - start) + cast(s32)replacement.count
if start < 0 || end < start || end > length || new_length >= cast(s32)buffer.count { return }
tail := length - end
if new_length > length {
for offset: cast(s32)0..tail { buffer[new_length - offset] = buffer[length - offset] }
} else {
for offset: cast(s32)0..tail { buffer[start + cast(s32)replacement.count + offset] = buffer[end + offset] }
}
for offset: cast(s64)0..replacement.count - 1 { buffer[cast(s64)start + offset] = replacement[offset] }
buffer[new_length] = cast(u8)0
}
// The title without surrounding spaces; the app's own title saves as empty.
AppsPropertiesSavedTitle :: (card: *AppCard) -> [256]u8 {
title := TextUntilNul(properties_title[:])
first: s64 = 0
last := title.count
while first < last && title[first] == #char " " { first += 1 }
while last > first && title[last - 1] == #char " " { last -= 1 }
original := TextUntilNul(card.original_title[:])
if original == "" { original = TextUntilNul(card.title[:]) }
saved: [256]u8
if title[first:last] != original { unused CopyCString(title[first:last], saved[:]) }
return saved
}
AppsPropertiesSave :: (card: *AppCard) {
properties_title = AppsPropertiesSavedTitle(card)
AppsAct("properties", card.id, Rules.AppVersion.{}, true)
}
AppsProperties :: (session: Session, card: *AppCard, scale: float32, language: string, font: s32) {
AppsPropertiesView(session, card, scale, language, font)
}
AppsPermission :: (name: string) -> u32 {
if name == "account" { return cast(u32)APP_PERMISSION_ACCOUNT }
if name == "friends" { return cast(u32)APP_PERMISSION_FRIENDS }
if name == "audio" { return cast(u32)APP_PERMISSION_AUDIO }
if name == "media" { return cast(u32)APP_PERMISSION_MEDIA }
return cast(u32)0
}
AppsPermissionLabel :: (bit: u32) -> string {
if bit == cast(u32)APP_PERMISSION_ACCOUNT { return "permission_account" }
if bit == cast(u32)APP_PERMISSION_FRIENDS { return "permission_friends" }
if bit == cast(u32)APP_PERMISSION_AUDIO { return "permission_audio" }
return "permission_media"
}
AppsDownloadOpen :: (card: *AppCard) {
dialog_id = card.id
dialog = 5
download_started = card.downloading
download_installing = false
download_frames = 0
download_fresh = true
}
// Console launches keep the game artwork and input in the normal page.
// Download is explicit consent; the verified game then opens its own title.
// Other collection presentations retain their existing install dialog.
AppsDownload :: (session: Session, card: *AppCard, scale: float32, language: string, font: s32) {
AppsDownloadView(session, card, scale, language, font)
}
AppsDialog :: (session: Session, scale: float32, language: string, font: s32) {
AppsDialogView(session, scale, language, font)
}
AppsSize :: (bytes: s64, output: []u8) -> bool {
if bytes <= 0 || bytes > 2147483647 { return false }
value: Vec(u8)
if bytes < 1000 { BuilderPrint(*value, "% B", bytes) }
else {
unit: s64 = 1000
suffix := " kB"
if bytes >= 999950 { unit = 1000000; suffix = " MB" }
tenths := (bytes * 10 + unit / 2) / unit
BuilderPrint(*value, "%.%", tenths / 10, tenths % 10)
BuilderPrint(*value, "%", suffix)
}
result := CopyCString(TextView(VecSlice(value, 0, value.count)), output)
VecFree(value)
return result
}
// Reuse the collection's artwork, logo, caption and Play control unchanged.
AppsTitleOpen :: (card: *AppCard) {
title_id = card.id
title_state = Carousel.CarouselState.{}
title_fresh = true
}
AppsConsolePending :: (id: [128]u8) -> s64 {
for index: cast(s64)0..console_downloads.count - 1 {
if TextUntilNul(console_downloads[index].id[:]) == TextUntilNul(id[:]) { return index }
}
return -1
}
AppsConsoleDownload :: (card: *AppCard) {
if AppsConsolePending(card.id) >= 0 || action_taken { return }
AppsAct("install", card.id)
if TextUntilNul(status[:]) != "rejected" {
unused VecPush(console_downloads, ConsoleDownload.{.id = card.id, .installing = card.on_device})
}
}
// Complete verified installation in the background. Opening always needs a
// fresh activation so finishing a transfer never interrupts browsing.
AppsConsoleDownloads :: () {
index: s64
while index < console_downloads.count {
pending := *console_downloads[index]
card := AppsFind(pending.id)
if card == null || card.download_failed || (card.on_device && card.installed) {
console_downloads[index] = console_downloads[console_downloads.count - 1]
unused VecPop(console_downloads)
continue
}
if card.on_device && !card.installed && !pending.installing && !action_taken {
AppsAct("install", card.id)
pending.installing = true
}
index += 1
}
}
AppsConsoleOpen :: (card: *AppCard) {
if (!card.on_device || !card.installed) && AppsSupported(card, Rules.APPS_INSTALL) { AppsConsoleDownload(card) }
else if AppsSupported(card, Rules.APPS_OPEN) {
if TextUntilNul(card.cover.action[:]) == "title" { AppsTitleOpen(card) }
else { AppsAct("open", card.id) }
}
}
AppsTitle :: (session: Session, bounds: Rectangle, scale: float32, language: string) {
AppsTitleView(session, bounds, scale, language)
}
AppsSceneryCircle :: (session: Session, key: u64, x: float32, y: float32, radius: float32, fill: Color) {
AppsSceneryCircleView(session, key, x, y, radius, fill)
}
// The first tile offset at or left of the edge for a pattern drifted by shift.
AppsSceneryStart :: (shift: float32, tile: float32) -> float32 {
start := shift - cast(float32)cast(s64)(shift / tile) * tile
if start > 0.0 { start -= tile }
return start
}
// A quiet dusk landscape pattern behind the console covers in either
// orientation: sky, sun glow, clouds and rolling hills with round trees. It
// repeats across any width, drifts gently with the selection and never paints a
// game's own artwork behind its cover.
AppsScenery :: (session: Session, bounds: Rectangle, scale: float32, position: float32) {
AppsSceneryView(session, bounds, scale, position)
}
// Console home: one cover carousel on a scenic pattern in both orientations.
// Landscape screens use landscape covers; portrait screens use portrait covers.
AppsConsole :: (session: Session, bounds: Rectangle, scale: float32, language: string, font: s32) {
AppsConsoleView(session, bounds, scale, language, font)
}
AppsFrame :: () -> bool {
return AppsFrameView()
}
#program_export
Main :: () -> s32 {
mode := AppMode()
if mode == APP_IDENTIFY { AppIdentity("apps"); AppDescribe("Apps", ""); return APP_PROTOCOL }
if mode == APP_FRAME { return ifx AppsFrame() then 0 else 1 }
if mode == APP_COMMAND { AppReply(AppService("apps", AppCommand())) }
return 0
}
AppsDownloadItemsClear :: (card: *AppCard) {
item := card.downloads
while item != null { next := item.next; free(item); item = next }
card.downloads = null
card.download_count = 0
}
NewItems :: () -> [7]ContextMenu.ContextMenuItem {
result: [7]ContextMenu.ContextMenuItem
return result
}
NewSnapshot :: () -> [256]u8 {
result: [256]u8
return result
}
NewSizeText :: () -> [32]u8 {
result: [32]u8
return result
}
NewByteBuffer :: () -> Vec(u8) {
result: Vec(u8)
return result
}
NewSize :: () -> [32]u8 {
result: [32]u8
return result
}
NewText :: () -> [512]u8 {
result: [512]u8
return result
}
NewLabel :: () -> [256]u8 {
result: [256]u8
return result
}
NewActions :: () -> [4]Modal.ActionModalAction {
result: [4]Modal.ActionModalAction
return result
}
NewItems1NativeCarouselCarouselItem :: () -> [1]Carousel.CarouselItem {
result: [1]Carousel.CarouselItem
return result
}
NewVisible :: () -> [4096]s32 {
result: [4096]s32
return result
}
NewFrames :: () -> [4096]Rectangle {
result: [4096]Rectangle
return result
}
NewMarkers :: () -> [4096]Rectangle {
result: [4096]Rectangle
return result
}
NewCoverItems :: () -> [4096]Carousel.CarouselItem {
result: [4096]Carousel.CarouselItem
return result
}
NewListSize :: () -> [32]u8 {
result: [32]u8
return result
}
AppsCardPresent :: (card: *AppCard) -> bool {
return card != null
}
// Native permission masks keep their u32 width in typed UI composition.
AppsPermissionFlag :: (index: u32) -> u32 {
return cast(u32)1 << index
}
AppsPermissionGranted :: (permissions: u32, flag: u32) -> bool {
return (permissions & flag) != cast(u32)0
}
// Indexed borrowed model access keeps native Vec element types in their module.
AppsCardAt :: (index: s64) -> *AppCard {
return *cards[index]
}
AppsIconAt :: (index: s64) -> *AppIconChoice {
return *icon_choices[index]
}