SystemWindow subclass: #LUFixedWindow
 instanceVariableNames: 'fixedExtent' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Undo'!

Object subclass: #LUBoard
 instanceVariableNames: 'text strokes' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Undo'!

Object subclass: #LUCommand
 instanceVariableNames: 'label before after' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Undo'!

Object subclass: #LUCommandGroup
 instanceVariableNames: 'label commands' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Undo'!

Object subclass: #LUHistory
 instanceVariableNames: 'model undoStack redoStack serial currentId savedId status' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Undo'!

Morph subclass: #LUCanvas
 instanceVariableNames: 'app pending drawingHand' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Undo'!

Morph subclass: #LUUndoApp
 instanceVariableNames: 'board history canvas summary statusMorph window' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-Undo'!

!LUFixedWindow methodsFor: 'tutorial'!
fixedExtent: aPoint
 fixedExtent := aPoint.
 super extent: aPoint
! !


!LUFixedWindow methodsFor: 'tutorial'!
extent: aPoint
 super extent: (fixedExtent ifNil: [aPoint])
! !


!LUBoard methodsFor: 'tutorial'!
initialize
 super initialize. text := ''. strokes := OrderedCollection new
! !


!LUBoard methodsFor: 'tutorial'!
text
 ^ text copy
! !


!LUBoard methodsFor: 'tutorial'!
text: aString
 aString size > 500 ifTrue: [self error: 'Keep the caption to 500 characters'].
 text := aString asString copy
! !


!LUBoard methodsFor: 'tutorial'!
strokes
 ^ strokes deepCopy
! !


!LUBoard methodsFor: 'tutorial'!
addStroke: points
 points isEmpty ifTrue: [^ self].
 (strokes size >= 80 or: [points size > 600]) ifTrue: [self error: 'Limit: 80 strokes, 600 points per stroke'].
 strokes addLast: points deepCopy
! !


!LUBoard methodsFor: 'tutorial'!
snapshot
 ^ {text copy. strokes deepCopy}
! !


!LUBoard methodsFor: 'tutorial'!
restore: state
 text := state first copy. strokes := state second deepCopy
! !


!LUCommand methodsFor: 'tutorial'!
label: aString before: firstState after: lastState
 label := aString copy. before := firstState deepCopy. after := lastState deepCopy
! !


!LUCommand methodsFor: 'tutorial'!
label
 ^ label
! !


!LUCommand methodsFor: 'tutorial'!
executeOn: aModel
 aModel snapshot = before ifFalse: [self error: 'Stale command'].
 aModel restore: after
! !


!LUCommand methodsFor: 'tutorial'!
undoOn: aModel
 aModel snapshot = after ifFalse: [self error: 'Stale undo'].
 aModel restore: before
! !


!LUHistory methodsFor: 'tutorial'!
initialize
 super initialize. undoStack := OrderedCollection new. redoStack := OrderedCollection new.
 serial := 0. currentId := 0. savedId := 0. status := 'Ready: changes become commands.'
! !


!LUHistory methodsFor: 'tutorial'!
model: anObject
 model := anObject
! !


!LUHistory methodsFor: 'tutorial'!
model
 ^ model
! !


!LUHistory methodsFor: 'tutorial'!
status
 ^ status
! !


!LUHistory methodsFor: 'tutorial'!
status: aString
 status := aString
! !


!LUHistory methodsFor: 'tutorial'!
undoCount
 ^ undoStack size
! !


!LUHistory methodsFor: 'tutorial'!
redoCount
 ^ redoStack size
! !


!LUHistory methodsFor: 'tutorial'!
isDirty
 ^ currentId ~= savedId
! !


!LUHistory methodsFor: 'tutorial'!
perform: aLabel change: aBlock
 | draft command |
 draft := model class new. draft restore: model snapshot.
 [aBlock value: draft] on: Error do: [:error | status := 'Rejected: ', error messageText. ^ false].
 draft snapshot = model snapshot ifTrue: [status := 'No change; no history entry.'. ^ false].
 command := LUCommand new label: aLabel before: model snapshot after: draft snapshot.
 ^ self execute: command
