Refactor provider module. Support starting without mode.

This commit is contained in:
2020-05-23 12:42:57 +02:00
parent 6c81d89d41
commit 2907cf6574
2 changed files with 77 additions and 64 deletions

View File

@@ -24,7 +24,7 @@ var
# Server vor recieving commands from external tools
serverWorker: system.Thread[ServerArgs]
proc enumToStrings(en: typedesc): seq[string] =
proc enumToStrings(en: typedesc): seq[string] =
for x in en:
result.add $x
@@ -44,7 +44,7 @@ proc notify(label: Label, message: string = "") =
proc newArgs(): Option[Args] =
let p = newParser("randopix"):
help(fmt"Version {version} - Display random images from different sources")
option("-m", "--mode", help="The image source mode.", choices=enumToStrings(ProviderKind))
option("-m", "--mode", help="The image source mode.", choices=enumToStrings(Mode))
option("-p", "--path", help="Path to a directory with images for the 'file' mode")
option("-t", "--timeout", help="Seconds before the image is refreshed", default="300")
flag("-w", "--windowed", help="Do not start in fullscreen mode")
@@ -52,12 +52,24 @@ proc newArgs(): Option[Args] =
try:
let opts = p.parse(commandLineParams())
let mode = some(parseEnum[ProviderKind](opts.mode))
imageProvider = newImageProvider(mode.get, opts.path)
# Parse the starting mode
var startMode: Mode
try:
startMode= parseEnum[Mode](opts.mode)
except ValueError:
startMode = Mode.None
# Create the image provider
if opts.path != "":
imageProvider = newImageProvider(opts.verbose, startMode, opts.path)
else:
imageProvider = newImageProvider(opts.verbose, startMode)
## Timeout is given in seconds as an argument
var timeout = 3000
try:
timeout = opts.timeout.parseInt * 1000
timeout = opts.timeout.parseInt * 1000
except ValueError:
raise newException(UsageError, fmt"Invalid timeout value: {opts.timeout}")
@@ -66,44 +78,48 @@ proc newArgs(): Option[Args] =
verbose: opts.verbose,
timeout: timeout))
except:
echo getCurrentExceptionMsg()
echo p.help
proc updateImage(image: Image): bool =
## Updates the UI with a new image
try:
if (args.verbose): log "Refreshing..."
if args.verbose: log "Refreshing..."
if imageProvider.mode == Mode.None:
log "No display mode"
label.notify "No mode selected"
return true
var wWidth, wHeight: int
window.getSize(wWidth, wHeight)
let op = imageProvider.next(wWidth, wHeight)
result = op.success
if op.success:
image.setFromFile(op.file)
else:
if not op.success:
label.notify op.errorMsg
return
image.setFromFile(op.file)
label.notify
except:
let
e = getCurrentException()
msg = getCurrentExceptionMsg()
log "Got exception ", repr(e), " with message ", msg
label.notify "Error while refreshing image, retrying..."
return false
proc timedUpdate(image: Image): bool =
let ok = updateImage(image);
if not ok:
label.notify "Error while refreshing image, retrying..."
else:
label.notify
proc timedUpdate(image: Image): bool =
discard updateImage(image);
updateTimeout = int(timeoutAdd(uint32(args.timeout), timedUpdate, image))
return false
proc forceUpdate(action: SimpleAction; parameter: Variant; image: Image): void =
log "Force refreshing image now"
log "Refreshing image..."
label.notify "Refreshing image..."
if updateTimeout > 0:
discard updateTimeout.remove
discard image.timedUpdate()
updateTimeout = int(timeoutAdd(500, timedUpdate, image))
proc checkServerChannel(image: Image): bool =
## Check the channel from the control server for incomming commands
@@ -127,7 +143,6 @@ proc checkServerChannel(image: Image): bool =
sleep(100)
result = true
# discard idleAdd(checkServerChannel, parameter)
proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: ApplicationWindow) =
## Fullscreen toggle event
@@ -140,7 +155,7 @@ proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: Applicat
proc cleanUp(w: ApplicationWindow, app: Application) =
## Stop the control server and exit the GTK application
log "Stopping control server..."
closeServer()
closeServer()
serverWorker.joinThread()
chan.close()
log "Server stopped."
@@ -149,7 +164,7 @@ proc cleanUp(w: ApplicationWindow, app: Application) =
proc quit(action: SimpleAction; parameter: Variant; app: Application) =
## Application quit event
cleanUp(window, app)
proc appActivate(app: Application) =
# Parse arguments from the command line
let parsed = newArgs()
@@ -163,7 +178,7 @@ proc appActivate(app: Application) =
window.setKeepAbove(false)
window.setDefaultSize(800, 600)
# Custom styling for e.g. the background color CSS data is in the "style.nim" module
# Custom styling for e.g. the background color CSS data is in the "style.nim" module
let provider = newCssProvider()
discard provider.loadFromData(css)
addProviderForScreen(getDefaultScreen(), provider, STYLE_PROVIDER_PRIORITY_USER)
@@ -176,14 +191,14 @@ proc appActivate(app: Application) =
let container = newOverlay()
container.addOverlay(label)
window.add(container)
let image = newImage()
container.add(image)
if args.fullscreen:
window.fullscreen
## Connect the GTK signals to the procs
## Connect the GTK signals to the procs
let fullscreenAction = newSimpleAction("fullscreen")
discard fullscreenAction.connect("activate", toggleFullscreen, window)
app.setAccelsForAction("win.fullscreen", "F")