Skip to content

Commit a10a660

Browse files
committed
More abstraction
1 parent 3d37290 commit a10a660

1 file changed

Lines changed: 12 additions & 11 deletions

File tree

ff-qtah/FF/Qt/TaskWidget.hs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ module FF.Qt.TaskWidget (
1212

1313
import Control.Monad (void, when)
1414
import Data.Foldable (for_)
15+
import Data.Function (fix)
1516
import Data.IORef (IORef, atomicWriteIORef, newIORef, readIORef)
1617
import Data.Maybe (fromMaybe)
1718
import Data.Text qualified as Text
1819
import Data.Time (defaultTimeLocale, formatTime)
19-
import Foreign.Hoppy.Runtime (delete, nullptr)
20+
import Foreign.Hoppy.Runtime (CppPtr, delete, nullptr)
2021
import Graphics.UI.Qtah.Core.Types qualified as Qt
2122
import Graphics.UI.Qtah.Signal (connect_)
2223
import Graphics.UI.Qtah.Widgets.QAbstractButton qualified as QAbstractButton
@@ -157,18 +158,18 @@ update keepOpen this noteDoc = do
157158
this.onTaskUpdated keepOpen entity
158159
where
159160
resetTags tags = do
160-
deleteChildrenWidgets this.tags
161+
deleteAllLayoutWidgets this.tags
161162
for_ tags \tag ->
162163
void $
163164
QBoxLayout.addWidget this.tags
164165
=<< qPushButton ! #text (Text.unpack tag) ! defaults
165166

166-
deleteChildrenWidgets :: (QLayoutPtr layout) => layout -> IO ()
167-
deleteChildrenWidgets layout = loop
168-
where
169-
loop = do
170-
child <- QLayout.takeAt layout 0
171-
when (child /= nullptr) do
172-
delete =<< QLayoutItem.widget child
173-
delete child
174-
loop
167+
deleteAllLayoutWidgets :: (QLayoutPtr layout) => layout -> IO ()
168+
deleteAllLayoutWidgets layout =
169+
whilePtrAlive (QLayout.takeAt layout 0) \childItem -> do
170+
delete =<< QLayoutItem.widget childItem
171+
delete childItem
172+
173+
whilePtrAlive :: (CppPtr t, Eq t, Monad m) => m t -> (t -> m a) -> m ()
174+
whilePtrAlive get action =
175+
fix \w -> do x <- get; when (x /= nullptr) $ action x *> w

0 commit comments

Comments
 (0)