[gnome-builder/gnome-builder-41] meson-templates: Add GTK4 Python template



commit 3102e2f18ca5e96cb5b1ca10abd60f0d74d4ccbc
Author: Tim Lauridsen <tla rasmil dk>
Date:   Sun Oct 31 18:32:04 2021 +0100

    meson-templates: Add GTK4 Python template

 .../meson-templates/meson-templates.gresource.xml  |  3 ++
 src/plugins/meson-templates/meson_templates.py     |  8 ++---
 .../meson-templates/resources/src/main-gtk4.py     | 42 ++++++++++++++++++++++
 .../resources/src/meson-py-gtk4.build              | 34 ++++++++++++++++++
 .../meson-templates/resources/src/window-gtk4.py   | 27 ++++++++++++++
 5 files changed, 110 insertions(+), 4 deletions(-)
---
diff --git a/src/plugins/meson-templates/meson-templates.gresource.xml 
b/src/plugins/meson-templates/meson-templates.gresource.xml
index 57620d8ca..328c6699f 100644
--- a/src/plugins/meson-templates/meson-templates.gresource.xml
+++ b/src/plugins/meson-templates/meson-templates.gresource.xml
@@ -6,6 +6,7 @@
     <file compressed="true">resources/src/help-overlay.ui</file>
     <file compressed="true">resources/src/window.js.tmpl</file>
     <file compressed="true">resources/src/meson-py.build</file>
+    <file compressed="true">resources/src/meson-py-gtk4.build</file>
     <file compressed="true">resources/src/window.h</file>
     <file compressed="true">resources/src/hello.js.in</file>
     <file compressed="true">resources/src/hello.gresource.xml</file>
@@ -33,6 +34,7 @@
     <file compressed="true">resources/src/window.cpp</file>
     <file compressed="true">resources/src/window.hpp</file>
     <file compressed="true">resources/src/window.py</file>
+    <file compressed="true">resources/src/window-gtk4.py</file>
     <file compressed="true">resources/src/window.vala</file>
     <file compressed="true">resources/src/config.rs.in</file>
     <file compressed="true">resources/src/config-gtk4.rs.in</file>
@@ -42,6 +44,7 @@
     <file compressed="true">resources/src/main-cli.vala</file>
     <file compressed="true">resources/src/hello.py.in</file>
     <file compressed="true">resources/src/main.py</file>
+    <file compressed="true">resources/src/main-gtk4.py</file>
     <file compressed="true">resources/src/main.rs</file>
     <file compressed="true">resources/src/main-gtk4.rs</file>
     <file compressed="true">resources/src/main-cli.rs</file>
diff --git a/src/plugins/meson-templates/meson_templates.py b/src/plugins/meson-templates/meson_templates.py
index 4f465b244..8ce88d509 100644
--- a/src/plugins/meson-templates/meson_templates.py
+++ b/src/plugins/meson-templates/meson_templates.py
@@ -330,7 +330,7 @@ class GnomeGTK4ProjectTemplate(MesonTemplate):
             _('GNOME Application'),
             'pattern-gnome',
             _('Create a GNOME application with GTK 4'),
-            ['C', 'Rust'],
+            ['C', 'Rust', 'Python'],
             0
          )
 
@@ -385,9 +385,9 @@ class GnomeGTK4ProjectTemplate(MesonTemplate):
         elif self.language == 'python':
             files['resources/src/hello.py.in'] = 'src/%(name)s.in'
             files['resources/src/__init__.py'] = 'src/__init__.py'
-            files['resources/src/window.py'] = 'src/window.py'
-            files['resources/src/main.py'] = 'src/main.py'
-            meson_file = 'resources/src/meson-py.build'
+            files['resources/src/window-gtk4.py'] = 'src/window.py'
+            files['resources/src/main-gtk4.py'] = 'src/main.py'
+            meson_file = 'resources/src/meson-py-gtk4.build'
         elif self.language == 'rust':
             files['resources/src/application.rs'] = 'src/application.rs'
             files['resources/src/config-gtk4.rs.in'] = 'src/config.rs.in'
