// The screen of an app the node offers, such as Chess. The app draws itself // with hosted Kryon widgets; this screen supplies its frame, its text in the // person's language and the account's friends. #import "byte_text_linux" #import "text_buffer" #import "json_scan" #import "vec" #import "text" #import "session" #import "geometry" #import "text_props" #import "text_align" #import "text_widget" #import "bundle_host" #import "hosted_values" #import "widget_host" #import "widget_codec" #import "kryon/app_context_host" #import "layout_props" #import "loading_indicator" #import "progress_widget" #import "progress_props" #import "drawing_props" #import "box_widget" #import "box_props" #import "layout" #import "layout_widget" #import "tree" #import "tree_input" #import "test_driver" #import, file "../platform/raylib_text_input.zi"; #if #defined(PLATFORM_WEB) == false { #import "kryon/raylib_runtime" } #import, file "../apps/node_apps.zi"; #import, file "../apps/node_app_runner.zi"; #import, file "../apps/node_app_open.zi"; #import, file "../apps/node_app_tree.zi"; #import, file "../app/smooth_frames.zi"; #import, file "../apps/node_app_input.zi"; #import "apps/node_types" #import "apps/node_codec" #import, file "../apps/lists_network.zi"; #import, file "../apps/hosted_assets.zi"; #import, file "../apps/package_files.zi"; #import, file "../apps/package_manager.zi"; #import, file "../apps/catalog.zi"; #import, file "../apps/module_host.zi"; #import, file "../app/app_types.zi"; #import, file "../app/app.zi"; #import, file "../app/app_nav_state.zi"; #import, file "../app/app_launcher_state.zi"; #import, file "../app/app_modal_state.zi"; #import, file "../app/modal_rules.zi"; #import, file "../app/locale.zi"; #import, file "../app/metrics.zi"; #import, file "../app/text_buffers.zi"; #import, file "../core/types.zi"; #import, file "../storage/storage_settings.zi"; #import, file "../storage/db.zi"; #import, file "profile_social.zi"; #import, file "../apps/media_store.zi"; #import, file "../platform/environment.zi"; #import, file "../platform/android/android_import.zi"; #import "file_picker" #import "file_linux" #import "android_picker" #import, file "node_app_loading.zi"; using HostValueKind; using TextWrap; using TextAlign; using AndroidImportKind; c :: #system_library "c"; GetTime :: () -> float64 #foreign c; NodePressedKey :: () -> s32 #foreign c "GetKeyPressed"; NodeKeyRepeated :: (key: s32) -> bool #foreign c "IsKeyPressedRepeat"; // A held key repeats as the system repeats typing, so a game keeps moving // while an arrow is held; GetKeyPressed reports only the first press. NodeRepeatedKey :: () -> s32 { for key: cast(s32)32..348 { if NodeKeyRepeated(key) { return key } } return 0 } app_request_social_refresh :: (app: *InnerBreeze) #foreign c; node_screen_frame: AppUiFrame; node_screen_frame_codec: WidgetAppUiFrameCodec; node_screen_person_codec: WidgetAppPersonCodec; node_screen_friend_id: [80]u8; node_screen_friend_name: [80]u8; node_screen_alias: [80]u8; // The account's public ID, copied: settings reads share one buffer. node_screen_account: [80]u8; node_screen_hold_pictures: bool; NodeScreenFriends :: (app: *InnerBreeze) -> string { if app.profile_friends_loaded == 0 { load_friends_cache(app) } return AppTextFromCString(*app.profile_friends_json[0], 8192) } NodeScreenFriendCount :: (app: *InnerBreeze) -> s32 { json := NodeScreenFriends(app) count := ArrayCount(json, FieldAt(json, 0, "friends")) return ifx count < 0 then 0 else count } NodeScreenFriend :: (app: *InnerBreeze, index: s32) -> AppPerson { json := NodeScreenFriends(app) friends := FieldAt(json, 0, "friends") if index < 0 || index >= ArrayCount(json, friends) { return AppPerson.{} } at := ElementAt(json, friends, index) id := NodeAppJsonText(json, at, "user_id_hash", node_screen_friend_id[:]) alias := NodeAppJsonText(json, at, "alias", node_screen_alias[:]) return AppPerson.{.id = id, .name = NodeAppPersonName(alias, id, node_screen_friend_name[:])} } node_screen_input_text: [1024]u8; node_screen_input_codec: WidgetTextFieldInputCodec; NodeScreenUiCall :: (context: *void, module: *u8, function: *u8, arguments: *HostValue, count: s32, result: *HostValue) -> s32 { app := cast(*InnerBreeze)context name := TextFromCString(function) if AppModule(TextFromCString(module)) && name == "ClipboardCopyHost" && count == 2 && HostScalarValid(arguments[0], "string", cast(s32)HostString) { result.* = HostScalar("bool", ifx TextInputWriteClipboard(AppArgumentText(arguments[0])) then 1 else 0) return 1 } if name == "ReadAppUiHost" && count == 0 { result.* = WidgetEncodeAppUiFrame(node_screen_frame, *node_screen_frame_codec) return 1 } if name == "AppKeyHost" && count == 0 { key := NodeAppKeyFor(NodePressedKey(), NodeRepeatedKey()) if key == 0 && TestDriverActive() { for candidate: cast(s32)32..348 { if TestDriverKeyPressed(candidate) { key = candidate break } } } if key == 0 { key = KeyboardTake(app.ui.session) } result.* = HostScalar("s32", key) return 1 } if name == "CloseAppHost" && count == 0 { app_block_current_click(app) app_launcher_open(app) result.* = HostScalar("bool", 1) return 1 } if name == "AppFieldInputHost" && count == 0 { input := SampleTextFieldInput(true, node_screen_input_text[:]) result.* = WidgetEncodeTextFieldInput(input, *node_screen_input_codec) return 1 } if name == "AppLocaleHost" && count == 1 && HostScalarValid(arguments[0], "string", cast(s32)HostString) { key := NodeAppText(arguments[0]) translated := NodeAppLocaleText(app.node_app_slot, AppTextFromCString(*app.language[0], 16), key) result.* = NodeAppString(ifx translated != "" then translated else LocaleText(key)) return 1 } if name == "AppFriendHost" && count == 1 && HostScalarValid(arguments[0], "s32", cast(s32)HostInteger) { person := NodeScreenFriend(app, cast(s32)arguments[0].integer) result.* = WidgetEncodeAppPerson(person, *node_screen_person_codec) return 1 } return WidgetHostCall(context, module, function, arguments, count, result) } // The platform picker a granted app asked for (MediaPickHost): the // desktop's file dialog, or Android's photo picker, whose answer arrives on // a later frame. Each picked file is added to the library once. node_screen_media_waiting: bool; NodeScreenMediaAdd :: (path: string) { if path.count == 0 { NodeAppMediaPicked(MediaImport.{}, true) return } NodeAppMediaPicked(MediaImportFile(path), true) } NodeScreenMediaPicker :: (app: *InnerBreeze) { if node_screen_media_waiting { path: [4096]u8 result := AndroidPickerPoll(cast(s32)ANDROID_IMPORT_KIND_MEDIA, path[:]) if result == .AndroidPickerNone { return } node_screen_media_waiting = false app.file_dialog_active = 0 if result == .AndroidPickerSelected { selected := AppTextFromCString(*path[0], cast(s32)path.count) NodeScreenMediaAdd(selected) // The picker's private copy; the library keeps its own file. if selected.count > 0 { unused RemoveFile(selected) } } else { NodeAppMediaPicked(MediaImport.{}, false) } return } if !node_media_pick_requested { return } node_media_pick_requested = false app.file_dialog_active = 1 if AndroidPlatform() != 0 { node_screen_media_waiting = AndroidPickerOpen(cast(s32)ANDROID_IMPORT_KIND_MEDIA, "image/*") if !node_screen_media_waiting { app.file_dialog_active = 0 NodeAppMediaPicked(MediaImport.{}, true) } return } path: [4096]u8 selected := PickFile(FilePickerProps.{.title = LocaleText("photos_add"), .filter = "Images | *.png *.jpg *.jpeg *.webp *.gif"}, path[:]) app.file_dialog_active = 0 if selected == 1 { NodeScreenMediaAdd(AppTextFromCString(*path[0], cast(s32)path.count)) } else { NodeAppMediaPicked(MediaImport.{}, selected < 0) } } // Opening the app reads friends and shared records again. An installed app // that is not running starts now, or downloads again when it cannot. #program_export node_app_screen_enter :: (app: *InnerBreeze) { if app == null { return } name := NodeAppName(app.node_app_slot) if name.count > 0 && !NodeAppRunning(app.node_app_slot) { unused NodeAppInstall(name) } if name.count > 0 && !NodeAppLocal(name) { index := NodeAppIndex(app.node_app_slot) if !PackagePending(index) { if PackageAutomaticEnabled() { PackageRequest(index) } else { PackageCheckForUpdate(index) } } } load_friends_cache(app) app_request_social_refresh(app) lists_refresh_at = 0.0 } // Route changes ask this before showing an app: only one on this device // opens, never one still downloading. #program_export node_app_screen_opens :: (slot: s32) -> bool { return NodeAppInstalled(slot) } node_screen_loading: NodeScreenLoadingState; NodeScreenIdentity :: (slot: s32) -> u64 { identity := (cast(u64)(slot + 1) << cast(u64)48) runtime := NodeTreeRuntime(slot) if runtime != null && runtime.node != null { identity = identity ^ KeyHash(TextUntilNul(runtime.node.identity.id[:])) identity = identity ^ KeyHash(TextUntilNul(runtime.node.entry.sha256[:])) } return identity } // Count only this app's requested pictures. Images queued by a background // app cannot hold the visible app behind its loading cover. NodeScreenPictures :: struct { ready: s32; total: s32 } NodeScreenPictureProgress :: (slot: s32) -> NodeScreenPictures { result: NodeScreenPictures #if #defined(PLATFORM_WEB) == false { runtime := NodeTreeRuntime(slot) if runtime == null || runtime.node == null { return result } digest := TextUntilNul(runtime.node.entry.sha256[:]) for image: node_tree_images { if TextUntilNul(image.digest[:]) != digest { continue } result.total += 1 if ImageWidth(image.path) >= 0 { result.ready += 1 } } } return result } NodeScreenProgress :: (app: *InnerBreeze, area: Rectangle, title: string, stage: string, done: s32, total: s32) { scale := MetricsScale() width := area.width - 48.0 * scale if width > 320.0 * scale { width = 320.0 * scale } left := area.x + (area.width - width) / 2.0 middle := area.y + area.height / 2.0 Text(app.ui.session, TextProps.{.key = KeyHash("inbe/node-app/loading-title"), .text = title, .font = AppFontSize(), .bounds = Rectangle.{left, middle - 56.0 * scale, width, 28.0 * scale}, .align = cast(TextAlign)TextAlignCenter}) Text(app.ui.session, TextProps.{.key = KeyHash("inbe/node-app/loading-stage"), .text = stage, .font = AppFontSize(), .bounds = Rectangle.{left, middle - 24.0 * scale, width, 28.0 * scale}, .align = cast(TextAlign)TextAlignCenter}) counts: Vec(u8) BuilderPrint(*counts, "% / %", done, total) Progress(app.ui.session, ProgressProps.{.key = KeyHash("inbe/node-app/loading-progress"), .bounds = Rectangle.{left, middle + 16.0 * scale, width, 24.0 * scale}, .min = 0, .max = total, .value = done, .label = TextView(VecSlice(counts, 0, counts.count))}) VecFree(counts) LoadingIndicator(app.ui.session, LoadingIndicatorProps.{.key = KeyHash("inbe/node-app/loading-animation"), .bounds = Rectangle.{left, middle + 56.0 * scale, width, 24.0 * scale}, .now = GetTime(), .scale = scale}) AppRequestSmoothFrames(2) } NodeScreenCover :: (app: *InnerBreeze, area: Rectangle, pictures: NodeScreenPictures) { Box(app.ui.session, BoxProps.{.key = KeyHash("inbe/node-app/cover"), .bounds = area, .fill = StyleTokenColor("canvas")}) NodeScreenProgress(app, area, NodeAppName(app.node_app_slot), LocaleText("app_loading_pictures"), pictures.ready, ifx pictures.total > 0 then pictures.total else 1) } NodeScreenLoading :: (app: *InnerBreeze) { area := Rectangle.{0.0, 0.0, cast(float32)app.ui.view_width, cast(float32)(app.ui.view_height - app_content_bottom_reserved(app))} slot := AppScreenSlot(app) if slot >= 0 { done := AppsFeaturesReadyCount() total := cast(s32)PackageCount - 1 NodeScreenProgress(app, area, LocaleText("app_loading_apps"), LocaleText("app_loading_open"), done, total) return } step := NodeOpenStep(app.node_app_slot) stages: [4]string = .["app_loading_wait", "app_loading_read", "app_loading_open", "app_loading_ready"] NodeScreenProgress(app, area, NodeAppName(app.node_app_slot), LocaleText(stages[step]), step, 3) } // The bundled app that draws the current screen, by module slot, or -1 for // a screen Breeze draws itself. AppScreenSlot :: (app: *InnerBreeze) -> s32 { screen := app.breathing.screen if screen == ScreenEList { return 0 } if screen == ScreenHabits || screen == ScreenHabitEdit || screen == ScreenHabitSessionEdit { return 1 } if screen == ScreenStart || screen == ScreenResults { return 2 } if screen == ScreenDiary { return 4 } if screen == ScreenLumi { return 5 } if screen == ScreenLeaderboard { return 6 } return -1 } // A screen whose app is still opening after the start shows Kryon's loading // indicator in its place, so the first frames never wait for the program. AppScreenOpening :: (app: *InnerBreeze) -> bool { slot := AppScreenSlot(app) return slot >= 0 && !AppsFeatureOpenNow(slot) } #program_export node_app_screen_draw :: (app: *InnerBreeze) { node_screen_hold_pictures = false if app == null { return } slot := app.node_app_slot name := NodeAppName(slot) if name.count == 0 { return } if !NodeAppRunning(slot) { if NodeAppInstalled(slot) { unused NodeAppOpen(slot) } if NodeAppOpenFailed(slot) && app.modal.active == 0 && !app.launcher_open { app_open_modal(app, ModalType.ModalNodeAppFailure) } NodeScreenLoading(app) return } node_screen_frame = AppUiFrame.{.session = app.ui.session, .width = app.ui.view_width, .height = app.ui.view_height, .bottom = app_content_bottom_reserved(app), .scale = MetricsScale(), .font = AppFontSize(), .now = GetTime(), .dark = app.dark_mode != 0} unused AppCopyText(storage_get_setting_text("sync_public_id"), node_screen_account[:]) message := AppMessage.{.action = APP_FRAME, .screen = ScreenNodeApp, .name = name, .locale = AppTextFromCString(*app.language[0], 16), .account = TextUntilNul(node_screen_account[:]), .signed_in = ListsNetworkReady(), .friend_count = NodeScreenFriendCount(app), .shared_count = NodeAppSharedCount(name), .invitation_count = NodeAppInvitationCount(name), .shared_revision = ListsSharedRevision(), .shared_status = lists_status, .time = now_seconds()} typed: [1024]u8 if NodeAppPortable(slot) || node_runners[slot].nested != null { input := SampleTextFieldInput(true, typed[:]) key := NodeAppKeyFor(NodePressedKey(), NodeRepeatedKey()) // Native and IME commands are sampled with the field input. Keep // them even when the raw key queue has already been drained. if input.commit { key = TextInputKeyEnter } else if input.backspace { key = TextInputKeyBackspace } else if input.escape { key = TextInputKeyEscape } modifiers: s32 = 0 if input.shift { modifiers |= 1 } if TextInputKeyDown(TextInputKeyLeftControl) || TextInputKeyDown(TextInputKeyRightControl) { modifiers |= 2 } if node_runners[slot].nested != null { NodeTreeFrame(slot, node_screen_frame, key, modifiers, input) } else { NodeAppPortableFrame(slot, app.ui.session, Rectangle.{0.0, 0.0, cast(float32)app.ui.view_width, cast(float32)(app.ui.view_height - app_content_bottom_reserved(app))}, app.dark_mode != 0, MetricsScale(), AppFontSize(), key, modifiers, input) } } if node_runners[slot].nested != null { WidgetHostBegin(app.ui.session, NodeTreeAssetAllowed, NodeTreeAssetPath) } else { WidgetHostBegin(app.ui.session, HostedAssetAllowed, WidgetHostPackedPath) } // The app draws only inside its area: covers sliding past its edge // would otherwise paint over the sidebar's negative content x. area := Rectangle.{0.0, 0.0, cast(float32)app.ui.view_width, cast(float32)(app.ui.view_height - app_content_bottom_reserved(app))} clip := Group(app.ui.session, ColumnProps.{.bounds = area, .key = KeyHash("inbe/node-app-clip")}) if clip.node >= 0 { TreeSetChildClip(app.ui.session, clip.node, area) } NodeAppUiBegin(NodeScreenUiCall, cast(*void)app) unused NodeAppRunMessage(slot, message) NodeAppUiEnd() identity := NodeScreenIdentity(slot) pictures := NodeScreenPictureProgress(slot) runtime := NodeTreeRuntime(slot) node_screen_hold_pictures = runtime != null && runtime.node != null && !runtime.node.collection && NodeScreenHoldPictures(*node_screen_loading, identity, pictures.ready >= pictures.total) if NodeScreenNeedsCover(*node_screen_loading, identity, pictures.ready >= pictures.total) { NodeScreenCover(app, area, pictures) } if clip.opened { unused End(app.ui.session) } WidgetHostEnd() if NodeTreeTakeProfile() { app_open_profile_tab(app, PROFILE_TAB_OVERVIEW) } NodeScreenMediaPicker(app) }