romgrk / node-gtk

GTK+ bindings for NodeJS (via GObject introspection)
MIT License
493 stars 41 forks source link

Basic example doesn't work outside of the terminal #356

Closed jcubic closed 7 months ago

jcubic commented 7 months ago

I was testing the library, here is my code:

#!/usr/bin/env node

const gi = require('node-gtk')
const Gtk = gi.require('Gtk', '3.0')

gi.startLoop()
Gtk.init()

const win = new Gtk.Window();
win.on('destroy', () => Gtk.mainQuit())
win.on('delete-event', () => false)

win.setDefaultSize.call(win, 200, 80)
win.add.call(win, new Gtk.Label({ label: 'Hello Gtk+' }))

win.showAll();
Gtk.main();

I'm not sure what the error is but it only works from Bash, If I click on the script file it doesn't work. It looks like the loop doesn't start at all.

I've using node v20.10.0 on Fedora Xfce.

ten0s commented 7 months ago

When you start the script directly the current working directory of the script is $HOME. You need to have a wrapper like below to correctly set the path.

index.sh:

#!/bin/bash

if [[ -L $0 ]]; then
    FILE_PATH=$(readlink $0)
else
    FILE_PATH=$0
fi

BASE_DIR=$(dirname $FILE_PATH)

exec node $BASE_DIR/index.js
$ chmod +x index.sh

Now start index.sh.

PS: Depending on your setup you may also need to hardcode the node's path, like /usr/bin/node

jcubic commented 7 months ago

It doesn't work, I even tried to run in the terminal from the activation applet and it still closes immediately. Even adding sleep 1m didn't help.

ten0s commented 7 months ago

Add the log. What is in there?

node $BASE_DIR/index.js >& /tmp/log.txt
jcubic commented 7 months ago

Yes, sorry it works, I made a silly mistake.