1 ## editing sandboxes after they've been created
  2 
  3 scenario clicking-on-sandbox-edit-button-moves-it-to-editor [
  4   local-scope
  5   trace-until 100/app  # trace too long
  6   assume-screen 100/width, 10/height
  7   # empty recipes
  8   assume-resources [
  9   ]
 10   env:&:environment <- new-programming-environment resources, screen, [add 2, 2]
 11   # run it
 12   assume-console [
 13     press F4
 14   ]
 15   event-loop screen, console, env, resources
 16   screen-should-contain [
 17     .                                                                                 run (F4)           .
 18     .                                                  ╎                                                 .
 19     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 20     .                                                  ╎0   edit          copy            delete         .
 21     .                                                  ╎add 2, 2                                         .
 22     .                                                  ╎4                                                .
 23     .                                                  ╎─────────────────────────────────────────────────.
 24     .                                                  ╎                                                 .
 25   ]
 26   # click at left edge of 'edit' button
 27   assume-console [
 28     left-click 3, 55
 29   ]
 30   run [
 31     event-loop screen, console, env, resources
 32   ]
 33   # it pops back into editor
 34   screen-should-contain [
 35     .                                                                                 run (F4)           .
 36     .                                                  ╎add 2, 2                                         .
 37     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 38     .                                                  ╎                                                 .
 39   ]
 40   # cursor should be in the right place
 41   assume-console [
 42     type [0]
 43   ]
 44   run [
 45     event-loop screen, console, env, resources
 46   ]
 47   screen-should-contain [
 48     .                                                                                 run (F4)           .
 49     .                                                  ╎0add 2, 2                                        .
 50     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 51     .                                                  ╎                                                 .
 52   ]
 53 ]
 54 
 55 scenario clicking-on-sandbox-edit-button-moves-it-to-editor-2 [
 56   local-scope
 57   trace-until 100/app  # trace too long
 58   assume-screen 100/width, 10/height
 59   # empty recipes
 60   assume-resources [
 61   ]
 62   env:&:environment <- new-programming-environment resources, screen, [add 2, 2]
 63   # run it
 64   assume-console [
 65     press F4
 66   ]
 67   event-loop screen, console, env, resources
 68   screen-should-contain [
 69     .                                                                                 run (F4)           .
 70     .                                                  ╎                                                 .
 71     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 72     .                                                  ╎0   edit          copy            delete         .
 73     .                                                  ╎add 2, 2                                         .
 74     .                                                  ╎4                                                .
 75     .                                                  ╎─────────────────────────────────────────────────.
 76     .                                                  ╎                                                 .
 77   ]
 78   # click at right edge of 'edit' button (just before 'copy')
 79   assume-console [
 80     left-click 3, 68
 81   ]
 82   run [
 83     event-loop screen, console, env, resources
 84   ]
 85   # it pops back into editor
 86   screen-should-contain [
 87     .                                                                                 run (F4)           .
 88     .                                                  ╎add 2, 2                                         .
 89     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
 90     .                                                  ╎                                                 .
 91   ]
 92   # cursor should be in the right place
 93   assume-console [
 94     type [0]
 95   ]
 96   run [
 97     event-loop screen, console, env, resources
 98   ]
 99   screen-should-contain [
100     .                                                                                 run (F4)           .
101     .                                                  ╎0add 2, 2                                        .
102     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
103     .                                                  ╎                                                 .
104   ]
105 ]
106 
107 after <global-touch> [
108   # support 'edit' button
109   {
110     edit?:bool <- should-attempt-edit? click-row, click-column, env
111     break-unless edit?
112     edit?, env <- try-edit-sandbox click-row, env
113     break-unless edit?
114     hide-screen screen
115     screen <- render-sandbox-side screen, env, render
116     screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
117     show-screen screen
118     loop +next-event
119   }
120 ]
121 
122 # some preconditions for attempting to edit a sandbox
123 def should-attempt-edit? click-row:num, click-column:num, env:&:environment -> result:bool [
124   local-scope
125   load-ingredients
126   # are we below the sandbox editor?
127   click-sandbox-area?:bool <- click-on-sandbox-area? click-row, click-column, env
128   return-unless click-sandbox-area?, 0/false
129   # narrower, is the click in the columns spanning the 'edit' button?
130   first-sandbox:&:editor <- get *env, current-sandbox:offset
131   assert first-sandbox, [!!]
132   sandbox-left-margin:num <- get *first-sandbox, left:offset
133   sandbox-right-margin:num <- get *first-sandbox, right:offset
134   edit-button-left:num, edit-button-right:num, _ <- sandbox-menu-columns sandbox-left-margin, sandbox-right-margin
135   edit-button-vertical-area?:bool <- within-range? click-column, edit-button-left, edit-button-right
136   return-unless edit-button-vertical-area?, 0/false
137   # finally, is sandbox editor empty?
138   current-sandbox:&:editor <- get *env, current-sandbox:offset
139   result <- empty-editor? current-sandbox
140 ]
141 
142 def try-edit-sandbox click-row:num, env:&:environment -> clicked-on-edit-button?:bool, env:&:environment [
143   local-scope
144   load-ingredients
145   # identify the sandbox to edit, if the click was actually on the 'edit' button
146   sandbox:&:sandbox <- find-sandbox env, click-row
147   return-unless sandbox, 0/false
148   clicked-on-edit-button? <- copy 1/true
149   # 'edit' button = 'copy' button + 'delete' button
150   text:text <- get *sandbox, data:offset
151   current-sandbox:&:editor <- get *env, current-sandbox:offset
152   current-sandbox <- insert-text current-sandbox, text
153   env <- delete-sandbox env, sandbox
154   # reset scroll
155   *env <- put *env, render-from:offset, -1
156   # position cursor in sandbox editor
157   *env <- put *env, sandbox-in-focus?:offset, 1/true
158 ]
159 
160 scenario sandbox-with-print-can-be-edited [
161   local-scope
162   trace-until 100/app  # trace too long
163   assume-screen 100/width, 20/height
164   # left editor is empty
165   assume-resources [
166   ]
167   # right editor contains a print instruction
168   env:&:environment <- new-programming-environment resources, screen, [print-integer screen, 4]
169   # run the sandbox
170   assume-console [
171     press F4
172   ]
173   event-loop screen, console, env, resources
174   screen-should-contain [
175     .                                                                                 run (F4)           .
176     .                                                  ╎                                                 .
177     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
178     .                                                  ╎0   edit          copy            delete         .
179     .                                                  ╎print-integer screen, 4                          .
180     .                                                  ╎screen:                                          .
181     .                                                  ╎  .4                             .               .
182     .                                                  ╎  .                              .               .
183     .                                                  ╎  .                              .               .
184     .                                                  ╎  .                              .               .
185     .                                                  ╎  .                              .               .
186     .                                                  ╎─────────────────────────────────────────────────.
187     .                                                  ╎                                                 .
188   ]
189   # edit the sandbox
190   assume-console [
191     left-click 3, 65
192   ]
193   run [
194     event-loop screen, console, env, resources
195   ]
196   screen-should-contain [
197     .                                                                                 run (F4)           .
198     .                                                  ╎print-integer screen, 4                          .
199     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
200     .                                                  ╎                                                 .
201     .                                                  ╎                                                 .
202   ]
203 ]
204 
205 scenario editing-sandbox-after-scrolling-resets-scroll [
206   local-scope
207   trace-until 100/app  # trace too long
208   assume-screen 100/width, 10/height
209   # initialize environment
210   assume-resources [
211   ]
212   env:&:environment <- new-programming-environment resources, screen, []
213   render-all screen, env, render
214   # create 2 sandboxes and scroll to second
215   assume-console [
216     press ctrl-n
217     type [add 2, 2]
218     press F4
219     type [add 1, 1]
220     press F4
221     press page-down
222     press page-down
223   ]
224   event-loop screen, console, env, resources
225   screen-should-contain [
226     .                                                                                 run (F4)           .
227     .                                                  ╎─────────────────────────────────────────────────.
228     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎1   edit          copy            delete         .
229     .                                                  ╎add 2, 2                                         .
230     .                                                  ╎4                                                .
231     .                                                  ╎─────────────────────────────────────────────────.
232     .                                                  ╎                                                 .
233   ]
234   # edit the second sandbox
235   assume-console [
236     left-click 2, 55
237   ]
238   run [
239     event-loop screen, console, env, resources
240   ]
241   # second sandbox shows in editor; scroll resets to display first sandbox
242   screen-should-contain [
243     .                                                                                 run (F4)           .
244     .                                                  ╎add 2, 2                                         .
245     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
246     .                                                  ╎0   edit          copy            delete         .
247     .                                                  ╎add 1, 1                                         .
248     .                                                  ╎2                                                .
249     .                                                  ╎─────────────────────────────────────────────────.
250     .                                                  ╎                                                 .
251   ]
252 ]
253 
254 scenario editing-sandbox-updates-sandbox-count [
255   local-scope
256   trace-until 100/app  # trace too long
257   assume-screen 100/width, 10/height
258   # initialize environment
259   assume-resources [
260   ]
261   env:&:environment <- new-programming-environment resources, screen, []
262   render-all screen, env, render
263   # create 2 sandboxes
264   assume-console [
265     press ctrl-n
266     type [add 2, 2]
267     press F4
268     type [add 1, 1]
269     press F4
270   ]
271   event-loop screen, console, env, resources
272   screen-should-contain [
273     .                                                                                 run (F4)           .
274     .                                                  ╎                                                 .
275     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
276     .                                                  ╎0   edit          copy            delete         .
277     .                                                  ╎add 1, 1                                         .
278     .                                                  ╎2                                                .
279     .                                                  ╎─────────────────────────────────────────────────.
280     .                                                  ╎1   edit          copy            delete         .
281     .                                                  ╎add 2, 2                                         .
282     .                                                  ╎4                                                .
283   ]
284   # edit the second sandbox, then resave
285   assume-console [
286     left-click 3, 60
287     press F4
288   ]
289   run [
290     event-loop screen, console, env, resources
291   ]
292   # no change in contents
293   screen-should-contain [
294     .                                                                                 run (F4)           .
295     .                                                  ╎                                                 .
296     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
297     .                                                  ╎0   edit          copy            delete         .
298     .                                                  ╎add 1, 1                                         .
299     .                                                  ╎2                                                .
300     .                                                  ╎─────────────────────────────────────────────────.
301     .                                                  ╎1   edit          copy            delete         .
302     .                                                  ╎add 2, 2                                         .
303     .                                                  ╎4                                                .
304   ]
305   # now try to scroll past end
306   assume-console [
307     press page-down
308     press page-down
309     press page-down
310   ]
311   run [
312     event-loop screen, console, env, resources
313   ]
314   # screen should show just final sandbox with the right index (1)
315   screen-should-contain [
316     .                                                                                 run (F4)           .
317     .                                                  ╎─────────────────────────────────────────────────.
318     .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎1   edit          copy            delete         .
319     .                                                  ╎add 2, 2                                         .
320     .                                                  ╎4                                                .
321     .                                                  ╎─────────────────────────────────────────────────.
322     .                                                  ╎                                                 .
323   ]
324 ]