Auto update image.
This commit is contained in:
@@ -20,6 +20,7 @@ type
|
|||||||
verbose: bool ## More debug information in notification label
|
verbose: bool ## More debug information in notification label
|
||||||
mode: Option[Mode] ## The chosen image source
|
mode: Option[Mode] ## The chosen image source
|
||||||
path: string ## File mode only: the path to the images
|
path: string ## File mode only: the path to the images
|
||||||
|
timeout: int ## Milliseconds between image refreshes
|
||||||
|
|
||||||
var
|
var
|
||||||
client = newHttpClient() ## For loading images from the web
|
client = newHttpClient() ## For loading images from the web
|
||||||
@@ -48,6 +49,7 @@ proc newArgs(): Args =
|
|||||||
help(fmt"Version {version} - Display random images from different sources")
|
help(fmt"Version {version} - Display random images from different sources")
|
||||||
option("-m", "--mode", help="The image source mode.", choices=enumToStrings(Mode))
|
option("-m", "--mode", help="The image source mode.", choices=enumToStrings(Mode))
|
||||||
option("-p", "--path", help="Path to a directory with images ('file' mode only)")
|
option("-p", "--path", help="Path to a directory with images ('file' mode only)")
|
||||||
|
option("-t", "--timeout", help="Seconds before the image is refreshed", default="300")
|
||||||
flag("-w", "--windowed", help="Do not start in fullscreen mode")
|
flag("-w", "--windowed", help="Do not start in fullscreen mode")
|
||||||
flag("-v", "--verbose", help="Show more information")
|
flag("-v", "--verbose", help="Show more information")
|
||||||
let opts = p.parse(commandLineParams())
|
let opts = p.parse(commandLineParams())
|
||||||
@@ -66,11 +68,19 @@ proc newArgs(): Args =
|
|||||||
if (mode.get == Mode.File):
|
if (mode.get == Mode.File):
|
||||||
fileProvider = newFileProvider(opts.path)
|
fileProvider = newFileProvider(opts.path)
|
||||||
|
|
||||||
|
## Timeout is given in seconds as an argument
|
||||||
|
var timeout = 3000
|
||||||
|
try:
|
||||||
|
timeout = opts.timeout.parseInt * 1000
|
||||||
|
except ValueError:
|
||||||
|
echo "Invalid timeout: ", opts.timeout
|
||||||
|
|
||||||
Args(
|
Args(
|
||||||
fullscreen: not opts.windowed,
|
fullscreen: not opts.windowed,
|
||||||
verbose: opts.verbose,
|
verbose: opts.verbose,
|
||||||
mode: mode,
|
mode: mode,
|
||||||
path: opts.path)
|
path: opts.path,
|
||||||
|
timeout: timeout)
|
||||||
|
|
||||||
proc downloadFox(): Pixbuf =
|
proc downloadFox(): Pixbuf =
|
||||||
let urlData = client.getContent(floofUrl)
|
let urlData = client.getContent(floofUrl)
|
||||||
@@ -96,29 +106,54 @@ proc tryGetImage(): Option[Pixbuf] =
|
|||||||
of Mode.File:
|
of Mode.File:
|
||||||
result = some(getLocalImage())
|
result = some(getLocalImage())
|
||||||
|
|
||||||
proc updateImage(action: SimpleAction; parameter: Variant;) =
|
proc updateImage(): bool =
|
||||||
## Updates the UI with a new image
|
## Updates the UI with a new image
|
||||||
# Loading new image
|
# Loading new image
|
||||||
let data = tryGetImage();
|
try:
|
||||||
if data.isNone:
|
if (args.verbose): echo "Refreshing..."
|
||||||
label.notify "No image to display..."
|
let data = tryGetImage();
|
||||||
return;
|
if data.isNone:
|
||||||
|
label.notify "No image to display..."
|
||||||
|
return false;
|
||||||
|
|
||||||
## Resize image to best fit the window
|
## Resize image to best fit the window
|
||||||
var pixbuf = data.get()
|
var pixbuf = data.get()
|
||||||
var wWidth, wHeight, width, height: int
|
var wWidth, wHeight, width, height: int
|
||||||
window.getSize(wWidth, wHeight)
|
window.getSize(wWidth, wHeight)
|
||||||
if (wWidth > wHeight):
|
if (wWidth > wHeight):
|
||||||
height = wHeight
|
height = wHeight
|
||||||
width = ((pixbuf.width * height) / pixbuf.height).toInt
|
width = ((pixbuf.width * height) / pixbuf.height).toInt
|
||||||
|
else:
|
||||||
|
width = wWidth
|
||||||
|
height = ((pixbuf.height * width) / pixbuf.width).toInt
|
||||||
|
pixbuf = pixbuf.scaleSimple(width, height, InterpType.bilinear)
|
||||||
|
|
||||||
|
# Update the UI with the image
|
||||||
|
imageWidget.setFromPixbuf(pixbuf)
|
||||||
|
if (args.verbose):
|
||||||
|
label.notify "New image set"
|
||||||
|
else:
|
||||||
|
label.notify
|
||||||
|
return true
|
||||||
|
|
||||||
|
except:
|
||||||
|
let
|
||||||
|
e = getCurrentException()
|
||||||
|
msg = getCurrentExceptionMsg()
|
||||||
|
echo "Got exception ", repr(e), " with message ", msg
|
||||||
|
return false
|
||||||
|
|
||||||
|
proc forceUpdate(action: SimpleAction; parameter: Variant;) =
|
||||||
|
discard updateImage()
|
||||||
|
|
||||||
|
proc timedUpdate(image: Widget): bool =
|
||||||
|
let ok = updateImage();
|
||||||
|
if ok:
|
||||||
|
return true;
|
||||||
else:
|
else:
|
||||||
width = wWidth
|
label.notify "Error while refreshing image, retrying..."
|
||||||
height = ((pixbuf.height * width) / pixbuf.width).toInt
|
discard timeoutAdd(uint32(args.timeout), timedUpdate, imageWidget)
|
||||||
pixbuf = pixbuf.scaleSimple(width, height, InterpType.bilinear)
|
return false
|
||||||
|
|
||||||
# Update the UI with the image
|
|
||||||
imageWidget.setFromPixbuf(pixbuf)
|
|
||||||
label.notify
|
|
||||||
|
|
||||||
proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: ApplicationWindow) =
|
proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: ApplicationWindow) =
|
||||||
## Fullscreen toggle event
|
## Fullscreen toggle event
|
||||||
@@ -145,11 +180,10 @@ proc connectSignals(app: Application) =
|
|||||||
window.actionMap.addAction(quitAction)
|
window.actionMap.addAction(quitAction)
|
||||||
|
|
||||||
let updateImageAction = newSimpleAction("update")
|
let updateImageAction = newSimpleAction("update")
|
||||||
discard updateImageAction.connect("activate", updateImage)
|
discard updateImageAction.connect("activate", forceUpdate)
|
||||||
app.setAccelsForAction("win.update", "U")
|
app.setAccelsForAction("win.update", "U")
|
||||||
window.actionMap.addAction(updateImageAction)
|
window.actionMap.addAction(updateImageAction)
|
||||||
|
|
||||||
|
|
||||||
proc appActivate(app: Application) =
|
proc appActivate(app: Application) =
|
||||||
# Parse arguments from the command line
|
# Parse arguments from the command line
|
||||||
args = newArgs()
|
args = newArgs()
|
||||||
@@ -183,6 +217,9 @@ proc appActivate(app: Application) =
|
|||||||
app.connectSignals
|
app.connectSignals
|
||||||
window.showAll
|
window.showAll
|
||||||
|
|
||||||
|
discard updateImage()
|
||||||
|
discard timeoutAdd(uint32(args.timeout), timedUpdate, imageWidget)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
let app = newApplication("org.luxick.randopix")
|
let app = newApplication("org.luxick.randopix")
|
||||||
connect(app, "activate", appActivate)
|
connect(app, "activate", appActivate)
|
||||||
|
|||||||
Reference in New Issue
Block a user