Class: RubyCurses::AbstractUndo

Inherits:
Object
  • Object
show all
Defined in:
lib/rbcurse/experimental/widgets/undomanager.rb

Overview

AbstractUndo has the basic workings of the undo redo facility. It leaves the actual undo and redo to the implementing source object. However, it does the work of storing edits, and passing the correct edit to the implementor when the source object calls for an undo or redo operation. It thus manages the edit (undo) queue.

Direct Known Subclasses

SimpleUndo

Instance Method Summary collapse

Constructor Details

#initialize(_source) ⇒ AbstractUndo

initialize the source object which will issue undo requests



32
33
34
35
36
37
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 32

def initialize _source
  source(_source) #if _source
  @pointer = 0
  @actions = []
  $log.debug " INSIDE UNDO CONSTR "
end

Instance Method Details

#add_edit(event) ⇒ Object

this is called whenever an undoable edit has happened. Currently, it is linked above in the bind statement. We’ve attached this method as a listener to the source.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 51

def add_edit event
  # this debug is very specific. it should be removed later. We do not know about the object
  $log.debug " UNDO GOT #{event}: #{event.type}, (#{event.text}), rej: #{@reject_update}  "
  return if @reject_update
  if @pointer < @actions.length
    $log.debug " removing some actions since #{@pointer} < #{@actions.length} "
    @actions.slice!(@pointer..-1)
    $log.debug " removed actions since #{@pointer} , #{@actions.length} "
  end
  @actions << event
  @pointer = @actions.length
end

#perform_redo(edit) ⇒ Object



91
92
93
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 91

def perform_redo edit
  raise "You must implement this for your undoable component "
end

#perform_undo(edit) ⇒ Object



94
95
96
97
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 94

def perform_undo edit
  raise "You must implement this for your undoable component "
  # to be implemented
end

#redoObject

this has to be bound in source typically bind C-r to redo() this method figures out the correct redo object to be sent to the implementor



81
82
83
84
85
86
87
88
89
90
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 81

def redo
  $log.debug "UNDO GOT REDO call #{@pointer}, #{@actions.size}  "
  return if @pointer >= @actions.size
  @reject_update = true
  edit = @actions[@pointer]
  perform_redo edit
  @source.repaint_required true
  @pointer +=1 #if @pointer > 0
  @reject_update = false
end

#source(_source) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 38

def source(_source)
  $log.debug " calling source= "
  raise "Cannot pass a nil source" unless _source
  @source = _source
  # currently this is very hardcode again. we need to see this in the light of other objects
  #@source.bind(:CHANGE){|eve| add_edit(eve) }
  # a little roundabout but done for getting things up fast
  @source.undo_handler(self)
  $log.debug " I am listening to change events on #{@source.name} "
end

#undoObject

this has to be bound in source component typically bind C-_ to undo() this method figures out the correct undo object to be sent to the implementor



67
68
69
70
71
72
73
74
75
76
# File 'lib/rbcurse/experimental/widgets/undomanager.rb', line 67

def undo
  $log.debug " got UNDO call #{@pointer}, sz:#{@actions.size}  "
  return if @pointer == 0
  @reject_update = true
  @pointer -=1 #if @pointer > 0
  @source.repaint_required true
  @reject_update = false
  edit = @actions[@pointer]
  perform_undo edit
end