[lightsoff] Split lightsoff.vala in two files.



commit 9dc496eb745304dc239d16284cea639fc2002469
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Tue Mar 29 01:33:39 2016 +0200

    Split lightsoff.vala in two files.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=764258

 configure.ac              |    4 ++
 data/lightsoff.ui         |    4 +-
 po/POTFILES.in            |    1 +
 po/POTFILES.skip          |    1 +
 src/Makefile.am           |    6 ++-
 src/lightsoff-window.vala |  124 +++++++++++++++++++++++++++++++++++++++++++++
 src/lightsoff.vala        |  110 +--------------------------------------
 7 files changed, 140 insertions(+), 110 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index e76ac68..9870402 100644
--- a/configure.ac
+++ b/configure.ac
@@ -18,18 +18,22 @@ dnl ###########################################################################
 dnl Dependencies
 dnl ###########################################################################
 
+GLIB_REQUIRED=2.38.0
 GTK_REQUIRED=3.13.4
 CLUTTER_REQUIRED=1.14.0
 CLUTTER_GTK_REQUIRED=1.5.0
 RSVG_REQUIRED=2.32.0
 
 PKG_CHECK_MODULES(LIGHTSOFF, [
+  glib-2.0 >= $GLIB_REQUIRED
   gtk+-3.0 >= $GTK_REQUIRED
   clutter-1.0 >= $CLUTTER_REQUIRED
   clutter-gtk-1.0 >= $CLUTTER_GTK_REQUIRED
   librsvg-2.0 >= $RSVG_REQUIRED
 ])
 
+AC_SUBST([GLIB_REQUIRED])
+
 AC_CHECK_LIB([m],[floor])
 
 AC_PATH_PROG([DESKTOP_FILE_VALIDATE], [desktop-file-validate], [/bin/true])
diff --git a/data/lightsoff.ui b/data/lightsoff.ui
index ba48c8a..dab0e53 100644
--- a/data/lightsoff.ui
+++ b/data/lightsoff.ui
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
   <requires lib="gtk+" version="3.12"/>
-  <object class="GtkApplicationWindow" id="window">
+  <template class="LightsoffWindow" parent="GtkApplicationWindow">
     <property name="resizable">False</property>
     <child type="titlebar">
       <object class="GtkHeaderBar" id="headerbar">
@@ -49,5 +49,5 @@
         </child>
       </object>
     </child>
-  </object>
+  </template>
 </interface>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 4ebd5e0..0f0137d 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -9,4 +9,5 @@ data/org.gnome.lightsoff.gschema.xml
 src/board-view.vala
 src/game-view.vala
 src/lightsoff.vala
+src/lightsoff-window.vala
 src/puzzle-generator.vala
diff --git a/po/POTFILES.skip b/po/POTFILES.skip
index 057e1dc..e2bc686 100644
--- a/po/POTFILES.skip
+++ b/po/POTFILES.skip
@@ -1,4 +1,5 @@
 src/board-view.c
 src/game-view.c
 src/lightsoff.c
+src/lightsoff-window.c
 src/puzzle-generator.c
diff --git a/src/Makefile.am b/src/Makefile.am
index e6cd4fd..8dbad72 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -4,15 +4,19 @@ BUILT_SOURCES = lightsoff-resources.c
 
 lightsoff_SOURCES = \
        board-view.vala \
-       config.vapi \
        lightsoff.vala \
+       lightsoff-window.vala \
        puzzle-generator.vala \
        game-view.vala \
+       config.vapi \
        $(BUILT_SOURCES)
 
 lightsoff_VALAFLAGS = \
        --pkg posix \
+       --pkg gtk+-3.0 \
        --pkg gmodule-2.0 \
+       --gresources lightsoff.gresource.xml \
+       --target-glib $(GLIB_REQUIRED) \
        --pkg clutter-1.0 \
        --pkg clutter-gtk-1.0
 
