35 lines
1.2 KiB
Text
35 lines
1.2 KiB
Text
|
|
;; 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))))
|
||
|
|
|
||
|
|
(: update (-> Msg Model Model))
|
||
|
|
(defun (update msg m)
|
||
|
|
(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 "")) update view #:title "Counter" #:width 320.0 #:height 200.0))
|