! !


!LUHistory methodsFor: 'tutorial'!
execute: command
 | original entry |
 original := model snapshot.
 [command executeOn: model] on: Error do: [:error |
  model restore: original. status := 'Command failed; state and stacks restored.'. ^ false].
 model snapshot = original ifTrue: [status := 'No change; no history entry.'. ^ false].
 serial := serial + 1. entry := {command. currentId. serial}.
 undoStack addLast: entry. undoStack size > 50 ifTrue: [undoStack removeFirst]. redoStack removeAll. currentId := serial.
 status := command label, ' applied.'.
 ^ true
! !


!LUHistory methodsFor: 'tutorial'!
undo
 | entry original |
 undoStack isEmpty ifTrue: [status := 'Nothing to undo.'. ^ false].
 entry := undoStack last. original := model snapshot.
 [entry first undoOn: model] on: Error do: [:error |
  model restore: original. status := 'Undo failed; state and stacks restored.'. ^ false].
 undoStack removeLast. redoStack addLast: entry. currentId := entry second.
 status := 'Undid ', entry first label, '.'. ^ true
! !


!LUHistory methodsFor: 'tutorial'!
redo
 | entry original |
 redoStack isEmpty ifTrue: [status := 'Nothing to redo.'. ^ false].
 entry := redoStack last. original := model snapshot.
 [entry first executeOn: model] on: Error do: [:error |
  model restore: original. status := 'Redo failed; state and stacks restored.'. ^ false].
 redoStack removeLast. undoStack addLast: entry. currentId := entry third.
 status := 'Redid ', entry first label, '.'. ^ true
! !


!LUCommandGroup methodsFor: 'tutorial'!
label: aString commands: aCollection
 label := aString. commands := aCollection copy
! !


!LUCommandGroup methodsFor: 'tutorial'!
label
 ^ label
! !


!LUCommandGroup methodsFor: 'tutorial'!
executeOn: aModel
 commands do: [:each | each executeOn: aModel]
! !


!LUCommandGroup methodsFor: 'tutorial'!
undoOn: aModel
 commands reverseDo: [:each | each undoOn: aModel]
! !


!LUHistory methodsFor: 'tutorial'!
performGroup: aLabel changes: blocks
 | draft commands previous |
 draft := model class new. draft restore: model snapshot. commands := OrderedCollection new.
 [blocks do: [:block |
  previous := draft snapshot. block value: draft.
  commands addLast: (LUCommand new label: aLabel before: previous after: draft snapshot)]]
 on: Error do: [:error | status := 'Group preparation failed; no change.'. ^ false].
 draft snapshot = model snapshot ifTrue: [^ false].
 ^ self execute: (LUCommandGroup new label: aLabel commands: commands)
! !


!LUHistory methodsFor: 'tutorial'!
markSaved
 savedId := currentId. status := 'Saved revision marked. Undo/redo can revisit it.'
! !


!LUCanvas methodsFor: 'tutorial'!
initialize
 super initialize. self color: Color white; extent: 690@230. pending := OrderedCollection new
! !


!LUCanvas methodsFor: 'tutorial'!
hasPendingStroke
 ^ pending notEmpty
! !


!LUCanvas methodsFor: 'tutorial'!
discardPendingStroke
 pending removeAll.
 drawingHand ifNotNil: [(drawingHand mouseFocus == self) ifTrue: [drawingHand newMouseFocus: nil]].
 (World activeHand mouseFocus == self) ifTrue: [World activeHand newMouseFocus: nil].
 drawingHand := nil. self changed
! !


!LUCanvas methodsFor: 'tutorial'!
app: anApp
 app := anApp
! !


