1
1
{-# language BlockArguments #-}
2
2
{-# language LambdaCase #-}
3
3
{-# language OverloadedStrings #-}
4
+ {-# language RecordWildCards #-}
4
5
5
6
module Main ( main ) where
6
7
7
8
import Control.Exception
8
9
import Control.Monad
9
10
import Control.Monad.IO.Class
10
11
import Control.Monad.Managed
12
+ import Data.Bits ((.|.) )
13
+ import Data.IORef
14
+ import Data.List (sortBy )
15
+ import Data.Foldable (traverse_ )
16
+
11
17
import DearImGui
12
18
import DearImGui.OpenGL2
13
19
import DearImGui.GLFW
@@ -40,14 +46,23 @@ main = do
40
46
-- Initialize ImGui's OpenGL backend
41
47
_ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
42
48
43
- liftIO $ mainLoop win
49
+ tableRef <- liftIO $ newIORef
50
+ [ (1 , " foo" )
51
+ , (2 , " bar" )
52
+ , (3 , " baz" )
53
+ , (10 , " spam" )
54
+ , (11 , " spam" )
55
+ , (12 , " spam" )
56
+ ]
57
+
58
+ liftIO $ mainLoop win tableRef
44
59
Nothing -> do
45
60
error " GLFW createWindow failed"
46
61
47
62
GLFW. terminate
48
63
49
- mainLoop :: Window -> IO ()
50
- mainLoop win = do
64
+ mainLoop :: Window -> IORef [( Integer , String )] -> IO ()
65
+ mainLoop win tableRef = do
51
66
-- Process the event loop
52
67
GLFW. pollEvents
53
68
close <- GLFW. windowShouldClose win
@@ -73,8 +88,9 @@ mainLoop win = do
73
88
when clicked $
74
89
closeCurrentPopup
75
90
76
- -- Show the ImGui demo window
77
- showDemoWindow
91
+ newLine
92
+
93
+ mkTable tableRef
78
94
79
95
-- Render
80
96
glClear GL_COLOR_BUFFER_BIT
@@ -84,4 +100,41 @@ mainLoop win = do
84
100
85
101
GLFW. swapBuffers win
86
102
87
- mainLoop win
103
+ mainLoop win tableRef
104
+
105
+ mkTable :: IORef [(Integer , String )] -> IO ()
106
+ mkTable tableRef =
107
+ withTableOpen sortable " MyTable" 3 $ do
108
+ tableSetupColumn " Hello"
109
+ tableSetupColumnWith defTableColumnOptions " World"
110
+
111
+ withSortableTable \ isDirty sortSpecs ->
112
+ when (isDirty && not (null sortSpecs)) do
113
+ -- XXX: do your sorting & cache it. Dont sort every frame.
114
+ putStrLn " So dirty!"
115
+ print sortSpecs
116
+ modifyIORef' tableRef . sortBy $
117
+ foldMap mkCompare sortSpecs
118
+
119
+ tableHeadersRow
120
+ readIORef tableRef >>=
121
+ traverse_ \ (ix, title) -> do
122
+ tableNextRow
123
+ tableNextColumn $ text (show ix)
124
+ tableNextColumn $ text title
125
+ tableNextColumn $ void (button " ♥" )
126
+ where
127
+ mkCompare TableSortingSpecs {.. } a b =
128
+ let
129
+ dir = if tableSortingReverse then flip else id
130
+ in
131
+ case tableSortingColumn of
132
+ 0 -> dir compare (fst a) (fst b)
133
+ 1 -> dir compare (snd a) (snd b)
134
+ _ -> EQ
135
+
136
+ sortable = defTableOptions
137
+ { tableFlags =
138
+ ImGuiTableFlags_Sortable .|.
139
+ ImGuiTableFlags_SortMulti
140
+ }
0 commit comments