Files

538 lines
22 KiB
Ziran
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#import "geometry"
#import, file "diary_date.zi";
Box :: #import "kryon/box";
Button :: #import "kryon/button";
Image :: #import "kryon/image";
Layout :: #import "kryon/layout";
Press :: #import "kryon/press";
Scroll :: #import "kryon/scroll";
Text :: #import "kryon/text";
TextArea :: #import "kryon/text_area";
MarkdownEditor :: #import "kryon/markdown_editor";
Markdown :: #import "kryon/markdown_preview";
#import "text_measure"
#import "control_props"
#import "drawing_props"
#import "text_align"
#import "std/text"
#import "vec"
#import "byte_text_linux"
#import "kryon/tree"
#import, file "diary_markdown.zi";
#import, file "diary_types.zi";
#import, file "diary_ui_types.zi";
#import "kryon/app_context"
#import "json_scan"
#import, file "diary_wire.zi";
CompressionIcon :: #import "mingcute/mingcute_regular_tool";
using Image.ImageFit;
using TextAlign;
using Text.Wrap;
using ButtonTone;
using ButtonEmphasis;
using IconPlacement;
using ContentAlignment;
library_json: string;
DiaryPhoto :: (destination: string) -> string {
if !AppSupports("media.asset") { return "" }
return AppService("media.asset", destination)
}
DiaryMediaQuery :: (day: string, trashed: bool) -> s32 {
library_json = "[]"
if AppSupports("media.query") {
library_json = AppService("media.query", day)
}
count := ArrayCount(library_json, 0)
return ifx count >= 0 then count else 0
}
DiaryMediaItem :: (index: s32) -> DiaryMediaEntry {
item := ElementAt(library_json, 0, index)
return DiaryMediaEntry.{
.hash = DiaryWireString(library_json, FieldAt(library_json, item, "hash")),
.name = DiaryWireString(library_json, FieldAt(library_json, item, "name")),
.day = DiaryWireString(library_json, FieldAt(library_json, item, "day")),
.format = DiaryWireString(library_json, FieldAt(library_json, item, "format"))}
}
state: DiaryMessage;
frame: DiaryUiFrame;
pending: bool;
DIARY_PREVIOUS_ICON :: "S1.8rr:M15 5L8 12L15 19"
DIARY_NEXT_ICON :: "S1.8rr:M9 5L16 12L9 19"
DIARY_CALENDAR_ICON :: "S1.6rr:M4 5H20V20H4ZM4 10H20M8 3V7M16 3V7"
DIARY_TIME_ICON :: "S1.6rr:M21 12C21 16.97 16.97 21 12 21C7.03 21 3 16.97 3 12C3 7.03 7.03 3 12 3C16.97 3 21 7.03 21 12ZM12 7V12L15.5 14"
DIARY_PHOTO_ICON :: "S1.6rr:M3 5H21V19H3ZM3 16L8 11L13 16L16 13L21 18M17.5 8.5C17.5 9.33 16.83 10 16 10C15.17 10 14.5 9.33 14.5 8.5C14.5 7.67 15.17 7 16 7C16.83 7 17.5 7.67 17.5 8.5Z"
DIARY_ATTACHMENT_ICON :: "S1.8rr:M21 11L12 20C9.8 22.2 6.2 22.2 4 20C1.8 17.8 1.8 14.2 4 12L14 2C15.66 0.34 18.34 0.34 20 2C21.66 3.66 21.66 6.34 20 8L10 18C8.9 19.1 7.1 19.1 6 18C4.9 16.9 4.9 15.1 6 14L15 5"
DIARY_UNDO_ICON :: "S1.8rr:M9 14L4 9L9 4M4 9H14C17.31 9 20 11.69 20 15C20 18.31 17.31 21 14 21H11"
DIARY_REDO_ICON :: "S1.8rr:M15 14L20 9L15 4M20 9H10C6.69 9 4 11.69 4 15C4 18.31 6.69 21 10 21H13"
DIARY_SPELLING_ICON :: "S2rr:M3 14L7.5 3L12 14M4.8 10H10.2M12 17L15.5 20.5L21.5 13"
DiaryObservedFrame :: () -> DiaryUiFrame {
return frame
}
// The entry previews its Markdown (diary_markdown.zi): away from the lines
// holding the caret or a selection, markers are hidden, emphasis is bold or
// slanted, headings are larger, links and list and quote marks take their
// colors, and a photo's embed line shows the photo in its place. On the
// caret's line the Markdown shows as typed, with the photo under it.
editor_blocks: [Markdown.PREVIEW_IMAGES + 1]TextArea.Block;
// A copy the view owns of text a host call answered. The host answers in its
// own buffers, which its next call may overwrite.
DiaryKept :: (value: string) -> string {
out: Vec(u8)
BuilderPrint(*out, "%", value)
return BuilderFinish(out)
}
// A photo's height in the entry: three quarters of its width, within limits
// that keep a few lines of text in view on a phone.
DiaryPhotoHeight :: (width: float32) -> s32 {
height := cast(s32)(width * 0.75)
if height > Scale(360) {
height = Scale(360)
}
if height < Scale(120) {
height = Scale(120)
}
return height
}
DiaryEditorProps :: (area: TextArea.Props) -> MarkdownEditor.Props {
props: MarkdownEditor.Props = .{.area = area, .muted_color = frame.muted_color,
.link_color = frame.link_color, .images = DiaryResolvedImages(area.value)}
height := DiaryPhotoHeight(area.bounds.width)
unavailable := AppLocale("diary_photo_unavailable")
for index: cast(s32)0..cast(s32)props.images.count - 1 {
embed := DiaryEmbedAt(area.value, props.images[index].start)
editor_blocks[index] = TextArea.Block.{.start = embed.line_start, .height = height,
.image = DiaryKept(DiaryPhoto(DiaryEmbedDestination(area.value, embed))),
.label = unavailable}
}
props.area.blocks = editor_blocks[:props.images.count]
return props
}
// The day's gallery is a separate view, opened by its toolbar icon. It
// lists the library's photos of that day, then the entry's other photos.
// Choosing one inserts it at the saved caret and returns to the entry.
DIARY_DAY_PHOTOS :: 64
DiaryDayPhoto :: struct {
// What an embed of the photo links to.
destination: string
name: string
// Its preview, or "" while the photo is not on this device.
asset: string
}
day_photos: [DIARY_DAY_PHOTOS]DiaryDayPhoto;
day_photo_count: s32;
// A photo is listed once: by what it links to, or by the picture it draws,
// since a Diary file the library keeps too draws as the library's photo.
DiaryDayPhotoKnown :: (destination: string, asset: string) -> bool {
for index: cast(s32)0..day_photo_count - 1 {
if day_photos[index].destination == destination {
return true
}
if asset.count > 0 && day_photos[index].asset == asset {
return true
}
}
return false
}
DiaryDayPhotoAdd :: (destination: string, name: string) {
if destination.count == 0 || day_photo_count >= DIARY_DAY_PHOTOS {
return
}
asset := DiaryPhoto(destination)
if DiaryDayPhotoKnown(destination, asset) {
return
}
day_photos[day_photo_count] = DiaryDayPhoto.{.destination = destination,
.name = DiaryKept(name), .asset = DiaryKept(asset)}
day_photo_count += 1
}
DiaryDayPhotos :: (text: string) {
day_photo_count = 0
if state.load_error || state.year <= 0 {
return
}
count := DiaryMediaQuery(ISO(Date.{state.year, state.month, state.day}), false)
for index: cast(s32)0..count - 1 {
if day_photo_count >= DIARY_DAY_PHOTOS {
break
}
entry := DiaryMediaItem(index)
DiaryDayPhotoAdd(DiaryLibraryPath(entry.day, entry.hash, entry.format), entry.name)
}
embed := DiaryEmbedNext(text, 0)
while embed.valid && day_photo_count < DIARY_DAY_PHOTOS {
DiaryDayPhotoAdd(DiaryEmbedDestination(text, embed), DiaryEmbedAlt(text, embed))
embed = DiaryEmbedNext(text, embed.end)
}
}
// The open gallery remembers its day, so another day opens with it closed.
DiaryDayKey :: () -> s32 {
return state.year * 10000 + state.month * 100 + state.day
}
DiaryGalleryOpen :: () -> bool {
return frame.photos_open_day != 0 && frame.photos_open_day == DiaryDayKey()
}
// Puts a photo's embed on its own line at the caret: one edit, saved at once.
DiaryInsertPhoto :: (destination: string, name: string) {
line := DiaryEmbedLine(destination, name)
edit := DiaryEmbedInsertion(state.text, state.cursor, line)
state.action = DIARY_EDIT
state.edit_start = edit.start
state.edit_end = edit.end
state.replacement = edit.replacement
pending = true
frame.save_at = frame.now
}
DiaryDayGallery :: (left: float32, top: float32, width: float32, height: float32, editable: bool) {
gap := cast(float32)Scale(8)
inner := width - cast(float32)Scale(14)
columns := cast(s32)((inner + gap) / cast(float32)Scale(112))
if columns < 1 {
columns = 1
}
tile := (inner - cast(float32)(columns - 1) * gap) / cast(float32)columns
rows := (day_photo_count + columns - 1) / columns
strip := Scroll.Draw(frame.session, Scroll.Props.{.key = cast(u64)290302,
.bounds = Rectangle.{left, top, width, height}, .scale = frame.scale,
.content_height = cast(s32)(cast(float32)rows * (tile + gap)),
.scroll_offset = frame.photos_scroll})
frame.photos_scroll = strip.scroll_offset
if !strip.opened {
return
}
for index: cast(s32)0..day_photo_count - 1 {
photo := day_photos[index]
bounds := Rectangle.{strip.content.x + cast(float32)(index % columns) * (tile + gap),
strip.content.y + cast(float32)(index / columns) * (tile + gap), tile, tile}
Box.Draw(frame.session, Box.Props.{.key = cast(u64)(292000 + index), .bounds = bounds,
.fill = Color.{156, 156, 156, 36}, .radius = cast(float32)Scale(6)})
if photo.asset.count > 0 {
Image.Draw(frame.session, Image.Props.{.key = cast(u64)(292100 + index),
.asset_path = photo.asset, .alt_text = photo.name, .bounds = bounds,
.fit = cast(Image.ImageFit)ImageFitCover, .smooth = true})
}
press := Press.Draw(frame.session, Press.Props.{.key = cast(u64)(292200 + index),
.id = 292200 + index, .bounds = bounds, .label = photo.name, .now = frame.now,
.disabled = !editable})
if press.clicked && !pending {
DiaryInsertPhoto(photo.destination, photo.name)
frame.photos_open_day = 0
}
}
End(frame.session)
}
DateTitle :: (year: s32, month: s32, day: s32) -> string {
if month < 1 || month > 12 { return "" }
keys: [12]string = .["habit_month_1", "habit_month_2", "habit_month_3",
"habit_month_4", "habit_month_5", "habit_month_6", "habit_month_7",
"habit_month_8", "habit_month_9", "habit_month_10", "habit_month_11", "habit_month_12"]
month_text := AppLocale(keys[month - 1])
title: Vec(u8)
eastern := month_text.count >= 2 && month_text[:2] == "%d"
if day > 0 && !eastern { BuilderPrint(*title, "% ", day) }
index: s64 = 0
while index < month_text.count {
if index + 1 < month_text.count && month_text[index:index + 2] == "%d" {
BuilderPrint(*title, "%", year)
index += 2
} else {
BuilderPrint(*title, "%", month_text[index:index + 1])
index += 1
}
}
if day > 0 && eastern {
BuilderPrint(*title, "%", day)
suffix := ifx month_text[month_text.count - 3:] == "월" then "일" else "日"
BuilderPrint(*title, "%", suffix)
}
return BuilderFinish(title)
}
HasWords :: (text: string) -> bool {
for byte: text {
if byte != cast(u8)32 && byte != cast(u8)10 && byte != cast(u8)13 && byte != cast(u8)9 {
return true
}
}
return false
}
Scale :: (value: s32) -> s32 {
return cast(s32)(cast(float32)value * frame.scale)
}
FinishView :: () -> DiaryMessage {
return state
}
// A button with an icon shows only the icon; its label names it for
// accessibility.
DiaryAction :: ( id: s32, label: string, bounds: Rectangle, enabled: bool = true,
icon: string = "", selected: bool = false, loading: bool = false) {
if Button.Draw(frame.session, Button.Props.{.id = id, .bounds = bounds,
.label = label, .disabled = !enabled, .selected = selected, .loading = loading,
.icon_shape = icon, .icon_only = icon != "", .fit_content = true,
.tone = cast(ButtonTone)ButtonToneNeutral,
.emphasis = cast(ButtonEmphasis)(ifx icon != "" then ButtonEmphasisGhost else ButtonEmphasisSoft)}) != 0 {
// Photos, undo, redo and spelling are host effects.
frame.focused = false
if id == 29005 {
frame.photo_requested = true
return
}
if id == 29014 {
frame.undo_requested = true
return
}
if id == 29015 {
frame.redo_requested = true
return
}
if id == 29016 {
frame.spelling_requested = true
return
}
if id == 29017 {
frame.photos_open_day = ifx DiaryGalleryOpen() then 0 else DiaryDayKey()
frame.photos_scroll = 0
return
}
state.action = DIARY_CONTROL
state.control = id
pending = true
frame.scroll = 0
frame.photos_scroll = 0
frame.photos_open_day = 0
}
}
DiaryView :: (message: DiaryMessage, observed: DiaryUiFrame, today: Date) -> DiaryMessage {
state = message
frame = observed
pending = false
column := Layout.CenteredColumnFor(frame.width, Scale(640), Scale(16))
left := cast(float32)(column.x + frame.origin_x)
width := cast(float32)column.width
part := (width - cast(float32)Scale(12)) / 3.0
compact := frame.height < Scale(480)
top := cast(float32)(frame.origin_y + ifx compact then Scale(4) else Scale(12))
control := cast(float32)Scale(44)
gap := cast(float32)Scale(8)
// Title bar: the selected date with the previous and next day right
// beside it, centered together.
title := DateTitle(state.year, state.month, state.day)
title_font := frame.font + Scale(4)
title_width := cast(float32)TextWidth(frame.session, Text.Props.{.text = title, .size = title_font})
if title_width > width - 2.0 * (control + gap) {
title_width = width - 2.0 * (control + gap)
}
title_left := left + (width - title_width) / 2.0
compression := AppSupports("media.compression.open")
if compression {
title_width = title_width - control - gap
if title_width < control {
title_width = control
}
title_left = left + (width - control - gap - title_width) / 2.0
if Button.Draw(frame.session, Button.Props.{.id = 29018,
.bounds = Rectangle.{left + width - control, top, control, control},
.label = AppLocale("media_compression_title"), .icon_only = true,
.icon_shape = CompressionIcon.Shape,
.tone = cast(ButtonTone)ButtonToneNeutral,
.emphasis = cast(ButtonEmphasis)ButtonEmphasisGhost}) != 0 {
unused AppService("media.compression.open", "")
}
}
DiaryAction(29001, "‹", Rectangle.{title_left - gap - control, top, control, control},
true, DIARY_PREVIOUS_ICON)
DiaryAction(29002, "›", Rectangle.{title_left + title_width + gap, top, control, control},
OnOrBefore(Next(Date.{state.year, state.month, state.day}, 1), today) &&
!OnOrBefore(today, Date.{state.year, state.month, state.day}),
DIARY_NEXT_ICON)
title_strong: [1]Text.Range = .[Text.Range.{.start = 0, .end = cast(s32)title.count}]
Text.Draw(frame.session, Text.Props.{.bounds = Rectangle.{title_left, top, title_width, control},
.text = title, .size = title_font, .strong = title_strong[:],
.align = cast(TextAlign)TextAlignCenter, .vertical_align = cast(TextAlign)TextAlignCenter,
.wrap = cast(Text.Wrap)TextWrapEllipsis})
// On phones the editing tools get their own row, keeping every target
// at 44 units instead of squeezing the icons beside Today.
tools := top + control + gap
tool_gap := cast(float32)Scale(4)
tool := control
today_width := cast(float32)Scale(96)
stacked := width < today_width + gap + 7.0 * tool + 6.0 * tool_gap
edit_tools := tools
if stacked && !state.calendar {
edit_tools += control + tool_gap
}
DiaryAction(29004, AppLocale("diary_today"), Rectangle.{left, tools, today_width, control})
DiaryAction(29003, AppLocale("tab_diary_calendar"),
Rectangle.{left + today_width + gap, tools, tool, control},
true, DIARY_CALENDAR_ICON, state.calendar)
editable := !state.load_error && state.year > 0
if !state.calendar {
idle := editable && !frame.spelling_busy
DiaryAction(29014, AppLocale("diary_undo"),
Rectangle.{left + width - 6.0 * tool - 5.0 * tool_gap, edit_tools, tool, control},
idle && frame.can_undo, DIARY_UNDO_ICON)
DiaryAction(29015, AppLocale("diary_redo"),
Rectangle.{left + width - 5.0 * tool - 4.0 * tool_gap, edit_tools, tool, control},
idle && frame.can_redo, DIARY_REDO_ICON)
DiaryAction(29016, AppLocale("diary_check_spelling"),
Rectangle.{left + width - 4.0 * tool - 3.0 * tool_gap, edit_tools, tool, control},
idle && HasWords(state.text) && AppSupports("text.correct.start"), DIARY_SPELLING_ICON, false, frame.spelling_busy)
DiaryAction(29013, AppLocale("diary_insert_time"),
Rectangle.{left + width - 3.0 * tool - 2.0 * tool_gap, edit_tools, tool, control},
!frame.spelling_busy, DIARY_TIME_ICON)
DiaryAction(29005, AppLocale("diary_attach_photo"),
Rectangle.{left + width - 2.0 * tool - tool_gap, edit_tools, tool, control},
AppSupports("media.pick"), DIARY_ATTACHMENT_ICON)
DiaryDayPhotos(state.text)
gallery_label := AppLocale("diary_show_gallery")
if DiaryGalleryOpen() {
gallery_label = AppLocale("diary_hide_gallery")
}
DiaryAction(29017, gallery_label,
Rectangle.{left + width - tool, edit_tools, tool, control},
editable && day_photo_count > 0, DIARY_PHOTO_ICON, DiaryGalleryOpen())
}
content := edit_tools + control + cast(float32)Scale(12)
content_height := cast(float32)(frame.height - frame.bottom) - content - cast(float32)Scale(16)
if !state.calendar && DiaryGalleryOpen() {
DiaryDayGallery(left, content, width, content_height, editable && !frame.spelling_busy)
return FinishView()
}
if state.calendar {
scroll := Scroll.Draw(frame.session, Scroll.Props.{.key = cast(u64)290300,
.bounds = Rectangle.{left, content, width, content_height},
.content_height = Scale(344), .scroll_offset = frame.calendar_scroll})
frame.calendar_scroll = scroll.scroll_offset
if !scroll.opened {
return FinishView()
}
origin := scroll.content.y
DiaryAction(29011, "‹", Rectangle.{left, origin, part, cast(float32)Scale(44)})
DiaryAction(29012, "›", Rectangle.{left + 2.0 * (part + cast(float32)Scale(6)),
origin, part, cast(float32)Scale(44)},
state.month_year < today.year ||
(state.month_year == today.year && state.month_number < today.month))
Text.Draw(frame.session, Text.Props.{.bounds = Rectangle.{left + part,
origin + cast(float32)Scale(8), part, cast(float32)Scale(32)},
.text = DateTitle(state.month_year, state.month_number, 0), .size = frame.font})
weekdays: [7]string = .["habit_day_mon", "habit_day_tue", "habit_day_wed", "habit_day_thu",
"habit_day_fri", "habit_day_sat", "habit_day_sun"]
for day: cast(s32)0..cast(s32)6 {
Text.Draw(frame.session, Text.Props.{.bounds = Rectangle.{left + cast(float32)day * width / 7.0,
origin + cast(float32)Scale(52), width / 7.0, cast(float32)Scale(24)},
.text = AppLocale(weekdays[day]), .size = frame.font})
}
for day: cast(s32)1..state.month_days {
position := state.month_weekday + day - 1
label: Vec(u8)
BuilderPrint(*label, "%", day)
if state.marks[day] != cast(u8)0 { BuilderPrint(*label, " •") }
day_title := BuilderFinish(label)
cell := width / 7.0
DiaryAction(29100 + day, day_title,
Rectangle.{left + cast(float32)(position % 7) * cell,
origin + cast(float32)Scale(80 + (position / 7) * 44), cell - 2.0,
cast(float32)Scale(42)},
OnOrBefore(Date.{state.month_year, state.month_number, day}, today))
}
End(frame.session)
return FinishView()
}
// The gallery never changes the entry's viewport or line positions.
notice := frame.photo_error || state.load_error || frame.spelling_notice != ""
notice_room := ifx notice then Scale(60) else 0
editor_height := cast(s32)content_height - notice_room
if editor_height < Scale(120) {
editor_height = Scale(120)
}
page := Scroll.Draw(frame.session, Scroll.Props.{.key = cast(u64)290301,
.bounds = Rectangle.{left, content, width, content_height},
.content_height = editor_height + notice_room,
.scroll_offset = frame.page_scroll, .scale = frame.scale})
frame.page_scroll = page.scroll_offset
if !page.opened {
return FinishView()
}
area: TextArea.Props
area.key = cast(u64)290200
area.focus_id = 29020
area.bounds = Rectangle.{left, page.content.y, width, cast(float32)editor_height}
area.value = state.text
area.label = AppLocale("tab_diary")
area.wrap = true
area.scale = frame.scale
area.cursor = state.cursor
area.anchor = state.anchor
area.scroll_y = frame.scroll
area.focused = frame.focused
area.selecting = frame.selecting
// The entry waits while its spelling is checked, so the correction
// always applies to the text that was sent.
area.read_only = !editable || frame.spelling_busy
area.max_bytes = 2147483646
area.max_codepoints = 2147483646
area.input = frame.input
edited := MarkdownEditor.Draw(frame.session, DiaryEditorProps(area))
state.cursor = edited.cursor
state.anchor = edited.anchor
frame.scroll = edited.scroll_y
frame.focused = edited.focused
frame.selecting = edited.selecting
if edited.edit.changed && !pending {
state.action = DIARY_EDIT
state.edit_start = edited.edit.start
state.edit_end = edited.edit.end
state.replacement = edited.edit.replacement
pending = true
frame.save_at = frame.now + 0.75
}
if edited.clipboard_write {
frame.clipboard_write = true
frame.clipboard_text = edited.clipboard_text
}
if notice {
status := frame.spelling_notice
if state.load_error {
status = "diary_read_error"
}
if frame.photo_error {
status = "diary_photo_error"
}
Text.Draw(frame.session, Text.Props.{.bounds = Rectangle.{left,
area.bounds.y + area.bounds.height + cast(float32)Scale(12), width, cast(float32)Scale(40)},
.text = AppLocale(status), .size = frame.font})
}
End(frame.session)
return FinishView()
}