Skip to content

Commit

Permalink
make gui examples individual executables
Browse files Browse the repository at this point in the history
  • Loading branch information
pa-ba committed Sep 26, 2024
1 parent 5eed3d9 commit 6576fa6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 69 deletions.
35 changes: 31 additions & 4 deletions examples/gui/gui.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,37 @@ author: Patrick Bahr, Jean-Claude Sebastian Disch, Asger Lademark H
maintainer: paba@itu.dk
build-type: Simple

executable gui
executable calculator
hs-source-dirs: src
main-is: GUI.hs
other-modules: Counter, TemperatureConverter, FlightBooker, Timer, Calculator
main-is: Calculator.hs
default-language: Haskell2010
build-depends: WidgetRattus>=0.2, base, text, containers, deepseq
build-depends: WidgetRattus>=0.2, base, text, containers
ghc-options: -W

executable counter
hs-source-dirs: src
main-is: Counter.hs
default-language: Haskell2010
build-depends: WidgetRattus>=0.2, base, text, containers
ghc-options: -W

executable flight
hs-source-dirs: src
main-is: FlightBooker.hs
default-language: Haskell2010
build-depends: WidgetRattus>=0.2, base, text, containers
ghc-options: -W

executable converter
hs-source-dirs: src
main-is: TemperatureConverter.hs
default-language: Haskell2010
build-depends: WidgetRattus>=0.2, base, text, containers
ghc-options: -W

executable timer
hs-source-dirs: src
main-is: timer.hs
default-language: Haskell2010
build-depends: WidgetRattus>=0.2, base, text, containers
ghc-options: -W
14 changes: 6 additions & 8 deletions examples/gui/src/Calculator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@
{-# HLINT ignore "Use const" #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Calculator where
import WidgetRattus
import WidgetRattus.Signal
import WidgetRattus.Widgets
import Prelude hiding (map, const, zipWith, zip, filter, getLine, putStrLn,null)
import Data.Text hiding (filter, map, all, foldl1)

-- Benchmark 4

reset :: Int -> Int
reset _ = 0


calc :: C VStack
calc = do
window :: C VStack
window = do
zero <- mkButton (const ("0" ::Text))
one <- mkButton (const ("1" ::Text))
two <- mkButton (const ("2" ::Text))
Expand Down Expand Up @@ -83,4 +78,7 @@ calc = do

input <- mkHStack (const [enabledWidget numbers, enabledWidget operators])

mkVStack (const [enabledWidget result, enabledWidget input])
mkVStack (const [enabledWidget result, enabledWidget input])

main :: IO ()
main = runApplication window
9 changes: 5 additions & 4 deletions examples/gui/src/Counter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
{-# HLINT ignore "Use const" #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Counter where
import WidgetRattus
import WidgetRattus.Signal
import WidgetRattus.Widgets

import Prelude hiding (map, const, zipWith, zip, filter, getLine, putStrLn,null)
import Data.Text hiding (filter, map, all)

--Benchmark 1
benchmark1 :: C VStack
benchmark1 = do
window :: C VStack
window = do
btn <- mkButton (const ("Increment" :: Text))
let sig = btnOnClickSig btn
let sig' = scanAwait (box (\ n _ -> n+1 :: Int)) 0 sig
lbl <- mkLabel sig'
mkVStack (const [enabledWidget lbl, enabledWidget btn])


main :: IO ()
main = runApplication window
10 changes: 6 additions & 4 deletions examples/gui/src/FlightBooker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
{-# HLINT ignore "Evaluate" #-}
{-# HLINT ignore "Use const" #-}

module FlightBooker where
import WidgetRattus
import WidgetRattus.Signal
import WidgetRattus.Widgets
Expand Down Expand Up @@ -58,8 +57,8 @@ bookingToText oneWay dep ret =
"You have booked a " <> if oneWay then "one-way flight on " <> dep
else "return flight from " <> dep <> " to " <> ret

benchmark3 :: C VStack
benchmark3 = do
window :: C VStack
window = do
dropDown <- mkTextDropdown (const ["One-Way", "Return-Flight"]) "One-Way"
tf1 <- mkTextField "01-01-2021"
tf2 <- mkTextField "01-02-2021"
Expand All @@ -85,4 +84,7 @@ benchmark3 = do

mkVStack (const
[enabledWidget popup, enabledWidget dropDown,
enabledWidget tf1, Widget tf2 isRF, Widget button validBooking])
enabledWidget tf1, Widget tf2 isRF, Widget button validBooking])

main :: IO ()
main = runApplication window
16 changes: 0 additions & 16 deletions examples/gui/src/GUI.hs

This file was deleted.

35 changes: 5 additions & 30 deletions examples/gui/src/TemperatureConverter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{-# HLINT ignore "Evaluate" #-}
{-# HLINT ignore "Use const" #-}

module TemperatureConverter where

import WidgetRattus
import WidgetRattus.Signal
import WidgetRattus.Widgets
Expand All @@ -27,35 +27,8 @@ fahrenheitToCelsius t =
Right (t', _) -> toText ((t' - 32) * 5 `div` 9)
Left _ -> "Invalid input"

-- Initial version of benchmark 2.
initialbenchmark2 :: C HStack
initialbenchmark2 = do
c1 <- chan
c2 <- chan

let sigC = mkSig (box (wait c1))
let sigF = mkSig (box (wait c2))

let convertFtoC = mapAwait (box fahrenheitToCelsius) sigF
let convertCtoF = mapAwait (box celsiusToFahrenheit) sigC

let sigC' = "0":::interleave (box (\ x _ -> x)) convertFtoC sigC
let sigF' = "32":::interleave (box (\ x _ -> x)) convertCtoF sigF

let tfC = TextField {tfContent = sigC', tfInput = c1}
let tfF = TextField {tfContent = sigF', tfInput = c2}

fLabel <- mkLabel (const ("Fahrenheit" :: Text)) -- requires type annotations
cLabel <- mkLabel (const ("Celsius" :: Text))

fStack <- mkVStack (const [enabledWidget tfC, enabledWidget cLabel])
cStack <- mkVStack (const [enabledWidget tfF, enabledWidget fLabel])

mkHStack (const [enabledWidget fStack, enabledWidget cStack])

-- Improved version of Benchmark 2
benchmark2 :: C HStack
benchmark2 = do
window :: C HStack
window = do
tfF1 <- mkTextField "32"
tfC1 <- mkTextField "0"

Expand All @@ -73,3 +46,5 @@ benchmark2 = do
mkHStack (const [enabledWidget fStack, enabledWidget cStack])


main :: IO ()
main = runApplication window
9 changes: 6 additions & 3 deletions examples/gui/src/Timer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

{-# LANGUAGE TypeOperators #-}

module Timer where

import WidgetRattus
import WidgetRattus.Signal
import WidgetRattus.Widgets
Expand Down Expand Up @@ -37,8 +37,8 @@ first (x :* _) = x
second :: (a :* b) -> b
second (_ :* y) = y

benchmark4 :: C VStack
benchmark4 = do
window :: C VStack
window = do
slider <- mkSlider 50 (const 1) (const 100)
resetBtn <- mkButton (const (pack "Reset"))

Expand All @@ -62,3 +62,6 @@ benchmark4 = do
enabledWidget resetBtn,
enabledWidget label,
enabledWidget pb])

main :: IO ()
main = runApplication window

0 comments on commit 6576fa6

Please sign in to comment.