Skip to content

Commit

Permalink
Trying to change std/threadpool with weave...
Browse files Browse the repository at this point in the history
  • Loading branch information
Patitotective committed Sep 7, 2024
1 parent 36117e8 commit 5f90559
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
17 changes: 9 additions & 8 deletions ImExample.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ backend = "cpp"

# Dependencies

requires "nim >= 1.6.2"
requires "kdl >= 2.0.1"
requires "nimgl >= 1.3.2"
requires "stb_image >= 2.5"
requires "imstyle >= 3.0.0"
requires "openurl >= 2.0.4"
requires "tinydialogs >= 1.0.0"
requires "constructor >= 1.2.0"
requires "nim ^= 1.6.2"
requires "kdl ^= 2.0.1"
requires "nimgl ^= 1.3.2"
requires "stb_image ^= 2.5"
requires "imstyle ^= 3.0.0"
requires "openurl ^= 2.0.4"
requires "tinydialogs ^= 1.0.0"
requires "constructor ^= 1.2.0"
requires "weave ^= 0.4.10"

import std/[strformat, options]
import src/configtype
Expand Down
5 changes: 3 additions & 2 deletions main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ proc drawMain(app: var App) = # Draw the main window
igText(FA_Info & " Application average %.3f ms/frame (%.1f FPS)", 1000f / igGetIO().framerate, igGetIO().framerate)

if igButton("Click me"):
spawn notifyPopup(app.config.name, "Do not do that again", IconType.Warning)
spawn notifyPopup(app.config.name, "Do not do that again", IconType.Warning)

app.fonts[1].igPushFont()
igText("Unicode fonts (NotoSansJP-Regular.otf)")
Expand Down Expand Up @@ -215,6 +215,7 @@ proc initApp(): App =
result.resources = readResources()

result.config = Config()
init(Weave)

let filename =
when defined(release): "prefs"
Expand Down Expand Up @@ -264,7 +265,7 @@ template initFonts(app: var App) =
io.fonts.igAddFontFromMemoryTTF(app.res(app.config.iconFontPath), font.size, config.unsafeAddr, iconFontGlyphRanges[0].unsafeAddr)

proc terminate(app: var App) =
sync() # Wait for spawned threads
exit(Weave)

var x, y, width, height: int32

Expand Down
25 changes: 14 additions & 11 deletions src/settingsmodal.nim
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import std/[threadpool, typetraits, strutils, options, tables, macros, os]
import std/[typetraits, strutils, options, tables, macros, os]
import micros
import kdl/prefs
import tinydialogs
import nimgl/imgui
import weave

import utils, icons, types
import utils, icons, types, settingstypes

proc settingLabel(name: string, setting: Setting[auto]): string =
(if setting.display.len == 0: name else: setting.display) & ": "

proc drawSettings(settings: var object, maxLabelWidth: float32): bool =
## Returns wheter or not to open the block dialog (because a file dailog or so was open)

## Returns wheter or not to open the block dialog (because a file dialog or so was open)
for name, setting in settings.fieldPairs:
let label = settingLabel(name, setting)
let id = cstring "##" & name
Expand Down Expand Up @@ -96,19 +96,22 @@ proc drawSettings(settings: var object, maxLabelWidth: float32): bool =
of stRGBA:
igColorEdit4(id, setting.rgbaCache, makeFlags(setting.rgbaFlags))
of stFile:
if not setting.fileCache.flowvar.isNil and setting.fileCache.flowvar.isReady and (let val = ^setting.fileCache.flowvar; val.len > 0):
setting.fileCache = (val: val, flowvar: nil) # Here we set flowvar to nil because once we acquire it's value it's not neccessary until it's spawned again
if setting.fileCache.flowvar.isSpawned and setting.fileCache.flowvar.isReady and (let val = sync setting.fileCache.flowvar; val.len > 0):
setting.fileCache.val = val

igPushID(id)
igInputTextWithHint("##input", "No file selected", cstring setting.fileCache.val, uint setting.fileCache.val.len, flags = ImGuiInputTextFlags.ReadOnly)
igSameLine()
if igButton("Browse " & FA_FolderOpen):
setting.fileCache.flowvar = spawn openFileDialog("Choose File", getCurrentDir() / "\0", setting.fileFilterPatterns, setting.fileSingleFilterDescription)
let args = OpenFileDialogArgs (title: "Choose File", defaultPath: getCurrentDir() / "\0",
filterPatterns: setting.fileFilterPatterns, singleFilterDescription: setting.fileSingleFilterDescription)
proc x(args: OpenFileDialogArgs): string = openFileDialog(args)
setting.fileCache.flowvar = spawn x(args)
result = true
igPopID()
of stFiles:
if not setting.filesCache.flowvar.isNil and setting.filesCache.flowvar.isReady and (let val = ^setting.filesCache.flowvar; val.len > 0):
setting.filesCache = (val: val, flowvar: nil) # Here we set flowvar to nil because once we acquire it's value it's not neccessary until it's spawned again
if setting.filesCache.flowvar.isSpawned and setting.filesCache.flowvar.isReady and (let val = sync setting.filesCache.flowvar; val.len > 0):
setting.filesCache.val = val

