From b67587df2b55648ef8506b5b945ed6ed3258d3c2 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Thu, 28 Mar 2024 17:06:25 -0400 Subject: [PATCH] TimedDialog bug fix --- CHANGELOG.md | 4 +- .../src/components/Dialog/TimedDialog.bs | 80 +++++++++++++++---- 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04d804e8..8ad0c171 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing worth mentioning yet. +### Fixed + +- Dialog with timer now stops as expected if any button is pressed ## [0.22.0] - 2024-03-24 diff --git a/playlet-lib/src/components/Dialog/TimedDialog.bs b/playlet-lib/src/components/Dialog/TimedDialog.bs index cd7b499a..439ea49c 100644 --- a/playlet-lib/src/components/Dialog/TimedDialog.bs +++ b/playlet-lib/src/components/Dialog/TimedDialog.bs @@ -29,6 +29,9 @@ function OnStartCountdown() m.timer.observeFieldScoped("fire", FuncName(TimerUpdateButtons)) m.currentCountDown = defaultActionTimeoutSeconds m.timer.control = "start" + + FindContentComponent() + WatchContentTranslation() end function function TimerUpdateButtons() as void @@ -39,13 +42,8 @@ function TimerUpdateButtons() as void return end if - FindButtonComponents() - if m.buttonComponents = invalid - return - end if - - defaultButtonIndex = m.top.defaultButtonIndex - m.buttonComponents[defaultButtonIndex].text = `${m.top.actualButtons[defaultButtonIndex]} (${m.currentCountDown})` + FindButtonComponent() + SetDefaultButtonText(m.currentCountDown) end function function OnButtonSelected() @@ -55,6 +53,7 @@ end function function OnDialogClosed() CancelTimer() + UnwatchContentTranslation() end function function onKeyEvent(key as string, press as boolean) as boolean @@ -63,14 +62,10 @@ function onKeyEvent(key as string, press as boolean) as boolean end if CancelTimer() + UnwatchContentTranslation() - FindButtonComponents() - if m.buttonComponents = invalid - return false - end if - - defaultButtonIndex = m.top.defaultButtonIndex - m.buttonComponents[defaultButtonIndex].text = m.top.actualButtons[defaultButtonIndex] + FindButtonComponent() + SetDefaultButtonText(0) return false end function @@ -82,7 +77,21 @@ function CancelTimer() end if end function -function FindButtonComponents() as void +function SetDefaultButtonText(timeLeft as integer) as void + if m.buttonComponents = invalid + return + end if + + defaultButtonIndex = m.top.defaultButtonIndex + + if timeLeft > 0 + m.buttonComponents[defaultButtonIndex].text = `${m.top.actualButtons[defaultButtonIndex]} (${timeLeft})` + else + m.buttonComponents[defaultButtonIndex].text = m.top.actualButtons[defaultButtonIndex] + end if +end function + +function FindButtonComponent() as void if m.buttonComponents <> invalid return end if @@ -96,3 +105,44 @@ function FindButtonComponents() as void end for LogWarn("No buttons found in dialog") end function + +function FindContentComponent() as void + if m.contentComponent <> invalid + return + end if + + areas = m.top.getChildren(-1, 0) + for each area in areas + if area.subtype() = "StdDlgContentArea" + if area.getChildCount() > 0 + m.contentComponent = area.getChild(0) + end if + return + end if + end for + LogWarn("No content component found in dialog") +end function + +function WatchContentTranslation() as void + if m.contentComponent = invalid + return + end if + + m.contentComponent.observeFieldScoped("translation", FuncName(OnContentMoved)) +end function + +function UnwatchContentTranslation() as void + if m.contentComponent = invalid + return + end if + + m.contentComponent.unobserveFieldScoped("translation") + m.contentComponent = invalid +end function + +function OnContentMoved() + CancelTimer() + UnwatchContentTranslation() + FindButtonComponent() + SetDefaultButtonText(0) +end function