|
|
|
|
@ -9,7 +9,6 @@ import filepath "core:path/filepath"
|
|
|
|
|
import "core:strings"
|
|
|
|
|
import rl "vendor:raylib"
|
|
|
|
|
import "../core"
|
|
|
|
|
import "../osdialog"
|
|
|
|
|
import "../shared"
|
|
|
|
|
import "../ui"
|
|
|
|
|
|
|
|
|
|
@ -64,53 +63,33 @@ GUI_App_State :: struct {
|
|
|
|
|
prev_screen: ui.App_Screen,
|
|
|
|
|
slide_offset: f32, // horizontal offset for screen transitions
|
|
|
|
|
slide_progress: f32, // 0→1 progress of slide animation
|
|
|
|
|
// ─── Cached API-key presence (refreshed ~1/sec) ───────────────
|
|
|
|
|
// load_config() returns temp-allocated strings that only live one frame,
|
|
|
|
|
// so we can't keep the Config around — but we only need the booleans for
|
|
|
|
|
// the UI. Refreshing on a time interval avoids re-reading .env every frame.
|
|
|
|
|
cached_has_deepseek: bool,
|
|
|
|
|
cached_has_fal: bool,
|
|
|
|
|
config_check_time: f64, // wall-clock time of last refresh
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clicked :: proc(id: clay.ElementId) -> bool {
|
|
|
|
|
return clay.PointerOver(id) && rl.IsMouseButtonPressed(.LEFT)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Per-frame String Pool ────────────────────────────────────────
|
|
|
|
|
// This pool holds strings that must survive across calls *within* a single
|
|
|
|
|
// frame (e.g. a downloaded panel path handed from resolve_image_path to
|
|
|
|
|
// LoadTexture). It is reset every frame, so it never grows unbounded. Strings
|
|
|
|
|
// that must outlive the frame are stored via strings.clone into owned storage.
|
|
|
|
|
// ─── Persistent String Pool (survives temp-allocator resets) ────
|
|
|
|
|
@(private)
|
|
|
|
|
frame_pool: [256 * 1024]u8
|
|
|
|
|
persistent_pool: [256 * 1024]u8
|
|
|
|
|
@(private)
|
|
|
|
|
frame_pool_offset: int
|
|
|
|
|
persistent_offset: int
|
|
|
|
|
|
|
|
|
|
// frame_pool_clone copies a string into the per-frame pool. The result is only
|
|
|
|
|
// valid until the next reset_frame_pool() call (once per frame).
|
|
|
|
|
@(private)
|
|
|
|
|
frame_pool_clone :: proc(s: string) -> string {
|
|
|
|
|
pool_clone :: proc(s: string) -> string {
|
|
|
|
|
if len(s) == 0 { return "" }
|
|
|
|
|
total := len(s) + 1
|
|
|
|
|
if frame_pool_offset + total > len(frame_pool) {
|
|
|
|
|
if persistent_offset + total > len(persistent_pool) {
|
|
|
|
|
// Pool full — fall back to strings.clone (heap) instead of corrupting old data
|
|
|
|
|
return strings.clone(s)
|
|
|
|
|
}
|
|
|
|
|
start := frame_pool_offset
|
|
|
|
|
start := persistent_offset
|
|
|
|
|
for c, i in s {
|
|
|
|
|
frame_pool[start + i] = u8(c)
|
|
|
|
|
persistent_pool[start + i] = u8(c)
|
|
|
|
|
}
|
|
|
|
|
frame_pool[start + len(s)] = 0
|
|
|
|
|
frame_pool_offset += total
|
|
|
|
|
return string(frame_pool[start:start+len(s)])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reset_frame_pool reclaims the entire per-frame pool. Called once per frame
|
|
|
|
|
// at the top of the loop, before any frame_pool_clone use.
|
|
|
|
|
@(private)
|
|
|
|
|
reset_frame_pool :: proc() {
|
|
|
|
|
frame_pool_offset = 0
|
|
|
|
|
persistent_pool[start + len(s)] = 0
|
|
|
|
|
persistent_offset += total
|
|
|
|
|
return string(persistent_pool[start:start+len(s)])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Panel Image Loading ─────────────────────────────────────────
|
|
|
|
|
@ -138,8 +117,7 @@ resolve_image_path :: proc(url: string) -> string {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cached_path, ok := download_path_cache[url]; ok {
|
|
|
|
|
// Cache stores heap-owned strings; return a per-frame clone for the caller.
|
|
|
|
|
return frame_pool_clone(cached_path)
|
|
|
|
|
return pool_clone(cached_path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, failed := download_failed_cache[url]; failed {
|
|
|
|
|
@ -160,9 +138,8 @@ resolve_image_path :: proc(url: string) -> string {
|
|
|
|
|
local_path := fmt.aprintf("%s/%s", cache_dir, filename)
|
|
|
|
|
|
|
|
|
|
if os.exists(local_path) {
|
|
|
|
|
// Store a heap-owned copy in the persistent cache map.
|
|
|
|
|
download_path_cache[url] = strings.clone(local_path)
|
|
|
|
|
return frame_pool_clone(local_path)
|
|
|
|
|
download_path_cache[url] = pool_clone(local_path)
|
|
|
|
|
return pool_clone(local_path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd := [6]string{"curl", "-L", "-sS", "-o", local_path, url}
|
|
|
|
|
@ -173,8 +150,8 @@ resolve_image_path :: proc(url: string) -> string {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
download_path_cache[url] = strings.clone(local_path)
|
|
|
|
|
return frame_pool_clone(local_path)
|
|
|
|
|
download_path_cache[url] = pool_clone(local_path)
|
|
|
|
|
return pool_clone(local_path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@(private)
|
|
|
|
|
@ -263,27 +240,15 @@ run_gui_app :: proc(state: ^core.Comic_State) -> shared.App_Error {
|
|
|
|
|
compact_mode := shared.is_compact(screen_h)
|
|
|
|
|
dt := rl.GetFrameTime()
|
|
|
|
|
|
|
|
|
|
// Reclaim the per-frame string pool before any frame_pool_clone use.
|
|
|
|
|
reset_frame_pool()
|
|
|
|
|
|
|
|
|
|
update_sidebar_anim(&app, bp, dt)
|
|
|
|
|
update_overlay_anim(&app, dt)
|
|
|
|
|
update_slide_anim(&app, dt)
|
|
|
|
|
|
|
|
|
|
sidebar_w := sidebar_width(bp, app.sidebar_collapsed, app.sidebar_anim)
|
|
|
|
|
main_w := shared.compute_main_width(screen_w, sidebar_w)
|
|
|
|
|
|
|
|
|
|
// load_config() returns temp-allocated strings (one frame of life), so
|
|
|
|
|
// we only cache the booleans we actually need and refresh ~1/sec instead
|
|
|
|
|
// of re-reading .env + environment every frame.
|
|
|
|
|
if rl.GetTime() - app.config_check_time >= 1.0 {
|
|
|
|
|
cfg := shared.load_config()
|
|
|
|
|
app.cached_has_deepseek = len(cfg.deepseek_api_key) > 0
|
|
|
|
|
app.cached_has_fal = len(cfg.fal_api_key) > 0
|
|
|
|
|
app.config_check_time = rl.GetTime()
|
|
|
|
|
}
|
|
|
|
|
has_deepseek_key := app.cached_has_deepseek
|
|
|
|
|
has_fal_key := app.cached_has_fal
|
|
|
|
|
cfg := shared.load_config()
|
|
|
|
|
has_deepseek_key := len(cfg.deepseek_api_key) > 0
|
|
|
|
|
has_fal_key := len(cfg.fal_api_key) > 0
|
|
|
|
|
|
|
|
|
|
clay_update_dimensions(screen_w, screen_h)
|
|
|
|
|
clay_update_input()
|
|
|
|
|
@ -619,10 +584,8 @@ run_gui_app :: proc(state: ^core.Comic_State) -> shared.App_Error {
|
|
|
|
|
push_status_if_nonempty(&app.status_msg, &app.action_log, autosave_tick_with_message(&app.project_path, app.controller.state, app.autosave_enabled, &app.is_dirty, &app.last_autosave_at, &app.last_save_at, app.autosave_interval_s))
|
|
|
|
|
|
|
|
|
|
// ─── Clay Layout Declaration ──────────────────────────────
|
|
|
|
|
if app.editor.show_debug_overlay {
|
|
|
|
|
fmt.eprintf("LAYOUT: screen=%dx%d sidebar_w=%.0f sidebar_anim=%.0f collapsed=%v bp=%v\n",
|
|
|
|
|
screen_w, screen_h, f32(sidebar_width(bp, app.sidebar_collapsed, 0)), app.sidebar_anim, app.sidebar_collapsed, bp)
|
|
|
|
|
}
|
|
|
|
|
fmt.eprintf("LAYOUT: screen=%dx%d sidebar_w=%.0f sidebar_anim=%.0f collapsed=%v bp=%v\n",
|
|
|
|
|
screen_w, screen_h, f32(sidebar_width(bp, app.sidebar_collapsed, 0)), app.sidebar_anim, app.sidebar_collapsed, bp)
|
|
|
|
|
clay.BeginLayout()
|
|
|
|
|
|
|
|
|
|
// Root: horizontal layout (sidebar + main)
|
|
|
|
|
@ -690,13 +653,9 @@ run_gui_app :: proc(state: ^core.Comic_State) -> shared.App_Error {
|
|
|
|
|
rl.EndDrawing()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The controller took the state by value in new_controller, so it owns its
|
|
|
|
|
// own copy of every heap-backed field it allocated or reassigned during the
|
|
|
|
|
// session. Dispose that copy here. The caller's `state` (owned by main())
|
|
|
|
|
// is left untouched for its own defer to clean up. We must NOT move the
|
|
|
|
|
// controller's state back into `state` — the two copies may share backing
|
|
|
|
|
// arrays (e.g. workflow.completed_steps), which would double-free.
|
|
|
|
|
core.dispose_state(&app.controller.state)
|
|
|
|
|
core.dispose_state(state)
|
|
|
|
|
state^ = app.controller.state
|
|
|
|
|
app.controller.state = core.Comic_State{}
|
|
|
|
|
return shared.ok()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -730,41 +689,6 @@ handle_format_clicks :: proc(app: ^GUI_App_State, has_deepseek: bool) {
|
|
|
|
|
if clicked(clay.ID("btn_cbz")) { push_status(&app.status_msg, &app.action_log, set_export_format_with_message(&app.export_format, &app.export_path, .CBZ, &app.is_dirty)) }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Native file dialogs (osdialog) ──────────────────────────────────
|
|
|
|
|
// The dialog returns a temp-allocator string; we clone it into the persistent
|
|
|
|
|
// path field. Guard with WINDOW_TOPMOST so the native dialog stays above the
|
|
|
|
|
// borderless raylib window on platforms that lose focus otherwise.
|
|
|
|
|
handle_browse_clicks :: proc(app: ^GUI_App_State) {
|
|
|
|
|
// Open Project — native .comic.json picker
|
|
|
|
|
if clicked(clay.ID("btn_browse_project")) {
|
|
|
|
|
picked := osdialog.open_file_dialog("", "Project Files:comic.json")
|
|
|
|
|
if len(picked) > 0 {
|
|
|
|
|
delete(app.project_path)
|
|
|
|
|
app.project_path = strings.clone(picked)
|
|
|
|
|
app.is_dirty = true
|
|
|
|
|
push_status(&app.status_msg, &app.action_log, fmt.tprintf("Project: %s", app.project_path))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Export path — native save dialog, filter by current format
|
|
|
|
|
if clicked(clay.ID("btn_browse_export")) {
|
|
|
|
|
ext: string = "pdf"
|
|
|
|
|
switch app.export_format {
|
|
|
|
|
case .PDF: ext = "pdf"
|
|
|
|
|
case .PNG: ext = "png"
|
|
|
|
|
case .CBZ: ext = "cbz"
|
|
|
|
|
}
|
|
|
|
|
filters_str := fmt.tprintf("Export:%s", ext)
|
|
|
|
|
picked := osdialog.save_file_dialog("", "comic", filters_str)
|
|
|
|
|
if len(picked) > 0 {
|
|
|
|
|
delete(app.export_path)
|
|
|
|
|
app.export_path = strings.clone(picked)
|
|
|
|
|
app.is_dirty = true
|
|
|
|
|
push_status(&app.status_msg, &app.action_log, fmt.tprintf("Export: %s", app.export_path))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handle_action_clicks :: proc(app: ^GUI_App_State, can_gen_panels, can_layout, can_export: bool, pages_count: int, shift_down: bool, proj_ok, export_ok: bool, autosave_secs: int, has_fal_key: bool) {
|
|
|
|
|
if clicked(clay.ID("btn_new")) {
|
|
|
|
|
if app.is_dirty && !shift_down { push_status(&app.status_msg, &app.action_log, request_confirmation(&app.show_confirm_overlay, &app.show_help_overlay, &app.pending_confirm, .Reset_Project, "Confirm reset?")) }
|
|
|
|
|
@ -1191,7 +1115,6 @@ process_clicks :: proc(app: ^GUI_App_State, can_gen_panels, can_layout, can_expo
|
|
|
|
|
handle_nav_clicks(app)
|
|
|
|
|
handle_field_clicks(app)
|
|
|
|
|
handle_format_clicks(app, has_deepseek)
|
|
|
|
|
handle_browse_clicks(app)
|
|
|
|
|
handle_action_clicks(app, can_gen_panels, can_layout, can_export, pages_count, shift_down, proj_ok, export_ok, autosave_secs, has_fal_key)
|
|
|
|
|
handle_workspace_nav(app)
|
|
|
|
|
handle_detail_clicks(app)
|
|
|
|
|
|