JordanViknar/SimpleSteamTinker

Development tips for LGI

Closed this issue · 11 comments

Hello, how are you?

I've been using LGI for a while and I want to leave you some tips to take advantage of the library and save you some dependencies.

  • I have noticed that the application hangs when executing commands (this is because of using os.execute and not being asynchronous), using subprocesses the application will never hang.

  • Reimplement the use of API-REST with libsoup for asynchronous (non-blocking) queries.

  • Replace lfs with GlibFilesystem

Hi, I also noticed the application crashes when using the os.execute.

It would be much better to implement it this way, and thus avoid hanging the application.

Another thing to reimplement would be libnotify, instead of using the notify-send you could use the Notify of lgi, and avoid the os.execute.

Also the clipboard, so to avoid using the xclip (avoid the os.execute which crashes the application), you could implement the Gtk.Clipboard.

So I think this would also go

  • Reimplement libnotify using lgi.Notify
  • Replace xclip with Gtk.Clipboard()

Work on SimpleSteamTinker has been slower these last few weeks due to school work and the holidays.
I have also been learning a new programming language recently.

However, I'm glad to see the project is generating some interest. 👍
I will try and implement your tips. Thank you for both of these comments.

  • Replace xclip with Gtk.Clipboard()

Thanks for the suggestion, but SimpleSteamTinker uses Gtk4 instead of Gtk3 and Gtk.Clipboard seemingly became Gdk.Clipboard under Gtk4.
I tried using it, but unfortunately, it seems like this change isn't handled well by LGI.
Trying to call its set function results in this error :
(lua:11580): Lgi-WARNING **: 13:35:31.762: Error raised while calling 'lgi.cbk (function: 0x55aabd0bade0): GObject': ./modules/ui/gameSettingsOverview.lua:94: bad argument #1 to 'get_content' (Gdk.Clipboard expected, got table)
The other set methods mentioned are apparently inaccessible to language bindings according to the wiki.

Thanks for the suggestion, but SimpleSteamTinker uses Gtk4 instead of Gtk3 and Gtk.Clipboard seemingly became Gdk.Clipboard under Gtk4.

I've seen it, I've tried to make an implementation, but it's a bit complicated.

I tried using it, but unfortunately, it seems like this change isn't handled well by LGI. Trying to call its set function results in this error : (lua:11580): Lgi-WARNING **: 13:35:31.762: Error raised while calling 'lgi.cbk (function: 0x55aabd0bade0): GObject': ./modules/ui/gameSettingsOverview.lua:94: bad argument #1 to 'get_content' (Gdk.Clipboard expected, got table) The other set methods mentioned are apparently inaccessible to language bindings according to the wiki.

The problem here is that you are calling the clipboard with . and not as an object itself.
an example would be

if you call the Gdk.Clipboard with clip.set() it will always ask you to add it to the clipboard, something like

local clip = Gdk.Clipboard()
clip.set(clip, gtype, text)

So that it works and does not give error

But the best way would be to make it as if it was a property of it, so you wouldn't have to call it as I did before.

clip:set(gtype, text)

I do not know if it is the best way to explain it, but I believe that it is understood.

My fault for not showing my code, but I was already calling Gdk.Clipboard with : instead of .

This is approximately what the code looks like in gameSettingsOverview.lua on my workspace right now :

-- At the start of the module
local Gdk = lgi.Gdk

-- Function connected to the game ID copy button
lgiHelper.replaceSignal(gameIDCopyButton, "on_clicked", function()
    Gdk.Clipboard:set() -- Empty here to demonstrate the error still happens with the only argument being Gdk.Clipboard itself, rather than an error occuring from the missing arguments.
    toastSystem:add_toast(Adw.Toast.new("Game ID copied to clipboard !")
end)

Reimplement libnotify using lgi.Notify

Added in 082c82f

My fault for not showing my code, but I was already calling Gdk.Clipboard with : instead of .

This is approximately what the code looks like in gameSettingsOverview.lua on my workspace right now :

-- At the start of the module
local Gdk = lgi.Gdk

-- Function connected to the game ID copy button
lgiHelper.replaceSignal(gameIDCopyButton, "on_clicked", function()
    Gdk.Clipboard:set() -- Empty here to demonstrate the error still happens with the only argument being Gdk.Clipboard itself, rather than an error occuring from the missing arguments.
    toastSystem:add_toast(Adw.Toast.new("Game ID copied to clipboard !")
end)

The problem I think is the way you are declaring the Gdk.Clipboard, you are declaring it directly, and I think you have to call it first through the Gdk.Display.

It would be something like :

display = Gdk.Display:get_default()
clipboard = display:get_clipboard()
clipboard:set(gvalue, text) -- the GValue is a GObject value(I think), I don't understand very well how Clipboard works in Gdk4, but the GValue is a GObject.value and the text is the text to copy.

The code is getting more complex, so I moved it back to the separate systemUtils.copyToClipboard function.

Your solution seemingly functions, I no longer get the Gdk.Clipboard expected, got table error.

However, I have made a slight mistake in regards to Gdk.Clipboard:set() function : it is not technically accessible to language bindings either, instead being overridden by Gdk.Clipboard:set_value().

So, now the only work left is in regards to the actual value we're trying to store : the GValue the function requires as per the documentation. Which brings us to my current code :

local lgi = require("lgi")
...
local Gdk = lgi.Gdk
local GObject = lgi.GObject

...

function systemUtils.copyToClipboard(text)
	-- We initialize the GValue, with type set to string
	local data = ?
	-- We give it said string.
	data:set_string(text)
	-- The code past this line is already functional.
	local display = Gdk.Display:get_default()
	local clipboard = display:get_clipboard()
	clipboard:set(data)
end

From there, I'll admit I'm quite lost and unsure on how to proceed from Lua.
I'll try and research more, but help would be very much appreciated.

From there, I'll admit I'm quite lost and unsure on how to proceed from Lua.
I'll try and research more, but help would be very much appreciated.

The truth is I'm as lost as you, the documentation of gdk(gnome in general) is a bit confusing, I only managed to implement it in a demo, but it was with gtk3 before it became part of gdk4, I don't know very well how to help now, I haven't found examples in python either(it's the first thing that comes to my mind when it comes to gobject), so, maybe looking for how to implement it in other languages(preferably a script language, since the compiled ones like c or vala implement it almost the same as the one in the wiki) we will finally have an idea.

In the meantime, I wanted to respond about one other particular issue :

  • Replace lfs with GlibFilesystem

While yes, this would slightly lower the number of dependencies, one of my goals is also for my project to be easy to work with.
I think, because of this, it'd be better to use libraries specifically intended for Lua if easily possible.
lfs is bundled by most distributions from what I've seen online, so it's not problematic to rely on it.

Closing for now due to inactivity.