This commit is contained in:
2020-01-23 21:44:08 +01:00
commit 62bd31b9e6
4 changed files with 108 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
bin/

18
randopics.nimble Normal file
View File

@@ -0,0 +1,18 @@
import strformat
# Package
version = "0.1.0"
author = "luxick"
description = "Play an image slide show from different sources"
license = "GPL-2.0"
srcDir = "src"
bin = @["randopics"]
# Dependencies
requires "nim >= 1.0.0", "gintro <= 0.5.5"
task debug, "Compile debug version":
exec "nim c --out:bin/randopics src/randopics.nim"
task release, "Compile release version":
exec fmt"nim c -d:release --out:bin/{version}/randopics src/randopics.nim"

87
src/randopics.nim Normal file
View File

@@ -0,0 +1,87 @@
import httpClient, json
import gintro/[gtk, glib, gobject, gio, gdkpixbuf]
var
client = newHttpClient()
window: ApplicationWindow
fullscreen = true
const
floofUrl = "https://randomfox.ca/floof/"
proc downloadImage(): Pixbuf =
let urlData = client.getContent(floofUrl)
let info = parseJson(urlData)
let imageData = client.getContent(info["image"].getStr)
let loader = newPixbufLoader()
discard loader.write(imageData)
loader.getPixbuf()
proc resizeImage(pixbuf: Pixbuf): Pixbuf =
var wWidth, wHeight, width, height: int
window.getSize(wWidth, wHeight)
if (wWidth > wHeight):
height = wHeight
width = ((pixbuf.width * height) / pixbuf.height).toInt
else:
width = wWidth
height = ((pixbuf.height * width) / pixbuf.width).toInt
pixbuf.scaleSimple(width, height, InterpType.bilinear)
proc updateImage(action: SimpleAction; parameter: Variant; widget: Image) =
var pixbuf = downloadImage()
pixbuf = pixbuf.resizeImage()
widget.setFromPixbuf(pixbuf)
proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: ApplicationWindow) =
if fullscreen:
window.unfullscreen
else:
window.fullscreen
fullscreen = not fullscreen
proc quit(action: SimpleAction; parameter: Variant; app: Application) =
app.quit()
proc appActivate(app: Application) =
window = newApplicationWindow(app)
window.title = "Randopics"
window.setKeepAbove(true)
let cssProvider = newCssProvider()
let data = "window { background: black; }"
discard cssProvider.loadFromData(data)
let styleContext = window.getStyleContext()
styleContext.addProvider(cssProvider, STYLE_PROVIDER_PRIORITY_USER)
let imageWidget = newImage()
window.add(imageWidget)
if fullscreen:
window.fullscreen
let fullscreenAction = newSimpleAction("fullscreen")
discard fullscreenAction.connect("activate", toggleFullscreen, window)
app.setAccelsForAction("win.fullscreen", "F")
window.actionMap.addAction(fullscreenAction)
let quitAction = newSimpleAction("quit")
discard quitAction.connect("activate", quit, app)
app.setAccelsForAction("win.quit", "Escape")
window.actionMap.addAction(quitAction)
let updateImageAction = newSimpleAction("update")
discard updateImageAction.connect("activate", updateImage, imageWidget)
app.setAccelsForAction("win.update", "U")
window.actionMap.addAction(updateImageAction)
window.showAll
proc main =
let app = newApplication("org.gtk.example")
connect(app, "activate", appActivate)
discard run(app)
when isMainModule:
main()

2
src/randopics.nim.cfg Normal file
View File

@@ -0,0 +1,2 @@
threads:on
d:ssl