!LUCanvas methodsFor: 'tutorial'!
drawOn: aCanvas
 super drawOn: aCanvas.
 app board strokes do: [:stroke | self drawStroke: stroke on: aCanvas].
 self drawStroke: pending on: aCanvas
! !


!LUCanvas methodsFor: 'tutorial'!
drawStroke: stroke on: aCanvas
 stroke size = 1 ifTrue: [aCanvas fillOval: (((stroke first + self position) - (3@3)) extent: 6@6) color: (Color fromString: '#236B8E')].
 2 to: stroke size do: [:index |
  aCanvas line: (stroke at: index - 1) + self position to: (stroke at: index) + self position width: 4 color: (Color fromString: '#236B8E')]
! !


!LUCanvas methodsFor: 'tutorial'!
handlesMouseDown: event
 ^ true
! !


!LUCanvas methodsFor: 'tutorial'!
handlesMouseMove: event
 ^ pending notEmpty
! !


!LUCanvas methodsFor: 'tutorial'!
mouseDown: event
 drawingHand := event hand. pending := OrderedCollection with: (self localPoint: event position). self changed. app refresh
! !


!LUCanvas methodsFor: 'tutorial'!
mouseMove: event
 (pending notEmpty and: [pending size < 599]) ifTrue: [pending addLast: (self localPoint: event position). self changed]
! !


!LUCanvas methodsFor: 'tutorial'!
mouseUp: event
 | points |
 pending isEmpty ifTrue: [^ self].
 pending addLast: (self localPoint: event position). points := pending copy. pending removeAll.
 drawingHand := nil. app addStroke: points. self changed
! !


!LUCanvas methodsFor: 'tutorial'!
localPoint: point
 | local | local := point - self position.
 ^ ((local x max: 0) min: self width - 1) @ ((local y max: 0) min: self height - 1)
! !


!LUUndoApp methodsFor: 'tutorial'!
initialize
 super initialize. self color: (Color fromString: '#ECF2F3'); extent: 740@540.
 board := LUBoard new. history := LUHistory new model: board.
 self label: 'Command Studio' at: 22@14 size: 25.
 self label: ('Checkpoint ', self stage printString, ' / 9: ', self stageTitle) at: 23@50 size: 14.
 self label: 'A caption and drawing share one deliberate history.' at: 23@79 size: 13.
 canvas := LUCanvas new app: self; position: self position + (24@184). self addMorph: canvas.
 summary := StringMorph contents: ''. summary position: self position + (24@145). self addMorph: summary.
 statusMorph := StringMorph contents: ''. statusMorph position: self position + (24@488). self addMorph: statusMorph.
 self buildToolbar. self refresh
! !


!LUUndoApp methodsFor: 'tutorial'!
label: string at: point size: size
 | label | label := StringMorph contents: string font: (TextStyle default fontOfSize: size).
 label color: (Color fromString: '#18384B'); position: self position + point. self addMorph: label
! !


!LUUndoApp methodsFor: 'tutorial'!
button: text at: point action: selector
 | button | button := SimpleButtonMorph new label: text; target: self; actionSelector: selector; color: Color white.
 button position: self position + point. self addMorph: button. ^ button
! !


!LUUndoApp methodsFor: 'tutorial'!
buttonNamed: label
 ^ self submorphs detect: [:each | (each isKindOf: SimpleButtonMorph) and: [each label = label]]
! !


!LUUndoApp methodsFor: 'tutorial'!
board
 ^ board
! !


!LUUndoApp methodsFor: 'tutorial'!
history
 ^ history
! !


!LUUndoApp methodsFor: 'tutorial'!
canvas
 ^ canvas
! !


!LUUndoApp methodsFor: 'tutorial'!
refresh
 summary contents: ('Caption: ', (board text isEmpty ifTrue: ['(empty)'] ifFalse: [board text truncateTo: 70])).
 statusMorph contents: (history status truncateTo: 99).
 canvas changed.
 window ifNotNil: [window setLabel: ('Command Studio | undo ', history undoCount printString, ' / redo ', history redoCount printString, (self isDirty ifTrue: [' | modified'] ifFalse: [' | saved']))].
 ^ self
