Skip to content

fix: cache preview PNG bytes to avoid regeneration on apply#18

Merged
jiuya merged 3 commits into
mainfrom
copilot/fix-image-regeneration-on-apply
Apr 21, 2026
Merged

fix: cache preview PNG bytes to avoid regeneration on apply#18
jiuya merged 3 commits into
mainfrom
copilot/fix-image-regeneration-on-apply

Conversation

Copilot AI commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Clicking "Apply" after previewing regenerated the image from scratch — wasting Gemini API quota and risking a different result than what was previewed (non-deterministic generation + sun position drift between calls).

Changes

AppState — new cache field

  • Added cached_preview: Mutex<Option<Vec<u8>>> to hold the last previewed PNG bytes in memory

commands/mod.rs — populate & consume cache

  • preview_image_with_config / preview_image_enhanced_with_config: now accept State<'_, AppState> and store generated bytes into the cache after encoding (no extra clone)
  • apply_to_lockscreen_with_config: takes cache via take() (consuming it atomically); if present, writes directly to disk and applies — skipping all regeneration; falls back to run_once_with_config if cache is empty

scheduler/mod.rs — cache invalidation

  • run_once_with_config clears the cache at the start of every scheduled/manual update cycle, preventing stale preview bytes from being applied by the scheduler
  • output_image_path promoted to pub so the apply command can resolve the output path without duplicating logic

Flow

preview_image_with_config(cfg, state)
  → render → encode → state.cached_preview = Some(bytes)   // cache populated

apply_to_lockscreen_with_config(cfg, app, state)
  → state.cached_preview.take()
      Some(bytes) → fs::write + set_lockscreen_image        // no regeneration
      None        → run_once_with_config(app, cfg)          // fallback

Copilot AI linked an issue Apr 14, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix image regeneration when applying settings fix: cache preview PNG bytes to avoid regeneration on apply Apr 14, 2026
Copilot AI requested a review from jiuya April 14, 2026 09:44
Copilot AI and others added 3 commits April 21, 2026 21:23
…ebase

After rebasing onto main (which updated enhance_image to take &[u8]),
revert call sites back to passing &png_bytes / &base_png accordingly.
@jiuya jiuya force-pushed the copilot/fix-image-regeneration-on-apply branch from 97bb043 to d4f7995 Compare April 21, 2026 12:28
@jiuya jiuya marked this pull request as ready for review April 21, 2026 12:29
@jiuya jiuya merged commit 7d6493c into main Apr 21, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

適用するときに画像を再生成している

2 participants