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   ¦ screen <- render-sandbox-side screen, env, render
115   ¦ screen <- update-cursor screen, recipes, current-sandbox, sandbox-in-focus?, env
116   ¦ loop +next-event
117   }
118 ]
119 
120 # some preconditions for attempting to edit a sandbox
121 def should-attempt-edit? click-row:num, click-column:num, env:&:environment -> result:bool [
122   local-scope
123   load-ingredients
124   # are we below the sandbox editor?
125   click-sandbox-area?:bool <- click-on-sandbox-area? click-row, click-column, env
126   return-unless click-sandbox-area?, 0/false
127   # narrower, is the click in the columns spanning the 'edit' button?
128   first-sandbox:&:editor <- get *env, current-sandbox:offset
129   assert first-sandbox, [!!]
130   sandbox-left-margin:num <- get *first-sandbox, left:offset
131   sandbox-right-margin:num <- get *first-sandbox, right:offset
132   edit-button-left:num, edit-button-right:num, _ <- sandbox-menu-columns sandbox-left-margin, sandbox-right-margin
133   edit-button-vertical-area?:bool <- within-range? click-column, edit-button-left, edit-button-right
134   return-unless edit-button-vertical-area?, 0/false
135   # finally, is sandbox editor empty?
136   current-sandbox:&:editor <- get *env, current-sandbox:offset
137   result <- empty-editor? current-sandbox
138 ]
139 
140 def try-edit-sandbox click-row:num, env:&:environment -> clicked-on-edit-button?:bool, env:&:environment [
141   local-scope
142   load-ingredients
143   # identify the sandbox to edit, if the click was actually on the 'edit' button
144   sandbox:&:sandbox <- find-sandbox env, click-row
145   return-unless sandbox, 0/false
146   clicked-on-edit-button? <- copy 1/true
147   # 'edit' button = 'copy' button + 'delete' button
148   text:text <- get *sandbox, data:offset
149   current-sandbox:&:editor <- get *env, current-sandbox:offset
150   current-sandbox <- insert-text current-sandbox, text
151   env <- delete-sandbox env, sandbox
152   # reset scroll
153   *env <- put *env, render-from:offset, -1
154   # position cursor in sandbox editor
155   *env <- put *env, sandbox-in-focus?:offset, 1/true
156 ]
157 
158 scenario sandbox-with-print-can-be-edited [
159   local-scope
160   trace-until 100/app  # trace too long
161   assume-screen 100/width, 20/height
162   # left editor is empty
163   assume-resources [
164   ]
165   # right editor contains a print instruction
166   env:&:environment <- new-programming-environment resources, screen, [print screen, 4]
167   # run the sandbox
168   assume-console [
169   ¦ press F4
170   ]
171   event-loop screen, console, env, resources
172   screen-should-contain [
173   ¦ .                                                                                 run (F4)           .
174   ¦ .                                                  ╎                                                 .
175   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
176   ¦ .                                                  ╎0   edit          copy            delete         .
177   ¦ .                                                  ╎print screen, 4                                  .
178   ¦ .                                                  ╎screen:                                          .
179   ¦ .                                                  ╎  .4                             .               .
180   ¦ .                                                  ╎  .                              .               .
181   ¦ .                                                  ╎  .                              .               .
182   ¦ .                                                  ╎  .                              .               .
183   ¦ .                                                  ╎  .                              .               .
184   ¦ .                                                  ╎─────────────────────────────────────────────────.
185   ¦ .                                                  ╎                                                 .
186   ]
187   # edit the sandbox
188   assume-console [
189   ¦ left-click 3, 65
190   ]
191   run [
192   ¦ event-loop screen, console, env, resources
193   ]
194   screen-should-contain [
195   ¦ .                                                                                 run (F4)           .
196   ¦ .                                                  ╎print screen, 4                                  .
197   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
198   ¦ .                                                  ╎                                                 .
199   ¦ .                                                  ╎                                                 .
200   ]
201 ]
202 
203 scenario editing-sandbox-after-scrolling-resets-scroll [
204   local-scope
205   trace-until 100/app  # trace too long
206   assume-screen 100/width, 10/height
207   # initialize environment
208   assume-resources [
209   ]
210   env:&:environment <- new-programming-environment resources, screen, []
211   render-all screen, env, render
212   # create 2 sandboxes and scroll to second
213   assume-console [
214   ¦ press ctrl-n
215   ¦ type [add 2, 2]
216   ¦ press F4
217   ¦ type [add 1, 1]
218   ¦ press F4
219   ¦ press page-down
220   ¦ press page-down
221   ]
222   event-loop screen, console, env, resources
223   screen-should-contain [
224   ¦ .                                                                                 run (F4)           .
225   ¦ .                                                  ╎─────────────────────────────────────────────────.
226   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎1   edit          copy            delete         .
227   ¦ .                                                  ╎add 2, 2                                         .
228   ¦ .                                                  ╎4                                                .
229   ¦ .                                                  ╎─────────────────────────────────────────────────.
230   ¦ .                                                  ╎                                                 .
231   ]
232   # edit the second sandbox
233   assume-console [
234   ¦ left-click 2, 55
235   ]
236   run [
237   ¦ event-loop screen, console, env, resources
238   ]
239   # second sandbox shows in editor; scroll resets to display first sandbox
240   screen-should-contain [
241   ¦ .                                                                                 run (F4)           .
242   ¦ .                                                  ╎add 2, 2                                         .
243   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
244   ¦ .                                                  ╎0   edit          copy            delete         .
245   ¦ .                                                  ╎add 1, 1                                         .
246   ¦ .                                                  ╎2                                                .
247   ¦ .                                                  ╎─────────────────────────────────────────────────.
248   ¦ .                                                  ╎                                                 .
249   ]
250 ]
251 
252 scenario editing-sandbox-updates-sandbox-count [
253   local-scope
254   trace-until 100/app  # trace too long
255   assume-screen 100/width, 10/height
256   # initialize environment
257   assume-resources [
258   ]
259   env:&:environment <- new-programming-environment resources, screen, []
260   render-all screen, env, render
261   # create 2 sandboxes
262   assume-console [
263   ¦ press ctrl-n
264   ¦ type [add 2, 2]
265   ¦ press F4
266   ¦ type [add 1, 1]
267   ¦ press F4
268   ]
269   event-loop screen, console, env, resources
270   screen-should-contain [
271   ¦ .                                                                                 run (F4)           .
272   ¦ .                                                  ╎                                                 .
273   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
274   ¦ .                                                  ╎0   edit          copy            delete         .
275   ¦ .                                                  ╎add 1, 1                                         .
276   ¦ .                                                  ╎2                                                .
277   ¦ .                                                  ╎─────────────────────────────────────────────────.
278   ¦ .                                                  ╎1   edit          copy            delete         .
279   ¦ .                                                  ╎add 2, 2                                         .
280   ¦ .                                                  ╎4                                                .
281   ]
282   # edit the second sandbox, then resave
283   assume-console [
284   ¦ left-click 3, 60
285   ¦ press F4
286   ]
287   run [
288   ¦ event-loop screen, console, env, resources
289   ]
290   # no change in contents
291   screen-should-contain [
292   ¦ .                                                                                 run (F4)           .
293   ¦ .                                                  ╎                                                 .
294   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎─────────────────────────────────────────────────.
295   ¦ .                                                  ╎0   edit          copy            delete         .
296   ¦ .                                                  ╎add 1, 1                                         .
297   ¦ .                                                  ╎2                                                .
298   ¦ .                                                  ╎─────────────────────────────────────────────────.
299   ¦ .                                                  ╎1   edit          copy            delete         .
300   ¦ .                                                  ╎add 2, 2                                         .
301   ¦ .                                                  ╎4                                                .
302   ]
303   # now try to scroll past end
304   assume-console [
305   ¦ press page-down
306   ¦ press page-down
307   ¦ press page-down
308   ]
309   run [
310   ¦ event-loop screen, console, env, resources
311   ]
312   # screen should show just final sandbox with the right index (1)
313   screen-should-contain [
314   ¦ .                                                                                 run (F4)           .
315   ¦ .                                                  ╎─────────────────────────────────────────────────.
316   ¦ .╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╎1   edit          copy            delete         .
317   ¦ .                                                  ╎add 2, 2                                         .
318   ¦ .                                                  ╎4                                                .
319   ¦ .                                                  ╎─────────────────────────────────────────────────.
320   ¦ .                                                  ╎                                                 .
321   ]
322 ]