let files = setting.filesCache.val.join(";")
igPushID(id)
Expand All @@ -119,8 +122,8 @@ proc drawSettings(settings: var object, maxLabelWidth: float32): bool =
result = true
igPopID()
of stFolder:
if not setting.folderCache.flowvar.isNil and setting.folderCache.flowvar.isReady and (let val = ^setting.folderCache.flowvar; val.len > 0):
setting.folderCache = (val: val, flowvar: nil) # Here we set flowvar to nil because once we acquire it's value it's not neccessary until it's spawned again
if setting.folderCache.flowvar.isSpawned and setting.folderCache.flowvar.isReady and (let val = sync setting.folderCache.flowvar; val.len > 0):
setting.folderCache = val

igPushID(id)
igInputTextWithHint("##input", "No folder selected", cstring setting.folderCache.val, uint setting.folderCache.val.len, flags = ImGuiInputTextFlags.ReadOnly)
Expand Down
2 changes: 1 addition & 1 deletion src/settingstypes.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import std/[threadpool]
import std/macros except eqIdent # since it conflicts with kdl/util.eqIdent
import nimgl/imgui
import weave

type
SettingType* = enum
Expand Down
8 changes: 5 additions & 3 deletions src/types.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import std/[threadpool, tables]
import std/[tables]

import nimgl/[imgui, glfw]
import tinydialogs
import kdl, kdl/[types, utils]
import constructor/defaults
import weave

import configtype, settingstypes

Expand Down Expand Up @@ -96,10 +97,10 @@ proc decodeKdl*(a: KdlNode, v: var Settings) =
decodeSettingsObj(a, v)

proc encodeKdl*[T](a: FlowVar[T], v: var KdlVal) =
if a.isNil or not a.isReady:
if not a.isSpawned or not a.isReady:
v = initKNull()
else:
v = encodeKdlVal(^a)
v = encodeKdlVal(sync a)

proc encodeKdl*(a: Empty, v: var KdlVal) =
v = initKNull()
Expand Down Expand Up @@ -180,3 +181,4 @@ type

ImageData* = tuple[image: seq[byte], width, height: int]

OpenFileDialogArgs* = tuple[title, defaultPath: string, filterPatterns: seq[string], singleFilterDescription: string]
13 changes: 7 additions & 6 deletions src/utils.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import std/[typetraits, threadpool, strutils, tables, macros, os]
import std/[typetraits, strutils, tables, macros, os]
import kdl, kdl/prefs
import stb_image/read as stbi
import nimgl/[imgui, glfw, opengl]
import tinydialogs
import weave

import types
import types, settingstypes

proc makeFlags*[T: enum](flags: varargs[T]): T =
## Mix multiple flags of a specific enum
Expand Down Expand Up @@ -210,7 +211,7 @@ macro checkFlowVarsReady*(app: App, fields: varargs[untyped]): bool =
# (app.field1.isNil or app.field1.isReady) and (app.field2.isNil or app.field2.isReady)
for field in fields:
let cond = quote do:
(`app`.`field`.isNil or `app`.`field`.isReady)
(not `app`.`field`.isSpawned or `app`.`field`.isReady)

if result.kind == nnkEmpty:
result = cond
Expand All @@ -224,13 +225,13 @@ proc checkSettingsFlowVarsReadyImpl(obj: object): bool =
for fieldName, field in obj.fieldPairs:
case field.kind
of stFile:
if not field.fileCache.flowvar.isNil and not field.fileCache.flowvar.isReady:
if field.fileCache.flowvar.isSpawned and not field.fileCache.flowvar.isReady:
return false
of stFiles:
if not field.filesCache.flowvar.isNil and not field.filesCache.flowvar.isReady:
if field.filesCache.flowvar.isSpawned and not field.filesCache.flowvar.isReady:
return false
of stFolder:
if not field.folderCache.flowvar.isNil and not field.folderCache.flowvar.isReady:
if field.folderCache.flowvar.isSpawned and not field.folderCache.flowvar.isReady:
return false
of stSection:
when field.content is object:
Expand Down

0 comments on commit 5f90559

Please sign in to comment.