Files
breeze/src/screens/diary_screen.zi
T
waozi 1e2fe7050d Log, pick files, watch window close, read idle time and notify through Kryon
These platform services now come from Kryon: raylib_trace routes raylib's
log (trace_log.zi keeps only Inner Breeze's formatted lines),
window_close watches the app's own window, idle_time reads the session's
idle time for break reminders, notification_desktop sends desktop
notifications, and file_picker opens data, photo and audio pickers,
including the browser's chooser, which no longer needs Inner Breeze's
JavaScript. Audio files are still picked only on Linux and BSD desktops.

Tests that compile host services gain Kryon's backend module path; the
tests of the moved code live in Kryon now.
2026-10-11 10:15:34 -03:00

408 lines
14 KiB
Ziran

#import "geometry"
#import "bundle_host"
#import "byte_text_linux"
#import, file "../cells/diary_ui_types.zi";
#import "widget_host"
#import "hosted_values"
#import, file "../cells/hosted_assets.zi";
#import, file "../cells/app_codec.zi";
#import, file "../cells/value_codec.zi";
#import "image_props"
#import "image_widget"
#import, file "../cells/diary_store.zi";
#import, file "../cells/diary_history.zi";
#import, file "../cells/diary_spelling.zi";
#import "file_picker"
#import, file "../platform/environment.zi";
#import, file "../platform/android/android_import.zi";
#import "layout"
#import "text_props"
#import "text_widget"
#import "text_input_props"
#import "text_area_widget"
#import "button_props"
#import "button_widget"
#import "control_props"
#import "text"
#import "vec"
#import "scroll_props"
#import "scroll_widget"
#import "tree"
#import, file "../app/app_types.zi";
#import, file "../app/app_nav_state.zi";
#import, file "../app/metrics.zi";
#import, file "../app/locale.zi";
#import, file "../app/text_buffers.zi";
#import, file "../platform/raylib_text_input.zi";
#import, file "../cells/module_host.zi";
#import, file "../cells/diary_types.zi";
#import, file "../cells/package_files.zi";
#import, file "../storage/sync_node_storage.zi";
#import, file "../storage/storage_core.zi";
using AndroidImportKind;
using AndroidImportResult;
using ImageFit;
using ButtonTone;
using ButtonEmphasis;
diary_state: DiaryMessage;
diary_loaded: bool;
diary_focused: bool;
diary_ui: DiaryUiFrame;
diary_ui_codec: WidgetDiaryUiFrameCodec;
diary_selecting: bool;
diary_scroll: s32;
diary_calendar_scroll: s32;
diary_page_scroll: s32;
diary_save_at: float64;
diary_pending: bool;
diary_photo_requested: bool;
diary_photo_waiting: bool;
diary_photo_year: s32;
diary_photo_month: s32;
diary_photo_day: s32;
diary_photo_error: bool;
// The host words a refused photo itself; the Diary app shows its photo error.
diary_photo_no_room: [320]u8;
diary_pending_replacement: string;
diary_frame_text: string;
diary_history: DiaryHistory;
diary_undo_requested: bool;
diary_redo_requested: bool;
diary_spelling_requested: bool;
// The day and text a spelling check was sent for; a correction applies only
// while both are unchanged.
diary_spelling_year: s32;
diary_spelling_month: s32;
diary_spelling_day: s32;
diary_spelling_sent: string;
diary_spelling_notice: string;
diary_spelling_notice_until: float64;
c :: #system_library "c";
DiaryNow :: () -> float64 #foreign c "GetTime";
DiaryPump :: () {
if !diary_pending {
return
}
diary_pending = false
if diary_state.action == DIARY_EDIT {
diary_state.replacement = diary_pending_replacement
}
diary_state = DiaryRun(diary_state)
diary_state.replacement = ""
PackageFree(diary_pending_replacement)
diary_pending_replacement = ""
}
#program_export
app_diary_save :: () -> bool {
DiaryPump()
if !diary_loaded || diary_state.saved || diary_state.load_error {
return true
}
diary_state.action = DIARY_SAVE
diary_state = DiaryRun(diary_state)
return diary_state.succeeded && diary_state.saved
}
app_diary_append :: (text: string) -> bool {
if text.count == 0 || !app_diary_save() {
return false
}
clock := DiaryStoreClock()
if clock.count < 16 {
return false
}
diary_state = DiaryRun(DiaryMessage.{.action = DIARY_LOAD})
diary_loaded = diary_state.succeeded
if !diary_loaded || diary_state.load_error {
return false
}
addition: Vec(u8)
if diary_state.text.count > 0 {
BuilderPrint(*addition, "\n\n")
}
BuilderPrint(*addition, "**%**\n%", clock[11:16], text)
replacement := BuilderFinish(addition)
defer { PackageFree(replacement) }
diary_state.action = DIARY_EDIT
diary_state.edit_start = cast(s32)diary_state.text.count
diary_state.edit_end = diary_state.edit_start
diary_state.replacement = replacement
diary_state = DiaryRun(diary_state)
diary_state.replacement = ""
diary_state.calendar = false
return diary_state.succeeded && app_diary_save()
}
app_diary_reset :: () {
diary_loaded = false
diary_pending = false
DiarySpellingCancel()
PackageFree(diary_spelling_sent)
diary_spelling_sent = ""
diary_spelling_notice = ""
DiaryHistoryClear(*diary_history)
diary_undo_requested = false
diary_redo_requested = false
diary_spelling_requested = false
PackageFree(diary_pending_replacement)
diary_pending_replacement = ""
PackageFree(diary_frame_text)
diary_frame_text = ""
diary_state = DiaryMessage.{}
diary_ui = DiaryUiFrame.{}
diary_focused = false
}
DiaryAcceptPhoto :: (path: string) {
if path.count == 0 || diary_state.load_error || !diary_loaded ||
diary_state.year != diary_photo_year || diary_state.month != diary_photo_month ||
diary_state.day != diary_photo_day {
diary_photo_error = path.count > 0
return
}
filename := DiaryStoreAttach(path)
defer { PackageFree(filename) }
if filename.count == 0 {
diary_photo_error = true
if !diary_attach_room.fits {
DiaryDescribeNoRoom()
}
return
}
at := path.count - 1
while at >= 0 && path[at] != cast(u8)47 && path[at] != cast(u8)92 { at -= 1 }
diary_state.action = DIARY_ATTACH
diary_state.attachment_file = filename
diary_state.attachment_name = path[at + 1:]
diary_state = DiaryRun(diary_state)
diary_photo_error = !diary_state.succeeded
}
DiaryDescribeNoRoom :: () {
address := PackageText(storage_get_setting_text("sync_server_url"), "")
defer {
PackageFree(address)
}
args: [1]FormatArg = .[FormatArg.{.text = SyncNodeDisplayName(address), .is_text = true}]
if !FormatTemplate(LocaleText("diary_photo_no_room"), args[:], diary_photo_no_room[:]) {
diary_photo_no_room[0] = cast(u8)0
}
}
DiaryPhotoPicker :: (app: *InnerBreeze) {
if diary_photo_waiting {
path: [4096]u8
result := android_import_poll_result_for(cast(s32)ANDROID_IMPORT_KIND_DIARY_PHOTO,
*path[0], cast(s32)path.count)
if result == cast(s32)ANDROID_IMPORT_RESULT_NONE { return }
diary_photo_waiting = false
app.file_dialog_active = 0
if result == cast(s32)ANDROID_IMPORT_RESULT_SELECTED {
DiaryAcceptPhoto(AppTextFromCString(*path[0], cast(s32)path.count))
}
}
if !diary_photo_requested { return }
diary_photo_requested = false
diary_photo_error = false
diary_photo_no_room[0] = cast(u8)0
diary_photo_year = diary_state.year
diary_photo_month = diary_state.month
diary_photo_day = diary_state.day
if !app_diary_save() { diary_photo_error = true; return }
app.file_dialog_active = 1
if AndroidPlatform() != 0 {
mime: [32]u8
unused AppCopyText("image/*", mime[:])
diary_photo_waiting = android_import_open_picker_for(
cast(s32)ANDROID_IMPORT_KIND_DIARY_PHOTO, *mime[0]) != 0
if !diary_photo_waiting { app.file_dialog_active = 0; diary_photo_error = true }
return
}
path: [4096]u8
selected := PickFile(FilePickerProps.{.title = LocaleText("diary_attach_photo"),
.filter = "Images | *.png *.jpg *.jpeg *.webp *.gif"}, path[:])
app.file_dialog_active = 0
if selected == 1 { DiaryAcceptPhoto(AppTextFromCString(*path[0], cast(s32)path.count)) }
if selected < 0 { diary_photo_error = true }
}
using HostValueKind;
DiaryUiCall :: (context: *void, module: *u8, function: *u8,
arguments: *HostValue, count: s32, result: *HostValue) -> s32 {
if TextFromCString(module) != "diary_view" {
return WidgetHostCall(context, module, function, arguments, count, result)
}
name := TextFromCString(function)
if name == "ReadDiaryUiHost" && count == 0 {
result.* = WidgetEncodeDiaryUiFrame(diary_ui, *diary_ui_codec)
return 1
}
if name == "WriteDiaryUiHost" && count == 1 && WidgetValidDiaryUiFrame(arguments[0]) {
observed := WidgetDecodeDiaryUiFrame(arguments[0], *diary_ui_codec)
diary_ui.focused = observed.focused
diary_ui.selecting = observed.selecting
diary_ui.scroll = observed.scroll
diary_ui.calendar_scroll = observed.calendar_scroll
diary_ui.page_scroll = observed.page_scroll
diary_ui.save_at = observed.save_at
diary_photo_requested = observed.photo_requested
diary_undo_requested = observed.undo_requested
diary_redo_requested = observed.redo_requested
diary_spelling_requested = observed.spelling_requested
diary_focused = observed.focused
if observed.clipboard_write { unused TextInputWriteClipboard(observed.clipboard_text) }
return 1
}
if name == "DiaryLocaleHost" && count == 1 &&
HostScalarValid(arguments[0], "string", cast(s32)HostString) {
key := TextFromBytes(BytesFromPointer(arguments[0].data, cast(u64)arguments[0].length))
text := LocaleText(key)
if key == "diary_photo_error" && diary_photo_no_room[0] != cast(u8)0 {
text = AppTextFromCString(*diary_photo_no_room[0], 320)
}
result.* = HostValue.{.kind = cast(s32)HostString, .type_name = HostTextData("string"),
.data = HostTextData(text), .length = cast(usize)text.count}
return 1
}
return 0
}
// Shows another version of the entry as one edit, so the caret lands just
// after the changed text and the entry saves on the next frame.
DiaryShowText :: (text: string) {
edit := DiaryHistoryEdit(diary_state.text, text)
diary_state.action = DIARY_EDIT
diary_state.edit_start = edit.start
diary_state.edit_end = edit.end
diary_state.replacement = edit.replacement
diary_state = DiaryRun(diary_state)
diary_state.replacement = ""
}
DiaryNotice :: (key: string) {
diary_spelling_notice = key
diary_spelling_notice_until = DiaryNow() + 6.0
}
DiaryHistoryCurrent :: () -> bool {
return diary_loaded && !diary_state.load_error &&
diary_history.year == diary_state.year && diary_history.month == diary_state.month &&
diary_history.day == diary_state.day
}
DiaryUndoRedo :: () {
undo := diary_undo_requested
redo := diary_redo_requested
diary_undo_requested = false
diary_redo_requested = false
if DiarySpellingWaiting() || !DiaryHistoryCurrent() {
return
}
if undo && DiaryHistoryUndo(*diary_history) {
DiaryShowText(diary_history.current)
}
if redo && DiaryHistoryRedo(*diary_history) {
DiaryShowText(diary_history.current)
}
}
DiarySpelling :: () {
if diary_spelling_requested && !DiarySpellingWaiting() && DiaryHistoryCurrent() {
notice := DiarySpellingStart(diary_state.text)
if notice == "" {
PackageFree(diary_spelling_sent)
diary_spelling_sent = PackageText(diary_state.text, "")
diary_spelling_year = diary_state.year
diary_spelling_month = diary_state.month
diary_spelling_day = diary_state.day
diary_spelling_notice = ""
} else {
DiaryNotice(notice)
}
}
diary_spelling_requested = false
if !DiarySpellingWaiting() {
return
}
result := DiarySpellingPoll()
if !result.finished {
return
}
same_day := diary_state.year == diary_spelling_year &&
diary_state.month == diary_spelling_month && diary_state.day == diary_spelling_day
unchanged := same_day && DiaryHistoryCurrent() && diary_state.text == diary_spelling_sent
PackageFree(diary_spelling_sent)
diary_spelling_sent = ""
if !same_day {
// The person moved to another day; the answer is for a page they left.
return
}
if result.notice != "" {
DiaryNotice(result.notice)
return
}
if !unchanged {
DiaryNotice("diary_spelling_error")
return
}
if result.text == diary_state.text {
DiaryNotice("diary_spelling_none")
return
}
DiaryShowText(result.text)
// The correction is one step, which undo reverts as a whole.
DiaryHistoryObserve(*diary_history, diary_state.year, diary_state.month, diary_state.day,
diary_state.text, DiaryNow(), false)
}
#program_export
diary_screen_draw :: (app: *InnerBreeze) {
DiaryPump()
if !diary_loaded {
diary_state = DiaryRun(DiaryMessage.{.action = DIARY_LOAD})
diary_loaded = diary_state.succeeded
}
DiaryPhotoPicker(app)
DiaryUndoRedo()
DiarySpelling()
diary_ui.session = app.ui.session
diary_ui.width = app.ui.view_width
diary_ui.height = app.ui.view_height
diary_ui.bottom = app_content_bottom_reserved(app)
diary_ui.scale = MetricsScale()
diary_ui.font = AppFontSize()
diary_ui.now = DiaryNow()
diary_ui.photo_error = diary_photo_error
diary_ui.photo_format = DiaryImageFormat(DiaryStorePreview(diary_state.photo_file))
diary_ui.photo_requested = false
diary_ui.undo_requested = false
diary_ui.redo_requested = false
diary_ui.spelling_requested = false
diary_ui.can_undo = DiaryHistoryCurrent() && diary_history.undo_count > 0
diary_ui.can_redo = DiaryHistoryCurrent() && diary_history.redo_count > 0
diary_ui.spelling_busy = DiarySpellingWaiting()
diary_ui.spelling_notice = ""
if diary_ui.now < diary_spelling_notice_until {
diary_ui.spelling_notice = diary_spelling_notice
}
diary_ui.clipboard_write = false
diary_ui.clipboard_text = ""
typed: [512]u8
diary_ui.input = SampleTextAreaInput(diary_focused, typed[:])
WidgetHostBegin(app.ui.session, HostedAssetAllowed)
CellsUiBegin(DiaryUiCall, cast(*void)app)
diary_state.action = DIARY_FRAME
diary_state = DiaryRun(diary_state)
CellsUiEnd()
WidgetHostEnd()
if diary_loaded && !diary_state.load_error {
DiaryHistoryObserve(*diary_history, diary_state.year, diary_state.month, diary_state.day,
diary_state.text, DiaryNow(), true)
}
}