Skip to content

Commit 3c1d381

Browse files
authored
Replace String arguments with Text (#138)
Shave a few allocations and pointer-chasing due to conversion.
1 parent 04fe618 commit 3c1d381

File tree

5 files changed

+292
-226
lines changed

5 files changed

+292
-226
lines changed

Main.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Data.IORef
1010
import qualified Data.Vector as Vector
1111
import DearImGui
1212
import DearImGui.OpenGL3
13+
import DearImGui.Internal.Text (pack)
1314
import DearImGui.SDL
1415
import DearImGui.SDL.OpenGL
1516
import Control.Exception
@@ -134,18 +135,18 @@ loop window checked color slider r pos size' selected tab1Ref tab2Ref = do
134135

135136
text "ListClipper"
136137
withChildOpen "##fixed" (ImVec2 0 200) True ImGuiWindowFlags_None do
137-
let lotsOfItems = Vector.generate 50 (mappend "Item " . show)
138+
let lotsOfItems = Vector.generate 50 (pack . mappend "Item " . show)
138139
withListClipper Nothing lotsOfItems text
139140

140141
text "ListClipper, Haskell-powered"
141142
withChildOpen "##infinite" (ImVec2 0 200) True ImGuiWindowFlags_None do
142-
let infiniteItems = map (mappend "Item " . show) [0 :: Int ..]
143+
let infiniteItems = map (pack . mappend "Item " . show) [0 :: Int ..]
143144
withListClipper Nothing infiniteItems text
144145

145146
text "Ethereal ListClipper"
146147
withChildOpen "##ethereal" (ImVec2 0 200) True ImGuiWindowFlags_None do
147148
withListClipper Nothing (ClipRange (0 :: Int) 1000) $
148-
text . mappend "Item " . show
149+
text . pack . mappend "Item " . show
149150

150151
plotHistogram "A histogram" [ 10, 10, 20, 30, 90 ]
151152

dear-imgui.cabal

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cabal-version: 3.0
22

33
name: dear-imgui
4-
version: 1.5.0
4+
version: 2.0.0
55
author: Oliver Charles
66
77
license: BSD-3-Clause
@@ -140,13 +140,14 @@ library
140140
exposed-modules:
141141
DearImGui
142142
DearImGui.FontAtlas
143+
DearImGui.Internal.Text
143144
DearImGui.Raw
144145
DearImGui.Raw.DrawList
145146
DearImGui.Raw.Font
146147
DearImGui.Raw.Font.Config
147148
DearImGui.Raw.Font.GlyphRanges
148-
DearImGui.Raw.ListClipper
149149
DearImGui.Raw.IO
150+
DearImGui.Raw.ListClipper
150151
other-modules:
151152
DearImGui.Context
152153
DearImGui.Enums
@@ -171,6 +172,7 @@ library
171172
, StateVar
172173
, unliftio
173174
, vector
175+
, text
174176

175177
if flag(disable-obsolete)
176178
cxx-options: -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS
@@ -286,7 +288,7 @@ library dear-imgui-generator
286288
, scientific
287289
>= 0.3.6.2 && < 0.3.8
288290
, text
289-
>= 1.2.4 && < 1.3
291+
>= 1.2.4 && < 2.1
290292
, th-lift
291293
>= 0.7 && < 0.9
292294
, transformers
@@ -308,10 +310,10 @@ executable glfw
308310
main-is: Main.hs
309311
hs-source-dirs: examples/glfw
310312
default-language: Haskell2010
311-
if (!flag(examples) || !flag(glfw) || !flag(opengl2))
313+
if (!flag(examples) || !flag(glfw) || !flag(opengl3))
312314
buildable: False
313315
else
314-
build-depends: base, GLFW-b, gl, dear-imgui, managed
316+
build-depends: base, GLFW-b, gl, dear-imgui, managed, text
315317

316318
executable readme
317319
import: common, exe-flags

examples/glfw/Main.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Data.Bits ((.|.))
1313
import Data.IORef
1414
import Data.List (sortBy)
1515
import Data.Foldable (traverse_)
16+
import Data.Text (Text, pack)
1617

1718
import DearImGui
1819
import DearImGui.OpenGL2
@@ -61,7 +62,7 @@ main = do
6162

6263
GLFW.terminate
6364

64-
mainLoop :: Window -> IORef [(Integer, String)] -> IO ()
65+
mainLoop :: Window -> IORef [(Integer, Text)] -> IO ()
6566
mainLoop win tableRef = do
6667
-- Process the event loop
6768
GLFW.pollEvents
@@ -102,7 +103,7 @@ mainLoop win tableRef = do
102103

103104
mainLoop win tableRef
104105

105-
mkTable :: IORef [(Integer, String)] -> IO ()
106+
mkTable :: IORef [(Integer, Text)] -> IO ()
106107
mkTable tableRef =
107108
withTableOpen sortable "MyTable" 3 $ do
108109
tableSetupColumn "Hello"
@@ -120,7 +121,7 @@ mkTable tableRef =
120121
readIORef tableRef >>=
121122
traverse_ \(ix, title) -> do
122123
tableNextRow
123-
tableNextColumn $ text (show ix)
124+
tableNextColumn $ text (pack $ show ix)
124125
tableNextColumn $ text title
125126
tableNextColumn $ void (button "")
126127
where

0 commit comments

Comments
 (0)