fjvallarino/monomer

Event registering twice with rapid clicks?

Closed this issue · 3 comments

{-# LANGUAGE OverloadedStrings, LambdaCase, OverloadedRecordDot #-}
module Main where

import Monomer
import TextShow

data NullModel = Count {term :: Int} deriving (Eq, Show)

data AppEvent
    = Init
    | Increment
    deriving (Eq, Show)

buildUI wenv model = widgetTree where
  widgetTree = box_ [alignCenter, onClick Increment] $ vstack
    [ label "Hello World!" 
    , spacer
    , label $ "Click count: " <> showt (term model) ]

nullEventHandler 
  :: WidgetEnv NullModel AppEvent
  -> WidgetNode NullModel AppEvent
  -> NullModel
  -> AppEvent
  -> [AppEventResponse NullModel AppEvent]
nullEventHandler wenv node model = \cases
    Init      -> []
    Increment -> [Model (model {term = succ model.term})]

main :: IO ()
main = startApp (Count 0) nullEventHandler buildUI config

config :: [AppConfig AppEvent]
config =
    [ appWindowTitle "Hello World!"
    , appTheme lightTheme
    , appWindowIcon "./assets/images/icon.bmp"
    , appFontDef "Regular" "./assets/fonts/Roboto-Regular.ttf"
    , appInitEvent Init ]

Modified version of your Hello World program. If you click quickly on Windows, using SDL 2.0.22, the button will increment twice.

@liamzee there was an issue in the box widget, that made some strange (and unnecessary) handling of the Release event. For further clarity, this does not happen with the regular example that uses button. You can test the fix by modifying your stack.yaml to point to:

- git: https://github.com/fjvallarino/monomer.git
  commit: 3dac85d785db391fcb41b80c3589407f03650c59

This commit hash will only be valid until the fix is merged to main.

Thanks for resolution!

@liamzee great! The PR has been merged to main. I'll close the issue now.