commit 62bd31b9e62496255bb44a3b35f5e45d4545b3c3 Author: luxick Date: Thu Jan 23 21:44:08 2020 +0100 Initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6dd29b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ \ No newline at end of file diff --git a/randopics.nimble b/randopics.nimble new file mode 100644 index 0000000..cbbb788 --- /dev/null +++ b/randopics.nimble @@ -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" diff --git a/src/randopics.nim b/src/randopics.nim new file mode 100644 index 0000000..289ee11 --- /dev/null +++ b/src/randopics.nim @@ -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() \ No newline at end of file diff --git a/src/randopics.nim.cfg b/src/randopics.nim.cfg new file mode 100644 index 0000000..231f6b4 --- /dev/null +++ b/src/randopics.nim.cfg @@ -0,0 +1,2 @@ +threads:on +d:ssl \ No newline at end of file