Skip to content

Commit bc590d9

Browse files
authored
Tweak tables and add an example (#139)
Previously: #135
1 parent e5969f6 commit bc590d9

File tree

4 files changed

+267
-138
lines changed

4 files changed

+267
-138
lines changed

examples/glfw/Main.hs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
{-# language BlockArguments #-}
22
{-# language LambdaCase #-}
33
{-# language OverloadedStrings #-}
4+
{-# language RecordWildCards #-}
45

56
module Main ( main ) where
67

78
import Control.Exception
89
import Control.Monad
910
import Control.Monad.IO.Class
1011
import Control.Monad.Managed
12+
import Data.Bits ((.|.))
13+
import Data.IORef
14+
import Data.List (sortBy)
15+
import Data.Foldable (traverse_)
16+
1117
import DearImGui
1218
import DearImGui.OpenGL2
1319
import DearImGui.GLFW
@@ -40,14 +46,23 @@ main = do
4046
-- Initialize ImGui's OpenGL backend
4147
_ <- managed_ $ bracket_ openGL2Init openGL2Shutdown
4248

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
4459
Nothing -> do
4560
error "GLFW createWindow failed"
4661

4762
GLFW.terminate
4863

49-
mainLoop :: Window -> IO ()
50-
mainLoop win = do
64+
mainLoop :: Window -> IORef [(Integer, String)] -> IO ()
65+
mainLoop win tableRef = do
5166
-- Process the event loop
5267
GLFW.pollEvents
5368
close <- GLFW.windowShouldClose win
@@ -73,8 +88,9 @@ mainLoop win = do
7388
when clicked $
7489
closeCurrentPopup
7590

76-
-- Show the ImGui demo window
77-
showDemoWindow
91+
newLine
92+
93+
mkTable tableRef
7894

7995
-- Render
8096
glClear GL_COLOR_BUFFER_BIT
@@ -84,4 +100,41 @@ mainLoop win = do
84100

85101
GLFW.swapBuffers win
86102

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

Comments
 (0)