diff --git a/src/lightsoff-window.vala b/src/lightsoff-window.vala
new file mode 100644
index 0000000..904ecac
--- /dev/null
+++ b/src/lightsoff-window.vala
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2010-2013 Robert Ancell
+ * Copyright (C) 2014 Michael Catanzaro
+ * Copyright (C) 2016 Arnaud Bonatti
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
+
+using Gtk;
+
+[GtkTemplate (ui = "/org/gnome/lightsoff/ui/lightsoff.ui")]
+public class LightsoffWindow : ApplicationWindow
+{
+    [GtkChild] private HeaderBar headerbar;
+
+    private GLib.Settings settings;
+    private GameView game_view;
+    private SimpleAction previous_level;
+
+    private const GLib.ActionEntry[] window_actions =
+    {
+        { "new-game",       new_game_cb },
+        { "previous-level", previous_level_cb },
+        { "next-level",     next_level_cb }
+    };
+
+    public LightsoffWindow ()
+    {
+        settings = new GLib.Settings ("org.gnome.lightsoff");
+
+        add_action_entries (window_actions, this);
+        previous_level = (SimpleAction) this.lookup_action ("previous-level");
+
+        level_changed_cb (settings.get_int ("level"));
+
+        var clutter_embed = new GtkClutter.Embed ();
+        clutter_embed.show ();
+        this.add (clutter_embed);
+
+        var stage = (Clutter.Stage) clutter_embed.get_stage ();
+        stage.key_release_event.connect (key_release_event_cb);
+        stage.background_color = Clutter.Color.from_string ("#000000");
+
+        game_view = new GameView (settings.get_int ("level"));
+        game_view.level_changed.connect (level_changed_cb);
+        game_view.moves_changed.connect (update_subtitle);
+        game_view.show ();
+        stage.add_child (game_view);
+
+        stage.set_size (game_view.width, game_view.height);
+        clutter_embed.set_size_request ((int) stage.width, (int) stage.height);
+    }
+
+    private void update_subtitle (int moves)
+    {
+        headerbar.subtitle = ngettext ("%d move", "%d moves", moves).printf (moves);
+    }
+
+    private void update_title (int level)
+    {
+        /* The title of the window, %d is the level number */
+        headerbar.title = _("Level %d").printf (level);
+
+        /* Subtitle is a game hint when playing level one, the number of moves otherwise */
+        if (level == 1)
+            headerbar.subtitle = _("Turn off all the lights!");
+        else
+            update_subtitle (0);
+    }
+
+    private void previous_level_cb ()
+    {
+        game_view.swap_board (-1);
+    }
+
+    private void next_level_cb ()
+    {
+        game_view.swap_board (1);
+    }
+
+    private void level_changed_cb (int level)
+    {
+        previous_level.set_enabled (level > 1);
+        update_title (level);
+        if (level != settings.get_int ("level"))
+            settings.set_int ("level", level);
+    }
+
+    private bool key_release_event_cb (Clutter.Actor actor, Clutter.KeyEvent event)
+    {
+        switch (event.keyval)
+        {
+        case Clutter.Key.Escape:
+            game_view.hide_cursor ();
+            return true;
+        case Clutter.Key.Down:
+            game_view.move_cursor (0, 1);
+            return true;
+        case Clutter.Key.Up:
+            game_view.move_cursor (0, -1);
+            return true;
+        case Clutter.Key.Left:
+            game_view.move_cursor (-1, 0);
+            return true;
+        case Clutter.Key.Right:
+            game_view.move_cursor (1, 0);
+            return true;
+        case Clutter.Key.Return:
+            game_view.activate_cursor ();
+            return true;
+        default:
+            return false;
+        }
+    }
+
+    private void new_game_cb ()
+    {
+        game_view.reset_game ();
+    }
+ }
diff --git a/src/lightsoff.vala b/src/lightsoff.vala
index 41db0ee..c1cc025 100644
--- a/src/lightsoff.vala
+++ b/src/lightsoff.vala
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2010-2013 Robert Ancell
  * Copyright (C) 2014 Michael Catanzaro
+ * Copyright (C) 2016 Arnaud Bonatti
  *
  * This program is free software: you can redistribute it and/or modify it under
  * the terms of the GNU General Public License as published by the Free Software
