From 23cc439ff22d9b9aefeb7c60198a1f08f5b47d0c Mon Sep 17 00:00:00 2001 From: sheaf Date: Fri, 27 Sep 2024 23:53:33 +0200 Subject: [PATCH] update list model upon undo/redo --- src/app/MetaBrush/Application/Action.hs | 34 +++++++++------- src/app/MetaBrush/UI/StrokeTreeView.hs | 4 -- src/metabrushes/MetaBrush/Document/History.hs | 39 ++++++++++++------- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/app/MetaBrush/Application/Action.hs b/src/app/MetaBrush/Application/Action.hs index 03cd009..dd01901 100644 --- a/src/app/MetaBrush/Application/Action.hs +++ b/src/app/MetaBrush/Application/Action.hs @@ -115,8 +115,8 @@ import MetaBrush.Asset.WindowIcons ( drawClose ) import MetaBrush.Document.Diff import MetaBrush.Document.History - ( DocumentHistory(..), newHistory - , back, fwd + ( DocumentHistory(..), Do(..) + , newHistory, back, fwd ) import MetaBrush.Document.Serialise ( saveDocument, loadDocument ) @@ -134,6 +134,8 @@ import MetaBrush.UI.InfoBar ( updateInfoBar ) import MetaBrush.UI.FileBar ( newFileTab, removeFileTab ) +import MetaBrush.UI.StrokeTreeView + ( applyDiffToListModel ) import MetaBrush.UI.Viewport ( Viewport(..) ) import MetaBrush.Unique @@ -557,25 +559,31 @@ data Redo = Redo instance HandleAction Redo where handleAction uiElts vars _ = updateHistory fwd uiElts vars -updateHistory :: ( DocumentHistory -> DocumentHistory ) -> UIElements -> Variables -> IO () +updateHistory :: ( DocumentHistory -> Maybe ( DocumentHistory, ( Do, HistoryDiff ) ) ) -> UIElements -> Variables -> IO () updateHistory f uiElts vars@( Variables {..} ) = do uiUpdateAction <- STM.atomically do mbUnique <- STM.readTVar activeDocumentTVar case mbUnique of Nothing -> pure ( pure () ) - Just unique -> do - mbDocHistory <- Map.lookup unique <$> STM.readTVar openDocumentsTVar + Just docUnique -> do + mbDocHistory <- Map.lookup docUnique <$> STM.readTVar openDocumentsTVar case mbDocHistory of Nothing -> pure ( pure () ) Just docHistory -> do - let - newDocHistory :: DocumentHistory - newDocHistory = f docHistory - STM.modifyTVar' openDocumentsTVar ( Map.insert unique newDocHistory ) - uiUpdateAction <- updateUIAction NoActiveDocChange uiElts vars - pure do - updateHistoryState uiElts ( Just newDocHistory ) - uiUpdateAction + let mbNewHist = f docHistory + case mbNewHist of + Nothing -> pure ( pure () ) + Just ( newDocHistory, ( doOrUndo, diff ) ) -> do + STM.modifyTVar' openDocumentsTVar ( Map.insert docUnique newDocHistory ) + uiUpdateAction <- updateUIAction NoActiveDocChange uiElts vars + pure do + case diff of + DocumentDiff {} -> return () + HierarchyDiff hDiff -> + applyDiffToListModel parStoresTVar docUnique ( doOrUndo, hDiff ) + ContentDiff {} -> return () + updateHistoryState uiElts ( Just newDocHistory ) + uiUpdateAction uiUpdateAction --------- diff --git a/src/app/MetaBrush/UI/StrokeTreeView.hs b/src/app/MetaBrush/UI/StrokeTreeView.hs index c98463c..739360d 100644 --- a/src/app/MetaBrush/UI/StrokeTreeView.hs +++ b/src/app/MetaBrush/UI/StrokeTreeView.hs @@ -863,10 +863,6 @@ getSelectedItem layersView = do -------------------------------------------------------------------------------- --- | Do or undo? -data Do = Do | Undo - deriving stock ( Eq, Show ) - -- | Do, undo or redo? data DoLayerChange = DoLayerChange !LayerChange | RedoChange | UndoChange diff --git a/src/metabrushes/MetaBrush/Document/History.hs b/src/metabrushes/MetaBrush/Document/History.hs index 380879a..e692b35 100644 --- a/src/metabrushes/MetaBrush/Document/History.hs +++ b/src/metabrushes/MetaBrush/Document/History.hs @@ -1,5 +1,6 @@ module MetaBrush.Document.History ( DocumentHistory(..) + , Do(..) , back, fwd, newHistory, newFutureStep , atStart, atEnd , affirmPresentSaved @@ -49,23 +50,33 @@ instance NFData DocumentHistory where rnf ( History { past = ps, present, future } ) = ps `deepseq` present `deepseq` future `deepseq` () -back :: DocumentHistory -> DocumentHistory -back hist@( History { past = ps, present = c, future = fs } ) = case ps of - Empty - -> hist - qs :|> ( q, diff ) - -> History { past = qs - , present = c { documentContent = q } - , future = ( diff, documentContent c ) : fs } +-- | Do or undo? +data Do = Do | Undo + deriving stock ( Eq, Show ) -fwd :: DocumentHistory -> DocumentHistory -fwd hist@( History { past = ps, present = c, future = fs } ) = case fs of +back :: DocumentHistory -> Maybe ( DocumentHistory, ( Do, HistoryDiff ) ) +back ( History { past = ps, present = c, future = fs } ) = case ps of + Empty + -> Nothing + qs :|> ( q, diff ) + -> Just + ( History { past = qs + , present = c { documentContent = q } + , future = ( diff, documentContent c ) : fs } + , ( Undo, diff ) + ) + +fwd :: DocumentHistory -> Maybe ( DocumentHistory, ( Do, HistoryDiff ) ) +fwd ( History { past = ps, present = c, future = fs } ) = case fs of [] - -> hist + -> Nothing ( diff, g ) : gs - -> History { past = ps :|> ( documentContent c, diff ) - , present = c { documentContent = g } - , future = gs } + -> Just + ( History { past = ps :|> ( documentContent c, diff ) + , present = c { documentContent = g } + , future = gs } + , ( Do, diff ) + ) newHistory :: Document -> DocumentHistory newHistory a = History { past = Empty, present = a, future = [] }