Files
entity/src/entity.zi
T

1078 lines
54 KiB
Ziran

// One profile UI. Hosts supply entity.json through the ordinary scoped store
// interface; extension replies name only routes supplied by that host.
#import "model"
#import "achievement_view"
#import "management"
#import "dew_model"
#import "dew_view"
#import "std/json_scan"
#import "std/number_text"
#import "node_url"
#import "qrcode/qrcode"
#import "std/c_string"
#import "std/text_buffer"
#import "std/vec"
#import "kryon/app_context"
#import "profile_theme"
#import "kryon/app_protocol"
#import "session"
#import "geometry"
#import "text_props"
#import "text_align"
#import "text_widget"
#import "text_input_props"
#import "text_field_widget"
#import "button_props"
#import "button_widget"
#import "control_props"
#import "scroll_props"
#import "scroll_widget"
#import "tree"
#import "image_props"
#import "image_widget"
#import "drawing_props"
#import "box_props"
#import "box_widget"
#import "toggle_widget"
#import "toggle_props"
#import "kryon/svg_icon_props"
#import "kryon/svg_icon_widget"
Field :: struct { cursor: s32; anchor: s32; scroll_x: s32; focused: bool }
state: EntityState;
loaded: bool;
fields: [6]Field;
node_text: [256]u8;
scroll_offset: s32;
choosing_picture: bool;
notice: string;
session: Session;
scale: float32;
asset_root: [4096]u8;
management: Management;
management_tick: s32;
management_waiting: s32;
management_seen: s32;
management_page: s32;
management_scroll: s32;
account_alias: [40]u8;
account_passphrase: [512]u8;
account_code: [1024]u8;
account_short_id: [16]u8;
qr_code: QrCode;
qr_text: [1024]u8;
qr_valid: bool;
Label :: (key: string, fallback: string) -> string {
value := AppLocale(key)
return ifx value == "" || value == key then fallback else value
}
TextLine :: (key: u64, text: string, x: float32, y: float32, width: float32, font: s32 = 16) {
Text(session, TextProps.{.key = key, .text = text,
.bounds = Rectangle.{x, y, width, (ifx font > 20 then cast(float32)font * 1.4 else 28.0) * scale}, .font = cast(s32)(cast(float32)font * scale),
.wrap = TextWrap.TextWrapEllipsis})
}
Action :: (id: s32, label: string, bounds: Rectangle, selected: bool = false, danger: bool = false) -> bool {
return Button(session, ButtonProps.{.key = cast(u64)id, .id = id,
.bounds = bounds, .label = label, .selected = selected,
.tone = ifx danger then ButtonTone.ButtonToneDanger else ifx selected then ButtonTone.ButtonToneAccent else ButtonTone.ButtonToneNeutral,
.emphasis = ifx selected then ButtonEmphasis.ButtonEmphasisFilled else ButtonEmphasis.ButtonEmphasisSoft,
.fit_content = true}) != 0
}
Save :: () -> bool {
document := EntityEncode(state)
if !WriteStore("entity.json", document) {
notice = Label("entity.save_error", "Could not save changes")
return false
} else {
notice = ""
}
return true
}
EditField :: (index: s32, buffer: []u8, label: string, bounds: Rectangle, maximum: s32, secure: bool = false) -> bool {
field := *fields[index]
snapshot: [1024]u8
for byte: 0..buffer.count - 1 { snapshot[byte] = buffer[byte] }
props := TextFieldProps.{.key = cast(u64)(100 + index), .focus_id = 100 + index,
.bounds = bounds, .label = label, .scale = scale,
.placeholder = ifx index == 0 then Label("entity.name_placeholder", "Your name") else label,
.value = TextUntilNul(snapshot[:]), .cursor = field.cursor,
.anchor = field.anchor, .scroll_x = field.scroll_x, .focused = field.focused,
.max_bytes = cast(s32)buffer.count - 1, .max_codepoints = maximum, .secure = secure}
if secure && snapshot[0] != cast(u8)0 { props.display_value = "••••••••" }
if field.focused { props.input = AppFieldInput() }
result := TextField(session, props)
if result.edit.changed {
edit := result.edit
length: s64 = 0
while length < buffer.count && buffer[length] != cast(u8)0 { length += 1 }
new_length := length - (edit.end - edit.start) + edit.replacement.count
if edit.start >= 0 && edit.end >= edit.start && edit.end <= length && new_length < buffer.count {
tail := length - edit.end
if new_length > length {
for offset: 0..tail { buffer[new_length - offset] = buffer[length - offset] }
} else {
for offset: 0..tail { buffer[edit.start + edit.replacement.count + offset] = buffer[edit.end + offset] }
}
for offset: 0..edit.replacement.count - 1 { buffer[edit.start + offset] = edit.replacement[offset] }
buffer[new_length] = cast(u8)0
}
}
if result.clipboard_write { unused ClipboardCopy(result.clipboard_text, 0) }
finished := result.committed || (field.focused && !result.focused)
field.* = Field.{result.cursor, result.anchor, result.scroll_x, result.focused && !result.escaped}
return finished
}
Paragraph :: (key: u64, text: string, x: float32, y: float32, width: float32, height: float32, font: s32 = 14) {
Text(session, TextProps.{.key = key, .text = text,
.bounds = Rectangle.{x, y, width, height * scale},
.font = cast(s32)(cast(float32)font * scale), .wrap = TextWrap.TextWrapAuto})
}
PollManagement :: () {
management_tick += 1
if management_tick != 1 && management_tick % 15 != 0 { return }
next: Management
if !ManagementDecode(AppService("entity.management", ""), *next) { return }
result_changed := TextUntilNul(next.result[:]) != TextUntilNul(management.result[:])
owner_changed := TextUntilNul(next.public_id[:]) != TextUntilNul(management.public_id[:])
management = next
if owner_changed && loaded { loaded = false; fields = .{} }
if !fields[3].focused { account_alias = management.alias }
if management.completed > management_seen || (result_changed && management.completed > 0) {
management_seen = management.completed
message: Vec(u8)
BuilderPrint(*message, "%", TextUntilNul(management.message[:]))
notice = BuilderFinish(message)
if management_waiting <= management.completed {
management_waiting = 0
if TextUntilNul(management.result[:]) == "switch" { management_page = 6; management_scroll = 0 }
else if TextUntilNul(management.result[:]) == "ok" {
loaded = false
if management_page == 2 || management_page == 3 || management_page == 6 { management_page = 1; management_scroll = 0 }
}
}
}
}
CanManage :: (capability: s32) -> bool {
return management.available && (management.capabilities & capability) != 0
}
Manage :: (operation: string, value: string = "", target: string = "", choice: s32 = 0) {
if management_waiting > management.completed || !CanManage(ManagementOperation(operation)) { return }
if TextUntilNul(state.owner_id[:]) != TextUntilNul(management.public_id[:]) {
loaded = false
notice = Label("entity.account_changed", "The account changed; open its current profile again")
return
}
if !Save() { return }
request := ManagementRequest.{.sequence = management.sequence + 1, .choice = choice}
request.owner_id = management.public_id
if !CopyCString(operation, request.operation[:]) || !CopyCString(value, request.value[:]) || !CopyCString(target, request.target[:]) { return }
reply := AppService("entity.management", ManagementRequestEncode(request))
if reply == "queued" {
management_waiting = request.sequence
management.sequence = request.sequence
management_tick = 0
notice = ""
for index: 0..account_passphrase.count - 1 { account_passphrase[index] = cast(u8)0 }
fields[4] = Field.{}
} else {
notice = Label("entity.action_error", "Could not start this action")
}
}
OpenManagement :: (page: s32) {
if !Save() { return }
management_page = page
management_scroll = 0
notice = ""
fields = .{}
}
AccountSummary :: (x: float32, y: *float32, width: float32) {
if management.account {
title := TextUntilNul(management.alias[:])
if title == "" { title = TextUntilNul(management.public_id[:]) }
if title == "" { title = Label("entity.account", "Account") }
if Action(702, title, Rectangle.{x, y.*, width, 44.0 * scale}) {
OpenManagement(1)
}
y.* += 64.0 * scale
return
}
title := Label("entity.no_account", "No account connected")
TextLine(700, title, x, y.*, width, 20)
y.* += 34.0 * scale
status := TextUntilNul(management.status[:])
if status == "" { status = Label("entity.account_local", "Your data stays on this device until you connect") }
Paragraph(701, status, x, y.*, width, 42.0)
y.* += 46.0 * scale
if Action(702, Label(ifx management.account then "entity.manage_account" else "entity.connect_account",
ifx management.account then "Manage account" else "Connect an account"), Rectangle.{x, y.*, width, 44.0 * scale}) {
OpenManagement(1)
}
y.* += 64.0 * scale
}
ManagementNodeAt :: (address: string) -> s32 {
for index: 0..management.node_count - 1 {
if TextUntilNul(management.nodes[index].address[:]) == address { return cast(s32)index }
}
return -1
}
NodeDetailsHeight :: (address: string) -> float32 {
index := ManagementNodeAt(address)
if index < 0 { return 0.0 }
node := management.nodes[index]
height: float32 = 0.0
if node.active { height += 28.0 }
if node.known || management.account { height += 36.0 }
if node.media_supported { height += 52.0 }
return height * scale
}
ByteLabel :: (bytes: s64) -> string {
out: Vec(u8)
if bytes >= 1073741824 { BuilderPrint(*out, "% GB", bytes / 1073741824) }
else if bytes >= 1048576 { BuilderPrint(*out, "% MB", bytes / 1048576) }
else if bytes >= 1024 { BuilderPrint(*out, "% KB", bytes / 1024) }
else { BuilderPrint(*out, "% B", bytes) }
return BuilderFinish(out)
}
StorageUsageLabel :: (used: s64, limit: s64) -> string {
template := Label("entity.storage_used_of", "%s of %s used")
if limit <= 0 {
template = Label("entity.storage_used_unlimited", "%s used · no limit")
}
values: [2]string = .[ByteLabel(used), ByteLabel(limit)]
out: Vec(u8)
start: s64 = 0
at: s64 = 0
argument: s32 = 0
while at + 1 < template.count && argument < 2 {
if template[at:at + 2] == "%s" {
BuilderPrint(*out, "%", template[start:at])
BuilderPrint(*out, "%", values[argument])
argument += 1
at += 2
start = at
} else {
at += 1
}
}
BuilderPrint(*out, "%", template[start:])
return BuilderFinish(out)
}
NodeDetails :: (address: string, x: float32, y: *float32, width: float32) {
index := ManagementNodeAt(address)
if index < 0 { return }
node := management.nodes[index]
if node.active {
TextLine(cast(u64)(720 + index), Label("entity.active_node", "Active node"), x + 12.0 * scale, y.*, width, 12)
y.* += 28.0 * scale
}
if node.known || management.account {
value := Label("entity.storage_unknown", "Storage usage not reported yet")
if node.known {
out: Vec(u8)
BuilderPrint(*out, "%", StorageUsageLabel(node.used, node.limit))
if node.full { BuilderPrint(*out, " · %", Label("entity.storage_full", "Storage full")) }
value = BuilderFinish(out)
}
TextLine(cast(u64)(740 + index), value, x + 12.0 * scale, y.*, width - 24.0 * scale, 13)
y.* += 36.0 * scale
}
if node.media_supported {
TextLine(cast(u64)(760 + index), Label("entity.node_media", "Upload photos and videos"), x + 12.0 * scale, y.* + 8.0 * scale, width - 84.0 * scale, 13)
result := Toggle(session, ToggleProps.{.key = cast(u64)(780 + index), .id = 780 + cast(s32)index,
.bounds = Rectangle.{x + width - 64.0 * scale, y.*, 52.0 * scale, 40.0 * scale},
.label = Label("entity.node_media", "Upload photos and videos"), .value = node.media,
.disabled = management.busy || management_waiting > management.completed})
if result.changed { Manage("media", "", address, ifx result.value then 1 else 0) }
y.* += 52.0 * scale
}
}
DrawConnectionCode :: (text: string, x: float32, y: float32, width: float32) {
if text != TextUntilNul(qr_text[:]) {
qr_valid = CopyCString(text, qr_text[:]) && QrEncodeText(text, QrEcc.QrEccMedium, *qr_code)
}
if !qr_valid { return }
modules := qr_code.size + 8
limit := cast(s32)width
if limit > cast(s32)(240.0 * scale) { limit = cast(s32)(240.0 * scale) }
cell := limit / modules
if cell < 1 { return }
side := cell * modules
left := cast(s32)(x + (width - cast(float32)side) * 0.5)
top := cast(s32)y
Box(session, BoxProps.{.key = cast(u64)19000,
.bounds = Rectangle.{cast(float32)left, y, cast(float32)side, cast(float32)side}, .fill = Color.{255, 255, 255, 255}})
for row: 0..qr_code.size - 1 {
column: s32 = 0
while column < qr_code.size {
if !QrModule(*qr_code, column, row) { column += 1; continue }
start := column
while column < qr_code.size && QrModule(*qr_code, column, row) { column += 1 }
Box(session, BoxProps.{.key = cast(u64)(20000 + row * QR_SIZE_MAX + start),
.bounds = Rectangle.{cast(float32)(left + (start + 4) * cell), cast(float32)(top + (row + 4) * cell),
cast(float32)((column - start) * cell), cast(float32)cell}, .fill = Color.{0, 0, 0, 255}})
}
}
}
DrawAccount :: () {
session = AppSession()
scale = AppScale()
if scale <= 0.0 { scale = 1.0 }
area := AppBounds()
width := area.width - 80.0 * scale
if width > 512.0 * scale { width = 512.0 * scale }
if width < 160.0 * scale { return }
height: float32 = ifx state.host_navigation then 180.0 else 244.0
if management_page == 1 {
if (state.controls & ENTITY_NODES) != 0 {
height += cast(float32)(112 + state.node_count * 56)
for index: 0..state.node_count - 1 { height += NodeDetailsHeight(TextUntilNul(state.nodes[index][:])) / scale }
}
if management.account {
height += 228.0
if CanManage(ACCOUNT_ALIAS) { height += 100.0 }
if CanManage(ACCOUNT_SYNC) { height += 112.0 }
if CanManage(ACCOUNT_BACKUP) { height += 56.0 }
if CanManage(ACCOUNT_LOGOUT) { height += 56.0 }
if CanManage(ACCOUNT_CLEAR_REMOTE) { height += 56.0 }
} else if CanManage(ACCOUNT_CREATE) { height += 56.0 }
if CanManage(ACCOUNT_IMPORT) { height += 56.0 }
if CanManage(ACCOUNT_LINK) {
height += 224.0
if management.link_code[0] != cast(u8)0 { height += 320.0 }
if management.link_message[0] != cast(u8)0 { height += 52.0 }
if CanManage(ACCOUNT_SCAN) { height += 56.0 }
if management.link_stage == 2 || management.link_stage == 4 { height += 56.0 }
if management.link_stage != 0 { height += 56.0 }
}
}
Box(session, BoxProps.{.key = cast(u64)2, .bounds = area,
.fill = ThemeOr("canvas", 0x142724ff, 0xeef4f2ff)})
scroll := Scroll(session, ScrollProps.{.key = cast(u64)1, .bounds = area,
.content_height = cast(s32)((height + 48.0) * scale), .scroll_offset = management_scroll, .scale = scale})
management_scroll = scroll.scroll_offset
if !scroll.opened { return }
x := scroll.content.x + (scroll.content.width - width) * 0.5
y := scroll.content.y + 48.0 * scale
if !state.host_navigation {
if Action(800, Label("entity.back", "Back"), Rectangle.{x, y, 84.0 * scale, 40.0 * scale}) {
if management_page == 1 { management_page = 0 }
else { management_page = 1 }
management_scroll = 0
for index: 0..account_passphrase.count - 1 { account_passphrase[index] = cast(u8)0 }
fields = .{}
}
y += 64.0 * scale
}
title := Label("entity.account", "Account")
if management_page == 2 { title = Label("entity.import_key", "Import account key") }
if management_page == 3 { title = Label("entity.backup_key", "Back up account key") }
if management_page == 4 { title = Label("entity.logout", "Log out on this device") }
if management_page == 5 { title = Label("entity.clear_remote", "Clear remote account data") }
if management_page == 6 { title = Label("entity.switch_account", "Switch account") }
if !state.host_navigation || management_page != 1 {
TextLine(801, title, x, y, width, 26)
y += 52.0 * scale
}
if management_page == 2 || management_page == 3 {
Paragraph(802, Label("entity.key_file_help", "Choose a key file. A passphrase protects the backup and is needed again when importing it."), x, y, width, 64.0)
y += 72.0 * scale
unused EditField(4, account_passphrase[:], Label("entity.passphrase", "Passphrase (optional)"), Rectangle.{x, y, width, 44.0 * scale}, 160, true)
y += 60.0 * scale
if Action(803, Label(ifx management_page == 3 then "entity.save" else "entity.choose_file", ifx management_page == 3 then "Save key file" else "Choose file"), Rectangle.{x, y, width, 44.0 * scale}, true) {
passphrase: [512]u8
for index: 0..account_passphrase.count - 1 { passphrase[index] = account_passphrase[index] }
{
value := TextUntilNul(passphrase[:])
Manage(ifx management_page == 2 then "import" else "backup", value)
}
for index: 0..passphrase.count - 1 { passphrase[index] = cast(u8)0 }
}
y += 56.0 * scale
} else if management_page >= 4 {
explanation := Label("entity.logout_help", "Your local records stay on this device. Keep a key backup so you can connect again.")
if management_page == 5 { explanation = Label("entity.clear_remote_help", "This removes this account's remote data. Local records stay on this device.") }
if management_page == 6 { explanation = Label("entity.switch_help", "Choose whether to keep and merge the local records or replace them for the incoming account.") }
Paragraph(804, explanation, x, y, width, 80.0)
y += 92.0 * scale
if management_page == 6 {
if Action(805, Label("entity.merge", "Keep and merge local data"), Rectangle.{x, y, width, 44.0 * scale}) { Manage("switch_apply", "", "", 1) }
y += 56.0 * scale
if Action(806, Label("entity.replace", "Replace local data"), Rectangle.{x, y, width, 44.0 * scale}, false, true) { Manage("switch_apply", "", "", 2) }
y += 56.0 * scale
if Action(807, Label("entity.cancel", "Cancel"), Rectangle.{x, y, width, 44.0 * scale}) { Manage("switch_cancel"); management_page = 1 }
} else if Action(808, title, Rectangle.{x, y, width, 44.0 * scale}, true, true) {
Manage(ifx management_page == 4 then "logout" else "clear_remote")
management_page = 1
}
y += 56.0 * scale
} else {
if management.account {
TextLine(810, TextUntilNul(management.alias[:]), x, y, width, 20)
y += 32.0 * scale
identity := TextUntilNul(management.public_id[:])
if identity.count > 8 {
unused CopyCString(identity[:4], account_short_id[:])
unused CopyCString("…", account_short_id[4:])
unused CopyCString(identity[identity.count - 4:], account_short_id[7:])
identity = TextUntilNul(account_short_id[:])
}
Paragraph(811, identity, x, y, width, 48.0, 13)
y += 52.0 * scale
if Action(812, Label("entity.copy_id", "Copy public ID"), Rectangle.{x, y, width, 44.0 * scale}) { unused ClipboardCopy(TextUntilNul(management.public_id[:]), 0) }
y += 56.0 * scale
Paragraph(813, TextUntilNul(management.status[:]), x, y, width, 48.0)
y += 52.0 * scale
if CanManage(ACCOUNT_SYNC) {
out: Vec(u8)
BuilderPrint(*out, "%: %", Label("entity.queued", "Queued changes"), management.queued)
TextLine(814, BuilderFinish(out), x, y, width, 14)
y += 36.0 * scale
}
if CanManage(ACCOUNT_ALIAS) {
unused EditField(3, account_alias[:], Label("entity.alias", "Account alias"), Rectangle.{x, y, width - 84.0 * scale, 44.0 * scale}, 39)
if Action(815, Label("entity.save", "Save"), Rectangle.{x + width - 72.0 * scale, y, 72.0 * scale, 44.0 * scale}) { Manage("alias", TextUntilNul(account_alias[:])) }
y += 100.0 * scale
}
if CanManage(ACCOUNT_SYNC) {
TextLine(816, Label("entity.sync", "Sync"), x, y + 8.0 * scale, width - 80.0 * scale, 16)
result := Toggle(session, ToggleProps.{.key = cast(u64)817, .id = 817,
.bounds = Rectangle.{x + width - 64.0 * scale, y, 64.0 * scale, 40.0 * scale},
.label = Label("entity.sync", "Sync"), .value = management.enabled, .disabled = management_waiting > management.completed})
if result.changed { Manage("set_sync", "", "", ifx result.value then 1 else 0) }
y += 56.0 * scale
if Action(818, Label("entity.sync_now", "Sync now"), Rectangle.{x, y, width, 44.0 * scale}) { Manage("sync") }
y += 56.0 * scale
}
if CanManage(ACCOUNT_BACKUP) {
if Action(819, Label("entity.backup_key", "Back up account key"), Rectangle.{x, y, width, 44.0 * scale}) { OpenManagement(3) }
y += 56.0 * scale
}
} else if CanManage(ACCOUNT_CREATE) {
if Action(820, Label("entity.create_account", "Create account"), Rectangle.{x, y, width, 44.0 * scale}, true) { Manage("create") }
y += 56.0 * scale
}
if CanManage(ACCOUNT_IMPORT) {
if Action(821, Label("entity.import_key", "Import account key"), Rectangle.{x, y, width, 44.0 * scale}) { OpenManagement(2) }
y += 56.0 * scale
}
if (state.controls & ENTITY_NODES) != 0 { Nodes(x, *y, width) }
if CanManage(ACCOUNT_LINK) {
TextLine(822, Label("entity.connect_device", "Connect another device"), x, y, width, 20)
y += 40.0 * scale
if management.link_code[0] != cast(u8)0 {
DrawConnectionCode(TextUntilNul(management.link_code[:]), x, y, width)
y += 252.0 * scale
if Action(823, Label("entity.copy_code", "Copy connection code"), Rectangle.{x, y, width, 44.0 * scale}) { unused ClipboardCopy(TextUntilNul(management.link_code[:]), 0) }
y += 64.0 * scale
}
if management.link_message[0] != cast(u8)0 {
Paragraph(824, TextUntilNul(management.link_message[:]), x, y, width, 48.0)
y += 52.0 * scale
}
unused EditField(5, account_code[:], Label("entity.connection_code", "Connection code"), Rectangle.{x, y, width, 44.0 * scale}, 1023)
y += 56.0 * scale
if Action(825, Label("entity.connect", "Connect"), Rectangle.{x, y, width, 44.0 * scale}) { Manage("link_join", TextUntilNul(account_code[:])) }
y += 56.0 * scale
if Action(826, Label("entity.show_code", "Show connection code"), Rectangle.{x, y, width, 44.0 * scale}) { Manage("link_show") }
y += 56.0 * scale
if CanManage(ACCOUNT_SCAN) {
if Action(827, Label("entity.scan_code", "Scan connection code"), Rectangle.{x, y, width, 44.0 * scale}) { Manage("link_scan") }
y += 56.0 * scale
}
if management.link_stage == 2 || management.link_stage == 4 {
if Action(828, Label("entity.confirm_device", "The digits match — connect"), Rectangle.{x, y, width, 44.0 * scale}, true) { Manage("link_confirm") }
y += 56.0 * scale
}
if management.link_stage != 0 {
if Action(829, Label("entity.cancel_connection", "Cancel connection"), Rectangle.{x, y, width, 44.0 * scale}) { Manage("link_cancel") }
y += 56.0 * scale
}
}
if management.account && CanManage(ACCOUNT_LOGOUT) {
if Action(830, Label("entity.logout", "Log out on this device"), Rectangle.{x, y, width, 44.0 * scale}) { OpenManagement(4) }
y += 56.0 * scale
}
if management.account && CanManage(ACCOUNT_CLEAR_REMOTE) {
if Action(831, Label("entity.clear_remote", "Clear remote account data"), Rectangle.{x, y, width, 44.0 * scale}) { OpenManagement(5) }
y += 56.0 * scale
}
}
if notice != "" { Paragraph(840, notice, x, y, width, 64.0) }
if management_waiting > management.completed { TextLine(841, Label("entity.working", "Working…"), x, y + 68.0 * scale, width, 14) }
unused End(session)
AppKeyboard(fields[3].focused || fields[4].focused || fields[5].focused)
}
Picture :: (index: s32, bounds: Rectangle) -> ImageProps {
image := ImageProps.{.asset_path = "assets/entity/pfp.png", .alt_text = Label("entity.picture", "Profile picture"),
.bounds = bounds, .source = Rectangle.{cast(float32)(index % 5) * 64.0, cast(float32)(index / 5) * 64.0, 64.0, 64.0}}
if index == 9 {
image.asset_path = "assets/entity/flowers.png"
image.source = Rectangle.{}
image.fit = ImageFit.ImageFitContain
}
if asset_root[0] != cast(u8)0 {
path: Vec(u8)
BuilderPrint(*path, "%/%", TextUntilNul(asset_root[:]), image.asset_path)
image.asset_path = BuilderFinish(path)
}
return image
}
Nodes :: (x: float32, y: *float32, width: float32) {
TextLine(400, Label("entity.nodes", "Nodes"), x, y.*, width, 20)
y.* += 36.0 * scale
for index: 0..state.node_count - 1 {
node: [256]u8
for byte: 0..255 { node[byte] = state.nodes[index][byte] }
preferred := state.preferred == index
remove_width := ifx state.node_count > 1 then 44.0 * scale else 0.0
row := Rectangle.{x, y.*, width - remove_width, 48.0 * scale}
Box(session, BoxProps.{.key = cast(u64)(600 + index), .bounds = row,
.fill = ThemeOr("canvas", 0x263f3aff, 0xf0f6f4ff),
.border = ifx preferred then ThemeOr("accent", 0x9abdb1ff, 0x9abdb1ff)
else ThemeOr("border", 0xdce6e2ff, 0xdce6e2ff), .radius = 10.0 * scale})
choose := ButtonProps.{.key = cast(u64)(410 + index), .id = 410 + cast(s32)index,
.bounds = row, .label = TextUntilNul(node[:]), .invisible = true,
.selected = preferred}
if Button(session, choose) != 0 {
state.preferred = cast(s32)index
Save()
}
TextLine(cast(u64)(620 + index), TextUntilNul(node[:]),
x + 14.0 * scale, y.* + 10.0 * scale, row.width - 64.0 * scale, 16)
center := Vector2.{row.x + row.width - 24.0 * scale, row.y + 24.0 * scale}
Box(session, BoxProps.{.key = cast(u64)(640 + index),
.bounds = Rectangle.{center.x - 10.0 * scale, center.y - 10.0 * scale, 20.0 * scale, 20.0 * scale},
.border = ifx preferred then ThemeOr("accent", 0x2e7d68ff, 0x2e7d68ff)
else ThemeOr("border", 0xb8ccc5ff, 0xb8ccc5ff), .radius = 10.0 * scale})
indicator := ButtonProps.{.key = cast(u64)(660 + index), .id = 660 + cast(s32)index,
.bounds = Rectangle.{center.x - 12.0 * scale, center.y - 12.0 * scale, 24.0 * scale, 24.0 * scale},
.label = TextUntilNul(node[:]), .icon_only = true, .circle = true,
.icon_shape = ifx preferred then "S2rr:M5 12L10 17L19 7" else "",
.tone = ButtonTone.ButtonToneAccent, .emphasis = ButtonEmphasis.ButtonEmphasisGhost}
if Button(session, indicator) != 0 { state.preferred = cast(s32)index; Save() }
if state.node_count > 1 {
remove := ButtonProps.{.key = cast(u64)(430 + index), .id = 430 + cast(s32)index,
.bounds = Rectangle.{x + width - 40.0 * scale, y.*, 40.0 * scale, 48.0 * scale},
.label = Label("entity.remove", "Remove node"), .icon_only = true,
.tone = ButtonTone.ButtonToneNeutral, .emphasis = ButtonEmphasis.ButtonEmphasisGhost,
.icon_shape = "S1.6rr:M3 6H21M9 6V3H15V6M5 6L6 21H18L19 6M10 10V17M14 10V17"}
if Button(session, remove) != 0 {
for next: index..state.node_count - 2 { state.nodes[next] = state.nodes[next + 1] }
state.node_count -= 1
if state.preferred >= index && state.preferred > 0 { state.preferred -= 1 }
Save()
break
}
}
y.* += 56.0 * scale
NodeDetails(TextUntilNul(node[:]), x, y, width)
}
y.* += 8.0 * scale
submitted := EditField(2, node_text[:], Label("entity.node_address", "Node address"),
Rectangle.{x, y.*, width - 84.0 * scale, 44.0 * scale}, 255)
add := Button(session, ButtonProps.{.key = cast(u64)450, .id = 450, .label = Label("entity.add", "Add"),
.bounds = Rectangle.{x + width - 72.0 * scale, y.*, 72.0 * scale, 44.0 * scale},
.size = ControlSize.ControlSizeLarge,
.tone = ButtonTone.ButtonToneAccent, .emphasis = ButtonEmphasis.ButtonEmphasisFilled}) != 0
clear_node: bool = false
if submitted || add {
snapshot := node_text
origin := NodeOrigin(TextUntilNul(snapshot[:]))
if origin == "" {
notice = Label("entity.node_invalid", "Enter a valid HTTPS node address")
} else {
existing: s32 = -1
for index: 0..state.node_count - 1 {
if TextUntilNul(state.nodes[index][:]) == origin { existing = cast(s32)index }
}
if existing >= 0 {
state.preferred = existing
Save()
clear_node = true
} else if state.node_count < ENTITY_NODE_LIMIT {
unused CopyCString(origin, state.nodes[state.node_count][:])
state.preferred = state.node_count
state.node_count += 1
Save()
clear_node = true
} else { notice = Label("entity.node_limit", "Eight nodes are already saved") }
}
}
if clear_node {
node_text[0] = cast(u8)0
fields[2] = Field.{}
}
y.* += 68.0 * scale
}
ExtensionShape :: (route: string) -> string {
if route == "dew" { return "S1.7rr:M12 3C10 6 5 11 5 15C5 24 19 24 19 15C19 11 14 6 12 3ZM8 15C8 18 10 19 12 19" }
if route == "achievements" { return "S1.7rr:M8 3H16V10C16 16 8 16 8 10ZM8 5H4V8L8 11M16 5H20V8L16 11M12 15V21M8 21H16" }
if route == "inbe" { return "S1.7rr:M3 13H7L10 5L14 20L17 11H21" }
if route == "friends" { return "S1.7rr:M9 3C4 3 4 11 9 11C14 11 14 3 9 3M2 21V18C2 12 16 12 16 18V21M17 4C22 4 22 11 17 11M19 14C22 15 22 18 22 21" }
if route == "telegram" { return "S1.7rr:M3 11L21 4L17 21L11 15L7 18L8 13L18 7L11 15" }
if route == "data" { return "S1.7rr:M4 5C4 1 20 1 20 5C20 9 4 9 4 5V18C4 22 20 22 20 18V5M4 11C4 15 20 15 20 11" }
return "S1.7rr:M5 5H19V19H5ZM9 9L15 15M9 15L15 9"
}
ExtensionRows :: (left: float32, top: *float32, width: float32) {
for index: 0..state.extension_count - 1 {
entry := state.extensions[index]
route := TextUntilNul(entry.id[:])
if management.available && route == "account" { continue }
if route == "dew" && AppSupports("entity.dew") { continue }
detailed := entry.summary[0] != cast(u8)0
row_height := (ifx detailed then 76.0 else 60.0) * scale
row := Rectangle.{left, top.*, width, row_height}
color := ThemeOr("text", 0xe5eef3ff, 0x203b4bff)
SvgIcon(session, SvgIconProps.{.shape = ExtensionShape(route), .color = color,
.bounds = Rectangle.{left + 4.0 * scale, top.* + 16.0 * scale, 24.0 * scale, 24.0 * scale}})
label_x := left + 44.0 * scale
TextLine(cast(u64)(920 + index), TextUntilNul(entry.label[:]), label_x,
top.* + (ifx detailed then 8.0 else 16.0) * scale, width - 76.0 * scale, 16)
if detailed {
TextLine(cast(u64)(940 + index), TextUntilNul(entry.summary[:]), label_x,
top.* + 36.0 * scale, width - 76.0 * scale, 13)
}
SvgIcon(session, SvgIconProps.{.shape = "S1.7rr:M9 5L16 12L9 19", .color = color,
.bounds = Rectangle.{left + width - 24.0 * scale, top.* + 18.0 * scale, 20.0 * scale, 20.0 * scale}})
if Button(session, ButtonProps.{.key = cast(u64)(500 + index), .id = 500 + cast(s32)index,
.bounds = row, .label = TextUntilNul(entry.label[:]), .invisible = true}) != 0 && Save() {
if route == "dew" && AppSupports("entity.dew") {
dew_page = true
dew_refresh = true
} else if !AchievementOpenRoute(route) { AppReply(route) }
}
Box(session, BoxProps.{.key = cast(u64)(900 + index),
.bounds = Rectangle.{label_x, top.* + row_height, width - 44.0 * scale, 1.0},
.fill = ThemeOr("border", 0x34514aff, 0xdde7e3ff)})
top.* += row_height + 8.0 * scale
}
top.* += 16.0 * scale
}
Draw :: () {
PollManagement()
if !loaded {
state = EntityDefault()
assets := AppService("entity.assets", "")
if assets.count > 0 && assets[0] == cast(u8)47 { unused CopyCString(assets, asset_root[:]) }
bytes: [16385]u8
count := ReadStore("entity.json", bytes[:])
if count > 0 { unused EntityDecode(TextUntilNul(bytes[:]), *state) }
loaded = true
dew_refresh = true
}
if AchievementViewFrame(!state.host_navigation) { return }
if dew_page { DrawDew(); return }
if management_page != 0 { DrawAccount(); return }
session = AppSession()
scale = AppScale()
if scale <= 0.0 { scale = 1.0 }
area := AppBounds()
panel_width := area.width - 32.0 * scale
if panel_width > 560.0 * scale { panel_width = 560.0 * scale }
if panel_width < 208.0 * scale { return }
width := panel_width - 48.0 * scale
inline_dew := AppSupports("entity.dew")
if inline_dew { ProfileDewRead() }
columns := cast(s32)((width + 12.0 * scale) / (56.0 * scale))
if columns > 7 { columns = 7 }
if columns < 3 { columns = 3 }
picture_count: s32 = 0
for index: 0..27 { if (state.pictures & (1 << cast(s32)index)) != 0 { picture_count += 1 } }
picker_height := cast(float32)((picture_count + columns - 1) / columns) * 56.0 * scale + 16.0 * scale
height := (ifx !state.host_navigation then 352.0 else 308.0) * scale
if (state.controls & ENTITY_IDENTITY) == 0 { height -= 100.0 * scale }
if inline_dew { height += ProfileDewBalance(0.0, 0.0, width, false).height }
if (state.controls & ENTITY_NODES) != 0 && !management.available { height += cast(float32)(112 + state.node_count * 56) * scale }
if management.available { height += (ifx management.account then 64.0 else 144.0) * scale }
if !management.available {
for index: 0..state.node_count - 1 { height += NodeDetailsHeight(TextUntilNul(state.nodes[index][:])) }
}
if (state.controls & ENTITY_INTENTION) != 0 { height += 100.0 * scale }
if (state.controls & ENTITY_APPEARANCE) != 0 { height += 100.0 * scale }
if (state.controls & ENTITY_ACCENT) != 0 { height += 68.0 * scale + 2.0 * ((width - 56.0 * scale) / 8.0) }
if (state.controls & ENTITY_WIDGETS) != 0 { height += 100.0 * scale }
if choosing_picture { height += picker_height }
for index: 0..state.extension_count - 1 {
entry := state.extensions[index]
if management.available && TextUntilNul(entry.id[:]) == "account" { continue }
if TextUntilNul(entry.id[:]) == "dew" && inline_dew { continue }
height += (ifx entry.summary[0] != cast(u8)0 then 84.0 else 68.0) * scale
}
if notice != "" { height += 48.0 * scale }
Box(session, BoxProps.{.key = cast(u64)2, .bounds = area,
.fill = ThemeOr("canvas", 0x142724ff, 0xeef4f2ff)})
scroll := Scroll(session, ScrollProps.{.key = cast(u64)1,
.bounds = area, .content_height = cast(s32)(height + 48.0 * scale),
.scroll_offset = scroll_offset, .scale = scale})
scroll_offset = scroll.scroll_offset
if !scroll.opened { return }
panel_x := scroll.content.x + (scroll.content.width - panel_width) * 0.5
panel_y := scroll.content.y + 24.0 * scale
Box(session, BoxProps.{.key = cast(u64)3, .bounds = Rectangle.{panel_x, panel_y, panel_width, height},
.fill = ThemeOr("surface", 0x1c3330ff, 0xffffffff),
.border = ThemeOr("border", 0x34514aff, 0xdde7e3ff), .radius = 20.0 * scale})
x := panel_x + 24.0 * scale
y := panel_y + 24.0 * scale
if !state.host_navigation {
TextLine(5, Label("entity.title", "Your profile"), x, y, width, 30)
y += 44.0 * scale
}
center_x := x + width * 0.5
Box(session, BoxProps.{.key = cast(u64)6,
.bounds = Rectangle.{center_x - 40.0 * scale, y, 80.0 * scale, 80.0 * scale},
.radius = 40.0 * scale, .fill = ThemeOr("canvas", 0x2a4540ff, 0xeaf1eaff)})
Image(session, Picture(state.picture, Rectangle.{center_x - 36.0 * scale, y + 4.0 * scale, 72.0 * scale, 72.0 * scale}))
y += 88.0 * scale
change := ButtonProps.{.key = cast(u64)10, .id = 10,
.bounds = Rectangle.{center_x - 72.0 * scale, y, 144.0 * scale, 32.0 * scale},
.label = Label(ifx choosing_picture then "entity.done" else "entity.change_picture", ifx choosing_picture then "Done" else "Change picture"),
.tone = ButtonTone.ButtonToneNeutral, .emphasis = ButtonEmphasis.ButtonEmphasisGhost}
if Button(session, change) != 0 { choosing_picture = !choosing_picture }
y += 52.0 * scale
if choosing_picture {
size := 44.0 * scale
gap := (width - cast(float32)columns * size) / cast(float32)(columns - 1)
position: s32 = 0
for index: 0..27 {
if (state.pictures & (1 << cast(s32)index)) == 0 { continue }
choice := ButtonProps.{.key = cast(u64)(200 + index), .id = 200 + cast(s32)index,
.label = Label("entity.picture", "Profile picture"), .icon_only = true,
.bounds = Rectangle.{x + cast(float32)(position % columns) * (size + gap),
y + cast(float32)(position / columns) * 56.0 * scale, size, size},
.image = Picture(cast(s32)index, Rectangle.{}), .selected = state.picture == index}
if Button(session, choice) != 0 { state.picture = cast(s32)index; Save() }
position += 1
}
y += picker_height
}
if management.available { AccountSummary(x, *y, width) }
if inline_dew { y += ProfileDewBalance(x, y, width, true).height }
if (state.controls & ENTITY_IDENTITY) != 0 {
TextLine(20, Label("entity.name", "Name"), x, y, width, 14)
if EditField(0, state.name[:], Label("entity.name", "Name"), Rectangle.{x, y + 28.0 * scale, width, 44.0 * scale}, 40) { Save() }
y += 100.0 * scale
}
if (state.controls & ENTITY_INTENTION) != 0 {
TextLine(21, Label("entity.intention", "Intention"), x, y, width, 14)
if EditField(1, state.intention[:], Label("entity.intention", "Intention"), Rectangle.{x, y + 28.0 * scale, width, 44.0 * scale}, 90) { Save() }
y += 100.0 * scale
}
ExtensionRows(x, *y, width)
if (state.controls & ENTITY_NODES) != 0 && !management.available { Nodes(x, *y, width) }
if notice != "" {
Text(session, TextProps.{.key = cast(u64)22, .text = notice,
.bounds = Rectangle.{x, y, width, 40.0 * scale}, .font = cast(s32)(13.0 * scale), .wrap = TextWrap.TextWrapAuto})
y += 48.0 * scale
}
if (state.controls & ENTITY_APPEARANCE) != 0 {
TextLine(30, Label("entity.appearance", "Appearance"), x, y, width, 20)
y += 36.0 * scale
modes: [3]string = .[Label("entity.system", "System"), Label("entity.light", "Light"), Label("entity.dark", "Dark")]
third := (width - 16.0 * scale) / 3.0
for index: 0..2 {
if Action(50 + cast(s32)index, modes[index], Rectangle.{x + cast(float32)index * (third + 8.0 * scale), y, third, 40.0 * scale}, state.theme == index) {
state.theme = cast(s32)index
Save()
}
}
y += 64.0 * scale
}
if (state.controls & ENTITY_ACCENT) != 0 {
TextLine(31, Label("entity.accent", "Accent"), x, y, width, 20)
y += 36.0 * scale
colors: [16]u32 = .[0x7355d9ff, 0x2563ebff, 0x059669ff, 0xdb2777ff, 0xd97706ff, 0x0891b2ff, 0xdc2626ff, 0x7c3aedff,
0x4f46e5ff, 0x0d9488ff, 0x65a30dff, 0xc026d3ff, 0xea580cff, 0x0284c7ff, 0x9333eaff, 0x475569ff]
size := (width - 7.0 * 8.0 * scale) / 8.0
for index: 0..15 {
if Button(session, ButtonProps.{.key = cast(u64)(300 + index), .id = 300 + cast(s32)index,
.bounds = Rectangle.{x + cast(float32)(index % 8) * (size + 8.0 * scale), y + cast(float32)(index / 8) * (size + 8.0 * scale), size, size},
.label = Label("entity.accent", "Accent"), .icon_only = true, .swatch = true, .swatch_color = ColorFromPacked(colors[index]), .selected = state.accent == index}) != 0 {
state.accent = cast(s32)index
Save()
}
}
y += 2.0 * (size + 8.0 * scale) + 16.0 * scale
}
if (state.controls & ENTITY_WIDGETS) != 0 {
TextLine(32, Label("entity.widgets", "Widgets"), x, y, width, 20)
y += 36.0 * scale
half := (width - 8.0 * scale) * 0.5
if Action(60, "Material", Rectangle.{x, y, half, 40.0 * scale}, state.material) { state.material = true; Save() }
if Action(61, "Classic", Rectangle.{x + half + 8.0 * scale, y, half, 40.0 * scale}, !state.material) { state.material = false; Save() }
y += 64.0 * scale
}
unused End(session)
AppKeyboard(fields[0].focused || fields[1].focused || fields[2].focused)
}
#program_export
Main :: () -> s64 {
mode := AppMode()
if mode == APP_IDENTIFY {
AppIdentity("entity")
AppDescribe("Entity", "S1.6rr:M12 12A4 4 0 1 0 12 4A4 4 0 0 0 12 12ZM4 22V20A8 8 0 0 1 20 20V22")
return APP_PROTOCOL
}
if mode == APP_COMMAND && AppCommand() == "back" && dew_page {
dew_page = false
dew_confirmation = -1
AppReply("handled")
return 0
}
if mode == APP_COMMAND && AchievementCommand(AppCommand()) { return 0 }
if mode == APP_FRAME { Draw() }
if mode == APP_COMMAND && AppCommand() == "reload" {
loaded = false
fields = .{}
}
if mode == APP_COMMAND && AppCommand() == "account" { management_page = 1; management_scroll = 0 }
if mode == APP_COMMAND && AppCommand() == "dew" && AppSupports("entity.dew") {
dew_page = true
dew_refresh = true
dew_confirmation = -1
management_scroll = 0
}
if mode == APP_COMMAND && AppCommand() == "back" && management_page != 0 {
management_page = ifx management_page == 1 then 0 else 1
management_scroll = 0
for index: 0..account_passphrase.count - 1 { account_passphrase[index] = cast(u8)0 }
fields = .{}
AppReply("handled")
}
if mode == APP_SAVE && loaded { return ifx Save() then 0 else 1 }
return 0
}
// One Dew page for native Entity providers. The host supplies verified node
// state and queues explicit requests; balances are never computed locally.
dew_page: bool;
dew_refresh: bool;
dew_confirmation: s32 = -1;
dew_view_tick: s32;
dew_view_json: [20000]u8;
dew_profile_account: DewAccount;
dew_profile_available: bool;
dew_profile_busy: bool;
dew_profile_error: bool;
dew_profile_host_actions: bool;
dew_profile_worth: [256]u8;
dew_profile_plan: [256]u8;
dew_profile_usage: [256]u8;
ProfileDewRead :: () {
dew_view_tick += 1
if dew_view_tick % 15 != 1 && !dew_refresh { return }
unused CopyCString(AppService("entity.dew", ""), dew_view_json[:])
json := TextUntilNul(dew_view_json[:])
owner: [65]u8
unused ReadStringText(json, FieldAt(json, 0, "owner"), owner[:])
same_owner := TextUntilNul(owner[:]) == TextUntilNul(management.public_id[:]) && TextUntilNul(owner[:]).count == 64
dew_profile_available = same_owner && MatchLiteral(json, FieldAt(json, 0, "available"), "true")
dew_profile_busy = MatchLiteral(json, FieldAt(json, 0, "busy"), "true")
dew_profile_error = MatchLiteral(json, FieldAt(json, 0, "error"), "true")
dew_profile_host_actions = MatchLiteral(json, FieldAt(json, 0, "host_actions"), "true")
dew_profile_account = .{}
dew_profile_worth = .{}
dew_profile_plan = .{}
dew_profile_usage = .{}
at := FieldAt(json, 0, "account")
end := ValueEnd(json, at, 0)
if same_owner && at >= 0 && end > at {
unused DewParseAccount(json[at:end], TextUntilNul(owner[:]), *dew_profile_account)
unused ReadStringText(json, FieldAt(json, 0, "worth"), dew_profile_worth[:])
unused ReadStringText(json, FieldAt(json, 0, "plan_summary"), dew_profile_plan[:])
unused ReadStringText(json, FieldAt(json, 0, "usage_summary"), dew_profile_usage[:])
}
if dew_refresh && dew_profile_available && !dew_profile_busy {
sequence := cast(s32)ReadNumber(json, FieldAt(json, 0, "sequence")).value
DewCommand("refresh", sequence)
dew_refresh = false
}
}
ProfileDewBalance :: (x: float32, y: float32, width: float32, draw: bool) -> DewBalanceResult {
account := dew_profile_account
amount: [64]u8
unused CopyCString("—", amount[:])
if account.known && dew_profile_available { unused DewFormat(account.balance, DewUnits, amount[:], "Dew") }
plan := TextUntilNul(dew_profile_plan[:])
usage := TextUntilNul(dew_profile_usage[:])
own_plan: [256]u8
own_usage: [256]u8
if account.known && plan == "" {
unused CopyCString(ifx account.plan_size > 0 then Label("profile_dew_paid_plan", "Paid storage") else Label("profile_dew_plan_free", "Free"), own_plan[:])
plan = TextUntilNul(own_plan[:])
}
if account.known && usage == "" && account.limit_bytes > 0 {
used: [64]u8
limit: [64]u8
unused DewFormat(account.used_bytes / 1048576, 1, used[:], "MB")
unused DewFormat(account.limit_bytes / 1048576, 1, limit[:], "MB")
at: s64 = 0
for byte: TextUntilNul(used[:]) { own_usage[at] = byte; at += 1 }
for byte: " / " { own_usage[at] = byte; at += 1 }
for byte: TextUntilNul(limit[:]) { own_usage[at] = byte; at += 1 }
usage = TextUntilNul(own_usage[:])
}
notice := ""
if !dew_profile_available { notice = Label("profile_dew_connect", "Connect your account to the Waozi node to view Dew.") }
else if dew_profile_error { notice = Label("profile_dew_error", "Could not refresh Dew. Any balance shown is the last confirmed balance.") }
else if !account.known { notice = Label("profile_dew_loading", "Checking Dew…") }
result := DewBalance(session, DewBalanceProps.{.bounds = Rectangle.{x, y, width, 0.0}, .scale = scale,
.title = Label("profile_dew_title", "Dew balance"), .balance = TextUntilNul(amount[:]),
.worth = TextUntilNul(dew_profile_worth[:]), .plan = plan, .usage = usage, .notice = notice,
.top_up = Label("profile_dew_topup", "Top up"), .plans = Label("profile_dew_plans", "Storage plans"),
.ready = dew_profile_available, .known = account.known, .busy = dew_profile_busy, .enabled = account.enabled,
.has_usage = account.limit_bytes > 0,
.usage_percent = ifx account.limit_bytes > 0 then cast(s32)(account.used_bytes * 100 / account.limit_bytes) else 0,
.fill = ThemeOr("canvas", 0x142724ff, 0xeef4f2ff), .track = ThemeOr("border", 0x34514aff, 0xdde7e3ff),
.accent = ThemeOr("accent", 0x49a68eff, 0x298a71ff),
.warning = Color.{163, 98, 0, 255}, .danger = Color.{179, 38, 30, 255}}, draw)
if result.action != 0 {
if dew_profile_host_actions { AppReply(ifx result.action == DewBalanceTopUp then "dew-topup" else "dew-plans") }
else { dew_page = true; dew_refresh = true; management_scroll = 0 }
}
return result
}
DewCommand :: (operation: string, sequence: s32, size: string = "", period: string = "") {
request: Vec(u8)
BuilderPrint(*request, "{\"owner\":\"%\",\"sequence\":%,\"operation\":\"%\",\"size\":\"%\",\"period\":\"%\"}",
TextUntilNul(management.public_id[:]), sequence + 1, operation, size, period)
document := BuilderFinish(request)
unused AppService("entity.dew", document)
}
DewDecimal :: (units: s64, divisor: s64, suffix: string) -> string {
text: Vec(u8)
fraction := units % divisor
BuilderPrint(*text, "%", units / divisor)
if fraction != 0 {
digits: [19]u8
places: s32 = 0
factor := divisor
while factor > 1 { places += 1; factor /= 10 }
value := fraction
index := places
while index > 0 { index -= 1; digits[index] = cast(u8)(48 + value % 10); value /= 10 }
last := places
while last > 0 && digits[last - 1] == cast(u8)48 { last -= 1 }
BuilderPrint(*text, ".%", TextView(digits[:last]))
}
BuilderPrint(*text, " %", suffix)
return BuilderFinish(text)
}
DrawDew :: () {
session = AppSession()
scale = AppScale()
if scale <= 0.0 { scale = 1.0 }
dew_view_tick += 1
if dew_view_tick % 15 == 1 || dew_refresh { unused CopyCString(AppService("entity.dew", ""), dew_view_json[:]) }
json := TextUntilNul(dew_view_json[:])
owner: [65]u8
unused ReadStringText(json, FieldAt(json, 0, "owner"), owner[:])
sequence := cast(s32)ReadNumber(json, FieldAt(json, 0, "sequence")).value
available := TextUntilNul(owner[:]) == TextUntilNul(management.public_id[:]) && MatchLiteral(json, FieldAt(json, 0, "available"), "true")
busy := MatchLiteral(json, FieldAt(json, 0, "busy"), "true")
if dew_refresh && available && !busy { DewCommand("refresh", sequence); dew_refresh = false }
area := AppBounds()
Box(session, BoxProps.{.key = cast(u64)45000, .bounds = area, .fill = ThemeOr("canvas", 0x142724ff, 0xeef4f2ff)})
scroll := Scroll(session, ScrollProps.{.key = cast(u64)45001, .bounds = area, .content_height = cast(s32)(1180.0 * scale), .scroll_offset = management_scroll})
management_scroll = scroll.scroll_offset
if !scroll.opened { return }
defer { End(session) }
x := scroll.content.x + 24.0 * scale
y := scroll.content.y + 16.0 * scale
width := scroll.content.width - 48.0 * scale
if !state.host_navigation {
if Action(45002, Label("entity.back", "Back"), Rectangle.{x, y, 76.0 * scale, 44.0 * scale}) { dew_page = false; dew_confirmation = -1 }
y += 56.0 * scale
}
TextLine(45003, "Dew", x, y, width, 28)
y += 48.0 * scale
if !available { Paragraph(45004, Label("entity.dew_connect", "Connect an account on the Waozi node to use Dew"), x, y, width, 70.0); return }
if busy { Paragraph(45005, Label("entity.working", "Working…"), x, y, width, 36.0); y += 42.0 * scale }
account: DewAccount
at := FieldAt(json, 0, "account")
account_end := ValueEnd(json, at, 0)
known := at >= 0 && account_end > at && DewParseAccount(json[at:account_end], TextUntilNul(owner[:]), *account)
if known {
amount := DewDecimal(account.balance, DewUnits, "Dew")
TextLine(45006, amount, x, y, width, 24)
y += 48.0 * scale
}
if !busy && Action(45007, Label("entity.refresh", "Refresh"), Rectangle.{x, y, width, 44.0 * scale}) { DewCommand("refresh", sequence) }
y += 56.0 * scale
if !busy && Action(45008, Label("entity.dew_fund", "Add Dew with Monero"), Rectangle.{x, y, width, 44.0 * scale}) { DewCommand("address", sequence) }
y += 56.0 * scale
funding: DewFunding
at = FieldAt(json, 0, "funding")
funding_end := ValueEnd(json, at, 0)
if at >= 0 && funding_end > at && DewParseFunding(json[at:funding_end], TextUntilNul(owner[:]), *funding) {
minimum := DewDecimal(funding.minimum, 1000000000000, "XMR")
explanation: Vec(u8)
BuilderPrint(*explanation, "%: % · %: %", Label("entity.dew_minimum", "Minimum"), minimum,
Label("entity.dew_confirmations", "Confirmations"), funding.confirmations)
Paragraph(45014, BuilderFinish(explanation), x, y, width, 64.0)
y += 72.0 * scale
Paragraph(45009, TextUntilNul(funding.address[:]), x, y, width, 100.0)
y += 108.0 * scale
if Action(45010, Label("entity.copy_address", "Copy address"), Rectangle.{x, y, width, 44.0 * scale}) { unused ClipboardCopy(TextUntilNul(funding.address[:]), 0) }
y += 56.0 * scale
}
if MatchLiteral(json, FieldAt(json, 0, "pending"), "true") {
Paragraph(45011, Label("entity.dew_pending", "A purchase is awaiting confirmation. Retry checks the same purchase."), x, y, width, 60.0)
y += 68.0 * scale
if !busy && Action(45012, Label("entity.retry", "Retry"), Rectangle.{x, y, width, 44.0 * scale}) { DewCommand("retry", sequence) }
y += 56.0 * scale
} else if known && account.enabled && !busy {
TextLine(45013, Label("entity.storage_plans", "Storage plans"), x, y, width, 20)
y += 40.0 * scale
for index: cast(s32)0..3 {
slot := index / 2
yearly := index % 2 == 1
cost := ifx yearly then account.plans[slot].yearly_units else account.plans[slot].monthly_units
if cost <= 0 || cost > account.balance { continue }
amount := DewDecimal(cost, DewUnits, "Dew")
title: Vec(u8)
BuilderPrint(*title, "% GB · % · %", ifx slot == 0 then 1 else 5,
ifx yearly then Label("entity.yearly", "Yearly") else Label("entity.monthly", "Monthly"), amount)
label := BuilderFinish(title)
if Action(45020 + index, label, Rectangle.{x, y, width, 44.0 * scale}) { dew_confirmation = index }
y += 56.0 * scale
if dew_confirmation == index {
if Action(45030 + index, Label("entity.confirm_purchase", "Confirm purchase"), Rectangle.{x, y, width, 44.0 * scale}, true) {
DewCommand("buy", sequence, DewPlanSizes[slot], ifx yearly then "yearly" else "monthly")
dew_confirmation = -1
}
y += 56.0 * scale
}
}
}
if MatchLiteral(json, FieldAt(json, 0, "error"), "true") { Paragraph(45040, Label("entity.dew_error", "Could not confirm the node's response. Refresh or retry."), x, y, width, 64.0) }
}