Skip to content

Commit

Permalink
Replace String arguments with Text (#138)
Browse files Browse the repository at this point in the history
Shave a few allocations and pointer-chasing due to conversion.
  • Loading branch information
dpwiz authored May 15, 2022
1 parent 04fe618 commit 3c1d381
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 226 deletions.
7 changes: 4 additions & 3 deletions Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Data.IORef
import qualified Data.Vector as Vector
import DearImGui
import DearImGui.OpenGL3
import DearImGui.Internal.Text (pack)
import DearImGui.SDL
import DearImGui.SDL.OpenGL
import Control.Exception
Expand Down Expand Up @@ -134,18 +135,18 @@ loop window checked color slider r pos size' selected tab1Ref tab2Ref = do

text "ListClipper"
withChildOpen "##fixed" (ImVec2 0 200) True ImGuiWindowFlags_None do
let lotsOfItems = Vector.generate 50 (mappend "Item " . show)
let lotsOfItems = Vector.generate 50 (pack . mappend "Item " . show)
withListClipper Nothing lotsOfItems text

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

text "Ethereal ListClipper"
withChildOpen "##ethereal" (ImVec2 0 200) True ImGuiWindowFlags_None do
withListClipper Nothing (ClipRange (0 :: Int) 1000) $
text . mappend "Item " . show
text . pack . mappend "Item " . show

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

Expand Down
12 changes: 7 additions & 5 deletions dear-imgui.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cabal-version: 3.0

name: dear-imgui
version: 1.5.0
version: 2.0.0
author: Oliver Charles
maintainer: ollie@ocharles.org.uk, aenor.realm@gmail.com
license: BSD-3-Clause
Expand Down Expand Up @@ -140,13 +140,14 @@ library
exposed-modules:
DearImGui
DearImGui.FontAtlas
DearImGui.Internal.Text
DearImGui.Raw
DearImGui.Raw.DrawList
DearImGui.Raw.Font
DearImGui.Raw.Font.Config
DearImGui.Raw.Font.GlyphRanges
DearImGui.Raw.ListClipper
DearImGui.Raw.IO
DearImGui.Raw.ListClipper
other-modules:
DearImGui.Context
DearImGui.Enums
Expand All @@ -171,6 +172,7 @@ library
, StateVar
, unliftio
, vector
, text

if flag(disable-obsolete)
cxx-options: -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS
Expand Down Expand Up @@ -286,7 +288,7 @@ library dear-imgui-generator
, scientific
>= 0.3.6.2 && < 0.3.8
, text
>= 1.2.4 && < 1.3
>= 1.2.4 && < 2.1
, th-lift
>= 0.7 && < 0.9
, transformers
Expand All @@ -308,10 +310,10 @@ executable glfw
main-is: Main.hs
hs-source-dirs: examples/glfw
default-language: Haskell2010
if (!flag(examples) || !flag(glfw) || !flag(opengl2))
if (!flag(examples) || !flag(glfw) || !flag(opengl3))
buildable: False
else
build-depends: base, GLFW-b, gl, dear-imgui, managed
build-depends: base, GLFW-b, gl, dear-imgui, managed, text

executable readme
import: common, exe-flags
Expand Down
7 changes: 4 additions & 3 deletions examples/glfw/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Data.Bits ((.|.))
import Data.IORef
import Data.List (sortBy)
import Data.Foldable (traverse_)
import Data.Text (Text, pack)

import DearImGui
import DearImGui.OpenGL2
Expand Down Expand Up @@ -61,7 +62,7 @@ main = do

GLFW.terminate

mainLoop :: Window -> IORef [(Integer, String)] -> IO ()
mainLoop :: Window -> IORef [(Integer, Text)] -> IO ()
mainLoop win tableRef = do
-- Process the event loop
GLFW.pollEvents
Expand Down Expand Up @@ -102,7 +103,7 @@ mainLoop win tableRef = do

mainLoop win tableRef

mkTable :: IORef [(Integer, String)] -> IO ()
mkTable :: IORef [(Integer, Text)] -> IO ()
mkTable tableRef =
withTableOpen sortable "MyTable" 3 $ do
tableSetupColumn "Hello"
Expand All @@ -120,7 +121,7 @@ mkTable tableRef =
readIORef tableRef >>=
traverse_ \(ix, title) -> do
tableNextRow
tableNextColumn $ text (show ix)
tableNextColumn $ text (pack $ show ix)
tableNextColumn $ text title
tableNextColumn $ void (button "")
where
Expand Down
Loading

0 comments on commit 3c1d381

Please sign in to comment.