-
Notifications
You must be signed in to change notification settings - Fork 118
App is not relocatable #7
Comments
You are right. Any suggestion for determining those paths relatively? |
Sorry, I don't know. Are there not XDG or LD environment variables which could be used? how do other gtk apps do this? |
Unfortunately - as far as I know - no. There doesn't exist such variables to use. So implementing such solution would require some hard coding. Or maybe there's a library which provides such functionality and I don't know about it. |
Other GTK apps often compile pixmaps, glade UI and CSS files as a gresource. This way you don't have to rely on hard coded paths, but use URIs like "resource:///com/uploadedlobster/peek/css/peek.css" defined inside the resource file. See https://developer.gnome.org/gio/stable/GResource.html Not quite sure how this works with Python, though. If using C or Vala one can simply compile the resources in, removing the needs for file system paths entirely. On Python you have to still load the compiled gresource file with some code like this: DATA_DIR = "data/"
resource = Gio.resource_load(DATA_DIR + "green-recorder.gresource")
Gio.Resource._register(self.resource)
builder = Gtk.Builder()
builder.add_from_resource('/some/path/to/resource/window.ui') Again one typical approach is to set the DATA_DIR path during installation. Or you search for it in known locations. |
|
That's not a hard coded file system path, that is the path inside the compiled resources ;) It is independent of the install location. |
Then there is this: https://git.gnome.org/browse/gnome-music/tree/gnome-music.in#n102 Side note: The app needs some restructuring :) |
@phw I do not understand , it is a python application how are there compiled resources? |
@gort818 The ui, images, and whatever assets you want inside the resources can be either
You create the gresource file by using glib-compile-resources test.gresource.xml --target=test.gresource Creates a Then you can access it with: resource = Gio.resource_load(os.path.join(PACKAGE_DATA_DIR, 'test.gresource'))
Gio.Resource._register(resource) and loaded resources from it with: builder = Gtk.Builder()
builder.add_from_resource('/tld/domain/project/resource.ui') Where
becomes:
A lot of people on Github do:
becomes:
|
@julianrichen Wow thank you very informative! |
Also forgot a few things. Gtk can automatically implement parts of your program with resources (page I linked -> Description -> Automatic resources). For example the app-menu and menubar can be loaded automatically by linking a ui file in the gresoures.xml that is either at Same can also be done with for Keyboard shortcuts via Here is an example that links up Preferences, Keyboard Shortcuts, Help, About and Quit to the app-menu/menubar to the application + adds the help-overlay (GAction is
<?xml version="1.0"?>
<interface>
<menu id="app-menu">
<section>
<item>
<attribute name="label" translatable="yes">_Preferences</attribute>
<attribute name="action">app.preferences</attribute>
<attribute name="accel"><Primary>comma</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">_Keyboard Shortcuts</attribute>
<attribute name="action">win.show-help-overlay</attribute>
</item>
<item>
<attribute name="label" translatable="yes">_Help</attribute>
<attribute name="action">app.help</attribute>
<attribute name="accel">F1</attribute>
</item>
<item>
<attribute name="label" translatable="yes">_About</attribute>
<attribute name="action">app.about</attribute>
<attribute name="accel">F2</attribute>
</item>
<item>
<attribute name="label" translatable="yes">_Quit</attribute>
<attribute name="action">app.quit</attribute>
<attribute name="accel"><Primary>q</attribute>
</item>
</section>
</menu>
<menu id="menubar">
<submenu>
<attribute name="label">_File</attribute>
<section>
<item>
<attribute name="label" translatable="yes">_Quit</attribute>
<attribute name="action">app.quit</attribute>
<attribute name="accel"><Primary>q</attribute>
</item>
</section>
</submenu>
<submenu>
<attribute name="label">_Edit</attribute>
<section>
<item>
<attribute name="label" translatable="yes">_Preferences</attribute>
<attribute name="action">app.preferences</attribute>
<attribute name="accel"><Primary>comma</attribute>
</item>
</section>
</submenu>
<submenu>
<attribute name="label">_Help</attribute>
<section>
<item>
<attribute name="label" translatable="yes">_Keyboard Shortcuts</attribute>
<attribute name="action">win.show-help-overlay</attribute>
</item>
<item>
<attribute name="label" translatable="yes">_Help</attribute>
<attribute name="action">app.help</attribute>
<attribute name="accel">F1</attribute>
</item>
<item>
<attribute name="label" translatable="yes">_About</attribute>
<attribute name="action">app.about</attribute>
<attribute name="accel">F2</attribute>
</item>
</section>
</submenu>
</menu>
</interface>
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<object class="GtkShortcutsWindow" id="help_overlay">
<property name="modal">True</property>
<child>
<object class="GtkShortcutsSection">
<property name="visible">True</property>
<property name="section-name">shortcuts</property>
<property name="max-height">17</property>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">True</property>
<property name="title" translatable="yes" context="shortcut window">General</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title" translatable="yes" context="shortcut window">Preferences</property>
<property name="accelerator"><Primary>comma</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title" translatable="yes" context="shortcut window">Keyboard Shortcuts</property>
<property name="accelerator"><Primary>F1</property>
<property name="accelerator"><Primary><Shift>question</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title" translatable="yes" context="shortcut window">Help</property>
<property name="accelerator">F1</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title" translatable="yes" context="shortcut window">About</property>
<property name="accelerator">F2</property>
</object>
</child>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">True</property>
<property name="title" translatable="yes" context="shortcut window">Quit</property>
<property name="accelerator"><Primary>Q</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
I would refrain from doing to many images as it can make the size of the gresource massive. SVG's are ok since they tend to be small and you only need one for most sizes. |
I've made a PR which should fix the issue, the commit in question: 59f0890 |
in some places green-recorder has hard wired paths to files. e.g.
and:-
builder.add_from_file("/usr/lib/green-recorder/ui.glade")
This assumption breaks the ability for green-recorder to work in some environments. Specifically when containerised or confined, that directory may be somewhere else (relative) on the filesystem, and not at that absolute path.
The text was updated successfully, but these errors were encountered: