wmww / gtk4-layer-shell

A library to create panels and other desktop components for Wayland using the Layer Shell protocol and GTK4
MIT License
128 stars 4 forks source link

Python Example #3

Closed dhansmair closed 1 year ago

dhansmair commented 1 year ago

Hi! I know this is wip, but I was just too excited to try it already. I have installed this repo and was able to run gtk4-layer-demo and simple-example-c, and they work as expected (gtk4-layer-demo sometimes causing hyprland to crash, but the panel functionality works). Then I tried to use it in python with the following code:


import gi
gi.require_version("Gtk", "4.0")

try:
    gi.require_version('Gtk4LayerShell', '1.0')

except ValueError:
    import sys
    raise RuntimeError('\n\n' +
        'If you haven\'t installed GTK Layer Shell, you need to point Python to the\n' +
        'library by setting GI_TYPELIB_PATH and LD_LIBRARY_PATH to <build-dir>/src/.\n' +
        'For example you might need to run:\n\n' +
        'GI_TYPELIB_PATH=build/src LD_LIBRARY_PATH=build/src python3 ' + ' '.join(sys.argv))

from gi.repository import Gtk, Gtk4LayerShell

def on_activate(app):
    win = Gtk.Window(application=app)
    win.set_default_size(300, 200)

    Gtk4LayerShell.init_for_window(win)
    Gtk4LayerShell.set_layer(win, Gtk4LayerShell.Layer.TOP)
    Gtk4LayerShell.set_anchor(win, Gtk4LayerShell.Edge.TOP, True)
    Gtk4LayerShell.auto_exclusive_zone_enable(win)

    btn = Gtk.Button(label="Click me!")
    btn.connect('clicked', lambda x: win.close())
    win.set_child(btn)
    win.present()

app = Gtk.Application(application_id='org.gtk.Example')
app.connect('activate', on_activate)
app.run(None)

The code is adapted from the gtk3 example, and it runs without errors when setting GI_TYPELIB_PATH and LD_LIBRARY_PATH, but the window is being tiled just as any other window (using hyprland). Could you maybe give me a quick hint on what I'm doing wrong? Thank you a lot!

wmww commented 1 year ago

Hey, so the big thing that's important (and that's new from the GTK3 version) is that gtk4-layer-shell.so needs to be linked before libwayland-client.so. The quick and dirty fix that should work is to run the script with the environment variable LD_PRELOAD=/path/to/gtk4-layer-shell.so set. When I get to porting the Python example I'll look into if there's a better way to do this. I also plan to somehow detect when the linking didn't happen right and show a useful error.

dhansmair commented 1 year ago

The quick fix indeed works, thanks!

wmww commented 1 year ago

I found a better way. You just put this at the top of the file (before using gi), and then you don't need LD_PRELOAD

from ctypes import CDLL
CDLL('libgtk4-layer-shell.so')
MelihDarcanxyz commented 1 year ago

Mine doesn't work, it throws an exception:

  File "/home/user/.clones/gtk4-layer-shell/examples/simple-example.py", line 10, in <module>
    gi.require_version('Gtk4LayerShell', '1.0')
  File "/usr/lib/python3.10/site-packages/gi/__init__.py", line 126, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk4LayerShell not available
wmww commented 1 year ago

You need to either install the library or set GI_TYPELIB_PATH and LD_LIBRARY_PATH as mentioned here for Python to find the bindings, have you done that?

MelihDarcanxyz commented 1 year ago

Somehow I didn't see that, it works now, thank you. By the way, what do you mean by installing the library? I have python-gobject library installed.

wmww commented 1 year ago

By "the library" I mean gtk4-layer-shell. You can install it with sudo ninja -C build install, though this will put a bunch of files in system paths which you may not want. The recommended ways of doing it are either waiting for it to be packaged for your distro and installing it with your package manager, or building it from source, not installing and setting GI_TYPELIB_PATH and LD_LIBRARY_PATH to build/src

MelihDarcanxyz commented 1 year ago

Thanks for the clarification. Actually yes, I did run that command before trying. Followed the README.md before running python example.

I don't know, maybe my path isn't properly configured. Will let you know when I get back to my computer.

Also, tested with:

sudo ninja -C build test

There were only 2 errors related to some Lua doc.