diff --git a/src/app/donation_modal.zi b/src/app/donation_modal.zi index cad072b37..25afcc7f5 100644 --- a/src/app/donation_modal.zi +++ b/src/app/donation_modal.zi @@ -27,7 +27,9 @@ using ButtonTone; using ButtonEmphasis; donation_scroll: s32; -donation_notice: string; +// Keep the message identity, rather than a view into a locale catalog that +// can be replaced when the language changes. +donation_notice_key: string; donation_button_height :: () -> s32 { return AppScale(36) } @@ -35,17 +37,17 @@ donation_coin_height :: (width: s32) -> s32 { stack := width < AppScale(330) buttons: s32 = ifx (stack) then (3) else (1) return AppScale(12) + AppScale(30) + AppScale(6) + - AppScale(52) + AppScale(10) + - buttons * donation_button_height() + - (buttons - 1) * AppScale(8) + AppScale(12) + AppScale(52) + AppScale(10) + + buttons * donation_button_height() + + (buttons - 1) * AppScale(8) + AppScale(12) } donation_label :: (app: *InnerBreeze, text: string, x: s32, y: s32, width: s32, height: s32) { Text(app.ui.session, TextProps.{ - .bounds = Rectangle.{cast(float32)x, cast(float32)y, - cast(float32)width, cast(float32)height}, - .text = text}) + .bounds = Rectangle.{cast(float32)x, cast(float32)y, + cast(float32)width, cast(float32)height}, + .text = text}) } donation_coin :: (app: *InnerBreeze, name: string, address: string, @@ -60,38 +62,38 @@ donation_coin :: (app: *InnerBreeze, name: string, address: string, button_width := ifx (stack) then (width) else ((width - gap * 2) / 3) button_height := donation_button_height() if Button(app.ui.session, ButtonProps.{ - .id = id, - .bounds = Rectangle.{cast(float32)x, cast(float32)y, - cast(float32)button_width, cast(float32)button_height}, - .label = LocaleText("copy_address_button"), - .tone = cast(ButtonTone)ButtonToneNeutral, - .emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft}) != 0 { + .id = id, + .bounds = Rectangle.{cast(float32)x, cast(float32)y, + cast(float32)button_width, cast(float32)button_height}, + .label = LocaleText("copy_address_button"), + .tone = cast(ButtonTone)ButtonToneNeutral, + .emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft}) != 0 { if TextInputWriteClipboard(address) { - donation_notice = LocaleText("address_copied") + donation_notice_key = "address_copied" } } if stack { y += button_height + gap } second_x := ifx (stack) then (x) else (x + button_width + gap) if Button(app.ui.session, ButtonProps.{ - .id = id + 1, - .bounds = Rectangle.{cast(float32)second_x, cast(float32)y, - cast(float32)button_width, cast(float32)button_height}, - .label = LocaleText("open_wallet_button"), - .tone = cast(ButtonTone)ButtonToneNeutral, - .emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft}) != 0 { + .id = id + 1, + .bounds = Rectangle.{cast(float32)second_x, cast(float32)y, + cast(float32)button_width, cast(float32)button_height}, + .label = LocaleText("open_wallet_button"), + .tone = cast(ButtonTone)ButtonToneNeutral, + .emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft}) != 0 { if !OpenUri(wallet_url) { - donation_notice = LocaleText("wallet_not_installed_toast") + donation_notice_key = "wallet_not_installed_toast" } } if stack { y += button_height + gap } third_x := ifx (stack) then (x) else (x + (button_width + gap) * 2) if Button(app.ui.session, ButtonProps.{ - .id = id + 2, - .bounds = Rectangle.{cast(float32)third_x, cast(float32)y, - cast(float32)button_width, cast(float32)button_height}, - .label = LocaleText("trocador_button"), - .tone = cast(ButtonTone)ButtonToneAccent, - .emphasis = cast(ButtonEmphasis)ButtonEmphasisFilled}) != 0 { + .id = id + 2, + .bounds = Rectangle.{cast(float32)third_x, cast(float32)y, + cast(float32)button_width, cast(float32)button_height}, + .label = LocaleText("trocador_button"), + .tone = cast(ButtonTone)ButtonToneAccent, + .emphasis = cast(ButtonEmphasis)ButtonEmphasisFilled}) != 0 { unused OpenUri(exchange_url) } } @@ -106,11 +108,11 @@ draw_about_donation_modal :: (app: *InnerBreeze) { if height > AppScale(620) { height = AppScale(620) } if height < AppScale(260) { height = AppScale(260) } frame := ModalFrame(app.ui.session, ModalFrameProps.{ - .key = KeyHash("donation/about"), - .title = LocaleText("donation_reminder_title"), - .width = width, .height = height, - .has_right_action = true, - .right_label = LocaleText("close_button")}) + .key = KeyHash("donation/about"), + .title = LocaleText("donation_reminder_title"), + .width = width, .height = height, + .has_right_action = true, + .right_label = LocaleText("close_button")}) if frame.dismissed || frame.right_clicked { app_close_modal(app) return @@ -129,7 +131,7 @@ draw_about_donation_modal :: (app: *InnerBreeze) { AppSmallFontSize(), AppScale(4)) coin_height := donation_coin_height(content_width) total_height := message_height + coin_height * 2 + - AppScale(50) + AppScale(16) + AppScale(50) + AppScale(16) scroll_height := content_height - AppScale(50) if scroll_height < 1 { scroll_height = 1 } scroll_key := KeyHash("donation/about/scroll") @@ -156,20 +158,20 @@ draw_about_donation_modal :: (app: *InnerBreeze) { x, row, content_width, 6111) unused End(app.ui.session) } - if donation_notice != "" { - donation_label(app, donation_notice, x, + if donation_notice_key != "" { + donation_label(app, LocaleText(donation_notice_key), x, y + content_height - AppScale(48), content_width - AppScale(110), AppScale(40)) } if Button(app.ui.session, ButtonProps.{ - .id = 6121, - .bounds = Rectangle.{cast(float32)(x + content_width - AppScale(100)), - cast(float32)(y + content_height - AppScale(44)), - cast(float32)AppScale(100), cast(float32)AppScale(40)}, - .label = LocaleText("close_button"), - .tone = cast(ButtonTone)ButtonToneNeutral, - .emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft}) != 0 { - donation_notice = "" + .id = 6121, + .bounds = Rectangle.{cast(float32)(x + content_width - AppScale(100)), + cast(float32)(y + content_height - AppScale(44)), + cast(float32)AppScale(100), cast(float32)AppScale(40)}, + .label = LocaleText("close_button"), + .tone = cast(ButtonTone)ButtonToneNeutral, + .emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft}) != 0 { + donation_notice_key = "" app_close_modal(app) } unused End(app.ui.session) diff --git a/src/app/locale.zi b/src/app/locale.zi index 5a4519c84..c49d276e6 100644 --- a/src/app/locale.zi +++ b/src/app/locale.zi @@ -27,7 +27,6 @@ active_catalog_asset: AssetView; locale_index_asset: AssetView; active_language: LocaleLanguage; preferred_languages: [64]LocaleLanguage; -locale_lookup: LocaleTextResult; english_catalog_ready: bool; active_catalog_ready: bool; active_catalog_language_index: s32; @@ -307,15 +306,15 @@ LocaleText :: (key: string) -> string { return LocaleResolveCatalog(active_catalog_asset.data, english_catalog_asset.data, key) } - locale_lookup = LocaleCatalogFind(active_catalog.entries[:], + lookup := LocaleCatalogFind(active_catalog.entries[:], active_catalog.slots[:], key) - if locale_lookup.found { - return locale_lookup.text + if lookup.found { + return lookup.text } - locale_lookup = LocaleCatalogFind(english_catalog.entries[:], + lookup = LocaleCatalogFind(english_catalog.entries[:], english_catalog.slots[:], key) - if locale_lookup.found { - return locale_lookup.text + if lookup.found { + return lookup.text } return key } @@ -339,7 +338,7 @@ FormatLocaleCount :: (out: *u8, out_size: s64, key: string, value: s32) { placeholder_at: s32 = -1 while index < cast(s32)translated.count { if translated[index] == #char "%" && index + 1 < cast(s32)translated.count && - translated[index + 1] == #char "d" { + translated[index + 1] == #char "d" { placeholder_at = index break } diff --git a/src/breaks/app_breaks.zi b/src/breaks/app_breaks.zi index 213600085..573097c8b 100644 --- a/src/breaks/app_breaks.zi +++ b/src/breaks/app_breaks.zi @@ -22,6 +22,7 @@ WEB :: #defined(PLATFORM_WEB) #import, file "break_calendar.zi"; #import, file "break_types.zi"; #import "date_time" +#import "std/vec" #import, file "../storage/break_stats.zi"; @@ -85,6 +86,17 @@ break_hud_session: Session; break_toast_state: ToastState; break_toast_request: string; +BreakToastMessage :: () -> string { + // Toast state outlives this frame and language catalogs can change while + // it is visible. Retain an owned value. + message: Vec(u8) + if !BuilderAppend(message, LocaleText("break_prelude_text")) { + VecFree(message) + return "" + } + return BuilderFinish(message) +} + #program_export app_breaks_draw_toast :: (app: *InnerBreeze, viewport: Rectangle) { if app == null || !SessionValid(app.ui.session) { return } @@ -493,7 +505,7 @@ app_breaks_update :: (app: *InnerBreeze) { // The activity sample only says whether input happened within the last // BreakActivityThresholdMs - it cannot vouch for a longer stall (occluded - // or hidden window, system suspend, a slow frame). Credit at most one + // or hidden window, system suspend, a slow frame). Credit at most one // second of the batch to the live sample; older catch-up ticks run as // idle so time away from the machine never counts toward the next break. active_ticks: s32 = 0 @@ -546,7 +558,7 @@ app_breaks_update :: (app: *InnerBreeze) { } if event == BreakEventPrompt { - break_toast_request = LocaleText("break_prelude_text") + break_toast_request = BreakToastMessage() break_play_cue(app, 0.8f) } else if event == BreakEventStart { break_engine_open_window(app, t) @@ -860,7 +872,7 @@ app_breaks_hud_draw :: (app: *InnerBreeze, hud_w: s32, hud_h: s32, row_h: s32, p }) } else if timer.state == BreakStateBreaking { // active break: count down the remaining rest time (this is the - // wait that resets the timer once it completes) + // wait that resets the timer once it completes) left: s32 = timer.duration_s - timer.idle_s if left < 0 { left = 0 diff --git a/src/screens/settings/settings_status.zi b/src/screens/settings/settings_status.zi index 5330833b5..f8f8cac30 100644 --- a/src/screens/settings/settings_status.zi +++ b/src/screens/settings/settings_status.zi @@ -1,4 +1,7 @@ -// Settings messages belong to Inbe. The screen reads this state when it draws. +#import "std/vec" + +// Settings messages own their bytes. Replacing a message preserves any older +// value a caller is still reading on native and portable targets. SettingsStatus :: struct { message: string detail: string @@ -7,41 +10,31 @@ SettingsStatus :: struct { current: SettingsStatus; StatusTextCapacity :: 4096 -StatusBytes :: struct { - message: [StatusTextCapacity]u8 - detail: [StatusTextCapacity]u8 -} -status_bytes: [2]StatusBytes; -status_slot: s32; - -StatusCopy :: (text: string, output: []u8) -> s64 { +StatusOwn :: (text: string) -> string { count := text.count - if count >= output.count { - count = output.count - 1 + if count >= StatusTextCapacity { + count = StatusTextCapacity - 1 // End before a partial UTF-8 sequence when bounding a long detail. while count > 0 && (text[count] & cast(u8)192) == cast(u8)128 { count -= 1 } } - index: s64 = 0 - while index < count { - output[index] = text[index] - index += 1 + bytes: Vec(u8) + if !BuilderAppend(bytes, text[0:count]) { + VecFree(bytes) + return "" } - output[count] = 0 - return count + return BuilderFinish(bytes) } StatusStore :: (message: string, detail: string, kind: s32) { - // Copy both inputs before publishing the next slot. Inputs may borrow - // temporary caller storage or the previous status, including swapped fields. - next := 1 - status_slot - message_length := StatusCopy(message, status_bytes[next].message[:]) - detail_length := StatusCopy(detail, status_bytes[next].detail[:]) - current.message = TextView(status_bytes[next].message[0:message_length]) - current.detail = TextView(status_bytes[next].detail[0:detail_length]) - current.kind = kind - status_slot = next + // Finish both copies before replacing current: either input may be a + // field of the previous status, including when the fields are swapped. + next: SettingsStatus + next.message = StatusOwn(message) + next.detail = StatusOwn(detail) + next.kind = kind + current = next } #program_export diff --git a/tests/locale_zi_test.sh b/tests/locale_zi_test.sh index 3496d8ac5..92ba12d0f 100644 --- a/tests/locale_zi_test.sh +++ b/tests/locale_zi_test.sh @@ -3,26 +3,28 @@ set -eu root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) bin=${1:-"$root/build/ziran-toolchain/bin"} +kryon=${KRYON_DIR:-"$root/build/packages/kryon"} +toolchain=${ZIRAN_DIR:-"$root/build/packages/ziran"} work=$root/build/locale-zi-test mkdir -p "$work/c" "$work/dropdown-c" "$bin/zi2zir" --check-only --root "$root/src" \ - --module-path "$root/build/packages/kryon/src/ui" \ - --module-path "$root/build/packages/ziran/std" "$root/src/app/locale.zi" + --module-path "$kryon/src/ui" \ + --module-path "$toolchain/std" "$root/src/app/locale.zi" "$bin/zi2zir" --check-only --root "$root/src" \ - --module-path "$root/build/packages/kryon/src/ui" \ - --module-path "$root/build/packages/ziran/std" \ + --module-path "$kryon/src/ui" \ + --module-path "$toolchain/std" \ "$root/src/app/locale_dropdown.zi" "$bin/zi2c" --no-main --root "$root/src" \ - --module-path "$root/build/packages/kryon/src/ui" \ - --module-path "$root/build/packages/ziran/std" \ + --module-path "$kryon/src/ui" \ + --module-path "$toolchain/std" \ -o "$work/dropdown-c" "$root/src/app/locale_dropdown.zi" -"${CC:-cc}" -std=c11 -I"$root/build/packages/ziran/include" \ +"${CC:-cc}" -std=c11 -I"$toolchain/include" \ -I"$work/dropdown-c" -c "$work/dropdown-c/app/locale_dropdown.c" \ -o "$work/locale_dropdown.o" "$bin/zi2c" --no-main --root "$root/tests" \ --module-path "$root/src" \ - --module-path "$root/build/packages/kryon/src/ui" \ - --module-path "$root/build/packages/ziran/std" \ + --module-path "$kryon/src/ui" \ + --module-path "$toolchain/std" \ -o "$work/c" "$root/tests/locale_link_behavior.zi" mkdir -p "$work/c/app" cp "$work/c/assets.h" "$work/c/app/assets.h" @@ -34,7 +36,7 @@ cat > "$work/c/main.c" <<'C' int main(void) { return Answer() == 42 ? 0 : 1; } C "${CC:-cc}" -std=c11 -DZIRAN_BOUNDS_CHECK -ffunction-sections \ - -fdata-sections -Wl,--gc-sections -I"$root/build/packages/ziran/include" \ + -fdata-sections -Wl,--gc-sections -I"$toolchain/include" \ -I"$work/c" -I"$root/src/app" \ "$work/assets.c" \ "$work/c"/*.c -o "$work/test" diff --git a/tests/settings_status_behavior.zi b/tests/settings_status_behavior.zi index 92d140677..169370c05 100644 --- a/tests/settings_status_behavior.zi +++ b/tests/settings_status_behavior.zi @@ -13,12 +13,14 @@ main :: () -> s32 { TransientStatus() status := settings_screen_status() if status.kind != 1 || status.message != "ok!" || status.detail != "path" { return 1 } + original := status settings_screen_set_status_success(status.detail, status.message) status = settings_screen_status() if status.message != "path" || status.detail != "ok!" { return 2 } settings_screen_set_status_error("失敗") status = settings_screen_status() if status.kind != 2 || status.message != "失敗" || status.detail != "" { return 3 } + if original.message != "ok!" || original.detail != "path" { return 6 } long_text: [4100]u8 index: s32 = 0 while index < 4094 { long_text[index] = 65; index += 1 } @@ -31,5 +33,6 @@ main :: () -> s32 { settings_screen_clear_status() status = settings_screen_status() if status.kind != 0 || status.message.count != 0 || status.detail.count != 0 { return 5 } + if original.message != "ok!" || original.detail != "path" { return 7 } return 0 } diff --git a/tests/settings_status_test.py b/tests/settings_status_test.py index 963b42d17..0ebc64dfa 100644 --- a/tests/settings_status_test.py +++ b/tests/settings_status_test.py @@ -22,4 +22,19 @@ with tempfile.TemporaryDirectory(prefix='settings-status-', dir=ROOT / 'build') subprocess.run([os.environ.get('CC', 'cc'), '-std=c11', '-Wall', '-Wextra', '-Werror', *includes, *sorted(generated.rglob('*.c')), '-o', work / 'test'], env=ENV, check=True) subprocess.run([work / 'test'], env=ENV, check=True) -print('Settings status ownership, replacement and UTF-8 bounds passed') + ir = work / 'ir' + subprocess.run([BIN / 'zi2zir', '--root', ROOT / 'tests', '-o', ir, + ROOT / 'tests/settings_status_behavior.zi'], env=ENV, check=True) + bundles = [] + for source, root in ((ROOT / 'tests/settings_status_behavior.zi', ROOT / 'tests'), + (ir / 'settings_status_behavior.zir', ir)): + bundle = work / ('source.zib' if source.suffix == '.zi' else 'saved.zib') + subprocess.run([BIN / 'zi2zib', 'bundle', '--root', root, + '--entry', 'settings_status_behavior:main', '-o', bundle, + source], env=ENV, check=True) + result = subprocess.run([BIN / 'zi2zib', 'run', bundle], env=ENV, + capture_output=True, text=True, check=True) + assert result.stdout.strip() == '0', (result.stdout, result.stderr) + bundles.append(bundle.read_bytes()) + assert bundles[0] == bundles[1] +print('Settings status ownership, replacement and UTF-8 bounds passed native and source/saved Zib')