2026-01-07 20:42:28 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2026-01-07 20:22:49 +01:00
|
|
|
// src/app/update.rs
|
|
|
|
|
//
|
|
|
|
|
// Application update loop: applies messages to the global model state.
|
|
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
use cosmic::{Action, Task};
|
|
|
|
|
|
2026-01-07 20:22:49 +01:00
|
|
|
use super::document;
|
|
|
|
|
use super::message::AppMessage;
|
2026-01-18 20:35:12 +01:00
|
|
|
use super::model::{AppModel, ToolMode, ViewMode};
|
|
|
|
|
use crate::config::AppConfig;
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
// Update Result
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
pub enum UpdateResult {
|
|
|
|
|
None,
|
|
|
|
|
Task(Task<Action<AppMessage>>),
|
|
|
|
|
}
|
2026-01-07 20:22:49 +01:00
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
// =============================================================================
|
|
|
|
|
// Main Update Function
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
pub fn update(model: &mut AppModel, msg: &AppMessage, config: &AppConfig) -> UpdateResult {
|
2026-01-07 20:22:49 +01:00
|
|
|
match msg {
|
2026-01-18 20:35:12 +01:00
|
|
|
// ---- File / navigation ----------------------------------------------------
|
2026-01-07 20:22:49 +01:00
|
|
|
AppMessage::OpenPath(path) => {
|
2026-01-18 20:35:12 +01:00
|
|
|
document::file::open_single_file(model, path);
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppMessage::NextDocument => {
|
2026-01-08 12:18:13 +01:00
|
|
|
document::file::navigate_next(model);
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppMessage::PrevDocument => {
|
2026-01-08 12:18:13 +01:00
|
|
|
document::file::navigate_prev(model);
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
AppMessage::GotoPage(page) => {
|
2026-01-19 19:42:54 +01:00
|
|
|
if let Some(doc) = &mut model.document
|
2026-01-22 20:40:36 +01:00
|
|
|
&& let Err(e) = doc.go_to_page(*page)
|
|
|
|
|
{
|
|
|
|
|
log::error!("Failed to navigate to page {page}: {e}");
|
|
|
|
|
}
|
2026-01-18 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Thumbnail generation -------------------------------------------------
|
|
|
|
|
AppMessage::GenerateThumbnailPage(page) => {
|
2026-01-19 19:42:54 +01:00
|
|
|
if let Some(doc) = &mut model.document
|
2026-01-22 20:40:36 +01:00
|
|
|
&& let Some(next_page) = doc.generate_thumbnail_page(*page)
|
|
|
|
|
{
|
|
|
|
|
return UpdateResult::Task(Task::batch([
|
|
|
|
|
Task::future(async move {
|
|
|
|
|
Action::App(AppMessage::GenerateThumbnailPage(next_page))
|
|
|
|
|
}),
|
|
|
|
|
Task::done(Action::App(AppMessage::RefreshView)),
|
|
|
|
|
]));
|
|
|
|
|
}
|
2026-01-18 20:35:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppMessage::RefreshView => {
|
|
|
|
|
model.tick += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- View / zoom ---------------------------------------------------------
|
|
|
|
|
AppMessage::ZoomIn => {
|
|
|
|
|
zoom_in(model, config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppMessage::ZoomOut => {
|
|
|
|
|
zoom_out(model, config);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-07 20:22:49 +01:00
|
|
|
AppMessage::ZoomReset => {
|
|
|
|
|
model.view_mode = ViewMode::ActualSize;
|
|
|
|
|
model.reset_pan();
|
|
|
|
|
}
|
2026-01-18 20:35:12 +01:00
|
|
|
|
2026-01-07 20:22:49 +01:00
|
|
|
AppMessage::ZoomFit => {
|
|
|
|
|
model.view_mode = ViewMode::Fit;
|
|
|
|
|
model.reset_pan();
|
|
|
|
|
}
|
2026-01-18 20:35:12 +01:00
|
|
|
|
|
|
|
|
AppMessage::ViewerStateChanged {
|
|
|
|
|
scale,
|
|
|
|
|
offset_x,
|
|
|
|
|
offset_y,
|
|
|
|
|
} => {
|
|
|
|
|
model.view_mode = ViewMode::Custom(*scale);
|
|
|
|
|
model.pan_x = *offset_x;
|
|
|
|
|
model.pan_y = *offset_y;
|
2026-01-15 18:10:57 +01:00
|
|
|
}
|
2026-01-07 20:22:49 +01:00
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
// ---- Pan control ---------------------------------------------------------
|
2026-01-07 20:22:49 +01:00
|
|
|
AppMessage::PanLeft => {
|
2026-01-18 20:35:12 +01:00
|
|
|
model.pan_x -= config.pan_step;
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
AppMessage::PanRight => {
|
2026-01-18 20:35:12 +01:00
|
|
|
model.pan_x += config.pan_step;
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
AppMessage::PanUp => {
|
2026-01-18 20:35:12 +01:00
|
|
|
model.pan_y -= config.pan_step;
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
AppMessage::PanDown => {
|
2026-01-18 20:35:12 +01:00
|
|
|
model.pan_y += config.pan_step;
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
AppMessage::PanReset => {
|
|
|
|
|
model.reset_pan();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
// ---- Tool modes ----------------------------------------------------------
|
2026-01-07 20:22:49 +01:00
|
|
|
AppMessage::ToggleCropMode => {
|
2026-01-22 20:40:36 +01:00
|
|
|
eprintln!(
|
|
|
|
|
"DEBUG: ToggleCropMode received, current tool_mode={:?}",
|
|
|
|
|
model.tool_mode
|
|
|
|
|
);
|
2026-01-07 20:22:49 +01:00
|
|
|
model.tool_mode = if model.tool_mode == ToolMode::Crop {
|
|
|
|
|
ToolMode::None
|
|
|
|
|
} else {
|
|
|
|
|
ToolMode::Crop
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
AppMessage::ToggleScaleMode => {
|
|
|
|
|
model.tool_mode = if model.tool_mode == ToolMode::Scale {
|
|
|
|
|
ToolMode::None
|
|
|
|
|
} else {
|
|
|
|
|
ToolMode::Scale
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 20:40:36 +01:00
|
|
|
// ---- Crop operations -----------------------------------------------------
|
|
|
|
|
AppMessage::StartCrop => {
|
|
|
|
|
if model.document.is_some() {
|
|
|
|
|
model.tool_mode = ToolMode::Crop;
|
|
|
|
|
model.crop_selection.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AppMessage::CancelCrop => {
|
|
|
|
|
if model.tool_mode == ToolMode::Crop {
|
|
|
|
|
model.tool_mode = ToolMode::None;
|
|
|
|
|
model.crop_selection.reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AppMessage::ApplyCrop => {
|
|
|
|
|
if model.tool_mode == ToolMode::Crop {
|
|
|
|
|
if let Some((x, y, width, height)) = model.crop_selection.as_pixel_rect() {
|
|
|
|
|
if let Some(path) = &model.current_path {
|
|
|
|
|
if let Some(doc) = &model.document {
|
|
|
|
|
match document::file::save_crop_as(doc, path, x, y, width, height) {
|
|
|
|
|
Ok(new_path) => {
|
|
|
|
|
document::file::open_single_file(model, &new_path);
|
|
|
|
|
model.tool_mode = ToolMode::None;
|
|
|
|
|
model.crop_selection.reset();
|
|
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
model.set_error(format!("Crop save failed: {e}"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AppMessage::CropDragStart { x, y, handle } => {
|
|
|
|
|
if model.tool_mode == ToolMode::Crop {
|
|
|
|
|
if *handle == super::view::crop::DragHandle::None {
|
|
|
|
|
model.crop_selection.start_new_selection(*x, *y);
|
|
|
|
|
} else {
|
|
|
|
|
model.crop_selection.start_handle_drag(*handle, *x, *y);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AppMessage::CropDragMove { x, y } => {
|
|
|
|
|
if model.tool_mode == ToolMode::Crop {
|
|
|
|
|
if let Some(doc) = &model.document {
|
|
|
|
|
let (w, h) = doc.dimensions();
|
|
|
|
|
#[allow(clippy::cast_precision_loss)]
|
|
|
|
|
model.crop_selection.update_drag(*x, *y, w as f32, h as f32);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AppMessage::CropDragEnd => {
|
|
|
|
|
if model.tool_mode == ToolMode::Crop {
|
|
|
|
|
model.crop_selection.end_drag();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- Save operations -----------------------------------------------------
|
|
|
|
|
AppMessage::SaveAs => {
|
|
|
|
|
save_as(model);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
// ---- Document transformations --------------------------------------------
|
2026-01-07 20:22:49 +01:00
|
|
|
AppMessage::FlipHorizontal => {
|
|
|
|
|
if let Some(doc) = &mut model.document {
|
2026-01-18 20:35:12 +01:00
|
|
|
doc.flip_horizontal();
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AppMessage::FlipVertical => {
|
|
|
|
|
if let Some(doc) = &mut model.document {
|
2026-01-18 20:35:12 +01:00
|
|
|
doc.flip_vertical();
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AppMessage::RotateCW => {
|
|
|
|
|
if let Some(doc) = &mut model.document {
|
2026-01-18 20:35:12 +01:00
|
|
|
doc.rotate_cw();
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AppMessage::RotateCCW => {
|
|
|
|
|
if let Some(doc) = &mut model.document {
|
2026-01-18 20:35:12 +01:00
|
|
|
doc.rotate_ccw();
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
// ---- Metadata ------------------------------------------------------------
|
Implement comprehensive metadata extraction for raster images with
EXIF support and display in the right panel.
New features:
- Extract basic metadata (filename, format, resolution, file size, color type)
- Parse EXIF data (camera, date, exposure, aperture, ISO, focal length, GPS)
- Display metadata in collapsible right panel (toggle with 'i' key)
- Auto-refresh metadata on document navigation
Changes by file:
Cargo.toml, Cargo.lock:
- Add kamadak-exif dependency for EXIF parsing
i18n/en/noctua.ftl:
- Add translation strings for all metadata labels
src/app/document/meta.rs:
- New module for metadata types (BasicMeta, ExifMeta, DocumentMeta)
- Extraction logic with EXIF parsing via kamadak-exif
- Helper methods for formatted display (resolution, file size, camera, GPS)
src/app/document/mod.rs:
- Re-export meta module
src/app/document/{raster,vector,portable}.rs:
- Add extract_metadata() method stubs (full impl for raster)
src/app/document/file.rs:
- Reset metadata on document change
src/app/message.rs:
- Add ToggleRightPanel and RefreshMetadata messages
src/app/model.rs:
- Add metadata: Option<DocumentMeta> field
- Add show_right_panel: bool field
src/app/update.rs:
- Handle panel toggle and metadata refresh
- Auto-refresh metadata on navigation when panel visible
src/app/view/panels.rs:
- Implement right_panel() with metadata display
- Conditional sections for basic info and EXIF data
src/app/view/canvas.rs:
- Integrate right panel into layout"
2026-01-10 11:46:07 +01:00
|
|
|
AppMessage::RefreshMetadata => {
|
|
|
|
|
refresh_metadata(model);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
// ---- Wallpaper -----------------------------------------------------------
|
2026-01-15 20:37:14 +01:00
|
|
|
AppMessage::SetAsWallpaper => {
|
|
|
|
|
set_as_wallpaper(model);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
// ---- Error handling ------------------------------------------------------
|
2026-01-07 20:22:49 +01:00
|
|
|
AppMessage::ShowError(msg) => {
|
2026-01-18 20:35:12 +01:00
|
|
|
model.set_error(msg.clone());
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
AppMessage::ClearError => {
|
|
|
|
|
model.clear_error();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
// ---- Handled elsewhere ---------------------------------------------------
|
|
|
|
|
AppMessage::ToggleContextPage(_) | AppMessage::ToggleNavBar => {}
|
2026-01-14 17:16:25 +01:00
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
AppMessage::NoOp => {}
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
2026-01-18 20:35:12 +01:00
|
|
|
|
|
|
|
|
UpdateResult::None
|
2026-01-07 20:22:49 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
// =============================================================================
|
|
|
|
|
// View Helpers
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
fn zoom_in(model: &mut AppModel, config: &AppConfig) {
|
2026-01-08 12:18:13 +01:00
|
|
|
let current = current_zoom(model);
|
2026-01-18 20:35:12 +01:00
|
|
|
let new_zoom = (current * config.scale_step).clamp(config.min_scale, config.max_scale);
|
|
|
|
|
let factor = new_zoom / current;
|
|
|
|
|
model.pan_x *= factor;
|
|
|
|
|
model.pan_y *= factor;
|
2026-01-07 20:22:49 +01:00
|
|
|
model.view_mode = ViewMode::Custom(new_zoom);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 20:35:12 +01:00
|
|
|
fn zoom_out(model: &mut AppModel, config: &AppConfig) {
|
2026-01-08 12:18:13 +01:00
|
|
|
let current = current_zoom(model);
|
2026-01-18 20:35:12 +01:00
|
|
|
let new_zoom = (current / config.scale_step).clamp(config.min_scale, config.max_scale);
|
|
|
|
|
let factor = new_zoom / current;
|
|
|
|
|
model.pan_x *= factor;
|
|
|
|
|
model.pan_y *= factor;
|
2026-01-07 20:22:49 +01:00
|
|
|
model.view_mode = ViewMode::Custom(new_zoom);
|
|
|
|
|
}
|
2026-01-08 12:18:13 +01:00
|
|
|
|
|
|
|
|
fn current_zoom(model: &AppModel) -> f32 {
|
|
|
|
|
match model.view_mode {
|
2026-01-18 20:35:12 +01:00
|
|
|
ViewMode::Fit | ViewMode::ActualSize => 1.0,
|
2026-01-08 12:18:13 +01:00
|
|
|
ViewMode::Custom(z) => z,
|
|
|
|
|
}
|
|
|
|
|
}
|
Implement comprehensive metadata extraction for raster images with
EXIF support and display in the right panel.
New features:
- Extract basic metadata (filename, format, resolution, file size, color type)
- Parse EXIF data (camera, date, exposure, aperture, ISO, focal length, GPS)
- Display metadata in collapsible right panel (toggle with 'i' key)
- Auto-refresh metadata on document navigation
Changes by file:
Cargo.toml, Cargo.lock:
- Add kamadak-exif dependency for EXIF parsing
i18n/en/noctua.ftl:
- Add translation strings for all metadata labels
src/app/document/meta.rs:
- New module for metadata types (BasicMeta, ExifMeta, DocumentMeta)
- Extraction logic with EXIF parsing via kamadak-exif
- Helper methods for formatted display (resolution, file size, camera, GPS)
src/app/document/mod.rs:
- Re-export meta module
src/app/document/{raster,vector,portable}.rs:
- Add extract_metadata() method stubs (full impl for raster)
src/app/document/file.rs:
- Reset metadata on document change
src/app/message.rs:
- Add ToggleRightPanel and RefreshMetadata messages
src/app/model.rs:
- Add metadata: Option<DocumentMeta> field
- Add show_right_panel: bool field
src/app/update.rs:
- Handle panel toggle and metadata refresh
- Auto-refresh metadata on navigation when panel visible
src/app/view/panels.rs:
- Implement right_panel() with metadata display
- Conditional sections for basic info and EXIF data
src/app/view/canvas.rs:
- Integrate right panel into layout"
2026-01-10 11:46:07 +01:00
|
|
|
|
|
|
|
|
fn refresh_metadata(model: &mut AppModel) {
|
2026-01-18 20:35:12 +01:00
|
|
|
model.metadata = match (&model.document, &model.current_path) {
|
|
|
|
|
(Some(doc), Some(path)) => Some(doc.extract_meta(path)),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
Implement comprehensive metadata extraction for raster images with
EXIF support and display in the right panel.
New features:
- Extract basic metadata (filename, format, resolution, file size, color type)
- Parse EXIF data (camera, date, exposure, aperture, ISO, focal length, GPS)
- Display metadata in collapsible right panel (toggle with 'i' key)
- Auto-refresh metadata on document navigation
Changes by file:
Cargo.toml, Cargo.lock:
- Add kamadak-exif dependency for EXIF parsing
i18n/en/noctua.ftl:
- Add translation strings for all metadata labels
src/app/document/meta.rs:
- New module for metadata types (BasicMeta, ExifMeta, DocumentMeta)
- Extraction logic with EXIF parsing via kamadak-exif
- Helper methods for formatted display (resolution, file size, camera, GPS)
src/app/document/mod.rs:
- Re-export meta module
src/app/document/{raster,vector,portable}.rs:
- Add extract_metadata() method stubs (full impl for raster)
src/app/document/file.rs:
- Reset metadata on document change
src/app/message.rs:
- Add ToggleRightPanel and RefreshMetadata messages
src/app/model.rs:
- Add metadata: Option<DocumentMeta> field
- Add show_right_panel: bool field
src/app/update.rs:
- Handle panel toggle and metadata refresh
- Auto-refresh metadata on navigation when panel visible
src/app/view/panels.rs:
- Implement right_panel() with metadata display
- Conditional sections for basic info and EXIF data
src/app/view/canvas.rs:
- Integrate right panel into layout"
2026-01-10 11:46:07 +01:00
|
|
|
}
|
2026-01-15 20:37:14 +01:00
|
|
|
|
|
|
|
|
fn set_as_wallpaper(model: &mut AppModel) {
|
|
|
|
|
let Some(path) = model.current_path.as_ref() else {
|
|
|
|
|
model.set_error("No image loaded");
|
|
|
|
|
return;
|
|
|
|
|
};
|
2026-01-18 20:35:12 +01:00
|
|
|
document::set_as_wallpaper(path);
|
2026-01-15 20:37:14 +01:00
|
|
|
}
|
2026-01-22 20:40:36 +01:00
|
|
|
|
|
|
|
|
fn save_as(model: &mut AppModel) {
|
|
|
|
|
// TODO: Implement file dialog for save path
|
|
|
|
|
// For now, show error that this needs UI integration
|
|
|
|
|
model.set_error("Save As: File dialog not yet implemented");
|
|
|
|
|
}
|