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