@@ -11,11 +12,7 @@
 
 public class LightsOff : Gtk.Application
 {
-    private Settings settings;
-    private Gtk.ApplicationWindow window;
-    private Gtk.HeaderBar headerbar;
-    private GameView game_view;
-    private SimpleAction previous_level;
+    private LightsoffWindow window;
 
     private const GLib.ActionEntry[] action_entries =
     {
@@ -24,13 +21,6 @@ public class LightsOff : Gtk.Application
         { "about",         about_cb    }
     };
 
-    private const ActionEntry[] window_actions =
-    {
-        { "new-game",       new_game_cb },
-        { "previous-level", previous_level_cb },
-        { "next-level",     next_level_cb }
-    };
-
     private LightsOff ()
     {
         Object (application_id: "org.gnome.lightsoff", flags: ApplicationFlags.FLAGS_NONE);
@@ -45,109 +35,15 @@ public class LightsOff : Gtk.Application
 
         add_action_entries (action_entries, this);
 
-        settings = new Settings ("org.gnome.lightsoff");
-
-        Gtk.Builder builder = new Gtk.Builder.from_resource ("/org/gnome/lightsoff/ui/lightsoff.ui");
-        window = (Gtk.ApplicationWindow) builder.get_object ("window");
-        headerbar = (Gtk.HeaderBar) builder.get_object ("headerbar");
-
-        window.add_action_entries (window_actions, this);
-        previous_level = (SimpleAction) window.lookup_action ("previous-level");
-
-        level_changed_cb (settings.get_int ("level"));
-
-        var clutter_embed = new GtkClutter.Embed ();
-        clutter_embed.show ();
-        window.add (clutter_embed);
-
-        var stage = (Clutter.Stage) clutter_embed.get_stage ();
-        stage.key_release_event.connect (key_release_event_cb);
-        stage.background_color = Clutter.Color.from_string ("#000000");
-
-        game_view = new GameView (settings.get_int ("level"));
-        game_view.level_changed.connect (level_changed_cb);
-        game_view.moves_changed.connect (update_subtitle);
-        game_view.show ();
-        stage.add_child (game_view);
-
-        stage.set_size (game_view.width, game_view.height);
-        clutter_embed.set_size_request ((int) stage.width, (int) stage.height);
-
+        window = new LightsoffWindow ();
         add_window (window);
     }
 
-    private void update_subtitle (int moves)
-    {
-        headerbar.subtitle = ngettext ("%d move", "%d moves", moves).printf (moves);
-    }
-
-    private void update_title (int level)
-    {
-        /* The title of the window, %d is the level number */
-        headerbar.title = _("Level %d").printf (level);
-
-        /* Subtitle is a game hint when playing level one, the number of moves otherwise */
-        if (level == 1)
-            headerbar.subtitle = _("Turn off all the lights!");
-        else
-            update_subtitle (0);
-    }
-
-    private void previous_level_cb ()
-    {
-        game_view.swap_board (-1);
-    }
-
-    private void next_level_cb ()
-    {
-        game_view.swap_board (1);
-    }
-
-    private void level_changed_cb (int level)
-    {
-        previous_level.set_enabled (level > 1);
-        update_title (level);
-        if (level != settings.get_int ("level"))
-            settings.set_int ("level", level);
-    }
-
-    private bool key_release_event_cb (Clutter.Actor actor, Clutter.KeyEvent event)
-    {
-        switch (event.keyval)
-        {
-        case Clutter.Key.Escape:
-            game_view.hide_cursor ();
-            return true;
-        case Clutter.Key.Down:
-            game_view.move_cursor (0, 1);
-            return true;
-        case Clutter.Key.Up:
-            game_view.move_cursor (0, -1);
-            return true;
-        case Clutter.Key.Left:
-            game_view.move_cursor (-1, 0);
-            return true;
-        case Clutter.Key.Right:
-            game_view.move_cursor (1, 0);
-            return true;
-        case Clutter.Key.Return:
-            game_view.activate_cursor ();
-            return true;
-        default:
-            return false;
-        }
-    }
-
     public override void activate ()
     {
         window.present ();
     }
 
-    private void new_game_cb ()
-    {
-        game_view.reset_game ();
-    }
-
     private void quit_cb ()
     {
         window.destroy ();


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