improve resource handling by using environment and GResource (fixes #19)

This commit is contained in:
coderkun 2016-08-02 11:50:32 +02:00
parent ff15af3dbe
commit 441b854a98
4 changed files with 52 additions and 10 deletions

Binary file not shown.

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/de/coderkun/mcg">
<file>gtk.glade</file>
<file>mcg.css</file>
<file>noise-texture.png</file>
</gresource>
</gresources>

View file

@ -14,3 +14,17 @@ datadir = os.path.join(srcdir, '..', 'data')
# Set GSettings schema dir (if not set already)
if not os.environ.get('GSETTINGS_SCHEMA_DIR'):
os.environ['GSETTINGS_SCHEMA_DIR'] = datadir
class Environment:
"""Wrapper class to access environment settings."""
def get_srcdir():
return srcdir
def get_data(subdir):
return os.path.join(datadir, subdir)

View file

@ -7,6 +7,7 @@ import urllib
from gi.repository import Gio, Gtk, Gdk
from mcg import Environment
from mcg import widgets
@ -14,22 +15,20 @@ from mcg import widgets
class Application(Gtk.Application):
TITLE = "MPDCoverGrid (Gtk)"
SETTINGS_BASE_KEY = 'de.coderkun.mcg'
ID = 'de.coderkun.mcg'
def __init__(self):
Gtk.Application.__init__(self, application_id="de.coderkun.mcg-dev", flags=Gio.ApplicationFlags.FLAGS_NONE)
Gtk.Application.__init__(self, application_id=Application.ID, flags=Gio.ApplicationFlags.FLAGS_NONE)
self._window = None
def do_startup(self):
Gtk.Application.do_startup(self)
self._settings = Gio.Settings.new(Application.SETTINGS_BASE_KEY)
self.load_css()
# Create builder to load UI
self._builder = Gtk.Builder()
self._builder.add_from_file('data/gtk.glade')
self._load_resource()
self._load_settings()
self._load_css()
self._load_ui()
def do_activate(self):
@ -39,11 +38,32 @@ class Application(Gtk.Application):
self._window.present()
def load_css(self):
def _load_resource(self):
self._resource = Gio.resource_load(
Environment.get_data(Application.ID + '.gresource')
)
Gio.Resource._register(self._resource)
def _load_settings(self):
self._settings = Gio.Settings.new(Application.ID)
def _load_css(self):
styleProvider = Gtk.CssProvider()
styleProvider.load_from_file(Gio.File.new_for_path('data/mcg.css'))
styleProvider.load_from_resource(self._get_resource_path('mcg.css'))
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
styleProvider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
def _load_ui(self):
# Create builder to load UI
self._builder = Gtk.Builder()
self._builder.add_from_resource(self._get_resource_path('gtk.glade'))
def _get_resource_path(self, path):
return "/{}/{}".format(Application.ID.replace('.', '/'), path)