[libadwaita/wip/exalm/screenshot: 3/3] screenshot: Load ui files from a dir rather than resources




commit 5639e28aca12ff2bb080353aa044aa9cd7956023
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Mon Jan 10 16:32:41 2022 +0500

    screenshot: Load ui files from a dir rather than resources
    
    Make it a bit more generic. Update readme.

 doc/tools/README.md                 |  12 ++-
 doc/tools/screenshot.c              | 141 +++++++++++++++++++++++-------------
 doc/tools/screenshot.gresources.xml |  87 ----------------------
 3 files changed, 94 insertions(+), 146 deletions(-)
---
diff --git a/doc/tools/README.md b/doc/tools/README.md
index 3fbef374..d7de64c2 100644
--- a/doc/tools/README.md
+++ b/doc/tools/README.md
@@ -20,17 +20,15 @@ onto it.
 If the widget needs special treatment - for example, it's a `GtkPopover` - it
 should be special-cased in `screenshot.c` based on its type.
 
-3. Add it to `screenshot.gresources.xml`
-4. From the build directory, run:
+3. From the build directory, run:
 
 ```
-ninja doc/tools/screenshot
-./doc/tools/screenshot ../doc/images/ -i IMAGE
+./doc/tools/screenshot ../doc/tools/data/ ../doc/images/ -i IMAGE
 ```
 
-5. The generator will create `IMAGE.png` and `IMAGE-dark.png` images. Add them
+4. The generator will create `IMAGE.png` and `IMAGE-dark.png` images. Add them
 to `libadwaita.toml.in`.
-6. Use them in the docs as follows:
+5. Use them in the docs as follows:
 
 ```html
 <picture>
@@ -44,7 +42,7 @@ to `libadwaita.toml.in`.
 To regenerate all screenshots, run:
 
 ```c
