GithubHelp home page GithubHelp logo

romgrk / node-gtk Goto Github PK

View Code? Open in Web Editor NEW
492.0 16.0 41.0 3.94 MB

GTK+ bindings for NodeJS (via GObject introspection)

License: MIT License

Python 0.81% JavaScript 26.43% C++ 65.06% Shell 0.33% C 7.37%
gnome gtk3 gobject-introspection gobject nodejs gtk

node-gtk's Introduction

NODE-GTK

node-gtk

GNOME Gtk+ bindings for NodeJS
Package Version

Node-Gtk is a gobject-introspection library for nodejs. It makes it possible to use any introspected library, such as Gtk+, usable. It is similar in essence to GJS or PyGObject. Please note this project is currently in a beta state and is being developed. Any contributors willing to help will be welcomed.

Supported Node.js versions: 16, 18 (other versions should work but are untested)
Pre-built binaries available for: Linux, macOS (all supported versions except 18)

Table of contents

Usage

Below is a minimal example of how to use the code, but take a look at our template or at react-gtk to bootstrap your project.

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(200, 80)
win.add(new Gtk.Label({ label: 'Hello Gtk+' }))

win.showAll()
Gtk.main()

Hello Gtk

See our examples folder for more examples, and in particular the browser demo source for a more complex application.

Hello Gtk

Documentation

Read our documentation here

Installing and building

Note that prebuilt binaries are available for common systems, in those cases building is not necessary.

Target Platforms
  • Linux: prebuilt binaries available
  • macOS: prebuilt binaries available
  • Windows: no prebuilt binaries

Requirements

  • git
  • nodejs@10 or higher
  • python3 (for node-gyp)
  • C compiler (gcc@8 or higher, or clang)

How to build on Ubuntu

Install basic dependencies.

sudo apt-get install \
  build-essential git \
  gobject-introspection \
  libgirepository1.0-dev \
  libcairo2 \
  libcairo2-dev

At this point npm install node-gtk should already install, fallback and build node-gtk without problems.

How to build on Fedora

Install basic dependencies:

sudo dnf install \
  @development-tools \
  nodejs \
  gobject-introspection \
  gtk3 \
  cairo

After installing of packages, run npm install node-gtk.

How to build on ArchLinux

The following should be the bare minimum to be able to build the project.

pacman -S --needed \
  base-devel git \
  nodejs npm \
  gtk3 gobject-introspection \
  cairo

Feel free to install all base-devel utilities.

After installing those packages, npm install node-gtk would do.

How to build on macOS

Assuming you have brew installed, the following has been successfully tested on El Captain.

brew install git node gobject-introspection gtk+3 cairo

At this point npm install node-gtk should already install, fallback and build node-gtk without problems.

How to build on Windows

Mandatory dependency is Visual C++ Build Environment: Visual Studio Build Tools (using "Visual C++ build tools" workload) or Visual Studio Community (using the "Desktop development with C++" workload).

The easiest/tested way to build this repository is within a MinGW shell provided by the MSYS2 installer.

Once VS and its C++ compiler is available and MSYS2 installed, launch the MinGW shell.

# update the system
# in case of errors, wait for the update to complete
# then close and open again MingW shell
pacman -Syyu --noconfirm

# install git, gtk3 and extra dependencie
pacman -S --needed --noconfirm git mingw-w64-$(uname -m)-{gtk3,gobject-introspection,pkg-config,cairo}

# where to put the repository clone?
# pick your flder or use ~/oss (Open Source Software)
mkdir -p ~/oss/
cd ~/oss

# clone node-gtk there
git clone https://github.com/romgrk/node-gtk
cd node-gtk

# don't include /mingw64/include directly since it conflicts with
# Windows SDK headers. we copy needed headers to __extra__ directory:
./windows/mingw_include_extra.sh

# if MSYS2 is NOT installed in C:/msys64 run:
export MINGW_WINDOWS_PATH=$(./windows/mingw_windows_path.sh)

# first run might take a while
GYP_MSVS_VERSION=2017 npm install

The GYP_MSVS_VERSION could be 2017 or above. Please verify which version you should use

The below blog post series will help you get started:

  1. Node.js GTK Hello World on Windows
  2. Find DLLs and Typelibs dependencies for Node.js GTK Application on Windows
  3. Package Node.js GTK Application on Windows

Possible issue on MinGW shell

In case you are launching the general executable without knowing the correct platform, the binary path might not be available.

In such case python won't be available either, and you can check via which python command.

If not found, you need to export the platform related binary path:

# example for the 32bit version
export PATH="/mingw32/bin:$PATH"
npm run install

This should do the trick. You can also check if there is any python at all via pacman -Qs python.

Testing the project

If you'd like to test everything builds and work properly, after installing and building you can run any of the examples:

node ./examples/hello-gtk.js

If you'll see a little window saying hello that's it: it works!

Please note in macOS the window doesn't automatically open above other windows. Try Cmd + Tab if you don't see it.

Browser demo

If you'd like to test ./examples/browser.js you'll need WebKit2 GTK+ libary.

  • in Ubuntu, you can apt-get install libwebkit2gtk-3.0 (4.0 works too) and try it out.
  • in Fedora, you should run sudo dnf install webkit2gtk3
  • in ArchLinux, you can pacman -S --needed webkitgtk and try it out.
  • in macOS, there is no way to run it right now because webkitgtk was removed from homebrew

Once installed, you can ./examples/browser.js google.com or any other page, and you might try the dark theme out too:

# macOS needs to have the Adwaita theme installed
# brew install adwaita-icon-theme

# Usage: ./examples/browser.js <url> [theme]
./examples/browser.js  google.com  dark

Contributing

If you'd like to help, we'd be more than happy to have support. To setup your development environment, you can run npm run configure. You can then build the project with npm run build.

