Generating QR code

There was a question about generating QR codes at the Xojo forums.

Below is a 8th programming language sample that generates QR code in it’s own thread and passes the result for the main GUI thread for display.

needs nk/gui
needs utils/qrcode

24 font:system font:new "font1" font:atlas! drop

"https://ifnotnil.com/" constant URL

nullvar qr-task-id

: init-window-size
  mobile? if
    hw:displaysize?
  else
    300 300
  then ;

init-window-size constant HEIGHT constant WIDTH

: qr-task
  m:new 
  URL img:ECC-LOW img:qr-gen img:qr>img
  "img" m:_!
  "cb" ( "img" m:_@ "qr-img" swap nk:m! null qr-task-id ! ) m:!
  nk:do ;

: new-win
  {
    name: "main",
    wide: @WIDTH,
    high: @HEIGHT,
    resizable: true,
    bg: "white",
    title: @URL
  } nk:win ;

: qr-image
  nk:widget if
    1 1 nk:layout-grid-begin
      "qr-img" nk:m@ null? !if
        0 1 0 1 nk:grid swap "white" nk:draw-image
      else
        drop
      then
      0 1 0 1 nk:grid 0 2 "black" nk:stroke-rect
    nk:layout-grid-end
  else
    drop
  then ;

: maintain-aspect-ratio  \ rect -- rect
  dup 2 nk:rect@ swap 3 nk:rect@ rot n:min tuck 2 swap nk:rect! 3 rot nk:rect! nk:center-rect ;

: main-render
  {
    bg: "white",
    flags: [ @nk:WINDOW_NO_SCROLLBAR ],
    padding: [0,0]
  }
  nk:begin
    null { rows: 1, cols: 1, margin: 8 }  nk:layout-grid-begin
      0 1 0 1 nk:grid maintain-aspect-ratio nk:rect>local nk:grid-push
        qr-image 
    nk:layout-grid-end
  nk:end ;

: app:main
  ' qr-task t:task qr-task-id !
  new-win ' main-render -1 nk:render-loop ;

qr