-./doc/tools/screenshot ../doc/images/
+./doc/tools/screenshot ../doc/tools/data/ ../doc/images/
 ```
 
 from the build directory.
diff --git a/doc/tools/screenshot.c b/doc/tools/screenshot.c
index 6105b44c..79b4f0a6 100644
--- a/doc/tools/screenshot.c
+++ b/doc/tools/screenshot.c
@@ -140,9 +140,12 @@ take_screenshot_cb (ScreenshotData *data)
 static void
 take_screenshot (const char *name,
                  gboolean    dark,
+                 GFile      *input_dir,
                  GFile      *output_dir)
 {
-  g_autofree char *ui_path = NULL;
+  g_autofree char *input_name = NULL;
+  g_autoptr (GFile) input_file = NULL;
+  g_autofree char *input_path = NULL;
   g_autofree char *output_name = NULL;
   g_autoptr (GtkBuilder) builder = NULL;
   g_autoptr (GFile) output_file = NULL;
@@ -152,7 +155,9 @@ take_screenshot (const char *name,
   GtkWidget *window;
   gboolean wait = FALSE;
 
-  ui_path = g_strdup_printf (RESOURCE_PATH "data/%s.ui", name);
+  input_name = g_strconcat (name, ".ui", NULL);
+  input_file = g_file_get_child (input_dir, input_name);
+  input_path = g_file_get_path (input_file);
 
   if (dark)
     output_name = g_strdup_printf ("%s-dark.png", name);
@@ -170,7 +175,7 @@ take_screenshot (const char *name,
     adw_style_manager_set_color_scheme (adw_style_manager_get_default (),
                                         ADW_COLOR_SCHEME_FORCE_LIGHT);
 
-  builder = gtk_builder_new_from_resource (ui_path);
+  builder = gtk_builder_new_from_file (input_path);
   widget = gtk_builder_get_object (builder, "widget");
   hover_widget = gtk_builder_get_object (builder, "hover");
 
@@ -247,88 +252,99 @@ init_libadwaita (void)
                 NULL);
 }
 
-static int
-compare_images (gconstpointer a,
-                gconstpointer b)
+static GList *
+list_images (GFile *input_dir)
 {
-  char **ap = (char **) a;
-  char **bp = (char **) b;
+  g_autoptr (GFileEnumerator) enumerator = NULL;
+  g_autoptr (GError) error = NULL;
+  GList *children = NULL;
+  GFileInfo *info;
+
+  enumerator =
+    g_file_enumerate_children (input_dir,
+                               G_FILE_ATTRIBUTE_STANDARD_NAME,
+                               G_FILE_QUERY_INFO_NONE,
+                               NULL,
+                               &error);
+  if (error) {
+    g_critical ("Couldn't enumerate images: %s", error->message);
+
+    return NULL;
+  }
 
-  return g_ascii_strcasecmp (*ap, *bp);
-}
+  while ((info = g_file_enumerator_next_file (enumerator, NULL, &error))) {
+    const char *name = NULL;
+    char *shortname;
 
-static char **
-list_images (void)
-{
-  g_autoptr (GError) error = NULL;
-  char **children =
-    g_resources_enumerate_children (RESOURCE_PATH "data",
-                                    G_RESOURCE_LOOKUP_FLAGS_NONE,
-                                    &error);
-  guint length;
+    if (error) {
+      g_critical ("Couldn't enumerate image: %s", error->message);
+
+      continue;
+    }
 
-  if (error)
-    g_critical ("Couldn't enumerate children: %s", error->message);
+    name = g_file_info_get_name (info);
 
-  length = g_strv_length (children);
-  qsort (children, length, sizeof (char *), compare_images);
+    if (!g_str_has_suffix (name, ".ui"))
+      continue;
 
-  return children;
+    shortname = get_shortname (name);
+
+    children = g_list_prepend (children, shortname);
+  }
+
+  return g_list_sort (children, (GCompareFunc) g_ascii_strcasecmp);
 }
 
 static void
 process_image (const char *name,
+               GFile      *input_dir,
                GFile      *output_dir)
 {
   g_print ("Processing %s\n", name);
 
-  take_screenshot (name, FALSE, output_dir);
-  take_screenshot (name, TRUE, output_dir);
+  take_screenshot (name, FALSE, input_dir, output_dir);
+  take_screenshot (name, TRUE,  input_dir, output_dir);
 }
 
 static void
-run_screenshot (GFile *output_dir)
+run_screenshot (GFile *input_dir,
+                GFile *output_dir)
 {
-  g_auto (GStrv) children = NULL;
-  int i = -1;
+  g_autoptr (GList) children = NULL;
+  GList *l;
 
   if (option_image) {
-    g_autofree char *path = g_strdup_printf (RESOURCE_PATH "data/%s.ui", option_image);
+    g_autofree char *input_name = g_strconcat (option_image, ".ui", NULL);
+    g_autoptr (GFile) input_file = g_file_get_child (input_dir, input_name);
 
-    if (!g_resources_get_info (path, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL, NULL, NULL)) {
+    if (!g_file_query_exists (input_file, NULL)) {
       g_printerr ("No such image: %s\n", option_image);
 
       return;
     }
 
-    process_image (option_image, output_dir);
+    process_image (option_image, input_dir, output_dir);
 
     return;
   }
 
-  children = list_images ();
+  children = list_images (input_dir);
 
-  if (!children)
-    return;
-
-  while (children[++i]) {
-    g_autofree char *shortname = get_shortname (children[i]);
+  for (l = children; l; l = l->next) {
+    g_autofree char *shortname = l->data;
 
-    process_image (shortname, output_dir);
+    process_image (shortname, input_dir, output_dir);
   }
 }
 
 static void
-run_list_images (void)
+run_list_images (GFile *input_dir)
 {
-  g_auto (GStrv) children = list_images ();
-  int i = -1;
-
-  if (!children)
-    return;
+  g_autoptr (GList) children = list_images (input_dir);
+  GList *l;
 
-  while (children[++i]) {
-    g_autofree char *shortname = get_shortname (children[i]);
+  for (l = children; l; l = l->next) {
+    g_autofree char *shortname = l->data;
 
     g_print ("%s\n", shortname);
   }
@@ -338,7 +354,8 @@ int
 main (int    argc,
       char **argv)
 {
-  GOptionContext *context = g_option_context_new ("PATH");
+  GOptionContext *context = g_option_context_new ("INPUT_DIR OUTPUT_DIR");
+  g_autoptr (GFile) input_dir = NULL;
   g_autoptr (GFile) output_dir = NULL;
   g_autoptr (GError) error = NULL;
 
@@ -350,18 +367,38 @@ main (int    argc,
   }
 
   if (option_list) {
-    run_list_images ();
+    if (argc < 2 || !argv[1]) {
+      g_printerr ("Input directory must be set to list images\n");
+
+      return 1;
+    }
+
+    input_dir = g_file_new_for_path (argv[1]);
+    if (!g_file_query_exists (input_dir, NULL)) {
+      g_critical ("Input directory does not exist");
+
+      return 1;
+    }
+
+    run_list_images (input_dir);
 
     return 0;
   }
 
-  if (argc < 2 || !argv[1]) {
+  if (argc < 3 || !argv[1] || !argv[2]) {
     g_printerr ("%s\n", g_option_context_get_help (context, FALSE, NULL));
 
     return 1;
   }
 
-  output_dir = g_file_new_for_path (argv[1]);
+  input_dir = g_file_new_for_path (argv[1]);
+  if (!g_file_query_exists (input_dir, NULL)) {
+    g_critical ("Input directory does not exist");
+
+    return 1;
+  }
+
+  output_dir = g_file_new_for_path (argv[2]);
 
   if (!g_file_query_exists (output_dir, NULL)) {
     g_file_make_directory_with_parents (output_dir, NULL, &error);
@@ -373,7 +410,7 @@ main (int    argc,
   }
 
   init_libadwaita ();
-  run_screenshot (output_dir);
+  run_screenshot (input_dir, output_dir);
 
   return 0;
 }
diff --git a/doc/tools/screenshot.gresources.xml b/doc/tools/screenshot.gresources.xml
index 2c2640ba..cbe21e7f 100644
--- a/doc/tools/screenshot.gresources.xml
+++ b/doc/tools/screenshot.gresources.xml
@@ -3,93 +3,6 @@
   <gresource prefix="/org/gnome/Adwaita/Screenshot">
     <file preprocess="xml-stripblanks">icons/scalable/apps/org.gnome.Boxes.svg</file>
 
-    <file preprocess="xml-stripblanks">data/action-row.ui</file>
-    <file preprocess="xml-stripblanks">data/adaptive-boxed-lists-narrow.ui</file>
-    <file preprocess="xml-stripblanks">data/adaptive-boxed-lists-wide.ui</file>
-    <file preprocess="xml-stripblanks">data/adaptive-split-headers-narrow-1.ui</file>
-    <file preprocess="xml-stripblanks">data/adaptive-split-headers-narrow-2.ui</file>
-    <file preprocess="xml-stripblanks">data/adaptive-split-headers-wide.ui</file>
-    <file preprocess="xml-stripblanks">data/adaptive-utility-pane-narrow.ui</file>
-    <file preprocess="xml-stripblanks">data/adaptive-utility-pane-wide.ui</file>
-    <file preprocess="xml-stripblanks">data/adaptive-view-switcher-narrow.ui</file>
-    <file preprocess="xml-stripblanks">data/adaptive-view-switcher-wide.ui</file>
-    <file preprocess="xml-stripblanks">data/app-icons.ui</file>
-    <file preprocess="xml-stripblanks">data/application-window.ui</file>
-    <file preprocess="xml-stripblanks">data/avatar.ui</file>
-    <file preprocess="xml-stripblanks">data/bin.ui</file>
-    <file preprocess="xml-stripblanks">data/boxed-lists.ui</file>
-    <file preprocess="xml-stripblanks">data/button-content.ui</file>
-    <file preprocess="xml-stripblanks">data/buttons-circular.ui</file>
-    <file preprocess="xml-stripblanks">data/buttons-destructive-action.ui</file>
-    <file preprocess="xml-stripblanks">data/buttons-flat.ui</file>
-    <file preprocess="xml-stripblanks">data/buttons-opaque.ui</file>
-    <file preprocess="xml-stripblanks">data/buttons-pill.ui</file>
-    <file preprocess="xml-stripblanks">data/buttons-raised.ui</file>
-    <file preprocess="xml-stripblanks">data/buttons-suggested-action.ui</file>
-    <file preprocess="xml-stripblanks">data/cards.ui</file>
-    <file preprocess="xml-stripblanks">data/carousel.ui</file>
-    <file preprocess="xml-stripblanks">data/carousel-indicator-dots.ui</file>
-    <file preprocess="xml-stripblanks">data/carousel-indicator-lines.ui</file>
-    <file preprocess="xml-stripblanks">data/clamp-narrow.ui</file>
-    <file preprocess="xml-stripblanks">data/clamp-wide.ui</file>
-    <file preprocess="xml-stripblanks">data/combo-row.ui</file>
-    <file preprocess="xml-stripblanks">data/deprecated-app-notification.ui</file>
-    <file preprocess="xml-stripblanks">data/deprecated-sidebar.ui</file>
-    <file preprocess="xml-stripblanks">data/devel-window.ui</file>
-    <file preprocess="xml-stripblanks">data/dim-label.ui</file>
-    <file preprocess="xml-stripblanks">data/expander-row.ui</file>
-    <file preprocess="xml-stripblanks">data/flap-narrow.ui</file>
-    <file preprocess="xml-stripblanks">data/flap-wide.ui</file>
-    <file preprocess="xml-stripblanks">data/flat-header-bar.ui</file>
-    <file preprocess="xml-stripblanks">data/header-bar.ui</file>
-    <file preprocess="xml-stripblanks">data/header-bar-split.ui</file>
-    <file preprocess="xml-stripblanks">data/hello-world.ui</file>
-    <file preprocess="xml-stripblanks">data/leaflet-narrow.ui</file>
-    <file preprocess="xml-stripblanks">data/leaflet-wide.ui</file>
-    <file preprocess="xml-stripblanks">data/linked-controls.ui</file>
-    <file preprocess="xml-stripblanks">data/navigation-sidebar.ui</file>
-    <file preprocess="xml-stripblanks">data/osd.ui</file>
-    <file preprocess="xml-stripblanks">data/osd-buttons.ui</file>
-    <file preprocess="xml-stripblanks">data/osd-progress-bar.ui</file>
-    <file preprocess="xml-stripblanks">data/osd-toolbar.ui</file>
-    <file preprocess="xml-stripblanks">data/popover-menu-list.ui</file>
-    <file preprocess="xml-stripblanks">data/preferences-group.ui</file>
-    <file preprocess="xml-stripblanks">data/preferences-page.ui</file>
-    <file preprocess="xml-stripblanks">data/preferences-window.ui</file>
-    <file preprocess="xml-stripblanks">data/search-bar-inline.ui</file>
-    <file preprocess="xml-stripblanks">data/selection-mode-checks.ui</file>
-    <file preprocess="xml-stripblanks">data/split-button.ui</file>
-    <file preprocess="xml-stripblanks">data/squeezer-narrow.ui</file>
-    <file preprocess="xml-stripblanks">data/squeezer-wide.ui</file>
-    <file preprocess="xml-stripblanks">data/status-page.ui</file>
-    <file preprocess="xml-stripblanks">data/status-page-compact.ui</file>
-    <file preprocess="xml-stripblanks">data/style-background.ui</file>
-    <file preprocess="xml-stripblanks">data/style-colors.ui</file>
-    <file preprocess="xml-stripblanks">data/style-frame.ui</file>
-    <file preprocess="xml-stripblanks">data/style-view.ui</file>
-    <file preprocess="xml-stripblanks">data/tab-bar.ui</file>
-    <file preprocess="xml-stripblanks">data/tab-bar-inline.ui</file>
-    <file preprocess="xml-stripblanks">data/toast-action.ui</file>
-    <file preprocess="xml-stripblanks">data/toast-overlay.ui</file>
-    <file preprocess="xml-stripblanks">data/toast-simple.ui</file>
-    <file preprocess="xml-stripblanks">data/toast-undo.ui</file>
-    <file preprocess="xml-stripblanks">data/toolbar.ui</file>
-    <file preprocess="xml-stripblanks">data/toolbar-flat.ui</file>
-    <file preprocess="xml-stripblanks">data/toolbar-raised.ui</file>
-    <file preprocess="xml-stripblanks">data/toolbar-spacer.ui</file>
-    <file preprocess="xml-stripblanks">data/typography-body.ui</file>
-    <file preprocess="xml-stripblanks">data/typography-captions.ui</file>
-    <file preprocess="xml-stripblanks">data/typography-heading.ui</file>
-    <file preprocess="xml-stripblanks">data/typography-large-title.ui</file>
-    <file preprocess="xml-stripblanks">data/typography-monospace.ui</file>
-    <file preprocess="xml-stripblanks">data/typography-numeric.ui</file>
-    <file preprocess="xml-stripblanks">data/typography-titles.ui</file>
-    <file preprocess="xml-stripblanks">data/view-switcher.ui</file>
-    <file preprocess="xml-stripblanks">data/view-switcher-bar.ui</file>
-    <file preprocess="xml-stripblanks">data/view-switcher-title.ui</file>
-    <file preprocess="xml-stripblanks">data/window.ui</file>
-    <file preprocess="xml-stripblanks">data/window-title.ui</file>
-
     <file compressed="true">style.css</file>
   </gresource>
 </gresources>


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