Don't hesitate to join our Discord channel.

Contributors

node-gtk's People

Contributors

benwaffle avatar cedlemo avatar chfritz avatar clayrisser avatar dependabot[bot] avatar dhonx avatar egeexyz avatar ishitatsuyuki avatar jumplink avatar k0d avatar kuba-orlik avatar lleyton avatar magcius avatar mildsunrise avatar paulcarroty avatar peat-psuwit avatar romgrk avatar sdroege avatar ten0s avatar webreflection avatar wotzlaff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-gtk's Issues

Add tests

  • List the features that need testing
  • function call: multiple return values
  • function call: inout parameters
  • conversion: c-array, GArray, GByteArray, GPtrArray
  • conversion: GList, GSList
  • conversion: GHashTable
  • conversion: enum, flag
  • conversion: Object
  • conversion: boxed
  • property: get/set
  • fields: get/set
  • GObject: events
  • ParamSpec

Attract maintainers

In order to have higher quality bindings, we need to put more work into this. More maintainers means more work. Therefore, we need to find more maintainers. (also, our bus factor is very bad)
The general strategy will be to generate noise about the project in order to get more popularity. More people seeing the project means more potential maintainers.

  • Find which kind of people are interested in having Gtk NodeJS bindings (besides me)
  • Add pre-built binaries for easy installation
  • Blog entries about the project (Medium?)
  • Short release cycle on NPM, to show there is activity

assertion object != null failed

const gi = require('node-gtk');
const Pango = gi.require('Pango');

var attrs = new Pango.AttrList();
attrs.insert(new Pango.AttrSize(300));

error: Pango-CRITICAL **: pango_attr_list_insert: assertion 'list != NULL' failed

Support G(S)Lists in GIArgumentToV8

When tag == GI_TYPE_TAG_GSLIST.

  • Handle case GI_TRANSFER_CONTAINER
  • Check if there are cases with GI_TRANSFER_EVERYTHING
  • Check if element type can vary (the fact that we can query for the type of each element suggests that they can.)

Support interfaces

  • find how interface are used in GLib
  • implement interfaces
  • add tests
  • pass tests

keeping same npm name

I'm seeing a lot of effort going on in here, I wouldn't mind giving you the rights to publish in npm as node-gtk package neither.

any interest ? or were you planing to massively PR my repo after ?

Support unions

In lib/index.js :: makeInfo, when type == GI_INFO_TYPE_STRUCT or type == GI_INFO_TYPE_UNION.

Arrays: Type checking on function call

Check types more strictly. (Note to self: do not try to support messy JS conversions; be strict)

> gtk.init(0, null)

(process:11805): GLib-CRITICAL **: g_array_free: assertion 'array' failed
**
ERROR:../src/function.cc:31:int GNodeJS::GetV8ArrayLength(v8::Local<v8::Value>): code should not be reached
[1]    11805 abort (core dumped)  node

debug.h missing

Hi, tried to test your fork after this discussion but can't build it, seems like some files are missing? Right now it can't find debug.h, which doesn't seem to have been added to the repo.

Fix license issues

Some functions are copied from PyGObject, which is LGPL, incompatible with our license. We need to remove those.

GUI doesn't show up

I'm trying the lib out with the simple example:

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(200, 80);
win.add(new Gtk.Label({ label: "Hello Gtk+" }));

win.showAll();

When running the script with node ., it closes after a second, without showing any window/ui.

Am I missing something?

InitGParameterFromProperty: throw when property has invalid type

Throw an error (instead of dumping) when a property is initialized with an invalid type.

const gi = require('../lib/')
const Soup = gi.require('Soup')

const message = new Soup.Message({
  method: 'GET',
  uri: 'http://google.com', // invalid type, should be SoupURI
})

Implement Gtk/Gdk overrides to expose a nice API

We need to implement overrides on the JS side to expose a nice API for users. One such example is described here: #4 (comment)

Off the top of my head, the things that need to be overriden are:

  • Multiple return values are returned as arrays [retval, out1, out2, ...]
    • Strip boolean return values that indicate failure (example)
    • Return objects when it's nicer to do so (as described in the issue #4 comment)

For more inspiration, look at https://gitlab.gnome.org/GNOME/pygobject/blob/master/gi/overrides/Gtk.py.
The file in which those overrides would be (for Gtk-3.0) is https://github.com/romgrk/node-gtk/blob/master/lib/overrides/Gtk-3.0.js.

ping @WebReflection

Use Node's EventEmitter semantics

Bindings should integrate smoothly with target language, therefore:

EventEmitter#on <--> GObject#connect

Maybe even inherit from EventEmitter?

Release 1.0

Technical stuff:

Non-technical stuff:

  • list supported nodejs versions
  • list supported platforms
  • update package.json
  • Add tests #14
  • License issues #34
  • Make node-pre-gyp work #46

CI

Personally, i prefer circleci, but travis is fine too

Core dump in examples/editor.js

const gi = require('../');
const Gtk = gi.require('Gtk');
const GtkSource = gi.require('GtkSource', '3.0')
const common = require('./__common__.js')

Gtk.init()

const textView = new GtkSource.View()
const buffer = textView.getBuffer()

buffer.placeCursor(buffer.getEndIter())

Output:

[1]    20786 segmentation fault  rr record -n node tests/gtk_text_buffer_place_cursor.js

Boxed: use constructor function when size is zero

const gi = require('node-gtk');
const Gtk = gi.require('Gtk');
const Pango = gi.require('Pango');

Gtk.init();

var label = new Gtk.Label({ label: 0 });
var attrs = new Pango.AttrList();
attrs.insert(new Pango.AttrSize(300));
label.attributes = attrs;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.