! !


!LUUndoApp methodsFor: 'tutorial'!
setCaption: aString
 history perform: 'caption' change: [:draft | draft text: aString]. self refresh
! !


!LUUndoApp methodsFor: 'tutorial'!
editCaption
 | answer | answer := self request: 'Caption (one undo step)' initial: board text.
 answer ifNil: [history status: 'Caption cancelled.'. ^ self refresh]. self setCaption: answer
! !


!LUUndoApp methodsFor: 'tutorial'!
sampleCaption
 self setCaption: 'Ideas become objects'
! !


!LUUndoApp methodsFor: 'tutorial'!
undo
 history undo. self refresh
! !


!LUUndoApp methodsFor: 'tutorial'!
redo
 history redo. self refresh
! !


!LUUndoApp methodsFor: 'tutorial'!
stamp
 history performGroup: 'two-stroke stamp + caption' changes: {
 [:draft | draft addStroke: {60@170. 120@70. 180@170}].
 [:draft | draft addStroke: {200@170. 260@70. 320@170}].
 [:draft | draft text: 'Two peaks, one command group']}.
 self refresh
! !


!LUUndoApp methodsFor: 'tutorial'!
addStroke: points
 history perform: 'pen stroke' change: [:draft | draft addStroke: points]. self refresh
! !


!LUUndoApp methodsFor: 'tutorial'!
sampleStroke
 self addStroke: ((1 to: 45) collect: [:index | (350 + (index * 6))@(115 + ((index / 5.0) sin * 45))])
! !


!LUUndoApp methodsFor: 'tutorial'!
markSaved
 history markSaved. self refresh
! !


!LUUndoApp methodsFor: 'tutorial'!
rejectOversize
 self setCaption: (String new: 501 withAll: $x)
! !


!LUUndoApp methodsFor: 'tutorial'!
showHelp
 history status: 'Drag on paper. One drag = one command. A new edit discards redo.'. self refresh
! !


!LUUndoApp methodsFor: 'tutorial'!
open
 window := LUFixedWindow new model: self; setLabel: 'Command Studio'.
 window addMorph: self fullFrame: (LayoutFrame fractions: (0@0 corner: 1@1)).
 window fixedExtent: 750@573. window openInWorld. self refresh. ^ self
! !


!LUUndoApp class methodsFor: 'tutorial'!
open
 ^ self new open
! !


!LUUndoApp methodsFor: 'tutorial'!
window
 ^ window
! !


!LUUndoApp methodsFor: 'tutorial'!
isDirty
 ^ history isDirty or: [canvas hasPendingStroke]
! !


!LUUndoApp methodsFor: 'tutorial'!
windowIsClosing
 canvas discardPendingStroke. window := nil
! !


!LUUndoApp methodsFor: 'tutorial'!
okToClose
 ^ self isDirty not or: [self confirm: 'Close and discard this command practice?']
! !


!LUUndoApp methodsFor: 'tutorial'!
stage
 ^ 9
! !


!LUUndoApp methodsFor: 'tutorial'!
stageTitle
 ^ 'Test and package the studio'
! !


!LUUndoApp methodsFor: 'tutorial'!
buildToolbar
 self button: 'Caption' at: 24@111 action: #editCaption.
 self button: 'Sample text' at: 90@111 action: #sampleCaption.
 self button: 'Undo' at: 182@111 action: #undo.
 self button: 'Redo' at: 235@111 action: #redo.
 self button: 'Stamp' at: 288@111 action: #stamp.
 self button: 'Wave' at: 347@111 action: #sampleStroke.
 self button: 'Mark saved' at: 405@111 action: #markSaved.
 self button: 'Reject 501' at: 505@111 action: #rejectOversize.
 self button: 'Help' at: 605@111 action: #showHelp.
! !

