Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d9772c026d |
5
src/providers/foxes.nim
Normal file
5
src/providers/foxes.nim
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import ../randopics
|
||||||
|
import gintro/[gdkpixbuf]
|
||||||
|
|
||||||
|
proc get(): Pixbuf =
|
||||||
|
discard
|
||||||
@@ -1,15 +1,21 @@
|
|||||||
import httpClient, json
|
import os, httpClient, json, threadpool
|
||||||
import gintro/[gtk, glib, gobject, gio, gdkpixbuf]
|
import gintro/[gtk, glib, gobject, gio, gdkpixbuf]
|
||||||
|
|
||||||
var
|
var
|
||||||
client = newHttpClient()
|
|
||||||
window: ApplicationWindow
|
window: ApplicationWindow
|
||||||
fullscreen = true
|
fullscreen = true
|
||||||
|
|
||||||
const
|
const
|
||||||
floofUrl = "https://randomfox.ca/floof/"
|
floofUrl = "https://randomfox.ca/floof/"
|
||||||
|
updateTime = 300
|
||||||
|
|
||||||
|
type ImageProvider =
|
||||||
|
tuple[
|
||||||
|
get: proc(): Pixbuf
|
||||||
|
]
|
||||||
|
|
||||||
proc downloadImage(): Pixbuf =
|
proc downloadImage(): Pixbuf =
|
||||||
|
let client = newHttpClient()
|
||||||
let urlData = client.getContent(floofUrl)
|
let urlData = client.getContent(floofUrl)
|
||||||
let info = parseJson(urlData)
|
let info = parseJson(urlData)
|
||||||
let imageData = client.getContent(info["image"].getStr)
|
let imageData = client.getContent(info["image"].getStr)
|
||||||
@@ -17,24 +23,27 @@ proc downloadImage(): Pixbuf =
|
|||||||
discard loader.write(imageData)
|
discard loader.write(imageData)
|
||||||
loader.getPixbuf()
|
loader.getPixbuf()
|
||||||
|
|
||||||
proc resizeImage(pixbuf: Pixbuf): Pixbuf =
|
proc resizeImage(pixbuf: Pixbuf, maxWidth, maxHeight: int): Pixbuf =
|
||||||
var wWidth, wHeight, width, height: int
|
var width, height: int
|
||||||
window.getSize(wWidth, wHeight)
|
if (maxWidth > maxHeight):
|
||||||
|
height = maxHeight
|
||||||
if (wWidth > wHeight):
|
|
||||||
height = wHeight
|
|
||||||
width = ((pixbuf.width * height) / pixbuf.height).toInt
|
width = ((pixbuf.width * height) / pixbuf.height).toInt
|
||||||
else:
|
else:
|
||||||
width = wWidth
|
width = maxWidth
|
||||||
height = ((pixbuf.height * width) / pixbuf.width).toInt
|
height = ((pixbuf.height * width) / pixbuf.width).toInt
|
||||||
|
|
||||||
pixbuf.scaleSimple(width, height, InterpType.bilinear)
|
pixbuf.scaleSimple(width, height, InterpType.bilinear)
|
||||||
|
|
||||||
proc updateImage(action: SimpleAction; parameter: Variant; widget: Image) =
|
proc replaceImage(widget: Image, width, height: int) =
|
||||||
var pixbuf = downloadImage()
|
var pixbuf = downloadImage()
|
||||||
pixbuf = pixbuf.resizeImage()
|
pixbuf = pixbuf.resizeImage(width, height)
|
||||||
widget.setFromPixbuf(pixbuf)
|
widget.setFromPixbuf(pixbuf)
|
||||||
|
|
||||||
|
proc updateCommand(action: SimpleAction; parameter: Variant; widget: Image) =
|
||||||
|
var width, height: int
|
||||||
|
window.getSize(width, height)
|
||||||
|
replaceImage(widget, width, height)
|
||||||
|
|
||||||
proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: ApplicationWindow) =
|
proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: ApplicationWindow) =
|
||||||
if fullscreen:
|
if fullscreen:
|
||||||
window.unfullscreen
|
window.unfullscreen
|
||||||
@@ -44,11 +53,18 @@ proc toggleFullscreen(action: SimpleAction; parameter: Variant; window: Applicat
|
|||||||
|
|
||||||
proc quit(action: SimpleAction; parameter: Variant; app: Application) =
|
proc quit(action: SimpleAction; parameter: Variant; app: Application) =
|
||||||
app.quit()
|
app.quit()
|
||||||
|
|
||||||
|
proc runUpdater(window: ApplicationWindow, image: Image) =
|
||||||
|
echo "Start Updater"
|
||||||
|
var width, height: int
|
||||||
|
window.getSize(width, height)
|
||||||
|
spawn replaceImage(image, width, height)
|
||||||
|
|
||||||
proc appActivate(app: Application) =
|
proc appActivate(app: Application) =
|
||||||
window = newApplicationWindow(app)
|
window = newApplicationWindow(app)
|
||||||
window.title = "Randopics"
|
window.title = "Randopics"
|
||||||
window.setKeepAbove(true)
|
window.setKeepAbove(true)
|
||||||
|
|
||||||
let cssProvider = newCssProvider()
|
let cssProvider = newCssProvider()
|
||||||
let data = "window { background: black; }"
|
let data = "window { background: black; }"
|
||||||
discard cssProvider.loadFromData(data)
|
discard cssProvider.loadFromData(data)
|
||||||
@@ -57,6 +73,7 @@ proc appActivate(app: Application) =
|
|||||||
|
|
||||||
let imageWidget = newImage()
|
let imageWidget = newImage()
|
||||||
window.add(imageWidget)
|
window.add(imageWidget)
|
||||||
|
window.connect("show", runUpdater, imageWidget)
|
||||||
|
|
||||||
if fullscreen:
|
if fullscreen:
|
||||||
window.fullscreen
|
window.fullscreen
|
||||||
@@ -71,10 +88,10 @@ proc appActivate(app: Application) =
|
|||||||
app.setAccelsForAction("win.quit", "Escape")
|
app.setAccelsForAction("win.quit", "Escape")
|
||||||
window.actionMap.addAction(quitAction)
|
window.actionMap.addAction(quitAction)
|
||||||
|
|
||||||
let updateImageAction = newSimpleAction("update")
|
let updateAction = newSimpleAction("update")
|
||||||
discard updateImageAction.connect("activate", updateImage, imageWidget)
|
discard updateAction.connect("activate", updateCommand, imageWidget)
|
||||||
app.setAccelsForAction("win.update", "U")
|
app.setAccelsForAction("win.update", "U")
|
||||||
window.actionMap.addAction(updateImageAction)
|
window.actionMap.addAction(updateAction)
|
||||||
|
|
||||||
window.showAll
|
window.showAll
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user