PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #3536: History: set the History Brush source from the panel (history.setSource) (#2966)
aiongg34c5e7d49eac
diff --cc crates/engine/src/commands.rsindex 07ad0336e,7288c1250..b7dff3e9f--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@@ -1505,7 -1443,7 +1505,8 @@@ fn build() -> Vec<CommandSpec>v.extend(crate::analysis_cmds::specs());v.extend(crate::notes_cmds::specs());v.extend(crate::history_cmds::specs());+ v.extend(crate::history_menu_cmds::specs());+ v.extend(crate::history_source_cmds::specs());v.extend(crate::history_select_cmds::specs());v.extend(crate::proof_sim::specs());v.extend(crate::presets::specs());diff --cc crates/engine/src/lib.rsindex f74a01ee9,af76b9d7e..5a7aa83df--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@@ -58,8 -56,8 +58,9 @@@ pub mod group_view_cmdspub mod guide_layout;pub mod hidden_target;pub mod history_cmds;+pub mod history_menu_cmds;pub mod history_select_cmds;+ pub mod history_source_cmds;pub mod image_cmds;pub mod inspect;pub mod installed_profiles;diff --cc crates/ops/src/lib.rsindex b3898429c,2cffcce1c..a0e274f80--- a/crates/ops/src/lib.rs+++ b/crates/ops/src/lib.rs@@@ -50,21 -52,14 +52,27 @@@ pub struct Historycurrent_label: String,/// The layers targeted when the current document was created (opened or edited).current_layers: LayerTarget,+ /// The states History panel › Clear History took off the list, kept so the next undo puts+ /// them back (Clear History is itself undoable). Any new step drops them.+ cleared: Option<Box<Cleared>>,+ /// [`HistoryState::id`] of the current state.+ current_id: u64,+ /// The id the next state gets: never reused, so a discarded state's id names no other.+ next_id: u64,+ /// The History Brush's source state, set in the History panel (`None`: the default).+ brush_source: Option<u64>,}+/// The undo and redo states [`History::clear_states`] removed.+#[derive(Clone, Debug)]+struct Cleared {+ undo: VecDeque<HistoryState>,+ redo: Vec<HistoryState>,+}++/// The undo label of the step [`History::clear_states`] adds (Edit › Undo Clear History).+pub const CLEAR_HISTORY_LABEL: &str = "Clear History";+impl Default for History {fn default() -> Self {Self::new(50)@@@ -81,7 -76,9 +89,10 @@@ impl Historymax_bytes: 0,current_label: "Open".into(),current_layers: LayerTarget::default(),+ cleared: None,+ current_id: 0,+ next_id: 1,+ brush_source: None,}}@@@ -222,44 -262,14 +295,50 @@@self.undo.clear();self.redo.clear();self.tiles = None;+ self.cleared = None;+ self.reset_brush_source();+ }++ /// Make the History Brush's source the default again (the oldest state held), pinning no state.+ pub fn reset_brush_source(&mut self) {+ self.brush_source = None;}+ /// History panel › Clear History: take every state but the current one off the list without+ /// changing the document. Unlike [`Self::clear`] (Edit › Purge › Histories) it is undoable:+ /// the next [`Self::undo`] puts the states back, and the next recorded step forgets them.+ /// Returns false (and changes nothing) when the current state is the only one.+ pub fn clear_states(&mut self) -> bool {+ if self.undo.is_empty() && self.redo.is_empty() {+ return false;+ }+ let undo = std::mem::take(&mut self.undo);+ let redo = std::mem::take(&mut self.redo);+ self.cleared = Some(Box::new(Cleared { undo, redo }));+ self.tiles = None;+ true+ }++ /// Whether the last change to the list was Clear History (so undo restores the states).+ pub fn is_cleared(&self) -> bool {+ self.cleared.is_some()+ }++ /// The undo and redo states history holds: the cleared ones while Clear History can still be+ /// undone (the list is then empty), so memory figures and the byte budget count them.+ fn held(&self) -> (&VecDeque<HistoryState>, &Vec<HistoryState>) {+ match &self.cleared {+ Some(c) => (&c.undo, &c.redo),+ None => (&self.undo, &self.redo),+ }+ }++ /// Every state history holds, cleared ones included (see [`Self::held`]).+ fn held_states(&self) -> impl Iterator<Item = &HistoryState> {+ let (undo, redo) = self.held();+ undo.iter().chain(redo.iter())+ }+/// Approximate unique pixel bytes held by history (tiles not shared with `current`).pub fn unique_bytes(&self, current: &Document) -> usize {let mut seen = HashSet::new();@@@ -348,22 -354,38 +429,38 @@@for d in pinned {total = total.saturating_add(tile_bytes(d, &mut seen));}- for s in self.redo.iter().rev() {+ for s in redo.iter().rev() {total = total.saturating_add(tile_bytes(&s.doc, &mut seen));}+ // The History Brush's source is kept whatever its age, so its own tiles come first; the+ // newest states then fill what remains of the budget (the most recent one always).- let source = self.brush_source.and_then(|id| self.undo.iter().position(|s| s.id == id));- if let Some(state) = source.and_then(|i| self.undo.get(i)) {++ let source = self.brush_source.and_then(|id| undo.iter().position(|s| s.id == id));++ if let Some(state) = source.and_then(|i| undo.get(i)) {+ total = total.saturating_add(tile_bytes(&state.doc, &mut seen));+ }let mut keep = 0;- for s in undo.iter().rev() {- for (i, s) in self.undo.iter().enumerate().rev() {++ for (i, s) in undo.iter().enumerate().rev() {+ if Some(i) == source {+ continue;+ }total = total.saturating_add(tile_bytes(&s.doc, &mut seen));if total > self.max_bytes && keep >= 1 {break;}keep += 1;}- let drop = undo.len() - keep;- for state in undo.drain(..drop) {- tiles.remove(&state.doc);+ let mut kept = VecDeque::with_capacity(keep + 1);+ let mut dropped = 0;- for (i, state) in std::mem::take(&mut self.undo).into_iter().enumerate().rev() {++ for (i, state) in std::mem::take(undo).into_iter().enumerate().rev() {+ if Some(i) == source || kept.len() < keep + usize::from(source.is_some_and(|j| j > i)) {+ kept.push_front(state);+ } else {+ tiles.remove(&state.doc);+ dropped += 1;+ }}- drop- self.undo = kept;++ *undo = kept;+ dropped}}diff --cc crates/ui-egui/src/retouch_ui.rsindex 20efb9bc0,2b8745314..633d0bb3f--- a/crates/ui-egui/src/retouch_ui.rs+++ b/crates/ui-egui/src/retouch_ui.rs@@@ -622,9 -604,9 +622,9 @@@ pub fn options_bar(app: &mut Photocraftcrate::widgets::value_field(ui, &mut o.cr_tolerance, 0.0..=100.0, "%", 62.0);crate::widgets::checkbox(ui, &mut o.cr_anti_alias, tl!("Anti-alias"));}- Tool::HistoryBrush => opt(ui, "Paints from the document's opening state"),+ Tool::HistoryBrush => opt(ui, tl!("Paints from the History panel's source state")),Tool::QuickSelection => {- crate::widgets::checkbox(ui, &mut o.sample_all_layers, tl!("Sample All Layers"));+ crate::widgets::checkbox(ui, &mut o.sample_all.quick_selection, tl!("Sample All Layers"));crate::widgets::checkbox(ui, &mut o.enhance_edge, tl!("Enhance Edge"));opt(ui, &crate::i18n::fmt(tl!("{key} to subtract"), &[("key", &crate::shortcuts::pretty("Alt"))]));crate::widgets::vline(ui, 22.0);