diff --git a/src/plugins/meson-templates/resources/src/main-gtk4.py 
b/src/plugins/meson-templates/resources/src/main-gtk4.py
new file mode 100755
index 000000000..eac683f18
--- /dev/null
+++ b/src/plugins/meson-templates/resources/src/main-gtk4.py
@@ -0,0 +1,42 @@
+{{include "license.py"}}
+
+import sys
+import gi
+
+gi.require_version('Gtk', '4.0')
+
+from gi.repository import Gtk, Gio
+
+from .window import {{PreFix}}Window, AboutDialog
+
+
+class Application(Gtk.Application):
+    def __init__(self):
+        super().__init__(application_id='{{appid}}',
+                         flags=Gio.ApplicationFlags.FLAGS_NONE)
+
+    def do_activate(self):
+        win = self.props.active_window
+        if not win:
+            win = {{PreFix}}Window(application=self)
+        self.create_action('about', self.on_about_action)
+        self.create_action('preferences', self.on_preferences_action)
+        win.present()
+
+    def on_about_action(self, widget, _):
+        about = AboutDialog(self.props.active_window)
+        about.present()
+
+    def on_preferences_action(self, widget, _):
+        print('app.preferences action activated')
+
+    def create_action(self, name, callback):
+        """ Add an Action and connect to a callback """
+        action = Gio.SimpleAction.new(name, None)
+        action.connect("activate", callback)
+        self.add_action(action)
+
+
+def main(version):
+    app = Application()
+    return app.run(sys.argv)
diff --git a/src/plugins/meson-templates/resources/src/meson-py-gtk4.build 
b/src/plugins/meson-templates/resources/src/meson-py-gtk4.build
new file mode 100644
index 000000000..8fe0ac197
--- /dev/null
+++ b/src/plugins/meson-templates/resources/src/meson-py-gtk4.build
@@ -0,0 +1,34 @@
+pkgdatadir = join_paths(get_option('prefix'), get_option('datadir'), meson.project_name())
+moduledir = join_paths(pkgdatadir, '{{name_}}')
+gnome = import('gnome')
+
+gnome.compile_resources('{{name}}',
+  '{{prefix}}.gresource.xml',
+  gresource_bundle: true,
+  install: true,
+  install_dir: pkgdatadir,
+)
+
+python = import('python')
+
+conf = configuration_data()
+conf.set('PYTHON', python.find_installation('python3').path())
+conf.set('VERSION', meson.project_version())
+conf.set('localedir', join_paths(get_option('prefix'), get_option('localedir')))
+conf.set('pkgdatadir', pkgdatadir)
+
+configure_file(
+  input: '{{name}}.in',
+  output: '{{name}}',
+  configuration: conf,
+  install: true,
+  install_dir: get_option('bindir')
+)
+
+{{name_}}_sources = [
+  '__init__.py',
+  'main.py',
+  'window.py',
+]
+
+install_data({{name_}}_sources, install_dir: moduledir)
diff --git a/src/plugins/meson-templates/resources/src/window-gtk4.py 
b/src/plugins/meson-templates/resources/src/window-gtk4.py
new file mode 100644
index 000000000..a316b0c76
--- /dev/null
+++ b/src/plugins/meson-templates/resources/src/window-gtk4.py
@@ -0,0 +1,27 @@
+{{include "license.py"}}
+
+from gi.repository import Gtk
+
+
+@Gtk.Template(resource_path='{{appid_path}}/{{ui_file}}')
+class {{PreFix}}Window(Gtk.ApplicationWindow):
+    __gtype_name__ = '{{PreFix}}Window'
+
+    label = Gtk.Template.Child()
+
+    def __init__(self, **kwargs):
+        super().__init__(**kwargs)
+
+
+class AboutDialog(Gtk.AboutDialog):
+
+    def __init__(self, parent):
+        Gtk.AboutDialog.__init__(self)
+        self.props.program_name = '{{name}}'
+        self.props.version = "0.1.0"
+        self.props.authors = ['{{author}}']
+        self.props.license_type = Gtk.License.GPL_3_0
+        self.props.copyright = '(C) 2021 {{author}}'
+        self.props.website = 'https://www.gnome.org/'
+        self.props.logo_icon_name = '{{appid}}'
+        self.set_transient_for(parent)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]