I'm trying out some stuff to see if NiGui is suited for my needs. Played a bit with editing samples, but got into an issue with threads. And I need those, or Windows will complain with a 'not responding' warning.
I started with the thread example adding a text area and a button to play/pause. When [Play/Pause] is clicked the first time (toggled to false), updates halt, as expected, but when toggled again (to true) nothing happens. The clicks are registered, but the thread does not respond anymore. Obviously I am doing it the wrong way.
# Based on "This example shows how to change a control from another thread" from the nigui examples
# Trying to start/stop var update in thread
import nigui
import strutils, strformat
var
window: Window
container: LayoutContainer
button_start: Button
button_play: Button
pbar: ProgressBar
thread: Thread[void]
var
p: int
s: string
play: bool = true
app.init()
window = newWindow("NiGui Example")
container = newLayoutContainer(Layout_Vertical)
container.padding = 10
window.add(container)
button_start = newButton("Start thread")
button_play = newButton("Play/Pause")
container.add(button_start)
var textArea = newTextArea()
# Create a multiline text box.
# By default, a text area is empty and editable.
proc update() =
{.gcsafe.}:
echo "In update pass ", p, ", [play] is ", play
if play:
pbar.value = pbar.value + 0.01
app.sleep(100)
p += 1
s = fmt("Pass {p:5d}")
textArea.addLine(s)
app.queueMain(update)
proc start() =
{.gcsafe.}:
app.queueMain(update)
button_start.onClick = proc(event: ClickEvent) =
container.remove(button_start)
container.add(button_play)
pbar = newProgressBar()
container.add(pbar)
container.add(textArea) # Add the text area to the container.
createThread(thread, start)
button_play.onClick = proc(event: ClickEvent) =
echo "Play/Pause clicked"
# play = !play (compiler error, can't do that in Nim)
if play == false:
play = true
else:
play = false
echo "toggled play to ", play
window.show()
app.run()
I've written a small Windows application using NiGui which uses thread for heavy processing and which makes the GUI usable. I don't know if it was the best solution, but here's a small example of how I did it: example
Maybe a timer would be a better alternative to check if the thread is still running, but I never tried.