Files
breeze/src/app/application.zi
T

1849 lines
68 KiB
Ziran

#import, file "../storage/dew.zi";
#import, file "../platform/dew_payment.zi";
#import, file "../storage/practice_unlocks.zi";
#import, file "../cells/shared_state.zi";
#import, file "../cells/lumi_online.zi";
#import, file "../cells/module_host.zi";
c :: #system_library "c";
raylib_log :: #system_library "raylib";
LOG_INFO :: 3
LOG_WARNING :: 4
LOG_ERROR :: 5
#import, file "../platform/trace_log.zi";
#import "raylib_trace"
GetTime :: () -> float64 #foreign c;
PackageNow :: () -> s64 #foreign c "now_seconds";
#import, file "modal_rules.zi";
#import, file "../practices/tai_chi/tai_chi_practice.zi";
#import, file "../practices/tai_chi/tai_chi_rules.zi";
#import, file "../practices/sun_salutation/sun_salutation_rules.zi";
#import, file "../../apps/practice/downloads_view.zi";
#import, file "../habits/habit_types.zi";
#import, file "../core/types.zi";
#import, file "app_types.zi";
#import, file "route_types.zi";
#import, file "exercise_types.zi";
#import, file "../platform/web_storage.zi";
#import, file "../platform/environment.zi";
#import "file_download"
#import "android_device"
#import "text_context_menu"
#import, file "../platform/android/android_activity.zi";
#import, file "../platform/android/graphics_context.zi";
#if #defined(PLATFORM_WEB) {
#import "kryon/canvas_host"
} else {
#import "kryon/raylib_runtime"
}
#if #defined(DESKTOP_TRAY_ENABLED) {
#import, file "../platform/desktop_tray.zi";
}
#import "open_uri"
#import "text_align"
#import "text_props"
#import "image_props"
#import "control_props"
#import "layout"
#import, file "app.zi";
#import, file "fonts.zi";
#import, file "../core/breath_timing.zi";
#import, file "manual_seen.zi";
#import, file "manual_seen_actions.zi";
#import, file "navigation_routes.zi";
#import, file "app_nav_state.zi";
#import, file "../widgets/bottom_nav.zi";
#import, file "profile_image_picker.zi";
#import, file "../breaks/break_rules.zi";
#import, file "../breaks/break_types.zi";
#import, file "../screens/elist_types.zi";
#import, file "../habits/habit_model.zi";
#import, file "../habits/habit_sessions.zi";
#import, file "../storage/data.zi";
#import, file "audio.zi";
#import, file "lumi_guide.zi";
#import, file "screen_guides.zi";
#import, file "app_nav.zi";
#import, file "app_launcher.zi";
#import, file "app_launcher_state.zi";
#import, file "return_bar.zi";
#import, file "session_window.zi";
#import, file "session_window_view.zi";
#import, file "../platform/practice_window_host.zi";
#import, file "../platform/android/android_picture_window.zi";
#import, file "account_activity.zi";
#import, file "account_activity_view.zi";
#import "tree_input"
#import, file "../screens/habits/edit.zi";
#import, file "../screens/habits_screen.zi";
#import, file "../screens/elist_screen.zi";
#import, file "../screens/settings/settings_status.zi";
#import, file "app_sync.zi";
#import, file "../cells/lists_network.zi";
#import, file "sync_safety.zi";
#import, file "telegram_account_flow.zi";
#import, file "../platform/telegram_bridge.zi";
#import, file "../storage/delegate_store.zi";
#import, file "app_route_state.zi";
#import, file "app_route_url.zi";
#import, file "app_profile.zi";
#import, file "app_frame_pacing.zi";
#import, file "app_frame_transition.zi";
#import, file "app_persistence.zi";
#import, file "app_modal_state.zi";
#import, file "app_chrome.zi";
#import, file "mini_mode.zi";
#import, file "mini_mode_expand.zi";
#import, file "app_settings.zi";
#import, file "language_selection.zi";
#import, file "../habits/view_settings.zi";
#import, file "locale.zi";
#import, file "update_check.zi";
#import, file "text_buffers.zi";
#import "c_string"
#import, file "app_notifications.zi";
#import, file "../screens/language_screen.zi";
#import, file "../screens/app_selection_screen.zi";
#import, file "../screens/diary_screen.zi";
#import, file "../screens/lumi_screen.zi";
#import, file "../screens/leaderboard_screen.zi";
#import, file "../cells/package_manager.zi";
#import, file "../practices/practice_downloads.zi";
#import, file "assets.zi";
#import, file "../cells/catalog.zi";
#import, file "../cells/inbe_types.zi";
#import, file "app_style.zi";
#import, file "../screens/manual_screen.zi";
#import, file "../screens/manual_dispatch.zi";
#import, file "../screens/break_overlay.zi";
#import, file "../breaks/app_breaks.zi";
#import, file "../screens/profile_screen.zi";
#import, file "../screens/profile_dew.zi";
#import, file "../screens/profile_dew_gift.zi";
#import, file "../screens/profile_social.zi";
#import, file "../screens/settings/settings_screen.zi";
#import, file "../screens/settings/settings_audio.zi";
#import, file "../screens/settings/settings_data_ui.zi";
#import, file "../screens/settings/settings_sync_account_impl.zi";
#import, file "../screens/settings/settings_theme.zi";
#import, file "../screens/practice_screen.zi";
#import, file "../practices/practice_registry.zi";
#import, file "../screens/session_results_screen.zi";
#import, file "../screens/habits/habit_session_screen.zi";
#import, file "device_preferences.zi";
#import, file "../practices/meditation/meditation_music.zi";
#import, file "metrics.zi";
#import, file "dpi_state.zi";
#import, file "draw.zi";
#import, file "direct_draw_plan.zi";
#import, file "current_app.zi";
#import "button_widget"
#import "image_widget"
#import "text_widget"
#import "drawing_props"
#import "geometry"
#import "session"
#import "button_props"
#import "modal_props"
#import "modal_widget"
#import "toast_props"
#import "tree_draw"
#import "tree"
#import "frame_pacing"
using SessionExitModalResult;
using AppClosePromptResult;
using NavigationMode;
using NavigationPlacement;
using ModalType;
using AppMainTab;
using ExerciseType;
using HabitSyncMode;
using TextAlign;
using TextWrap;
using ImageFit;
using ButtonTone;
using ButtonEmphasis;
KEY_Q :: 81;
KEY_LEFT_CONTROL :: 341;
KEY_RIGHT_CONTROL :: 345;
KEY_BACKSPACE :: 259;
APP_KEY_BACK :: 4;
APP_KEY_ESCAPE :: 256;
IsKeyPressed :: (key: s32) -> bool #foreign raylib_log "IsKeyPressed";
IsKeyDown :: (key: s32) -> bool #foreign raylib_log "IsKeyDown";
BeginMode2D :: (camera: Camera) #foreign raylib_log "BeginMode2D";
EndMode2D :: () #foreign raylib_log "EndMode2D";
BeginScissorMode :: (x: s32, y: s32, width: s32, height: s32) #foreign raylib_log "BeginScissorMode";
EndScissorMode :: () #foreign raylib_log "EndScissorMode";
#if #defined(PLATFORM_WEB) {
}
#if #defined(ANDROID_BUILD) {
#import, file "../platform/android/android_wakelock.zi";
#import, file "../platform/android/android_insets.zi";
android_network_pump_begin :: () -> bool #foreign c;
android_network_pump_end :: () #foreign c;
}
#if #defined(PLATFORM_WEB) || #defined(ANDROID_BUILD) {
APP_DEFAULT_WIDTH :: 320
APP_DEFAULT_HEIGHT :: 560
} else {
APP_DEFAULT_WIDTH :: 900
APP_DEFAULT_HEIGHT :: 720
}
config: AppConfig = AppConfig.{.width = APP_DEFAULT_WIDTH, .height = APP_DEFAULT_HEIGHT};
view_width: s32 = APP_DEFAULT_WIDTH;
view_height: s32 = APP_DEFAULT_HEIGHT;
app_full_view_width: s32 = APP_DEFAULT_WIDTH;
// Theme colors are now accessed via theme accessor functions
#program_export
app_leave_practice_config :: (app: *InnerBreeze) {
if app == null {
return
}
if app.settings_dirty != 0 {
save_settings(app)
}
{
practice: *PracticeDefinition = practice_get(app.exercise_type)
if practice.leave_config != null {
practice.leave_config(app)
}
}
app.settings_scroll = 0
}
#program_export
app_request_desktop_close :: (app: *InnerBreeze) {
if app == null {
return
}
// The mini window has no tray or close prompt.
if AppMiniMode() {
app.request_quit = 1
return
}
// Configured close behaviour overrides the ask prompt (desktop only).
if app.desktop_close_action == CLOSE_QUIT {
app.request_quit = 1
return
}
#if #defined(DESKTOP_TRAY_ENABLED) {
if desktop_tray_ready() == 0 {
app.request_quit = 1
return
}
}
if app.desktop_close_action == CLOSE_KEEP_RUNNING {
#if #defined(DESKTOP_TRAY_ENABLED) {
if desktop_tray_keep_running() == 0 {
app.request_quit = 1
}
} else {
app.request_quit = 1
}
return
}
app.close_prompt_open = 1
app_block_current_click(app)
app.close_prompt_result = cast(AppClosePromptResult)AppClosePromptNone
}
//
// Desktop quit request from an explicit command such as Ctrl+Q. Window-close
// requests may still ask whether to keep running, but a quit command should
// always terminate the app.
//
#program_export
app_request_desktop_quit :: (app: *InnerBreeze) {
if app == null {
return
}
app.request_quit = 1
}
#program_export
app_consume_close_prompt_result :: (app: *InnerBreeze) -> AppClosePromptResult {
result: AppClosePromptResult
if app == null {
return cast(AppClosePromptResult)AppClosePromptNone
}
result = app.close_prompt_result
app.close_prompt_result = cast(AppClosePromptResult)AppClosePromptNone
return result
}
#program_export
app_draw_session_exit_modal :: (app: *InnerBreeze, can_save: s32, save_message: string, discard_message: string) -> SessionExitModalResult {
modal_result: s32
if can_save != 0 {
actions: [3] ActionModalAction = .[
ActionModalAction.{.label = LocaleText("cancel_button"), .tone = cast(ButtonTone)ButtonToneNeutral, .emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft},
ActionModalAction.{.label = LocaleText("save_button"), .tone = cast(ButtonTone)ButtonToneAccent, .emphasis = cast(ButtonEmphasis)ButtonEmphasisFilled},
ActionModalAction.{.label = LocaleText("discard_button"), .tone = cast(ButtonTone)ButtonToneDanger, .emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft},
]
modal_result = ActionModal(app.ui.session, ActionModalProps.{.title = LocaleText("exit_session_title"), .message = save_message}, actions[:3]).action
if modal_result == 2 {
return cast(SessionExitModalResult)SessionExitModalSave
}
if modal_result == 3 {
return cast(SessionExitModalResult)SessionExitModalDiscard
}
} else {
actions: [2] ActionModalAction = .[
ActionModalAction.{.label = LocaleText("cancel_button"), .tone = cast(ButtonTone)ButtonToneNeutral, .emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft},
ActionModalAction.{.label = LocaleText("exit_button"), .tone = cast(ButtonTone)ButtonToneDanger, .emphasis = cast(ButtonEmphasis)ButtonEmphasisFilled},
]
modal_result = ActionModal(app.ui.session, ActionModalProps.{.title = LocaleText("exit_session_title"), .message = discard_message}, actions[:2]).action
if modal_result == 2 {
return cast(SessionExitModalResult)SessionExitModalDiscard
}
}
return ifx (modal_result == 1) then (cast(SessionExitModalResult)SessionExitModalCancel) else (cast(SessionExitModalResult)SessionExitModalNone)
}
app_draw_close_prompt :: (app: *InnerBreeze) {
modal_result: s32
actions: [2] ActionModalAction = .[ActionModalAction.{}, ActionModalAction.{}]
props: ActionModalProps = .{}
if app == null || app.close_prompt_open == 0 {
return
}
if app.blocked_input_frame == app.breathing.frame {
return
}
actions[0].label = LocaleText("desktop_close_keep_running_button")
actions[0].tone = cast(ButtonTone)ButtonToneAccent
actions[0].emphasis = cast(ButtonEmphasis)ButtonEmphasisFilled
actions[1].label = LocaleText("desktop_close_quit_button")
actions[1].tone = cast(ButtonTone)ButtonToneDanger
actions[1].emphasis = cast(ButtonEmphasis)ButtonEmphasisFilled
props.key = KeyHash("inbe/desktop-close")
props.title = LocaleText("desktop_close_prompt_title")
props.message = LocaleText("desktop_close_prompt_message")
props.max_width = 420
modal_result = ActionModal(app.ui.session, props, actions[:2]).action
if modal_result == {
case -1;
app.close_prompt_open = 0
case 1;
app.close_prompt_open = 0
app.close_prompt_result = cast(AppClosePromptResult)AppClosePromptKeepRunning
case 2;
app.close_prompt_open = 0
app.close_prompt_result = cast(AppClosePromptResult)AppClosePromptQuit
app_request_desktop_quit(app)
}
}
app_reload_graphics_resources :: (app: *InnerBreeze) -> bool {
if app == null {
return false
}
context_lost: bool = false
#if #defined(ANDROID_BUILD) {
context := AndroidGraphicsContext()
if context == null {
// EndDrawing continues polling Android events until the surface
// is rebound. Do not populate any GPU caches in the meantime.
return false
}
context_lost = app.graphics_context != null &&
app.graphics_context != context
app.graphics_context = context
}
if app.graphics_reload_requested == 0 && !context_lost {
return true
}
app.graphics_reload_requested = 0
if PreviewMode() != 0 {
return true
}
TraceLog(LOG_INFO, "ANDROID: Reloading graphics resources")
#if #defined(ANDROID_BUILD) {
ResetRaylibGraphics(context_lost)
// Retained frames are gone even when the context survived. A route
// fade must not keep a borrowed texture from the discarded frame.
app_route_transition_cancel(app, context_lost)
app.ui.retained_target_active = false
app.ui.retained_frame_unchanged = false
}
if !LoadLocaleFont(app) {
TraceLog(LOG_WARNING, "FONT: Failed to reload Noto UI font")
}
return true
}
#program_export
refresh_locale_dependent_text :: (app: *InnerBreeze) {
if app == null {
return
}
if config.title_custom == 0 {
CopyCString(LocaleText("app_title"), config.title[:])
}
manual_screen_reset_layouts(app)
app.language_index = LocaleActiveIndex()
if app.language_index < 0 {
app.language_index = 0
}
}
load_config :: () {
if config.loaded != 0 {
return
}
if config.title[0] == #char "\0" {
AppCopyText(LocaleText("app_title"), config.title[:])
config.title_custom = 0
}
#if #defined(PLATFORM_WEB) {
refresh_theme_colors(0, 1)
} else {
refresh_theme_colors(0, 0)
}
config.loaded = 1
}
#program_export
clampi :: (value: s32, min: s32, max: s32) -> s32 {
if value < min {
return min
}
if value > max {
return max
}
return value
}
#program_export
app_reload_after_import :: (app: *InnerBreeze, reload_settings: s32) {
detail_habit_id: [HABIT_ID_SIZE] u8 = .[0]
detail_index: s32 = -1
view_mode: s32 = HABIT_VIEW_CALENDAR
habit_tab: s32 = HABIT_TAB_WEEKLY
month_offset: s32 = 0
scroll: s32 = 0
weekly_days: s32 = 0
hold_stats_range_days: s32 = 0
i: s32
if app == null {
return
}
if reload_settings == 0 {
app.app_choices_pending = true
if app.habit_detail_index >= 0 && app.habit_detail_index < app.habits.count {
AppCopyText(AppTextFromCString(
*app.habits.items[app.habit_detail_index].id[0],
HABIT_ID_SIZE), detail_habit_id[:])
}
detail_index = app.habit_detail_index
view_mode = app.habits.view_mode
habit_tab = app.habits.tab
month_offset = app.habits.month_offset
scroll = app.habits.scroll
weekly_days = app.habits.weekly_days
hold_stats_range_days = app.habits.hold_stats_range_days
}
if reload_settings != 0 {
settings_missing := app_load_settings(app)
refresh_locale_dependent_text(app)
app_device_preferences_init(app)
if settings_missing != 0 {
save_settings(app)
}
app_apply_style(app)
reset_settings_preview(app)
practice_update_session_sounds(app)
}
if !TelegramAccountFlowOwnsContext() {
habits_init_with_defaults(*app.habits, app.language_selected)
}
if reload_settings != 0 {
app_restore_habits_view_settings(app)
}
if reload_settings == 0 {
app.habits.selected = -1
app.habit_detail_index = -1
{
i: s32 = 0
while i < app.habits.count {
if detail_habit_id[0] != cast(u8)0 &&
AppTextFromCString(*app.habits.items[i].id[0], HABIT_ID_SIZE) ==
AppTextFromCString(*detail_habit_id[0], HABIT_ID_SIZE) {
app.habit_detail_index = i
break
}
i += 1
}
}
if app.habit_detail_index < 0 && detail_index >= 0 && detail_index < app.habits.count {
app.habit_detail_index = detail_index
}
app.habits.view_mode = view_mode
app.habits.tab = habit_tab
app.habits.month_offset = month_offset
app.habits.scroll = scroll
app.habits.weekly_days = weekly_days
app.habits.hold_stats_range_days = ifx (hold_stats_range_days == 31) then (31) else (7)
}
if app.habit_detail_index >= app.habits.count {
app.habit_detail_index = -1
}
if app.habits.selected >= app.habits.count {
app.habits.selected = -1
}
app.habit_session_edit.active = 0
app.habit_edit.active = 0
}
#if #defined(ANDROID_BUILD) {
#program_export
app_request_graphics_reload :: (app: *InnerBreeze) {
if app != null {
app.graphics_reload_requested = 1
}
}
}
app_draw_blank_home_easteregg :: (app: *InnerBreeze) {
if app == null {
return
}
available_h := view_height - app_content_bottom_reserved(app)
if available_h < AppScale(120) {
available_h = view_height
}
Image(CurrentApp().ui.session, ImageProps.{
.asset_path = "assets/easteregg/art.png",
.bounds = Rectangle.{0, 0, cast(float32)view_width, cast(float32)available_h},
.fit = cast(ImageFit)ImageFitCover
})
logo_size := cast(float32)view_width * 0.58f
if logo_size > cast(float32)available_h * 0.58f {
logo_size = cast(float32)available_h * 0.58f
}
if logo_size > cast(float32)AppScale(320) {
logo_size = cast(float32)AppScale(320)
}
bounds := Rectangle.{(cast(float32)view_width - logo_size) / 2,
(cast(float32)available_h - logo_size) / 2, logo_size, logo_size
}
if Button(CurrentApp().ui.session, ButtonProps.{
.bounds = bounds,
.image = ImageProps.{
.asset_path = "assets/easteregg/waozi.png",
.bounds = bounds, .fit = cast(ImageFit)ImageFitContain
},
.tone = cast(ButtonTone)ButtonToneNeutral, .emphasis = cast(ButtonEmphasis)ButtonEmphasisGhost
}) != 0 {
OpenUri("https://waozi.xyz")
}
}
app_apply_initial_screen :: (app: *InnerBreeze) {
if app == null {
return
}
// Mini mode uses the root's temporary Practice selection and skips the
// full app's onboarding without bypassing its route policy.
if AppMiniMode() {
app.main_tab = APP_MAIN_TAB_PRACTICE
}
initial := InbeRun(InbeMessage.{.action = INBE_INITIAL_SCREEN,
.enabled_apps = ifx AppMiniMode() then APP_PRACTICE else app.enabled_apps,
.language_selected = AppMiniMode() || app.language_selected != 0,
.apps_selected = AppMiniMode() || app.apps_selected != 0,
.screen = app_screen_for_main_tab(app.main_tab)})
if !initial.succeeded {
app.request_quit = 1
return
}
app.breathing.screen = initial.screen
if initial.screen == ScreenAppSelection && !app.launcher_open {
app_launcher_toggle(app)
}
app.habits.focus_selected_tab = ifx (app.breathing.screen == ScreenHabits) then (1) else (0)
}
#program_export
app_mini_restore_selection :: (app: *InnerBreeze) {
if app == null {
return
}
// An active mini practice continues when the full window opens, while
// the stored selection still controls full-app navigation and saving.
active := practice_active(app) != null
mask := app.enabled_apps
if active {
mask |= APP_PRACTICE
}
if !CellsSetEnabled(mask) {
app.request_quit = 1
return
}
if !active {
app_apply_initial_screen(app)
}
}
// Host feature IDs use the app-selection bits; zero restores the parent route.
#program_export
app_open_feature :: (vapp: *void, feature: s32) -> s32 {
app := cast(*InnerBreeze)vapp
if app == null || !CellsReady() || AppMiniMode() {
return 0
}
before := app_current_route(app)
if feature == 0 {
app_apply_initial_screen(app)
app_observe_direct_route_change(app, before)
return cast(s32)(app.request_quit == 0)
}
bit: s32 = 0
tab: s32 = APP_MAIN_TAB_NONE
if feature == APP_PRACTICE {
bit = APP_PRACTICE
tab = APP_MAIN_TAB_PRACTICE
} else if feature == APP_HABITS {
bit = APP_HABITS
tab = APP_MAIN_TAB_HABITS
} else if feature == APP_LISTS {
bit = APP_LISTS
tab = APP_MAIN_TAB_ELIST
} else if feature == APP_LUMI {
bit = APP_LUMI
tab = APP_MAIN_TAB_LUMI
} else if feature == APP_DIARY {
bit = APP_DIARY
tab = APP_MAIN_TAB_DIARY
} else if feature == APP_LEADERBOARD {
bit = APP_LEADERBOARD
tab = APP_MAIN_TAB_LEADERBOARD
} else {
return 0
}
if app.breathing.screen == ScreenDiary && !app_diary_save() {
return 0
}
selected := InbeRun(InbeMessage.{.action = INBE_TOGGLE_APP,
.enabled_apps = app.enabled_apps, .app = bit, .checked = true})
if !selected.succeeded || !CellsSetEnabled(selected.enabled_apps) {
return 0
}
app.enabled_apps = selected.enabled_apps
app.apps_selected = 1
if (CellsInstalledMask() & bit) == 0 {
for index: cast(s32)1..cast(s32)(PackageCount - 1) {
if PackageBit(index) == bit {
PackageRequest(index)
}
}
app_update_app_navigation(app, bit)
app_switch_screen(app, ScreenAppSelection)
save_settings(app)
return 0
}
// The opened app shows above the Apps overlay.
if app.launcher_open {
app_launcher_close(app)
}
app.main_tab = tab
app_update_app_navigation(app, bit)
app_switch_screen(app, app_screen_for_main_tab(tab))
app_observe_direct_route_change(app, before)
save_settings(app)
return cast(s32)(app.breathing.screen == app_screen_for_main_tab(tab))
}
#program_export
app_init :: (vapp: *void) {
app: *InnerBreeze = cast(*InnerBreeze)vapp
i: s32
if app == null {
return
}
FramePacingReset()
app.ui.session = SessionOpen()
if !SessionValid(app.ui.session) { return }
AndroidTextInputOpen(app.ui.session)
#if #defined(ANDROID_BUILD) {
app.graphics_context = AndroidGraphicsContext()
if practice_active(app) != null {
android_allow_screen_off()
}
practice_active_background_stop(app)
}
InitDPI()
TraceLogInit(LOG_INFO, "APP: app init width=%d height=%d embedded=%d",
config.width, config.height, config.loaded)
load_config()
app_notifications_init(app)
view_width = ifx (config.width > 0) then (config.width) else (APP_DEFAULT_WIDTH)
view_height = ifx (config.height > 0) then (config.height) else (APP_DEFAULT_HEIGHT)
UpdateDPI(view_width, view_height)
view_width = GetLayoutWidth()
view_height = GetLayoutHeight()
TraceLogScale(LOG_INFO, "APP: render scale=%.2f layout=%dx%d",
GetRenderScale(), view_width, view_height)
app_capture_navigation_viewport(app, view_width, view_height)
app.breathing = BreathSessionInit()
app.breaks = BreakInit()
app.breaks_enabled = 0
app.desktop_startup_mode = STARTUP_SHOW
app.desktop_close_action = CLOSE_ASK
app.break_hud_x = -1
app.break_hud_y = -1
#if !#defined(ANDROID_BUILD) && !#defined(PLATFORM_WEB) {
app.break_sounds_enabled = 1
} else {
app.break_sounds_enabled = 0
}
data_init()
TelegramAccountFlowStart()
if !CellsStart(app) {
TraceLog(LOG_ERROR, "APP: required Lists, Habits or Practice bundle failed validation")
app.request_quit = 1
return
}
TraceLog(LOG_INFO, "APP PROBE: data initialized")
// Purchases are known from the first frame, so a paid practice the
// account owns keeps its place instead of giving way at start.
practice_owned = UnlockOwned
practice_grant = UnlockPackGrant
practice_grant_forget = UnlockForgetGrant
practice_log = PracticeTraceLog
lumi_practice_status = LumiPracticeStatus
settings_missing := app_load_settings(app)
configuration_action: s32 = INBE_CONFIGURE
if AppMiniMode() {
configuration_action = INBE_MINI_CONFIGURE
}
selection := InbeRun(InbeMessage.{.action = configuration_action,
.enabled_apps = app.enabled_apps})
if !selection.succeeded || !CellsSetEnabled(selection.enabled_apps) {
app.request_quit = 1
return
}
if !AppMiniMode() {
app.enabled_apps = selection.enabled_apps
previous_routes := app.bottom_nav_routes
previous_count := app.bottom_nav_route_count
app_update_app_navigation(app, app.enabled_apps)
navigation_changed := previous_count != app.bottom_nav_route_count
for index: cast(s32)0..previous_count - 1 {
navigation_changed = navigation_changed ||
previous_routes[index] != app.bottom_nav_routes[index]
}
if navigation_changed {
save_settings(app)
}
if app.apps_selected != 0 {
for index: cast(s32)1..cast(s32)(PackageCount - 1) {
bit := PackageBit(index)
if (app.enabled_apps & bit) != 0 && (CellsInstalledMask() & bit) == 0 {
PackageRequest(index)
}
}
}
}
refresh_locale_dependent_text(app)
app_device_preferences_init(app)
if settings_missing != 0 {
save_settings(app)
}
app_apply_style(app)
if !LoadLocaleFont(app) {
TraceLog(LOG_WARNING, "FONT: Failed to load Noto UI font -> using built-in default")
}
TraceLog(LOG_INFO, "APP PROBE: locale font initialized")
if !AppMiniMode() {
update_check_start()
}
app.practice_tab = PRACTICE_TAB_PLAY
app.practice_config_tab = 0
app.session_result = SessionResult.{}
if app.language_needs_save != 0 {
save_settings(app)
app.language_needs_save = 0
}
if !TelegramAccountFlowOwnsContext() {
habits_init_with_defaults(*app.habits, app.language_selected)
}
app_restore_habits_view_settings(app)
app.habit_detail_index = -1
elist_state: *EListState = *app.elist
elist_state.editing_item = -1
app.habit_session_edit = HabitSessionEditState.{}
app.habit_session_edit.round = -1
app_apply_initial_screen(app)
practice_update_circle_bounds(app, app_content_top_reserved(app),
app_content_bottom_reserved(app))
#if !#defined(PLATFORM_WEB) && !#defined(ANDROID_BUILD) {
if PreviewMode() == 0 {
init_audio(app)
}
}
TraceLog(LOG_INFO, "APP PROBE: audio setup complete")
{
i: s32 = 0
while i < practice_count() {
practice: *PracticeDefinition = practice_get(i)
if practice.init != null {
practice.init(app)
}
i += 1
}
}
app.camera = Camera.{}
app.camera.zoom = 1.0
app.play_circle_hover = 0
app.play_circle_scale = 1.0f
app.settings_tab = SETTINGS_TAB_DEVICE
app.settings_overview = 1
app.habit_edit = HabitEditState.{}
app.habit_edit.index = -1
app.habit_edit.color.r = 99
app.habit_edit.color.g = 196
app.habit_edit.color.b = 165
app.habit_edit.color.a = 255
app.habit_edit.sync_mode = HABIT_SYNC_NONE
practice_update_session_sounds(app)
reset_settings_preview(app)
app.start_speed_preview = BreathSessionInit()
// Icons resolve from Kryon's embedded sheets; no app-side textures.
app.modal = AppModal.{}
app.meditation.duration_seconds = 0
app.meditation.remaining_seconds = 0
app.meditation.frame_ticks = 0
if !AppMiniMode() {
unused app_request_sync(app)
}
TraceLog(LOG_INFO, "APP PROBE: app init complete")
}
handle_back_button :: (app: *InnerBreeze) {
// If modal is active, let modal drawing handle it
if app.modal.active != 0 {
return
}
if app_lumi_settings_back(app) {
return
}
if app_return_bar_takes_back(app) {
app_return_to_apps(app)
return
}
screen := app.breathing.screen
if screen == ScreenStart {
if app.practice_tab == PRACTICE_TAB_CONFIG {
app_leave_practice_config(app)
}
if app.practice_tab != PRACTICE_TAB_PLAY {
route: AppRoute = app_current_route(app)
route.practice_tab = PRACTICE_TAB_PLAY
app.manual_scroll = 0
app.settings_scroll = 0
app.tutorial_step = 0
app_switch_route(app, route)
}
} else if screen == ScreenSettings {
app_settings_back(app)
} else if screen == ScreenProfile {
profile_screen_back(app)
} else if screen == ScreenCustomizeNav {
app_return_from_customize_nav(app)
} else if screen == ScreenPracticeConfig {
app_leave_practice_config(app)
app_switch_screen(app, ScreenStart)
} else if screen == ScreenHabits {
if app.habits.screen_mode == HABITS_SCREEN_HISTORY || app.habits.screen_mode == HABITS_SCREEN_STATISTICS {
app.habits.screen_mode = HABITS_SCREEN_OVERVIEW
app.habits.selected = -1
app.habits.tab = HABIT_TAB_WEEKLY
app.habits.view_mode = HABIT_VIEW_WEEKLY
app.habits.scroll = 0
save_settings(app)
} else if app.habits.tab == HABIT_TAB_EDIT && app.habit_edit.active != 0 {
habit_edit_cancel(app)
} else {
app_switch_screen(app, ScreenStart)
}
} else if screen == ScreenEList {
app_switch_screen(app, app_screen_for_main_tab(APP_MAIN_TAB_PRACTICE))
} else if screen == ScreenHabitEdit {
habit_edit_cancel(app)
app_switch_screen(app, ScreenHabits)
} else if screen == ScreenHabitSessionEdit {
habit_session_cancel_edit(app)
app_switch_screen(app, ScreenHabits)
} else if screen == ScreenLanguage || screen == ScreenManual {
manual_screen_close_tutorial(app, 0)
} else if screen == ScreenResults {
practice_ensure_results_saved(app)
#if #defined(ANDROID_BUILD) {
android_allow_screen_off()
android_wakelock_release()
}
app.session_result = SessionResult.{}
app_switch_screen(app, ScreenStart)
} else if screen == ScreenSession {
// Paused, it leaves at once; otherwise it asks first.
practice: *PracticeDefinition = practice_get(EXERCISE_WIM_HOF)
if practice.request_exit != null {
practice.request_exit(app)
}
} else if screen == ScreenMeditation {
{
practice: *PracticeDefinition = practice_get(EXERCISE_MEDITATION)
if practice.request_exit != null {
practice.request_exit(app)
}
}
} else if screen == ScreenPatterns {
{
practice: *PracticeDefinition = practice_get(EXERCISE_PATTERNS)
if practice.request_exit != null {
practice.request_exit(app)
}
}
}
}
draw_profile_picture_picker_modal :: (app: *InnerBreeze) {
if app == null {
return
}
props: ProfileImagePickerProps
props.title = LocaleText("profile_picture_title")
props.selected_icon_type = app.profile_picture_icon
props.max_width = 520
props.scroll_offset = app.profile_picture_picker_scroll
props.view_width = app.ui.view_width
props.view_height = app.ui.view_height
result := ProfileImagePicker(app.ui.session, props)
app.profile_picture_picker_scroll = result.scroll_offset
if result.changed {
app.profile_picture_icon = result.selected_icon_type
save_settings(app)
}
if result.closed {
app_close_modal(app)
}
}
PracticeTraceLog :: (warning: bool, message: string) {
TraceLog(ifx warning then LOG_WARNING else LOG_INFO, message)
}
// What Lumi must ask before starting a practice on this device.
LumiPracticeStatus :: (practice: s32) -> s32 {
if PracticeHidden(practice) {
return LUMI_PRACTICE_HIDDEN
}
if PracticeInstalled(practice) {
return LUMI_PRACTICE_INSTALLED
}
product := PracticeProduct(practice)
if product.count > 0 && !UnlockOwned(product) {
return LUMI_PRACTICE_LOCKED
}
return LUMI_PRACTICE_DOWNLOAD
}
draw_global_modal :: (app: *InnerBreeze) {
modal_result: s32
if app == null || app.modal.active == 0 {
return
}
if app.blocked_input_frame == app.breathing.frame {
return
}
if settings_data_draw_modals(app) != 0 {
return
}
if app.modal.type == cast(ModalType)ModalMeditationNetworkError {
actions: [2] ActionModalAction = .[
ActionModalAction.{.label = LocaleText("cancel_button"), .tone = cast(ButtonTone)ButtonToneNeutral, .emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft},
ActionModalAction.{.label = LocaleText("retry_button"), .tone = cast(ButtonTone)ButtonToneAccent, .emphasis = cast(ButtonEmphasis)ButtonEmphasisFilled},
]
modal_result = ActionModal(app.ui.session, ActionModalProps.{.title = LocaleText("meditation_music_network_error_title"), .message = LocaleText("meditation_music_network_error_message")}, actions[:2]).action
if modal_result == 1 {
TraceLog(LOG_INFO, "AUDIO: Network error download cancelled")
app_close_modal(app)
} else if modal_result == 2 {
TraceLog(LOG_INFO, "AUDIO: Network error download retry requested")
app_close_modal(app)
meditation_music_start_download(app)
}
}
if app.modal.type == cast(ModalType)ModalPracticeDownloads {
PracticeDownloadsModalDraw(app)
return
}
if app.modal.type == cast(ModalType)ModalStoragePlans {
ProfileStoragePlansModalDraw(app)
return
}
if app.modal.type == cast(ModalType)ModalDewTopUp {
ProfileTopUpModalDraw(app)
return
}
if app.modal.type == cast(ModalType)ModalDewGift {
ProfileDewGiftModalDraw(app)
return
}
if app.modal.type == cast(ModalType)ModalTaiChiCourse ||
app.modal.type == cast(ModalType)ModalTaiChiCustomize {
tai_chi_modal_draw(app)
return
}
if app.modal.type == cast(ModalType)ModalAppIconPicker {
settings_screen_draw_app_icon_modal(app)
return
}
if app.modal.type == cast(ModalType)ModalThemePicker {
settings_screen_draw_theme_picker_modal(app)
}
if app.modal.type == cast(ModalType)ModalProfilePicturePicker {
draw_profile_picture_picker_modal(app)
return
}
if app.modal.type == cast(ModalType)ModalConfirmRemoveFriend {
profile_screen_draw_remove_friend_modal(app)
return
}
if app.modal.type == cast(ModalType)ModalPracticeManual || app.modal.type == cast(ModalType)ModalPracticeConfig || app.modal.type == cast(ModalType)ModalEditProgressiveStartSpeed {
practice_screen_draw_modal(app)
return
}
if app.modal.type == cast(ModalType)ModalSyncAlias {
modal_result = settings_sync_account_draw_alias_modal(app)
if modal_result == {
case 1;
then_backup: s32 = app.sync_alias_then_backup
app.sync_alias_then_backup = 0
app_close_modal(app)
settings_screen_set_status_success(LocaleText("sync_alias_saved"), "")
if then_backup != 0 {
app_open_modal(app, cast(ModalType)ModalSyncAccountBackup)
}
case 2;
settings_screen_set_status_error(LocaleText("sync_alias_failed"))
case 3;
then_backup: s32 = app.sync_alias_then_backup
app.sync_alias_then_backup = 0
app_close_modal(app)
if then_backup != 0 {
app_open_modal(app, cast(ModalType)ModalSyncAccountBackup)
}
}
}
if app.modal.type == cast(ModalType)ModalSyncPublicId {
modal_result = settings_sync_account_draw_public_id_modal(app)
if modal_result != 0 {
app_close_modal(app)
}
}
}
updateapp :: (app: *InnerBreeze) {
app_activity_pump(app)
hover: s32 = 0
center_x: s32 = view_width / 2
frame_view_height: s32 = view_height
center_y: s32
i: s32
frame_route: AppRoute
first_run_guide_active: s32 = 0
habits_guide_active: s32 = 0
screen_guide_active: s32 = SCREEN_GUIDE_NONE
practice_fullscreen_modal: bool = false
global_modal_drawn: bool = false
if TelegramAccountFlowOwnsContext() {
// Host identity must still be current before exposing any cached
// plaintext or actions, rather than waiting until the post-draw pump.
if !TelegramContextCurrent() && telegram_account_flow.phase != 90 &&
telegram_account_flow.phase != 91 {
TelegramFlowFail(*telegram_account_flow)
}
if TelegramContextCurrent() && StoredDelegateAvailable() != 0 &&
(app.enabled_apps & APP_LUMI) != 0 {
if account_activity.view_open && ActivityRemoteVisible() {
unused ActivityDrawRemote(app, true)
} else {
lumi_screen_draw(app)
ActivityDrawIndicator(app, true)
}
}
DrawTelegramAccountFlow(app)
app.breathing.frame += 1
return
}
app_sync_route_from_url(app)
frame_route = app_current_route(app)
#if #defined(ANDROID_BUILD) {
{
practice_id: s32 = android_take_pending_practice_start()
if practice_id >= 0 {
active_break: s32
practice: *PracticeDefinition
app.exercise_type = practice_clamp_id(practice_id)
if app.modal.active != 0 {
app_close_modal(app)
}
active_break = BreakActive(app.breaks)
if active_break >= 0 {
app_breaks_start_practice(app, active_break, app.exercise_type)
} else {
app.main_tab = APP_MAIN_TAB_PRACTICE
app.practice_tab = PRACTICE_TAB_PLAY
practice = practice_get(app.exercise_type)
if practice != null && practice.start != null {
practice.start(app)
}
}
}
}
}
// Mini mode is only the practices: no break or reminder prompts.
if !AppMiniMode() {
app_breaks_update(app)
if app_notifications_tick(app) {
save_settings(app)
}
}
{
i: s32 = 0
while i < practice_count() {
practice: *PracticeDefinition = practice_get(i)
if practice.update != null {
practice.update(app)
}
i += 1
}
}
// Streams need fresh buffers even when their controls are on another tab.
settings_audio_update_preview(app)
if app.practice_coming_soon_ticks > 0 {
app.practice_coming_soon_ticks -= 1
}
practice_screen_prepare_first_run_guide(app)
habits_screen_prepare_first_run_guide(app)
first_run_guide_active = practice_screen_first_run_guide_active(app)
habits_guide_active = habits_screen_first_run_guide_active(app)
screen_guide_active = ScreenGuideActive(app)
app_clear_invalid_screen_local_modal(app)
// These modals cover the practice page, which must then not draw the
// same manual or configuration a second time with the same widget keys.
practice_fullscreen_modal = app.modal.active != 0 &&
(app.modal.type == cast(ModalType)ModalEditProgressiveStartSpeed ||
app.modal.type == cast(ModalType)ModalPracticeManual ||
app.modal.type == cast(ModalType)ModalPracticeConfig)
view_height = app_page_height(app, view_height)
center_y = view_height / 2
// A practice in its small window keeps running, and the navigation
// slides toward where it belongs on this screen.
app_session_window_tick(app)
app_session_nav_update(app)
// Resolve dock input before drawing the page on every viewport. A
// bottom-dock click must draw its destination in this frame, just like
// the desktop rail, so a cold page cannot swallow the next tap.
app_draw_bottom_nav(app)
app_draw_return_bar(app)
back_requested := IsKeyPressed(APP_KEY_BACK) ||
((app.launcher_open || (app.modal.active != 0 &&
(app.modal.type == cast(ModalType)ModalPracticeDownloads ||
app.modal.type == cast(ModalType)ModalStoragePlans ||
app.modal.type == cast(ModalType)ModalDewTopUp ||
app.modal.type == cast(ModalType)ModalDewGift))) && IsKeyPressed(APP_KEY_ESCAPE))
#if !#defined(ANDROID_BUILD) && !#defined(PLATFORM_WEB) {
if IsKeyPressed(KEY_BACKSPACE) && ((app.breathing.screen == ScreenStart && app.practice_tab != PRACTICE_TAB_PLAY) || app.breathing.screen == ScreenSession || app.breathing.screen == ScreenMeditation || app.breathing.screen == ScreenSunSalutation || app.breathing.screen == ScreenPatterns || app.breathing.screen == ScreenTaiChi) {
back_requested = true
}
}
if back_requested {
if app.launcher_open {
if app.breathing.screen != ScreenAppSelection {
app_launcher_close(app)
}
} else if app.close_prompt_open != 0 {
app.close_prompt_open = 0
} else if first_run_guide_active != 0 || habits_guide_active != 0 ||
screen_guide_active != SCREEN_GUIDE_NONE {
if first_run_guide_active != 0 {
practice_screen_dismiss_first_run_guide(app)
}
if habits_guide_active != 0 {
habits_screen_dismiss_first_run_guide(app)
}
if screen_guide_active != SCREEN_GUIDE_NONE {
ScreenGuideDismiss(app)
}
} else if app.modal.active != 0 {
app_close_modal(app)
} else {
handle_back_button(app)
}
}
#if !#defined(ANDROID_BUILD) && !#defined(PLATFORM_WEB) {
// Ctrl+Q quits from any screen; Esc quits only from idle home.
if IsKeyPressed(KEY_Q) && (IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL)) {
app_request_desktop_quit(app)
} else if !back_requested && IsKeyPressed(APP_KEY_ESCAPE) &&
app.breathing.screen == ScreenStart &&
app.practice_tab == PRACTICE_TAB_PLAY &&
app.modal.active == 0 && first_run_guide_active == 0 &&
habits_guide_active == 0 {
app_request_desktop_quit(app)
}
}
launcher_input: InputSnapshot
launcher_owns_input := app.launcher_open
if launcher_owns_input { launcher_input = TreeInputSuspend(app.ui.session) }
handled_screen: bool = false
if app.launcher_open {
handled_screen = true
} else if app.breathing.screen == ScreenSettings {
settings_screen_draw(app)
handled_screen = true
} else if app.breathing.screen == ScreenProfile {
profile_screen_draw(app)
handled_screen = true
} else if app.breathing.screen == ScreenCustomizeNav {
app_draw_customize_nav_page(app)
handled_screen = true
} else if app.breathing.screen == ScreenLanguage {
language_screen_draw(app)
handled_screen = true
} else if app.breathing.screen == ScreenAppSelection {
app_selection_screen_draw(app)
handled_screen = true
} else if app.breathing.screen == ScreenHabits {
draw_habits_screen(app)
handled_screen = true
} else if app.breathing.screen == ScreenLumi {
lumi_screen_draw(app)
handled_screen = true
} else if app.breathing.screen == ScreenDiary {
diary_screen_draw(app)
handled_screen = true
} else if app.breathing.screen == ScreenLeaderboard {
leaderboard_screen_draw(app)
handled_screen = true
} else if app.breathing.screen == ScreenEList {
draw_elist_screen(app)
handled_screen = true
} else if app.breathing.screen == ScreenHabitEdit {
habit_edit_draw(app)
handled_screen = true
} else if app.breathing.screen == ScreenHabitSessionEdit {
habit_session_draw_edit_screen(app)
handled_screen = true
} else if app.breathing.screen == ScreenBreak {
break_overlay_draw(app)
handled_screen = true
}
if !handled_screen {
if app.breathing.screen == ScreenStart {
practice_update_circle_bounds(app, app_content_top_reserved(app),
app_content_bottom_reserved(app))
} else if app.breathing.screen == ScreenSession {
practice_update_circle_bounds(app, AppTitleBarHeight(), 84)
}
if app.breathing.screen == ScreenSession {
practice_draw_active_breathing(app, center_x, center_y)
}
if app.breathing.screen == ScreenStart {
{
if !practice_fullscreen_modal && app.main_tab == APP_MAIN_TAB_NONE {
app_draw_blank_home_easteregg(app)
} else if !practice_fullscreen_modal && app.main_tab != APP_MAIN_TAB_NONE {
if app.practice_tab == PRACTICE_TAB_MANUAL {
manual_screen_draw(app)
} else if app.practice_tab == PRACTICE_TAB_CONFIG {
practice: *PracticeDefinition = practice_get(app.exercise_type)
if practice.draw_config != null {
practice.draw_config(app)
}
} else {
practice_screen_draw_home(app)
}
}
}
// Skip drawing on the same frame modal opens to prevent click propagation
if app.modal.active != 0 &&
app.modal.type == cast(ModalType)ModalMeditationSetup &&
app.blocked_input_frame != app.breathing.frame {
practice: *PracticeDefinition = practice_get(EXERCISE_MEDITATION)
if practice.draw_setup_modal != null {
practice.draw_setup_modal(app)
}
}
} else if app.breathing.screen == ScreenSession {
practice_update_active_breathing(app, center_x, center_y, *hover)
} else if app.breathing.screen == ScreenMeditation {
{
practice: *PracticeDefinition = practice_get(EXERCISE_MEDITATION)
if practice.draw_active_session != null {
practice.draw_active_session(app, center_x, center_y)
}
}
} else if app.breathing.screen == ScreenSunSalutation {
{
practice: *PracticeDefinition = practice_get(EXERCISE_SUN_SALUTATION)
if practice.draw_active_session != null {
practice.draw_active_session(app, center_x, center_y)
}
}
} else if app.breathing.screen == ScreenTaiChi {
{
practice: *PracticeDefinition = practice_get(EXERCISE_TAI_CHI)
if practice.draw_active_session != null {
practice.draw_active_session(app, center_x, center_y)
}
}
} else if app.breathing.screen == ScreenPatterns {
{
practice: *PracticeDefinition = practice_get(EXERCISE_PATTERNS)
if practice.draw_active_session != null {
practice.draw_active_session(app, center_x, center_y)
}
}
} else if app.breathing.screen == ScreenResults {
practice_draw_results(app, center_x, center_y, *hover)
}
}
if practice_fullscreen_modal {
draw_global_modal(app)
global_modal_drawn = true
}
if launcher_owns_input { unused TreeInputResume(app.ui.session, launcher_input) }
view_height = frame_view_height
app_draw_session_nav(app)
// The Apps screen covers the page, so the practice window is drawn on it
// while it is open.
launcher_covers_page := app.launcher_open
if !launcher_covers_page {
app_session_window_draw(app)
}
ActivityDrawIndicator(app)
app_draw_mini_expand_button(app)
practice_screen_draw_first_run_guide(app)
habits_screen_draw_first_run_guide(app)
ScreenGuideDraw(app)
if !global_modal_drawn {
draw_global_modal(app)
}
if !launcher_covers_page {
app_draw_close_prompt(app)
}
app_draw_launcher(app)
if launcher_covers_page {
// Apps is an overlay above the page, so the window and the close
// prompt are drawn inside its panel, above the apps. If Apps closed
// during this frame, they are drawn over the page instead.
inside_launcher := app.launcher_panel_node >= 0 &&
TreePushScope(app.ui.session, app.launcher_panel_node)
app_session_window_draw(app)
app_draw_close_prompt(app)
if inside_launcher {
unused End(app.ui.session)
}
}
app_flush_deferred_settings(app)
app_observe_direct_route_change(app, frame_route)
LumiGuideFrameEnd(app)
app.breathing.frame += 1
}
// Exchanges live activity records and sync. On Android the frame and the
// background practice timer both call this, and around picture-in-picture
// both run at once: only one exchanges at a time, so a finished sync is
// applied and released by one thread only.
app_network_pump :: (app: *InnerBreeze) {
#if #defined(ANDROID_BUILD) {
if !android_network_pump_begin() {
return
}
}
ActivityPump(app)
app_sync_pump(app)
ListsNetworkTick(app, GetTime(), app.breathing.screen == ScreenEList)
#if #defined(ANDROID_BUILD) {
android_network_pump_end()
}
}
// Exchanges live activity records alone, under the same one-thread rule.
app_activity_pump :: (app: *InnerBreeze) {
#if #defined(ANDROID_BUILD) {
if !android_network_pump_begin() {
return
}
}
ActivityPump(app)
#if #defined(ANDROID_BUILD) {
android_network_pump_end()
}
}
// Android's practice timer exchanges live records while a practice continues
// in the background; drawing and result ownership remain on their existing
// paths.
#program_export
app_activity_background_sync :: (vapp: *void) {
app := cast(*InnerBreeze)vapp
if app == null {
return
}
app_network_pump(app)
}
app_update_draw_last_full_w: s32 = -1;
app_update_draw_last_full_h: s32 = -1;
app_update_draw_last_content_x: s32 = -1;
app_update_draw_last_content_w: s32 = -1;
app_update_draw_last_log_time: float64 = -1.0;
app_last_frame_status: s32;
app_finish_frame :: (app: *InnerBreeze) {
status: s32
paint_unchanged: bool = false
// The previous frame is presented again only when the direct draws
// match too; otherwise the frame is repainted, widgets included.
direct_unchanged := DirectDrawFrameFinish()
if app.ui.retained_target_active {
result := EndFrameRetained(app.ui.session, direct_unchanged)
status = cast(s32)result.status
paint_unchanged = result.paint_unchanged
} else {
status = cast(s32)EndFrame(app.ui.session)
}
if status != 0 && status != app_last_frame_status {
TraceLogResult(LOG_WARNING, "APP: frame rejected with status %d", status)
TraceLogResult(LOG_WARNING, "APP: rejected widget key %d",
cast(s32)TreeFrameErrorKey(app.ui.session))
TraceLogResult(LOG_WARNING, "APP: rejected widget kind %d",
TreeFrameErrorKind(app.ui.session))
}
app_last_frame_status = status
app.ui.retained_frame_unchanged = status == 0 && paint_unchanged &&
direct_unchanged
}
#program_export
app_update_draw :: (vapp: *void, viewport: Rectangle) {
app: *InnerBreeze = cast(*InnerBreeze)vapp
profile_frame_start: float64
profile_update_start: float64
profile_habits_start: float64
profile_sync_start: float64
full_width: s32
full_height: s32
content_x: s32 = 0
rail_width: s32 = 0
top_height: s32 = 0
content_w: s32
layout_width: s32
layout_height: s32
layout_content_x: s32
layout_content_w: s32
render_scale: float32
if app == null || !CellsReady() {
return
}
if viewport.width <= 0 || viewport.height <= 0 {
return
}
profile_frame_start = app_profile_now()
FrameTextReset(*app.ui.frame_text)
// Styles follow the current app route, with cached parsing between edits.
unused app_apply_style(app)
frame_sample: FrameActivitySample
frame_sample.mouse_x = app.ui.mouse_x
frame_sample.mouse_y = app.ui.mouse_y
frame_sample.pointer_active = app.ui.pointer_active
frame_sample.keyboard_active = app.ui.keyboard_active
frame_sample.text_input_active = app.ui.text_input_active
frame_decision := app_update_frame_pacing(app, frame_sample)
app.ui.frame_target_fps = frame_decision.state.target_fps
app.ui.frame_target_changed = frame_decision.apply_target
if !app_reload_graphics_resources(app) {
return
}
#if #defined(ANDROID_BUILD) {
picture_state := AndroidPictureWindowState()
app.session_window.system_visible = (picture_state & 2) != 0
generation := picture_state >> 4
if generation != app.session_window.system_generation {
app.session_window.system_generation = generation
app.session_window.system_last_tick = GetTime()
}
// Opening the floating window back into the app shows its practice,
// not the page that was open behind the practice's in-app window.
// Like other window input from outside the frame, the window tick
// applies it after the frame has read the current route.
returned := (picture_state & 4) != 0
if returned != app.session_window.system_returned {
app.session_window.system_returned = returned
app.session_window.restore_requested = true
}
if app.session_window.system_visible {
app_draw_android_picture_window(app, viewport)
return
}
app.session_window.system_last_tick = GetTime()
}
full_width = cast(s32)viewport.width
full_height = cast(s32)viewport.height
view_width = full_width
view_height = full_height
// Update DPI cache
UpdateDPI(view_width, view_height)
if !(GetDPIScale() > 0.0f) || GetDPIScale() > 8.0f {
TraceLogScale(LOG_WARNING, "APP_EMBED: repairing invalid dpi %.2f for %dx%d",
GetDPIScale(), view_width, view_height)
InitDPI()
UpdateDPI(view_width, view_height)
}
// The layout is already in density-independent units and the render
// scale applies the density, so app metrics must not scale it again.
AppMetricsConfigure(1.0)
layout_width = GetLayoutWidth()
layout_height = GetLayoutHeight()
render_scale = GetRenderScale()
if !(render_scale > 0.0f) {
render_scale = 1.0f
}
app_full_view_width = layout_width
view_width = layout_width
view_height = layout_height
app.ui.view_width = view_width
app.ui.view_height = view_height
// Freeze responsive navigation from the full viewport before narrowing content.
app_capture_navigation_viewport(app, view_width, view_height)
rail_width = ifx (app_nav_desktop_rail_enabled(app) != 0) then (app_nav_desktop_rail_width(app)) else (0)
layout_content_x = ifx (app_navigation_placement(app) == NAVIGATION_LEFT) then (rail_width) else (0)
layout_content_w = layout_width - rail_width
if layout_content_w < 1 {
layout_content_w = 1
}
content_x = cast(s32)(cast(float32)layout_content_x * render_scale + 0.5f)
rail_width = cast(s32)(cast(float32)rail_width * render_scale + 0.5f)
top_layout := app_return_bar_height(app)
if app_navigation_placement(app) == NAVIGATION_TOP {
top_layout += app_navigation_height(app)
}
top_height = cast(s32)(cast(float32)top_layout * render_scale + 0.5f)
practice_update_circle_bounds(app, app_content_top_reserved(app),
app_content_bottom_reserved(app))
app_device_preferences_update(app)
update_check_poll()
app_refresh_theme(app)
DirectDrawFrameBegin(viewport)
DrawRectangleRec(viewport, StyleTokenColor("canvas"))
content_w = full_width - rail_width
if content_w < 1 {
content_w = 1
}
{
now: float64 = GetTime()
changed := full_width != app_update_draw_last_full_w ||
full_height != app_update_draw_last_full_h ||
content_x != app_update_draw_last_content_x ||
content_w != app_update_draw_last_content_w
if changed && (app_update_draw_last_log_time < 0.0 || now - app_update_draw_last_log_time >= 0.25) {
TraceLogGeometry(LOG_INFO, "APP_EMBED: geometry viewport=%dx%d layout=%dx%d content_x=%d content=%dx%d render_scale=%.2f",
full_width, full_height, layout_width, layout_height,
layout_content_x, layout_content_w, layout_height,
GetRenderScale())
app_update_draw_last_log_time = now
}
if changed {
app_update_draw_last_full_w = full_width
app_update_draw_last_full_h = full_height
app_update_draw_last_content_x = content_x
app_update_draw_last_content_w = content_w
}
}
view_width = layout_content_w
view_height = layout_height - cast(s32)(cast(float32)top_height / render_scale + 0.5f)
app.ui.view_width = view_width
app.ui.view_height = view_height
app.camera = Camera.{}
app.camera.zoom = render_scale
app.camera.offset.x = ifx (PreviewMode() != 0) then (0.0f) else (viewport.x + cast(float32)content_x)
app.camera.offset.y = ifx (PreviewMode() != 0) then (0.0f) else (viewport.y + cast(float32)top_height)
#if #defined(ANDROID_BUILD) {
TextMenuSetTransform(render_scale, app.camera.offset.x, app.camera.offset.y)
}
if PreviewMode() == 0 {
DirectDrawTransform(app.camera)
}
// Retained hit testing and widget bounds use the same logical coordinates
// as the camera. A left navigation rail occupies negative content x.
frame_origin := Vector2.{0.0, 0.0}
if PreviewMode() == 0 {
frame_origin = ScreenToWorld(Vector2.{viewport.x, viewport.y}, app.camera)
}
logical_frame := Rectangle.{frame_origin.x, frame_origin.y,
viewport.width / render_scale, viewport.height / render_scale}
app.ui.retained_frame_unchanged = false
if cast(s32)BeginFrame(app.ui.session, KeyHash("inbe/frame"),
logical_frame) != 0 {
return
}
if PreviewMode() != 0 {
DrawRectangle(0, 0, view_width, view_height, StyleTokenColor("canvas"))
profile_update_start = app_profile_now()
updateapp(app)
app_profile_record_update(profile_update_start)
app_breaks_draw_toast(app,
Rectangle.{0.0, 0.0, cast(float32)view_width, cast(float32)view_height})
app_route_transition_draw(app, logical_frame, GetTime())
app_finish_frame(app)
} else {
BeginScissorMode(cast(s32)viewport.x, cast(s32)viewport.y,
full_width, full_height)
BeginMode2D(app.camera)
DrawRectangle(0, 0, view_width, view_height, StyleTokenColor("canvas"))
profile_update_start = app_profile_now()
updateapp(app)
app_profile_record_update(profile_update_start)
app_breaks_draw_toast(app,
Rectangle.{0.0, 0.0, cast(float32)view_width, cast(float32)view_height})
app_route_transition_draw(app, logical_frame, GetTime())
app_finish_frame(app)
EndMode2D()
EndScissorMode()
}
profile_habits_start = app_profile_now()
if app.request_quit != 0 {
return
}
if TelegramAccountFlowOwnsContext() {
TelegramAccountFlowPump(app)
app_activity_pump(app)
app_profile_frame_end(profile_frame_start)
return
}
app_flush_habits_after_frame(app)
app_profile_record_habits(profile_habits_start)
profile_sync_start = app_profile_now()
if DewAvailable() {
// The balance stays current on the Profile overview and while a gift is chosen.
dew_visible := (app.breathing.screen == ScreenProfile && app.profile_tab == PROFILE_TAB_OVERVIEW) ||
(app.modal.active != 0 && app.modal.type == cast(ModalType)ModalDewGift)
DewTick(dew_visible, GetTime())
if DewTakeSyncRequest() {
unused app_request_sync(app)
}
}
settings_sync_account_pump(app)
app_network_pump(app)
// Lumi and package requests run on the frame thread on every platform.
if !app_practice_holds_frame_work(app) {
app_lumi_pump(app)
PackagePump(app.enabled_apps, cast(s32)PackageNow())
content_generation := AssetGeneration()
// Paid practices need Dew, which the Google Play build does not offer.
practice_paid_allowed = DewAvailable()
// Each narrated practice's voice, for its downloads and playback.
tai_chi_male := TaiChiMaleVoice(app.tai_chi) && PracticeVoiceRecorded(EXERCISE_TAI_CHI, "male")
PracticeVoiceSelect(EXERCISE_TAI_CHI, ifx tai_chi_male then "male" else "")
salutation_male := SalutationMaleVoice(app.sun_salutation_voice, app.sun_salutation.character) &&
PracticeVoiceRecorded(EXERCISE_SUN_SALUTATION, "male")
PracticeVoiceSelect(EXERCISE_SUN_SALUTATION, ifx salutation_male then "male" else "")
PracticeNarrationPrefer(EXERCISE_TAI_CHI, AppTextFromCString(*app.tai_chi_narration_language[0], 8))
PracticeNarrationPrefer(EXERCISE_SUN_SALUTATION, AppTextFromCString(*app.sun_salutation_narration_language[0], 8))
if practice_paid_allowed {
UnlockSync()
}
PracticeDownloadsPump(app.exercise_type, app.voice_narration_enabled != 0,
practice_active(app) == null && (app.modal.active == 0 ||
app.modal.type == cast(ModalType)ModalPracticeDownloads ||
app.modal.type == cast(ModalType)ModalPracticeConfig ||
app.modal.type == cast(ModalType)ModalTaiChiCustomize))
if content_generation != AssetGeneration() {
#if #defined(PLATFORM_WEB) {
InvalidateCanvasAssets()
} else {
InvalidateRaylibAssets()
}
}
}
if app.app_choices_pending && practice_active(app) == null && app.modal.active == 0 &&
app.breathing.screen != ScreenHabitEdit && app.breathing.screen != ScreenHabitSessionEdit &&
app_diary_save() {
app_reload_app_choices(app)
app.app_choices_pending = false
}
if app.package_revision != PackageRevision() && app.modal.active == 0 &&
practice_active(app) == null &&
app.breathing.screen != ScreenHabitEdit && app.breathing.screen != ScreenHabitSessionEdit &&
app_diary_save() {
if CellsReloadFeatures(app.enabled_apps) {
#if #defined(PLATFORM_WEB) {
InvalidateCanvasAssets()
} else {
InvalidateRaylibAssets()
}
app.package_revision = PackageRevision()
app_diary_reset()
app_update_app_navigation(app)
}
}
app_profile_record_sync(profile_sync_start)
app_profile_frame_end(profile_frame_start)
}
// Android supplies the floating activity surface. Draw only the live practice,
// with a stable 200-unit layout while the OS moves and resizes it.
app_draw_android_picture_window :: (app: *InnerBreeze, viewport: Rectangle) {
#if #defined(ANDROID_BUILD) {
now := GetTime()
elapsed := now - app.session_window.system_last_tick
app.session_window.system_last_tick = now
// The in-app card must not replay the whole PiP interval on return.
app.session_window.last_tick = now
// A finished practice leaves the in-app card for its results, as the
// card's tick decides; that tick does not run behind this window.
if app.breathing.screen == ScreenResults {
app.session_window.screen = ScreenStart
}
practice := practice_active(app)
if practice != null {
screen := app.session_window.screen
if screen == ScreenStart {
screen = app.breathing.screen
}
app.session_window.system_screen = screen
if app.backgrounded == 0 && elapsed > 0.0 && practice.advance_elapsed != null {
total := elapsed * 1000.0 + app.session_window.system_elapsed_carry
milliseconds := cast(s32)total
app.session_window.system_elapsed_carry = total - cast(float64)milliseconds
before := app_current_route(app)
practice.advance_elapsed(app, milliseconds)
// A practice that finishes here opens its results outside the
// app's frame. Reporting the change keeps the frame after the
// window opens back into the app from returning to the
// practice's page.
app_observe_direct_route_change(app, before)
}
}
scale := viewport.width / 200.0
bounds := Rectangle.{0.0, 0.0, 200.0, viewport.height / scale}
camera: Camera
camera.zoom = scale
camera.offset = Vector2.{viewport.x, viewport.y}
AppMetricsConfigure(1.0)
DirectDrawFrameBegin(viewport)
DrawRectangleRec(viewport, StyleTokenColor("surface"))
// A practice that finishes here shows that it has finished and its
// results instead of its last picture, also while its screen waits
// to be extended.
result := *app.session_result
waiting: SessionResult
finished := app.breathing.screen == ScreenResults && result.active != 0
if !finished && practice != null && practice.window_result != null &&
practice.window_result(app, *waiting) != 0 {
result = *waiting
finished = true
}
if cast(s32)BeginFrame(app.ui.session, KeyHash("inbe/picture-window"), bounds) == 0 {
BeginMode2D(camera)
if finished {
practice_draw_results_window(app, bounds, result)
} else {
app_session_window_picture(app, app.session_window.system_screen, bounds, 0.0)
}
app_finish_frame(app)
EndMode2D()
}
app_network_pump(app)
}
unused app
unused viewport
}
#program_export
app_destroy :: (vapp: *void) {
app: *InnerBreeze = cast(*InnerBreeze)vapp
i: s32
if app == null {
return
}
if !CellsReady() {
CellsClose()
return
}
if TelegramAccountFlowOwnsContext() {
// Closing preserves the protected delegate for an explicitly verified
// Restore on the next launch; only Cancel or revocation clears it.
TelegramFlowStopOperations(*telegram_account_flow)
TelegramFlowWipeSecrets(*telegram_account_flow)
telegram_account_flow.hosted = false
ClearDelegate()
}
CloseBreakWindows()
ClosePracticeWindow(app)
FileDownloadRelease(*app.meditation.music_download)
unused app_diary_save()
PackageCancel()
save_settings(app)
settings_sync_account_cancel(app)
DewCancel()
LumiOnlineCancel()
app_clear_disconnected_sync_work()
AndroidTextInputClose(app.ui.session)
SessionClose(app.ui.session)
habits_flush_save(app)
if PreviewMode() == 0 {
ReleaseLocaleFont(app)
}
{
i: s32 = 0
while i < practice_count() {
practice: *PracticeDefinition = practice_get(i)
if practice.destroy != null {
practice.destroy(app)
}
i += 1
}
}
settings_audio_stop_preview(app)
app_audio_shutdown(app)
app_diary_reset()
app_lumi_reset()
app.* = InnerBreeze.{}
CellsClose()
}
#program_export
app_should_use_tab_bar :: (app: *InnerBreeze) -> s32 {
if app == null {
return 0
}
return ifx (app.navigation_mode == NAV_MODE_TABBAR) then (1) else (0)
}