[gnome-builder] meson-templates: introduce Adwaita Templates



commit 5483416fcb79c28ef6fa3a4757a25658cd493db3
Author: Günther Wagner <info gunibert de>
Date:   Thu Feb 17 20:02:12 2022 +0100

    meson-templates: introduce Adwaita Templates
    
    For now only C and Rust are supported by our Templates.

 src/plugins/meson-templates/meson_templates.py     | 90 +++++++++++++++++++++-
 .../meson-templates/resources/flatpak.json.in      |  2 +-
 src/plugins/meson-templates/resources/meson.build  |  4 +-
 .../meson-templates/resources/src/Cargo-gtk4.toml  |  8 +-
 .../meson-templates/resources/src/application.c    |  2 +-
 .../meson-templates/resources/src/application.h    |  6 +-
 .../meson-templates/resources/src/application.rs   | 13 +++-
 .../resources/src/hello.gresource.xml              |  2 +-
 .../resources/src/meson-c-vala.build               |  4 +-
 9 files changed, 118 insertions(+), 13 deletions(-)
---
diff --git a/src/plugins/meson-templates/meson_templates.py b/src/plugins/meson-templates/meson_templates.py
index 59efbb845..a6842ff7b 100644
--- a/src/plugins/meson-templates/meson_templates.py
+++ b/src/plugins/meson-templates/meson_templates.py
@@ -33,7 +33,8 @@ _ = Ide.gettext
 
 class LibraryTemplateProvider(GObject.Object, Ide.TemplateProvider):
     def do_get_project_templates(self):
