revert: restore thumbnail capture for native saves (#956)

This commit is contained in:
Hakan Seven committed 2026-09-03 21:11:00 +03:00
1 parent edd3faeeae
commit 611ea0696b
4 files changed
+189 -45

No files matched your search

+18
View File
@@ -671,6 +671,8 @@ pub(super) struct OpenCADStudio {
main_window: Option<window::Id>, main_window: Option<window::Id>,
/// Hides drawing UI overlays for one thumbnail capture frame. /// Hides drawing UI overlays for one thumbnail capture frame.
thumbnail_capture_clean: bool, thumbnail_capture_clean: bool,
#[cfg(not(target_arch = "wasm32"))]
pending_native_thumbnail_save: Option<PendingNativeThumbnailSave>,
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
pending_web_thumbnail_save: Option<PendingWebThumbnailSave>, pending_web_thumbnail_save: Option<PendingWebThumbnailSave>,
// ── Floating panel windows ──────────────────────────────────────────── // ── Floating panel windows ────────────────────────────────────────────
@@ -1227,6 +1229,18 @@ pub(super) enum SaveContinuation {
Quit, Quit,
} }
#[derive(Debug, Clone)]
#[cfg(not(target_arch = "wasm32"))]
pub(super) struct PendingNativeThumbnailSave {
tab_id: u64,
path: PathBuf,
version: acadrust::DxfVersion,
purpose: SavePurpose,
continuation: SaveContinuation,
set_current_path: bool,
check_external_change: bool,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
pub(super) struct PendingWebThumbnailSave { pub(super) struct PendingWebThumbnailSave {
@@ -2049,6 +2063,8 @@ pub enum Message {
AutoSave, AutoSave,
/// A clean viewport frame is ready for thumbnail capture. /// A clean viewport frame is ready for thumbnail capture.
ThumbnailCaptureFrame, ThumbnailCaptureFrame,
/// Restore drawing UI after the compositor screenshot is captured.
ThumbnailCaptureFinished,
/// Native background save/autosave completed. /// Native background save/autosave completed.
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
SaveFinished(SaveOutcome), SaveFinished(SaveOutcome),
@@ -3373,6 +3389,8 @@ impl OpenCADStudio {
last_point: None, last_point: None,
main_window: None, main_window: None,
thumbnail_capture_clean: false, thumbnail_capture_clean: false,
#[cfg(not(target_arch = "wasm32"))]
pending_native_thumbnail_save: None,
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
pending_web_thumbnail_save: None, pending_web_thumbnail_save: None,
color_pick_target: None, color_pick_target: None,
+165 -34
View File
@@ -1709,7 +1709,22 @@ pub(super) fn on_open_file(&mut self) -> Task<Message> {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
pub(super) fn on_thumbnail_capture_frame(&mut self) -> Task<Message> { pub(super) fn on_thumbnail_capture_frame(&mut self) -> Task<Message> {
Task::none() let Some(pending) = self.pending_native_thumbnail_save.take() else {
return Task::none();
};
let Some(i) = self.tabs.iter().position(|tab| tab.id == pending.tab_id) else {
self.thumbnail_capture_clean = false;
return Task::none();
};
self.queue_native_save(
i,
pending.path,
pending.version,
pending.purpose,
pending.continuation,
pending.set_current_path,
pending.check_external_change,
)
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
@@ -1801,11 +1816,27 @@ pub(super) fn on_open_file(&mut self) -> Task<Message> {
check_external_change: bool, check_external_change: bool,
) -> Task<Message> { ) -> Task<Message> {
let tab_id = self.tabs[i].id; let tab_id = self.tabs[i].id;
if self
.pending_native_thumbnail_save
.as_ref()
.is_some_and(|pending| pending.tab_id == tab_id)
{
if purpose != crate::app::SavePurpose::Autosave {
self.command_line
.push_info(crate::t!("Save already running for this drawing.").as_ref());
}
return Task::none();
}
let capture_ready =
self.thumbnail_capture_clean && self.pending_native_thumbnail_save.is_none();
if self.active_save_jobs.contains_key(&tab_id) { if self.active_save_jobs.contains_key(&tab_id) {
if purpose != crate::app::SavePurpose::Autosave { if purpose != crate::app::SavePurpose::Autosave {
self.command_line self.command_line
.push_info(crate::t!("Save already running for this drawing.").as_ref()); .push_info(crate::t!("Save already running for this drawing.").as_ref());
} }
if capture_ready {
self.thumbnail_capture_clean = false;
}
return Task::none(); return Task::none();
} }
let destination_is_current = self.tabs[i] let destination_is_current = self.tabs[i]
@@ -1823,6 +1854,9 @@ pub(super) fn on_open_file(&mut self) -> Task<Message> {
self.active_tab = i; self.active_tab = i;
self.save_dialog_for_unsaved = self.save_dialog_for_unsaved =
continuation != crate::app::SaveContinuation::None; continuation != crate::app::SaveContinuation::None;
if capture_ready {
self.thumbnail_capture_clean = false;
}
return self.open_save_dialog_window(i); return self.open_save_dialog_window(i);
} }
if purpose != crate::app::SavePurpose::Autosave if purpose != crate::app::SavePurpose::Autosave
@@ -1847,6 +1881,33 @@ pub(super) fn on_open_file(&mut self) -> Task<Message> {
}); });
self.restore_failed_save_continuation(continuation, i); self.restore_failed_save_continuation(continuation, i);
self.active_modal = Some(crate::app::ModalKind::FileInUse); self.active_modal = Some(crate::app::ModalKind::FileInUse);
if capture_ready {
self.thumbnail_capture_clean = false;
}
return Task::none();
}
if purpose != crate::app::SavePurpose::Autosave
&& i == self.active_tab
&& !self.thumbnail_capture_clean
&& self.main_window.is_some()
&& crate::ui::wrap_bar::dropdown_bounds(
crate::app::view::VIEWPORT_CAPTURE_BOUNDS_ID,
)
.is_some()
{
self.pending_native_thumbnail_save = Some(
crate::app::PendingNativeThumbnailSave {
tab_id,
path,
version,
purpose,
continuation,
set_current_path,
check_external_change,
},
);
self.thumbnail_capture_clean = true;
return Task::none(); return Task::none();
} }
@@ -1873,6 +1934,9 @@ pub(super) fn on_open_file(&mut self) -> Task<Message> {
}); });
self.restore_failed_save_continuation(continuation, i); self.restore_failed_save_continuation(continuation, i);
self.active_modal = Some(crate::app::ModalKind::FileInUse); self.active_modal = Some(crate::app::ModalKind::FileInUse);
if capture_ready {
self.thumbnail_capture_clean = false;
}
return Task::none(); return Task::none();
} }
Err(crate::io::edit_lock::EditLeaseError::Unavailable(error)) => { Err(crate::io::edit_lock::EditLeaseError::Unavailable(error)) => {
@@ -1886,6 +1950,13 @@ pub(super) fn on_open_file(&mut self) -> Task<Message> {
let epoch = self.tabs[i].scene.geometry_epoch; let epoch = self.tabs[i].scene.geometry_epoch;
let revision = self.tabs[i].edit_revision; let revision = self.tabs[i].edit_revision;
let camera_generation = self.tabs[i].scene.camera_generation; let camera_generation = self.tabs[i].scene.camera_generation;
let thumbnail = (purpose != crate::app::SavePurpose::Autosave && i == self.active_tab)
.then_some(version >= acadrust::DxfVersion::AC1027);
let capture_bounds = thumbnail.and_then(|_| {
crate::ui::wrap_bar::dropdown_bounds(
crate::app::view::VIEWPORT_CAPTURE_BOUNDS_ID,
)
});
let clone_started = iced::time::Instant::now(); let clone_started = iced::time::Instant::now();
let snapshot = self.tabs[i].scene.document.clone(); let snapshot = self.tabs[i].scene.document.clone();
let clone_ms = clone_started.elapsed().as_secs_f64() * 1000.0; let clone_ms = clone_started.elapsed().as_secs_f64() * 1000.0;
@@ -1933,6 +2004,9 @@ pub(super) fn on_open_file(&mut self) -> Task<Message> {
let (expected_fingerprint, verify_reader) = match verification { let (expected_fingerprint, verify_reader) = match verification {
Ok(verification) => verification, Ok(verification) => verification,
Err(error) => { Err(error) => {
if capture_ready {
self.thumbnail_capture_clean = false;
}
return Task::perform( return Task::perform(
async move { async move {
crate::app::SaveOutcome { crate::app::SaveOutcome {
@@ -1956,40 +2030,97 @@ pub(super) fn on_open_file(&mut self) -> Task<Message> {
} }
}; };
let worker_path = path.clone(); let worker_path = path.clone();
Task::perform( let capture_window = thumbnail.and(capture_bounds).and(self.main_window);
async move { let mut work = Some((
let result = std::thread::spawn(move || { snapshot,
crate::io::save_owned_as_version_atomic( thumbnail,
snapshot, capture_bounds,
&worker_path, worker_path,
expected_fingerprint,
verify_reader,
path,
previous_autosave,
));
let mut run_save = move |screenshot: Option<iced::window::Screenshot>| {
let (
mut snapshot,
thumbnail,
capture_bounds,
worker_path,
expected_fingerprint,
verify_reader,
path,
previous_autosave,
) = work.take().expect("save capture produced more than one result");
Task::perform(
async move {
let (result, refreshed_preview) = std::thread::spawn(move || {
let mut refreshed_preview = None;
if let Some(png) = thumbnail {
let started = iced::time::Instant::now();
if let Some(preview) = screenshot.as_ref().and_then(|screenshot| {
capture_bounds.and_then(|bounds| {
crate::io::thumbnail::from_screenshot(screenshot, bounds, png)
})
}) {
snapshot.preview = Some(preview);
refreshed_preview = Some(snapshot.preview.clone());
}
if crate::perf::enabled() {
crate::perf_record!(
"[perf] save-thumbnail {:.1}ms",
started.elapsed().as_secs_f64() * 1000.0,
);
}
}
let result = crate::io::save_owned_as_version_atomic(
snapshot,
&worker_path,
version,
backup,
expected_fingerprint,
verify_reader,
);
(result, refreshed_preview)
})
.join()
.unwrap_or_else(|_| {
(
Err(crate::io::SaveFailure::other("save worker panicked")),
None,
)
});
crate::app::SaveOutcome {
job_id,
tab_id,
epoch,
revision,
camera_generation,
path,
version, version,
backup, previous_autosave,
expected_fingerprint, set_current_path,
verify_reader, purpose,
) continuation,
}) refreshed_preview,
.join() result,
.unwrap_or_else(|_| { }
Err(crate::io::SaveFailure::other("save worker panicked")) },
}); Message::SaveFinished,
crate::app::SaveOutcome { )
job_id, };
tab_id, match capture_window {
epoch, Some(window) => iced::window::screenshot(window).map(Some).then(move |screenshot| {
revision, Task::batch([
camera_generation, Task::done(Message::ThumbnailCaptureFinished),
path, run_save(screenshot),
version, ])
previous_autosave, }),
set_current_path, None => {
purpose, self.thumbnail_capture_clean = false;
continuation, run_save(None)
refreshed_preview: None, }
result, }
}
},
Message::SaveFinished,
)
} }
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
+5
View File
@@ -6994,6 +6994,11 @@ impl OpenCADStudio {
Message::ThumbnailCaptureFrame => self.on_thumbnail_capture_frame(), Message::ThumbnailCaptureFrame => self.on_thumbnail_capture_frame(),
Message::ThumbnailCaptureFinished => {
self.thumbnail_capture_clean = false;
Task::none()
}
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
Message::SaveFinished(outcome) => self.on_save_finished(outcome), Message::SaveFinished(outcome) => self.on_save_finished(outcome),
+1 -11
View File
@@ -7,18 +7,12 @@
//! the shared [`dwg_thumbnailer`] core crate (also used by the Windows/macOS //! the shared [`dwg_thumbnailer`] core crate (also used by the Windows/macOS
//! thumbnail handlers). //! thumbnail handlers).
#[cfg(any(target_arch = "wasm32", test))]
use acadrust::{Preview, PreviewFormat}; use acadrust::{Preview, PreviewFormat};
#[cfg(target_arch = "wasm32")]
use iced::Rectangle; use iced::Rectangle;
use image::ImageFormat; use image::{ImageFormat, RgbImage};
#[cfg(any(target_arch = "wasm32", test))]
use image::RgbImage;
#[cfg(any(target_arch = "wasm32", test))]
use std::io::Cursor; use std::io::Cursor;
/// Build a preview from the visible drawing area of an Iced window screenshot. /// Build a preview from the visible drawing area of an Iced window screenshot.
#[cfg(target_arch = "wasm32")]
pub fn from_screenshot( pub fn from_screenshot(
screenshot: &iced::window::Screenshot, screenshot: &iced::window::Screenshot,
bounds: Rectangle, bounds: Rectangle,
@@ -65,7 +59,6 @@ pub fn from_screenshot(
const MAX_DIM: u32 = 256; const MAX_DIM: u32 = 256;
/// Canvas dimensions for an aspect ratio, longest edge = [`MAX_DIM`]. /// Canvas dimensions for an aspect ratio, longest edge = [`MAX_DIM`].
#[cfg(any(target_arch = "wasm32", test))]
fn canvas_dims(aspect: f64) -> (u32, u32) { fn canvas_dims(aspect: f64) -> (u32, u32) {
if aspect >= 1.0 { if aspect >= 1.0 {
(MAX_DIM, ((MAX_DIM as f64 / aspect).round() as u32).clamp(16, MAX_DIM)) (MAX_DIM, ((MAX_DIM as f64 / aspect).round() as u32).clamp(16, MAX_DIM))
@@ -75,7 +68,6 @@ fn canvas_dims(aspect: f64) -> (u32, u32) {
} }
/// Encode PNG for R2013+; older targets receive a BMP/DIB. /// Encode PNG for R2013+; older targets receive a BMP/DIB.
#[cfg(any(target_arch = "wasm32", test))]
fn encode(img: RgbImage, png: bool) -> Option<Preview> { fn encode(img: RgbImage, png: bool) -> Option<Preview> {
if png { if png {
let mut buf = Cursor::new(Vec::new()); let mut buf = Cursor::new(Vec::new());
@@ -89,7 +81,6 @@ fn encode(img: RgbImage, png: bool) -> Option<Preview> {
/// Build an 8-bit palettised, `BI_RLE8`-compressed DIB. `None` when the image /// Build an 8-bit palettised, `BI_RLE8`-compressed DIB. `None` when the image
/// holds more than 256 distinct colours (the caller then uses 24-bit). /// holds more than 256 distinct colours (the caller then uses 24-bit).
#[cfg(any(target_arch = "wasm32", test))]
fn rle8_dib(img: &RgbImage) -> Option<Vec<u8>> { fn rle8_dib(img: &RgbImage) -> Option<Vec<u8>> {
let (w, h) = (img.width(), img.height()); let (w, h) = (img.width(), img.height());
// Exact palette + per-pixel index (top-to-bottom, left-to-right). // Exact palette + per-pixel index (top-to-bottom, left-to-right).
@@ -158,7 +149,6 @@ fn rle8_dib(img: &RgbImage) -> Option<Vec<u8>> {
} }
/// 24-bit uncompressed DIB (fallback): the `image` BMP minus its file header. /// 24-bit uncompressed DIB (fallback): the `image` BMP minus its file header.
#[cfg(any(target_arch = "wasm32", test))]
fn bmp24_dib(img: &RgbImage) -> Option<Vec<u8>> { fn bmp24_dib(img: &RgbImage) -> Option<Vec<u8>> {
let mut buf = Cursor::new(Vec::new()); let mut buf = Cursor::new(Vec::new());
img.write_to(&mut buf, ImageFormat::Bmp).ok()?; img.write_to(&mut buf, ImageFormat::Bmp).ok()?;