From 5f9055995d6ae10de81f56008b7abe92b2b5ad9b Mon Sep 17 00:00:00 2001 From: Cristobal Date: Sat, 7 Sep 2024 13:18:53 -0500 Subject: [PATCH] Trying to change std/threadpool with weave... --- ImExample.nimble | 17 +++++++++-------- main.nim | 5 +++-- src/settingsmodal.nim | 25 ++++++++++++++----------- src/settingstypes.nim | 2 +- src/types.nim | 8 +++++--- src/utils.nim | 13 +++++++------ 6 files changed, 39 insertions(+), 31 deletions(-) diff --git a/ImExample.nimble b/ImExample.nimble index 6691bc9..bcd9d79 100644 --- a/ImExample.nimble +++ b/ImExample.nimble @@ -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 diff --git a/main.nim b/main.nim index 8315e9c..a402d25 100644 --- a/main.nim +++ b/main.nim @@ -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)") @@ -215,6 +215,7 @@ proc initApp(): App = result.resources = readResources() result.config = Config() + init(Weave) let filename = when defined(release): "prefs" @@ -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 diff --git a/src/settingsmodal.nim b/src/settingsmodal.nim index 5aef20c..ce8fa18 100644 --- a/src/settingsmodal.nim +++ b/src/settingsmodal.nim @@ -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 @@ -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) @@ -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) diff --git a/src/settingstypes.nim b/src/settingstypes.nim index 260ecc2..e19e892 100644 --- a/src/settingstypes.nim +++ b/src/settingstypes.nim @@ -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 diff --git a/src/types.nim b/src/types.nim index 38e4142..b2ad76d 100644 --- a/src/types.nim +++ b/src/types.nim @@ -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 @@ -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() @@ -180,3 +181,4 @@ type ImageData* = tuple[image: seq[byte], width, height: int] + OpenFileDialogArgs* = tuple[title, defaultPath: string, filterPatterns: seq[string], singleFilterDescription: string] diff --git a/src/utils.nim b/src/utils.nim index 4737773..3e8b921 100644 --- a/src/utils.nim +++ b/src/utils.nim @@ -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 @@ -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 @@ -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: