refactor options to use Time.time to track when key is pressed, instead of using a Real.real value

This commit is contained in:
2025-08-29 12:33:45 +01:00
parent 91bf6854ea
commit a58953e90d
4 changed files with 26 additions and 15 deletions

View File

@@ -13,15 +13,15 @@ sig
type options_type =
{ focus: focus
, isSelected: bool
, lastUpPress: real
, lastDownPress: real
, lastUpPress: Time.time
, lastDownPress: Time.time
, tempKeys: CoreKey.user_key
}
val init: CoreKey.user_key -> options_type
end
structure OptionsType :> OPTIONS_TYPE =
structure OptionsType : OPTIONS_TYPE =
struct
datatype focus =
LEFT_KEY
@@ -36,16 +36,16 @@ struct
type options_type =
{ focus: focus
, isSelected: bool
, lastUpPress: real
, lastDownPress: real
, lastUpPress: Time.time
, lastDownPress: Time.time
, tempKeys: CoreKey.user_key
}
fun init userKeys =
{ focus = LEFT_KEY
, isSelected = false
, lastUpPress = 0.0
, lastDownPress = 0.0
, lastUpPress = Time.zeroTime
, lastDownPress = Time.zeroTime
, tempKeys = userKeys
}
end

View File

@@ -49,8 +49,8 @@ struct
* as neither is being pressed. *)
val options =
{ focus = focus
, lastUpPress = 0.0
, lastDownPress = 0.0
, lastUpPress = Time.zeroTime
, lastDownPress = Time.zeroTime
, isSelected = isSelected
, tempKeys = tempKeys
}
@@ -60,6 +60,9 @@ struct
fun moveFocusUp (options: OptionsType.options_type, newFocus, userKeys, time) =
let
(* only opening Time for time comparison and adding; no impurities here *)
open Time
val {focus, isSelected, lastUpPress, tempKeys, ...} = options
(* only switch to newFocus if it is time for key delay to be triggered.
* We set lastDownPress to 0 because up is currently being pressed instead
@@ -68,14 +71,14 @@ struct
if lastUpPress + Constants.keyDelay <= time then
{ focus = newFocus
, lastUpPress = time
, lastDownPress = 0.0
, lastDownPress = Time.zeroTime
, isSelected = isSelected
, tempKeys = tempKeys
}
else
{ focus = focus
, lastUpPress = lastUpPress
, lastDownPress = 0.0
, lastDownPress = Time.zeroTime
, isSelected = isSelected
, tempKeys = tempKeys
}
@@ -86,18 +89,21 @@ struct
fun moveFocusDown
(options: OptionsType.options_type, newFocus, userKeys, time) =
let
(* only opening Time for time comparison and adding; no impurities here *)
open Time
val {focus, isSelected, lastDownPress, tempKeys, ...} = options
val options =
if lastDownPress + Constants.keyDelay <= time then
{ focus = newFocus
, lastUpPress = 0.0
, lastUpPress = Time.zeroTime
, lastDownPress = time
, isSelected = isSelected
, tempKeys = tempKeys
}
else
{ focus = focus
, lastUpPress = 0.0
, lastUpPress = Time.zeroTime
, lastDownPress = lastDownPress
, isSelected = isSelected
, tempKeys = tempKeys