page.scm 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2019, 2020, 2022 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (gnu installer newt page)
  21. #:use-module (gnu installer steps)
  22. #:use-module (gnu installer utils)
  23. #:use-module (gnu installer newt utils)
  24. #:use-module (guix build utils)
  25. #:use-module (guix i18n)
  26. #:use-module (ice-9 i18n)
  27. #:use-module (ice-9 match)
  28. #:use-module (ice-9 receive)
  29. #:use-module (srfi srfi-1)
  30. #:use-module (srfi srfi-11)
  31. #:use-module (srfi srfi-26)
  32. #:use-module (srfi srfi-34)
  33. #:use-module (srfi srfi-35)
  34. #:use-module (newt)
  35. #:export (default-listbox-height
  36. draw-info-page
  37. draw-connecting-page
  38. run-input-page
  39. run-error-page
  40. run-confirmation-page
  41. run-listbox-selection-page
  42. run-scale-page
  43. run-checkbox-tree-page
  44. run-file-textbox-page
  45. %ok-button
  46. %exit-button
  47. run-textbox-page
  48. run-dump-page
  49. run-form-with-clients))
  50. ;;; Commentary:
  51. ;;;
  52. ;;; Some helpers around guile-newt to draw or run generic pages. The
  53. ;;; difference between 'draw' and 'run' terms comes from newt library. A page
  54. ;;; is drawn when the form it contains does not expect any user
  55. ;;; interaction. In that case, it is necessary to call (newt-refresh) to force
  56. ;;; the page to be displayed. When a form is 'run', it is blocked waiting for
  57. ;;; any action from the user (press a button, input some text, ...).
  58. ;;;
  59. ;;; Code:
  60. (define* (watch-clients! form #:optional (clients (current-clients)))
  61. "Have FORM watch the file descriptors corresponding to current client
  62. connections. Consequently, FORM may exit with the 'exit-fd-ready' reason."
  63. (when (current-server-socket)
  64. (form-watch-fd form (fileno (current-server-socket))
  65. FD-READ))
  66. (for-each (lambda (client)
  67. (form-watch-fd form (fileno client)
  68. (logior FD-READ FD-EXCEPT)))
  69. clients))
  70. (define close-port-and-reuse-fd
  71. (let ((bit-bucket #f))
  72. (lambda (port)
  73. "Close PORT and redirect its underlying FD to point to a valid open file
  74. descriptor."
  75. (let ((fd (fileno port)))
  76. (unless bit-bucket
  77. (set! bit-bucket (car (pipe))))
  78. (close-port port)
  79. ;; FIXME: We're leaking FD.
  80. (dup2 (fileno bit-bucket) fd)))))
  81. (define* (run-form-with-clients form exp)
  82. "Run FORM such as it watches the file descriptors beneath CLIENTS after
  83. sending EXP to all the clients.
  84. Automatically restart the form when it exits with 'exit-fd-ready but without
  85. an actual client reply--e.g., it got a connection request or a client
  86. disconnect.
  87. Like 'run-form', return two values: the exit reason, and an \"argument\"."
  88. (define* (discard-client! port #:optional errno)
  89. (if errno
  90. (installer-log-line "removing client ~d due to ~s"
  91. (fileno port) (strerror errno))
  92. (installer-log-line "removing client ~d due to EOF"
  93. (fileno port)))
  94. ;; XXX: Watch out! There's no 'form-unwatch-fd' procedure in Newt so we
  95. ;; cheat: we keep PORT's file descriptor open, but make it a duplicate of
  96. ;; a valid but inactive FD. Failing to do that, 'run-form' would
  97. ;; select(2) on the now-closed port and keep spinning as select(2) returns
  98. ;; EBADF.
  99. (close-port-and-reuse-fd port)
  100. (current-clients (delq port (current-clients)))
  101. (close-port port))
  102. (define title
  103. ;; Title of FORM.
  104. (match exp
  105. (((? symbol? tag) alist ...)
  106. (match (assq 'title alist)
  107. ((_ title) title)
  108. (_ tag)))
  109. (((? symbol? tag) _ ...)
  110. tag)
  111. (_
  112. 'unknown)))
  113. ;; Send EXP to all the currently-connected clients.
  114. (send-to-clients exp)
  115. (let loop ()
  116. (installer-log-line "running form ~s (~s) with ~d clients"
  117. form title (length (current-clients)))
  118. ;; Call 'watch-clients!' within the loop because there might be new
  119. ;; clients.
  120. (watch-clients! form)
  121. (let-values (((reason argument) (run-form form)))
  122. (match reason
  123. ('exit-fd-ready
  124. (match (fdes->ports argument)
  125. ((port _ ...)
  126. (if (memq port (current-clients))
  127. ;; Read a reply from a client or handle its departure.
  128. (catch 'system-error
  129. (lambda ()
  130. (match (read port)
  131. ((? eof-object? eof)
  132. (discard-client! port)
  133. (loop))
  134. (obj
  135. (installer-log-line "form ~s (~s): client ~d replied ~s"
  136. form title (fileno port) obj)
  137. (values 'exit-fd-ready obj))))
  138. (lambda args
  139. (discard-client! port (system-error-errno args))
  140. (loop)))
  141. ;; Accept a new client and send it EXP.
  142. (match (accept port)
  143. ((client . _)
  144. (installer-log-line
  145. "accepting new client ~d while on form ~s"
  146. (fileno client) form)
  147. (catch 'system-error
  148. (lambda ()
  149. (write exp client)
  150. (newline client)
  151. (force-output client)
  152. (current-clients (cons client (current-clients))))
  153. (lambda _
  154. (close-port client)))
  155. (loop)))))))
  156. (_
  157. (values reason argument))))))
  158. (define (default-listbox-height)
  159. "Return the default listbox height."
  160. (max 5 (- (screen-rows) 20)))
  161. (define (draw-info-page text title)
  162. "Draw an informative page with the given TEXT as content. Set the title of
  163. this page to TITLE."
  164. (send-to-clients `(info (title ,title) (text ,text)))
  165. (let* ((text-box
  166. (make-reflowed-textbox -1 -1 text 40
  167. #:flags FLAG-BORDER))
  168. (grid (make-grid 1 1))
  169. (form (make-form)))
  170. (set-grid-field grid 0 0 GRID-ELEMENT-COMPONENT text-box)
  171. (add-component-to-form form text-box)
  172. (make-wrapped-grid-window grid title)
  173. (draw-form form)
  174. ;; This call is imperative, otherwise the form won't be displayed. See the
  175. ;; explanation in the above commentary.
  176. (newt-refresh)
  177. form))
  178. (define (draw-connecting-page service-name)
  179. "Draw a page to indicate a connection in in progress."
  180. (draw-info-page
  181. (format #f (G_ "Connecting to ~a, please wait.") service-name)
  182. (G_ "Connection in progress")))
  183. (define* (run-input-page text title
  184. #:key
  185. (allow-empty-input? #f)
  186. (default-text #f)
  187. (input-visibility-checkbox? #f)
  188. (input-field-width 40)
  189. (input-flags 0))
  190. "Run a page to prompt user for an input. The given TEXT will be displayed
  191. above the input field. The page title is set to TITLE. Unless
  192. allow-empty-input? is set to #t, an error page will be displayed if the user
  193. enters an empty input. INPUT-FLAGS is a bitwise-or'd set of flags for the
  194. input box, such as FLAG-PASSWORD."
  195. (let* ((text-box
  196. (make-reflowed-textbox -1 -1 text
  197. input-field-width
  198. #:flags FLAG-BORDER))
  199. (input-visible-cb
  200. (make-checkbox -1 -1 (G_ "Show") #\space "x "))
  201. (input-flags* (if input-visibility-checkbox?
  202. (logior FLAG-PASSWORD FLAG-SCROLL
  203. input-flags)
  204. input-flags))
  205. (input-entry (make-entry -1 -1 20
  206. #:flags input-flags*))
  207. (ok-button (make-button -1 -1 (G_ "OK")))
  208. (grid (vertically-stacked-grid
  209. GRID-ELEMENT-COMPONENT text-box
  210. GRID-ELEMENT-SUBGRID
  211. (apply
  212. horizontal-stacked-grid
  213. GRID-ELEMENT-COMPONENT input-entry
  214. `(,@(if input-visibility-checkbox?
  215. (list GRID-ELEMENT-COMPONENT input-visible-cb)
  216. '())))
  217. GRID-ELEMENT-COMPONENT ok-button))
  218. (form (make-form #:flags FLAG-NOF12)))
  219. (add-component-callback
  220. input-visible-cb
  221. (lambda ()
  222. (set-entry-flags input-entry
  223. FLAG-PASSWORD
  224. FLAG-ROLE-TOGGLE)))
  225. (when default-text
  226. (set-entry-text input-entry default-text))
  227. (add-form-to-grid grid form #t)
  228. (make-wrapped-grid-window grid title)
  229. (let ((error-page (lambda ()
  230. (run-error-page (G_ "Please enter a non empty input.")
  231. (G_ "Empty input")))))
  232. (let loop ()
  233. (receive (exit-reason argument)
  234. (run-form-with-clients form
  235. `(input (title ,title) (text ,text)
  236. (default ,default-text)))
  237. (let ((input (if (eq? exit-reason 'exit-fd-ready)
  238. argument
  239. (entry-value input-entry))))
  240. (cond ((not input) ;client disconnect or something
  241. (loop))
  242. ((and (not allow-empty-input?)
  243. (eq? exit-reason 'exit-component)
  244. (string=? input ""))
  245. ;; Display the error page.
  246. (error-page)
  247. ;; Set the focus back to the input input field.
  248. (set-current-component form input-entry)
  249. (loop))
  250. (else
  251. (destroy-form-and-pop form)
  252. input))))))))
  253. (define* (run-error-page text title #:key (width 40))
  254. "Run a page to inform the user of an error. The page is WIDTH column wide
  255. and contains the given TEXT to explain the error and an \"OK\" button to
  256. acknowledge the error. The title of the page is set to TITLE."
  257. (let* ((text-box
  258. (make-reflowed-textbox -1 -1 text width
  259. #:flags FLAG-BORDER))
  260. (grid (make-grid 1 2))
  261. (ok-button (make-button -1 -1 "OK"))
  262. (form (make-form)))
  263. (set-grid-field grid 0 0 GRID-ELEMENT-COMPONENT text-box)
  264. (set-grid-field grid 0 1 GRID-ELEMENT-COMPONENT ok-button
  265. #:pad-top 1)
  266. ;; Set the background color to red to indicate something went wrong.
  267. (newt-set-color COLORSET-ROOT "white" "red")
  268. (add-components-to-form form text-box ok-button)
  269. (make-wrapped-grid-window grid title)
  270. (run-form-with-clients form
  271. `(error (title ,title) (text ,text)))
  272. ;; Restore the background to its original color.
  273. (newt-set-color COLORSET-ROOT "white" "blue")
  274. (destroy-form-and-pop form)))
  275. (define* (run-confirmation-page text title
  276. #:key (exit-button-procedure (const #f)))
  277. "Run a page to inform the user of an error. The page contains the given TEXT
  278. to explain the error and an \"OK\" button to acknowledge the error. The title
  279. of the page is set to TITLE."
  280. (let* ((text-box
  281. (make-reflowed-textbox -1 -1 text 40
  282. #:flags FLAG-BORDER))
  283. (ok-button (make-button -1 -1 (G_ "Continue")))
  284. (exit-button (make-button -1 -1 (G_ "Exit")))
  285. (grid (vertically-stacked-grid
  286. GRID-ELEMENT-COMPONENT text-box
  287. GRID-ELEMENT-SUBGRID
  288. (horizontal-stacked-grid
  289. GRID-ELEMENT-COMPONENT ok-button
  290. GRID-ELEMENT-COMPONENT exit-button)))
  291. (form (make-form #:flags FLAG-NOF12)))
  292. (add-form-to-grid grid form #t)
  293. (make-wrapped-grid-window grid title)
  294. (receive (exit-reason argument)
  295. (run-form-with-clients form
  296. `(confirmation (title ,title)
  297. (text ,text)))
  298. (dynamic-wind
  299. (const #t)
  300. (lambda ()
  301. (match exit-reason
  302. ('exit-component
  303. (cond
  304. ((components=? argument ok-button)
  305. #t)
  306. ((components=? argument exit-button)
  307. (exit-button-procedure))))
  308. ('exit-fd-ready
  309. (if argument
  310. #t
  311. (exit-button-procedure)))))
  312. (lambda ()
  313. (destroy-form-and-pop form))))))
  314. (define* (run-listbox-selection-page #:key
  315. info-text
  316. title
  317. (info-textbox-width 50)
  318. listbox-items
  319. listbox-item->text
  320. (listbox-height
  321. (default-listbox-height))
  322. (listbox-default-item #f)
  323. (listbox-allow-multiple? #f)
  324. (sort-listbox-items? #t)
  325. (allow-delete? #f)
  326. (skip-item-procedure?
  327. (const #f))
  328. button-text
  329. (button-callback-procedure
  330. (const #t))
  331. (button2-text #f)
  332. (button2-callback-procedure
  333. (const #t))
  334. (listbox-callback-procedure
  335. identity)
  336. (client-callback-procedure
  337. listbox-callback-procedure)
  338. (hotkey-callback-procedure
  339. (const #t)))
  340. "Run a page asking the user to select an item in a listbox. The page
  341. contains, stacked vertically from the top to the bottom, an informative text
  342. set to INFO-TEXT, a listbox and a button. The listbox will be filled with
  343. LISTBOX-ITEMS converted to text by applying the procedure LISTBOX-ITEM->TEXT
  344. on every item. The selected item from LISTBOX-ITEMS is returned. The button
  345. text is set to BUTTON-TEXT and the procedure BUTTON-CALLBACK-PROCEDURE called
  346. when it is pressed. The procedure LISTBOX-CALLBACK-PROCEDURE is called when an
  347. item from the listbox is selected (by pressing the <ENTER> key).
  348. INFO-TEXTBOX-WIDTH is the width of the textbox where INFO-TEXT will be
  349. displayed. LISTBOX-HEIGHT is the height of the listbox.
  350. If LISTBOX-DEFAULT-ITEM is set to the value of one of the items in
  351. LISTBOX-ITEMS, it will be selected by default. Otherwise, the first element of
  352. the listbox is selected.
  353. If LISTBOX-ALLOW-MULTIPLE? is set to #t, multiple items from the listbox can
  354. be selected (using the <SPACE> key). It that case, a list containing the
  355. selected items will be returned.
  356. If SORT-LISTBOX-ITEMS? is set to #t, the listbox items are sorted using
  357. 'string-locale<?' procedure (after being converted to text).
  358. If ALLOW-DELETE? is #t, the form will return if the <DELETE> key is pressed,
  359. otherwise nothing will happen.
  360. Each time the listbox current item changes, call SKIP-ITEM-PROCEDURE? with the
  361. current listbox item as argument. If it returns #t, skip the element and jump
  362. to the next/previous one depending on the previous item, otherwise do
  363. nothing."
  364. (let loop ()
  365. (define (fill-listbox listbox items)
  366. "Append the given ITEMS to LISTBOX, once they have been converted to text
  367. with LISTBOX-ITEM->TEXT. Each item appended to the LISTBOX is given a key by
  368. newt. Save this key by returning an association list under the form:
  369. ((NEWT-LISTBOX-KEY . ITEM) ...)
  370. where NEWT-LISTBOX-KEY is the key returned by APPEND-ENTRY-TO-LISTBOX, when
  371. ITEM was inserted into LISTBOX."
  372. (map (lambda (item)
  373. (let* ((text (listbox-item->text item))
  374. (key (append-entry-to-listbox listbox text)))
  375. (cons key item)))
  376. items))
  377. (define (sort-listbox-items listbox-items)
  378. "Return LISTBOX-ITEMS sorted using the 'string-locale<?' procedure on the text
  379. corresponding to each item in the list."
  380. (let* ((items (map (lambda (item)
  381. (cons item (listbox-item->text item)))
  382. listbox-items))
  383. (sorted-items
  384. (sort items (lambda (a b)
  385. (let ((text-a (cdr a))
  386. (text-b (cdr b)))
  387. (string-locale<? text-a text-b))))))
  388. (map car sorted-items)))
  389. ;; Store the last selected listbox item's key.
  390. (define last-listbox-key (make-parameter #f))
  391. (define (previous-key keys key)
  392. (let ((index (list-index (cut eq? key <>) keys)))
  393. (and index
  394. (> index 0)
  395. (list-ref keys (- index 1)))))
  396. (define (next-key keys key)
  397. (let ((index (list-index (cut eq? key <>) keys)))
  398. (and index
  399. (< index (- (length keys) 1))
  400. (list-ref keys (+ index 1)))))
  401. (define (set-default-item listbox listbox-keys default-item)
  402. "Set the default item of LISTBOX to DEFAULT-ITEM. LISTBOX-KEYS is the
  403. association list returned by the FILL-LISTBOX procedure. It is used because
  404. the current listbox item has to be selected by key."
  405. (for-each (match-lambda
  406. ((key . item)
  407. (when (equal? item default-item)
  408. (set-current-listbox-entry-by-key listbox key))))
  409. listbox-keys))
  410. (let* ((listbox (make-listbox
  411. -1 -1
  412. listbox-height
  413. (logior FLAG-SCROLL FLAG-BORDER FLAG-RETURNEXIT
  414. (if listbox-allow-multiple?
  415. FLAG-MULTIPLE
  416. 0))))
  417. (form (make-form #:flags FLAG-NOF12))
  418. (info-textbox
  419. (make-reflowed-textbox -1 -1 info-text
  420. info-textbox-width
  421. #:flags FLAG-BORDER))
  422. (button (make-button -1 -1 button-text))
  423. (button2 (and button2-text
  424. (make-button -1 -1 button2-text)))
  425. (grid (vertically-stacked-grid
  426. GRID-ELEMENT-COMPONENT info-textbox
  427. GRID-ELEMENT-COMPONENT listbox
  428. GRID-ELEMENT-SUBGRID
  429. (apply
  430. horizontal-stacked-grid
  431. GRID-ELEMENT-COMPONENT button
  432. `(,@(if button2
  433. (list GRID-ELEMENT-COMPONENT button2)
  434. '())))))
  435. (sorted-items (if sort-listbox-items?
  436. (sort-listbox-items listbox-items)
  437. listbox-items))
  438. (keys (fill-listbox listbox sorted-items)))
  439. (define (choice->item str)
  440. ;; Return the item that corresponds to STR.
  441. (match (find (match-lambda
  442. ((key . item)
  443. (string=? str (listbox-item->text item))))
  444. keys)
  445. ((key . item) item)
  446. (#f (abort-to-prompt 'installer-step 'abort))))
  447. ;; On every listbox element change, check if we need to skip it. If yes,
  448. ;; depending on the 'last-listbox-key', jump forward or backward. If no,
  449. ;; do nothing.
  450. (add-component-callback
  451. listbox
  452. (lambda ()
  453. (let* ((current-key (current-listbox-entry listbox))
  454. (listbox-keys (map car keys))
  455. (last-key (last-listbox-key))
  456. (item (assoc-ref keys current-key))
  457. (prev-key (previous-key listbox-keys current-key))
  458. (next-key (next-key listbox-keys current-key)))
  459. ;; Update last-listbox-key before a potential call to
  460. ;; set-current-listbox-entry-by-key, because it will immediately
  461. ;; cause this callback to be called for the new entry.
  462. (last-listbox-key current-key)
  463. (when (skip-item-procedure? item)
  464. (when (eq? prev-key last-key)
  465. (if next-key
  466. (set-current-listbox-entry-by-key listbox next-key)
  467. (set-current-listbox-entry-by-key listbox prev-key)))
  468. (when (eq? next-key last-key)
  469. (if prev-key
  470. (set-current-listbox-entry-by-key listbox prev-key)
  471. (set-current-listbox-entry-by-key listbox next-key)))))))
  472. (when listbox-default-item
  473. (set-default-item listbox keys listbox-default-item))
  474. (when allow-delete?
  475. (form-add-hotkey form KEY-DELETE))
  476. (add-form-to-grid grid form #t)
  477. (make-wrapped-grid-window grid title)
  478. (receive (exit-reason argument)
  479. (run-form-with-clients form
  480. `(list-selection (title ,title)
  481. (multiple-choices?
  482. ,listbox-allow-multiple?)
  483. (items
  484. ,(map listbox-item->text
  485. listbox-items))))
  486. (dynamic-wind
  487. (const #t)
  488. (lambda ()
  489. (match exit-reason
  490. ('exit-component
  491. (cond
  492. ((components=? argument button)
  493. (button-callback-procedure))
  494. ((and button2
  495. (components=? argument button2))
  496. (button2-callback-procedure))
  497. ((components=? argument listbox)
  498. (if listbox-allow-multiple?
  499. (let* ((entries (listbox-selection listbox))
  500. (items (map (lambda (entry)
  501. (assoc-ref keys entry))
  502. entries)))
  503. (listbox-callback-procedure items))
  504. (let* ((entry (current-listbox-entry listbox))
  505. (item (assoc-ref keys entry)))
  506. (listbox-callback-procedure item))))))
  507. ('exit-fd-ready
  508. (let* ((choice argument)
  509. (item (if listbox-allow-multiple?
  510. (map choice->item choice)
  511. (choice->item choice))))
  512. (client-callback-procedure item)))
  513. ('exit-hotkey
  514. (let* ((entry (current-listbox-entry listbox))
  515. (item (assoc-ref keys entry)))
  516. (hotkey-callback-procedure argument item)))))
  517. (lambda ()
  518. (destroy-form-and-pop form)))))))
  519. (define* (run-scale-page #:key
  520. title
  521. info-text
  522. (info-textbox-width 50)
  523. (scale-width 40)
  524. (scale-full-value 100)
  525. scale-update-proc
  526. (max-scale-update 5))
  527. "Run a page with a progress bar (called 'scale' in newt). The given
  528. INFO-TEXT is displayed in a textbox above the scale. The width of the textbox
  529. is set to INFO-TEXTBOX-WIDTH. The width of the scale is set to
  530. SCALE-WIDTH. SCALE-FULL-VALUE indicates the value that correspond to 100% of
  531. the scale.
  532. The procedure SCALE-UPDATE-PROC shall return a new scale
  533. value. SCALE-UPDATE-PROC will be called until the returned value is superior
  534. or equal to SCALE-FULL-VALUE, but no more than MAX-SCALE-UPDATE times. An
  535. error is raised if the MAX-SCALE-UPDATE limit is reached."
  536. (let* ((info-textbox
  537. (make-reflowed-textbox -1 -1 info-text
  538. info-textbox-width
  539. #:flags FLAG-BORDER))
  540. (scale (make-scale -1 -1 scale-width scale-full-value))
  541. (grid (vertically-stacked-grid
  542. GRID-ELEMENT-COMPONENT info-textbox
  543. GRID-ELEMENT-COMPONENT scale))
  544. (form (make-form)))
  545. (add-form-to-grid grid form #t)
  546. (make-wrapped-grid-window grid title)
  547. (draw-form form)
  548. ;; This call is imperative, otherwise the form won't be displayed. See the
  549. ;; explanation in the above commentary.
  550. (newt-refresh)
  551. (dynamic-wind
  552. (const #t)
  553. (lambda ()
  554. (let loop ((i max-scale-update)
  555. (last-value 0))
  556. (let ((value (scale-update-proc last-value)))
  557. (set-scale-value scale value)
  558. ;; Same as above.
  559. (newt-refresh)
  560. (unless (>= value scale-full-value)
  561. (if (> i 0)
  562. (loop (- i 1) value)
  563. (error "Max scale updates reached."))))))
  564. (lambda ()
  565. (destroy-form-and-pop form)))))
  566. (define %none-selected
  567. (circular-list #f))
  568. (define* (run-checkbox-tree-page #:key
  569. info-text
  570. title
  571. items
  572. (selection %none-selected)
  573. item->text
  574. (info-textbox-width 50)
  575. (checkbox-tree-height 10)
  576. (ok-button-callback-procedure
  577. (const #t))
  578. (exit-button-callback-procedure
  579. (const #t)))
  580. "Run a page allowing the user to select one or multiple items among ITEMS in
  581. a checkbox list. The page contains vertically stacked from the top to the
  582. bottom, an informative text set to INFO-TEXT, the checkbox list and two
  583. buttons, 'Ok' and 'Exit'. The page title's is set to TITLE. ITEMS are
  584. converted to text using ITEM->TEXT before being displayed in the checkbox
  585. list. SELECTION is a list of Booleans of the same length as ITEMS that
  586. specifies which items are initially checked.
  587. INFO-TEXTBOX-WIDTH is the width of the textbox where INFO-TEXT will be
  588. displayed. CHECKBOX-TREE-HEIGHT is the height of the checkbox list.
  589. OK-BUTTON-CALLBACK-PROCEDURE is called when the 'Ok' button is pressed.
  590. EXIT-BUTTON-CALLBACK-PROCEDURE is called when the 'Exit' button is
  591. pressed.
  592. This procedure returns the list of checked items in the checkbox list among
  593. ITEMS when 'Ok' is pressed."
  594. (define (fill-checkbox-tree checkbox-tree items)
  595. (map (lambda (item selected?)
  596. (let* ((item-text (item->text item))
  597. (key (add-entry-to-checkboxtree checkbox-tree item-text
  598. (if selected?
  599. FLAG-SELECTED
  600. 0))))
  601. (cons key item)))
  602. items
  603. selection))
  604. (let loop ()
  605. (let* ((checkbox-tree
  606. (make-checkboxtree -1 -1
  607. checkbox-tree-height
  608. FLAG-BORDER))
  609. (info-textbox
  610. (make-reflowed-textbox -1 -1 info-text
  611. info-textbox-width
  612. #:flags FLAG-BORDER))
  613. (ok-button (make-button -1 -1 (G_ "OK")))
  614. (exit-button (make-button -1 -1 (G_ "Exit")))
  615. (grid (vertically-stacked-grid
  616. GRID-ELEMENT-COMPONENT info-textbox
  617. GRID-ELEMENT-COMPONENT checkbox-tree
  618. GRID-ELEMENT-SUBGRID
  619. (horizontal-stacked-grid
  620. GRID-ELEMENT-COMPONENT ok-button
  621. GRID-ELEMENT-COMPONENT exit-button)))
  622. (keys (fill-checkbox-tree checkbox-tree items))
  623. (form (make-form #:flags FLAG-NOF12)))
  624. (define (choice->item str)
  625. ;; Return the item that corresponds to STR.
  626. (match (find (match-lambda
  627. ((key . item)
  628. (string=? str (item->text item))))
  629. keys)
  630. ((key . item) item)
  631. (#f (abort-to-prompt 'installer-step 'abort))))
  632. (add-form-to-grid grid form #t)
  633. (make-wrapped-grid-window grid title)
  634. (receive (exit-reason argument)
  635. (run-form-with-clients form
  636. `(checkbox-list (title ,title)
  637. (text ,info-text)
  638. (items
  639. ,(map item->text items))))
  640. (dynamic-wind
  641. (const #t)
  642. (lambda ()
  643. (match exit-reason
  644. ('exit-component
  645. (cond
  646. ((components=? argument ok-button)
  647. (let* ((entries (current-checkbox-selection checkbox-tree))
  648. (current-items (map (lambda (entry)
  649. (assoc-ref keys entry))
  650. entries)))
  651. (ok-button-callback-procedure)
  652. current-items))
  653. ((components=? argument exit-button)
  654. (exit-button-callback-procedure))))
  655. ('exit-fd-ready
  656. (map choice->item argument))))
  657. (lambda ()
  658. (destroy-form-and-pop form)))))))
  659. (define* (edit-file file #:key locale)
  660. "Spawn an editor for FILE."
  661. (clear-screen)
  662. (newt-suspend)
  663. ;; Use Nano because it syntax-highlights Scheme by default.
  664. ;; TODO: Add a menu to choose an editor?
  665. (invoke "nano" file)
  666. (newt-resume))
  667. (define* (run-file-textbox-page #:key
  668. info-text
  669. title
  670. file
  671. (info-textbox-width 50)
  672. (file-textbox-width 50)
  673. (file-textbox-height 30)
  674. (exit-button? #t)
  675. (edit-button? #f)
  676. (editor-locale #f)
  677. (ok-button-callback-procedure
  678. (const #t))
  679. (exit-button-callback-procedure
  680. (const #t)))
  681. (let loop ()
  682. (let* ((info-textbox
  683. (make-reflowed-textbox -1 -1 info-text
  684. info-textbox-width
  685. #:flags FLAG-BORDER))
  686. (file-textbox
  687. (make-textbox -1 -1
  688. file-textbox-width
  689. file-textbox-height
  690. (logior FLAG-SCROLL FLAG-BORDER)))
  691. (ok-button (make-button -1 -1 (G_ "OK")))
  692. (exit-button (make-button -1 -1 (G_ "Exit")))
  693. (edit-button (and edit-button?
  694. (make-button -1 -1 (G_ "Edit"))))
  695. (grid (vertically-stacked-grid
  696. GRID-ELEMENT-COMPONENT info-textbox
  697. GRID-ELEMENT-COMPONENT file-textbox
  698. GRID-ELEMENT-SUBGRID
  699. (apply
  700. horizontal-stacked-grid
  701. GRID-ELEMENT-COMPONENT ok-button
  702. `(,@(if edit-button?
  703. (list GRID-ELEMENT-COMPONENT edit-button)
  704. '())
  705. ,@(if exit-button?
  706. (list GRID-ELEMENT-COMPONENT exit-button)
  707. '())))))
  708. (form (make-form #:flags FLAG-NOF12)))
  709. (add-form-to-grid grid form #t)
  710. (make-wrapped-grid-window grid title)
  711. (set-textbox-text file-textbox
  712. (receive (_w _h text)
  713. (reflow-text (read-all file)
  714. file-textbox-width
  715. 0 0)
  716. text))
  717. (receive (exit-reason argument)
  718. (run-form-with-clients form
  719. `(file-dialog (title ,title)
  720. (text ,info-text)
  721. (file ,file)))
  722. (define result
  723. (dynamic-wind
  724. (const #t)
  725. (lambda ()
  726. (match exit-reason
  727. ('exit-component
  728. (cond
  729. ((components=? argument ok-button)
  730. (ok-button-callback-procedure))
  731. ((and exit-button?
  732. (components=? argument exit-button))
  733. (exit-button-callback-procedure))
  734. ((and edit-button?
  735. (components=? argument edit-button))
  736. (edit-file file))))
  737. ('exit-fd-ready
  738. (if argument
  739. (ok-button-callback-procedure)
  740. (exit-button-callback-procedure)))))
  741. (lambda ()
  742. (destroy-form-and-pop form))))
  743. (if (and (eq? exit-reason 'exit-component)
  744. edit-button
  745. (components=? argument edit-button))
  746. (loop) ;recurse in tail position
  747. result)))))
  748. (define %ok-button
  749. (cons (G_ "Ok") (lambda () #t)))
  750. (define %exit-button
  751. (cons (G_ "Exit") (lambda () (abort-to-prompt 'installer-step 'abort))))
  752. (define %default-buttons
  753. (list %ok-button %exit-button))
  754. (define (make-newt-buttons buttons-spec)
  755. (map
  756. (match-lambda ((title . proc)
  757. (cons (make-button -1 -1 title) proc)))
  758. buttons-spec))
  759. (define* (run-textbox-page #:key
  760. title
  761. info-text
  762. content
  763. (buttons-spec %default-buttons))
  764. "Run a page to display INFO-TEXT followed by CONTENT to the user, who has to
  765. choose an action among the buttons specified by BUTTONS-SPEC.
  766. BUTTONS-SPEC is an association list with button labels as keys, and callback
  767. procedures as values.
  768. This procedure returns the result of the callback procedure of the button
  769. chosen by the user."
  770. (define info-textbox
  771. (make-reflowed-textbox -1 -1 info-text
  772. 50
  773. #:flags FLAG-BORDER))
  774. (define content-textbox
  775. (make-textbox -1 -1
  776. 50
  777. 30
  778. (logior FLAG-SCROLL FLAG-BORDER)))
  779. (define buttons
  780. (make-newt-buttons buttons-spec))
  781. (define grid
  782. (vertically-stacked-grid
  783. GRID-ELEMENT-COMPONENT info-textbox
  784. GRID-ELEMENT-COMPONENT content-textbox
  785. GRID-ELEMENT-SUBGRID
  786. (apply
  787. horizontal-stacked-grid
  788. (append-map (match-lambda ((button . proc)
  789. (list GRID-ELEMENT-COMPONENT button)))
  790. buttons))))
  791. (define form (make-form #:flags FLAG-NOF12))
  792. (add-form-to-grid grid form #t)
  793. (make-wrapped-grid-window grid title)
  794. (set-textbox-text content-textbox
  795. (receive (_w _h text)
  796. (reflow-text content
  797. 50
  798. 0 0)
  799. text))
  800. (receive (exit-reason argument)
  801. (run-form-with-clients form
  802. `(contents-dialog (title ,title)
  803. (text ,info-text)
  804. (content ,content)))
  805. (destroy-form-and-pop form)
  806. (match exit-reason
  807. ('exit-component
  808. (let ((proc (assq-ref buttons argument)))
  809. (if proc
  810. (proc)
  811. (raise
  812. (condition
  813. (&serious)
  814. (&message
  815. (message (format #f "Unable to find corresponding PROC for \
  816. component ~a." argument))))))))
  817. ;; TODO
  818. ('exit-fd-ready
  819. (raise (condition (&serious)))))))
  820. (define* (run-dump-page base-dir file-choices)
  821. (define info-textbox
  822. (make-reflowed-textbox -1 -1 "Please select files you wish to include in \
  823. the dump."
  824. 50
  825. #:flags FLAG-BORDER))
  826. (define components
  827. (map (match-lambda ((file . enabled)
  828. (list
  829. (make-compact-button -1 -1 "Edit")
  830. (make-checkbox -1 -1 file (if enabled #\x #\ ) " x")
  831. file)))
  832. file-choices))
  833. (define sub-grid (make-grid 2 (length components)))
  834. (for-each
  835. (match-lambda* (((button checkbox _) index)
  836. (set-grid-field sub-grid 0 index
  837. GRID-ELEMENT-COMPONENT checkbox
  838. #:anchor ANCHOR-LEFT)
  839. (set-grid-field sub-grid 1 index
  840. GRID-ELEMENT-COMPONENT button
  841. #:anchor ANCHOR-LEFT)))
  842. components (iota (length components)))
  843. (define grid
  844. (vertically-stacked-grid
  845. GRID-ELEMENT-COMPONENT info-textbox
  846. GRID-ELEMENT-SUBGRID sub-grid
  847. GRID-ELEMENT-COMPONENT (make-button -1 -1 "Create")))
  848. (define form (make-form #:flags FLAG-NOF12))
  849. (add-form-to-grid grid form #t)
  850. (make-wrapped-grid-window grid "Installer dump")
  851. (define prompt-tag (make-prompt-tag))
  852. (let loop ()
  853. (call-with-prompt prompt-tag
  854. (lambda ()
  855. (receive (exit-reason argument)
  856. (run-form-with-clients form
  857. `(dump-page))
  858. (match exit-reason
  859. ('exit-component
  860. (let ((result
  861. (map (match-lambda
  862. ((edit checkbox filename)
  863. (if (components=? edit argument)
  864. (abort-to-prompt prompt-tag filename)
  865. (cons filename (eq? #\x
  866. (checkbox-value checkbox))))))
  867. components)))
  868. (destroy-form-and-pop form)
  869. result))
  870. ;; TODO
  871. ('exit-fd-ready
  872. (raise (condition (&serious)))))))
  873. (lambda (k file)
  874. (edit-file (string-append base-dir "/" file))
  875. (loop)))))