bjogui/examples/counter.bjo

36 lines
1.2 KiB
Text
Raw Permalink Normal View History

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; bjogui/examples/counter.bjo — a counter and a greeting in a window.
;;
;; bjo run examples/counter.bjo
(import (std prelude))
(import (bjogui view))
(import (bjogui app))
(type (: Msg (Union Inc Dec (: Typed string))))
(type (: Model (Record (: count int) (: name string))))
(: gui-update (-> Msg Model (Tuple Model (List (Event Msg)))))
(defun (gui-update msg m)
(next
(match msg
(Inc (record-set m (count (+ (.count m) 1))))
(Dec (record-set m (count (- (.count m) 1))))
((Typed s) (record-set m (name s))))))
(: view (-> Model (View Msg)))
(defun (view m)
(column #:spacing 8.0 #:margin 16.0
(text-input (.name m) Typed #:placeholder "Your name")
(text #"Hello, ${(if (string-empty? (.name m)) "stranger" (.name m))}!" #:size 18.0)
(row #:spacing 8.0
(button "-" Dec #:width 40.0)
(text #"${(.count m)}" #:size 18.0 #:margin 4.0)
(button "+" Inc #:width 40.0))))
(defun (main)
(run-app (Model (count 0) (name "")) gui-update view #:title "Counter" #:width 320.0 #:height 200.0))