-        return [GnomeGTK4ProjectTemplate(),
+        return [GnomeAdwaitaProjectTemplate(),
+                GnomeGTK4ProjectTemplate(),
                 GnomeProjectTemplate(),
                 LibraryProjectTemplate(),
                 CLIProjectTemplate(),
@@ -148,13 +149,16 @@ class MesonTemplate(Ide.TemplateBase, Ide.ProjectTemplate):
         scope.get('Spaces').assign_string(" " * len(PreFix))
 
         enable_gnome = (isinstance(self, GnomeProjectTemplate) or
-                        isinstance(self, GnomeGTK4ProjectTemplate))
+                        isinstance(self, GnomeGTK4ProjectTemplate) or
+                        isinstance(self, GnomeAdwaitaProjectTemplate))
         scope.get('project_version').assign_string('0.1.0')
         scope.get('enable_i18n').assign_boolean(enable_gnome)
         scope.get('enable_gnome').assign_boolean(enable_gnome)
         scope.get('language').assign_string(self.language)
         scope.get('author').assign_string(author_name)
 
+        scope.get('is_adwaita').assign_boolean(True if isinstance(self, GnomeAdwaitaProjectTemplate) else 
False)
+
         # Just avoiding dealing with template bugs
         if self.language in ('c', 'c++'):
             ui_file = prefix + '-window.ui'
@@ -403,6 +407,88 @@ class GnomeGTK4ProjectTemplate(MesonTemplate):
 
         files[meson_file] = 'src/meson.build'
 
+class GnomeAdwaitaProjectTemplate(MesonTemplate):
+    def __init__(self):
+        super().__init__(
+            'gnome-app-adwaita',
+            _('GNOME Adwaita Application'),
+            'pattern-gnome',
+            _('Create a GNOME Adwaita application'),
+            ['C', 'Rust'],
+            0
+         )
+
+    def prepare_files(self, files):
+        # Shared files
+        files['resources/flatpak.json'] = '%(appid)s.json'
+        files['resources/data/hello.desktop.in'] = 'data/%(appid)s.desktop.in'
+        files['resources/data/hello.appdata.xml.in'] = 'data/%(appid)s.appdata.xml.in'
+        files['resources/data/hello.gschema.xml'] = 'data/%(appid)s.gschema.xml'
+        files['resources/data/meson.build'] = 'data/meson.build'
+        files['resources/data/icons/meson.build'] = 'data/icons/meson.build'
+        files['resources/data/icons/hicolor/scalable/apps/hello.svg'] = 
'data/icons/hicolor/scalable/apps/%(appid)s.svg'
+        files['resources/data/icons/hicolor/symbolic/apps/hello-symbolic.svg'] = 
'data/icons/hicolor/symbolic/apps/%(appid)s-symbolic.svg'
+        files['resources/po/LINGUAS'] = 'po/LINGUAS'
+        files['resources/po/meson.build'] = 'po/meson.build'
+        files['resources/po/POTFILES'] = 'po/POTFILES'
+        files['resources/src/help-overlay.ui'] = 'src/gtk/help-overlay.ui'
+        window_ui_name = 'src/window.ui'
+        resource_name = 'src/%(prefix)s.gresource.xml'
+        meson_file = 'resources/src/meson-c-vala.build'
+
+        if self.language == 'c':
+            files['resources/src/main-gtk4.c'] = 'src/main.c'
+            files['resources/src/window.c'] = 'src/%(prefix)s-window.c'
+            files['resources/src/window.h'] = 'src/%(prefix)s-window.h'
+            files['resources/src/application.c'] = 'src/%(prefix)s-application.c'
+            files['resources/src/application.h'] = 'src/%(prefix)s-application.h'
+            window_ui_name = 'src/%(prefix)s-window.ui'
+        elif self.language == 'c++':
+            files['resources/src/main.cpp'] = 'src/main.cpp'
+            files['resources/src/window.cpp'] = 'src/%(prefix)s-window.cpp'
+            files['resources/src/window.hpp'] = 'src/%(prefix)s-window.h'
+            window_ui_name = 'src/%(prefix)s-window.ui'
+        elif self.language == 'c♯':
+            files['resources/src/main.cs'] = 'src/main.cs'
+            files['resources/src/application.in'] = 'src/%(exec_name)s.in'
+            files['resources/flatpak-gtksharp.json.tmpl'] = '%(appid)s.json'
+            meson_file = 'resources/src/meson-cs.build'
+            resource_name = None
+            window_ui_name = None
+        elif self.language == 'vala':
+            files['resources/src/main-gtk4.vala'] = 'src/main.vala'
+            files['resources/src/window-gtk4.vala'] = 'src/window.vala'
+            files['resources/src/application-gtk4.vala'] = 'src/application.vala'
+        elif self.language == 'javascript':
+            files['resources/src/main-gtk4.js.tmpl'] = 'src/main.js'
+            files['resources/src/hello.js.in'] = 'src/%(appid)s.in'
+            files['resources/src/window.js.tmpl'] = 'src/window.js'
+            files['resources/src/hello.src.gresource.xml'] = 'src/%(appid)s.src.gresource.xml'
+            resource_name = 'src/%(appid)s.data.gresource.xml'
+            meson_file = 'resources/src/meson-js.build'
+        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-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'
+            files['resources/src/main-gtk4.rs'] = 'src/main.rs'
+            files['resources/src/window-gtk4.rs'] = 'src/window.rs'
+            files['resources/src/Cargo.lock'] = 'Cargo.lock'
+            files['resources/src/Cargo-gtk4.toml'] = 'Cargo.toml'
+            files['resources/build-aux/cargo.sh'] = 'build-aux/cargo.sh'
+            meson_file = 'resources/src/meson-rs-gtk4.build'
+
+        if resource_name:
+            files['resources/src/hello.gresource.xml'] = resource_name
+        if window_ui_name:
+            files['resources/src/window-gtk4.ui'] = window_ui_name
+
+        files[meson_file] = 'src/meson.build'
+
 class LibraryProjectTemplate(MesonTemplate):
     def __init__(self):
         super().__init__(
diff --git a/src/plugins/meson-templates/resources/flatpak.json.in 
b/src/plugins/meson-templates/resources/flatpak.json.in
index 64c3cac0c..79d326545 100644
--- a/src/plugins/meson-templates/resources/flatpak.json.in
+++ b/src/plugins/meson-templates/resources/flatpak.json.in
@@ -13,7 +13,7 @@
         "--share=network",
         "--share=ipc",
         "--socket=fallback-x11",
-{{if template == "gnome-app-gtk4"}}
+{{if (template == "gnome-app-gtk4") || (template == "gnome-app-adwaita")}}
         "--device=dri",
 {{endif}}
         "--socket=wayland"
diff --git a/src/plugins/meson-templates/resources/meson.build 
b/src/plugins/meson-templates/resources/meson.build
index b053379fa..a73b3eb77 100644
--- a/src/plugins/meson-templates/resources/meson.build
+++ b/src/plugins/meson-templates/resources/meson.build
@@ -1,7 +1,7 @@
 project('{{name}}',{{if language == "c"}} 'c',{{else if language == "c++"}} ['cpp', 'c'],{{else if language 
== "vala"}} ['c', 'vala'],{{else if language == "c♯"}} 'cs',{{else if language == "rust"}} 'rust', {{end}}
 
           version: '{{project_version}}',
-    meson_version: '>= 0.57.0',
+    meson_version: '>= 0.59.0',
   default_options: [ 'warning_level=2',
 {{if language == "c"}}
                      'c_std=gnu11',
@@ -32,7 +32,7 @@ configure_file(
   configuration: config_h,
 )
 add_project_arguments([
-  '-I' + meson.build_root(),
+  '-I' + meson.project_build_root(),
 ], language: 'c')
 {{end}}
 
diff --git a/src/plugins/meson-templates/resources/src/Cargo-gtk4.toml 
b/src/plugins/meson-templates/resources/src/Cargo-gtk4.toml
index c8c9ce406..ed7e26c24 100644
--- a/src/plugins/meson-templates/resources/src/Cargo-gtk4.toml
+++ b/src/plugins/meson-templates/resources/src/Cargo-gtk4.toml
@@ -5,4 +5,10 @@ edition = "2018"
 
 [dependencies]
 gettext-rs = { version = "0.7", features = ["gettext-system"] }
-gtk = { version = "0.3", package = "gtk4" }
+gtk = { version = "0.4", package = "gtk4" }
+
+{{if is_adwaita}}
+[dependencies.adw]
+package = "libadwaita"
+version  = "0.1.0"
+{{end}}
diff --git a/src/plugins/meson-templates/resources/src/application.c 
b/src/plugins/meson-templates/resources/src/application.c
index 2092c21f7..025fda446 100644
--- a/src/plugins/meson-templates/resources/src/application.c
+++ b/src/plugins/meson-templates/resources/src/application.c
@@ -8,7 +8,7 @@ struct _{{PreFix}}Application
   GtkApplication parent_instance;
 };
 
-G_DEFINE_TYPE ({{PreFix}}Application, {{prefix_}}_application, GTK_TYPE_APPLICATION)
+G_DEFINE_TYPE ({{PreFix}}Application, {{prefix_}}_application, {{if 
is_adwaita}}ADW_TYPE_APPLICATION{{else}}GTK_TYPE_APPLICATION{{end}})
 
 {{PreFix}}Application *
 {{prefix_}}_application_new (gchar *application_id,
diff --git a/src/plugins/meson-templates/resources/src/application.h 
b/src/plugins/meson-templates/resources/src/application.h
index d4a9bada0..b63af79ae 100644
--- a/src/plugins/meson-templates/resources/src/application.h
+++ b/src/plugins/meson-templates/resources/src/application.h
@@ -2,13 +2,17 @@
 
 #pragma once
 
+{{if is_adwaita}}
+#include <adwaita.h>
+{{else}}
 #include <gtk/gtk.h>
+{{end}}
 
 G_BEGIN_DECLS
 
 #define {{PREFIX}}_TYPE_APPLICATION ({{prefix_}}_application_get_type())
 
-G_DECLARE_FINAL_TYPE ({{PreFix}}Application, {{prefix_}}_application, {{PREFIX}}, APPLICATION, 
GtkApplication)
+G_DECLARE_FINAL_TYPE ({{PreFix}}Application, {{prefix_}}_application, {{PREFIX}}, APPLICATION, {{if 
is_adwaita}}AdwApplication{{else}}GtkApplication{{end}})
 
 {{PreFix}}Application *{{prefix_}}_application_new (gchar *application_id,
 {{Spaces}}{{spaces}}                               GApplicationFlags  flags);
diff --git a/src/plugins/meson-templates/resources/src/application.rs 
b/src/plugins/meson-templates/resources/src/application.rs
index fea527d93..9d2b56cb3 100644
--- a/src/plugins/meson-templates/resources/src/application.rs
+++ b/src/plugins/meson-templates/resources/src/application.rs
@@ -2,6 +2,9 @@ use glib::clone;
 use gtk::prelude::*;
 use gtk::subclass::prelude::*;
 use gtk::{gio, glib};
+{{if is_adwaita}}
+use adw::subclass::prelude::*;
+{{end}}
 
 use crate::config::VERSION;
 use crate::{{PreFix}}Window;
@@ -16,7 +19,7 @@ mod imp {
     impl ObjectSubclass for {{PreFix}}Application {
         const NAME: &'static str = "{{PreFix}}Application";
         type Type = super::{{PreFix}}Application;
-        type ParentType = gtk::Application;
+        type ParentType = {{if is_adwaita}}adw::Application{{else}}gtk::Application{{end}};
     }
 
     impl ObjectImpl for {{PreFix}}Application {
@@ -48,11 +51,15 @@ mod imp {
     }
 
     impl GtkApplicationImpl for {{PreFix}}Application {}
+    {{if is_adwaita}}
+impl AdwApplicationImpl for {{PreFix}}Application {}
+{{end}}
 }
 
 glib::wrapper! {
     pub struct {{PreFix}}Application(ObjectSubclass<imp::{{PreFix}}Application>)
-        @extends gio::Application, gtk::Application,
+        @extends gio::Application, gtk::Application, {{if is_adwaita}}adw::Application,{{end}}
+
         @implements gio::ActionGroup, gio::ActionMap;
 }
 
@@ -78,7 +85,7 @@ impl {{PreFix}}Application {
 
     fn show_about(&self) {
         let window = self.active_window().unwrap();
-        let dialog = gtk::AboutDialogBuilder::new()
+        let dialog = gtk::AboutDialog::builder()
             .transient_for(&window)
             .modal(true)
             .program_name("{{name}}")
diff --git a/src/plugins/meson-templates/resources/src/hello.gresource.xml 
b/src/plugins/meson-templates/resources/src/hello.gresource.xml
index bc5275809..198ae49fc 100644
--- a/src/plugins/meson-templates/resources/src/hello.gresource.xml
+++ b/src/plugins/meson-templates/resources/src/hello.gresource.xml
@@ -2,7 +2,7 @@
 <gresources>
   <gresource prefix="{{appid_path}}">
     <file>{{ui_file}}</file>
-{{if template == "gnome-app-gtk4"}}
+{{if (template == "gnome-app-gtk4") || (template == "gnome-app-adwaita")}}
     <file>gtk/help-overlay.ui</file>
 {{end}}
   </gresource>
diff --git a/src/plugins/meson-templates/resources/src/meson-c-vala.build 
b/src/plugins/meson-templates/resources/src/meson-c-vala.build
index 013896751..33d5656ef 100644
--- a/src/plugins/meson-templates/resources/src/meson-c-vala.build
+++ b/src/plugins/meson-templates/resources/src/meson-c-vala.build
@@ -2,7 +2,7 @@
 {{if language == "c"}}
   'main.c',
   '{{prefix}}-window.c',
-  {{if template == "gnome-app-gtk4"}}
+  {{if (template == "gnome-app-gtk4") || (template == "gnome-app-adwaita")}}
   '{{prefix}}-application.c',
   {{end}}
 {{else if language == "c++"}}
@@ -22,6 +22,8 @@
   dependency('gtkmm-3.0', version: '>= 3.18'),
 {{else if template == "gnome-app-gtk4"}}
   dependency('gtk4'),
+{{else if template == "gnome-app-adwaita"}}
+  dependency('libadwaita-1', version: '>= 1.0'),
 {{else}}
   dependency('gio-2.0', version: '>= 2.50'),
   dependency('gtk+-3.0', version: '>= 3.22'),


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