[gnome-shell] environment: move more init stuff here from main.js



commit 7921954a3187ae8f4742c2c99d91275361fc436c
Author: Dan Winship <danw gnome org>
Date:   Sat Apr 30 10:27:36 2011 -0400

    environment: move more init stuff here from main.js
    
    Move some more environment-initializationy stuff from main.js to
    environment.js, and be more careful about not importing shell JS
    modules until after the environment has been fully patched.
    
    Change gnome-shell-plugin to call Environment.init() before
    Main.start(); this means that Environment.init() now runs before any
    shell JS modules (besides environment itself) have been imported.
    
    Make run-js-test create a ShellGlobal and use its js_context, so that
    the shell_global_set_property_mutable() stuff in Environment.init()
    will work correctly in tests as well.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=649203

 js/ui/environment.js     |   55 ++++++++++++++++++++++++++++-----------------
 js/ui/main.js            |   15 +-----------
 src/gnome-shell-plugin.c |    3 +-
 src/run-js-test.c        |   26 +++++++++++-----------
 4 files changed, 50 insertions(+), 49 deletions(-)
---
diff --git a/js/ui/environment.js b/js/ui/environment.js
index 758a2cb..90e7fa6 100644
--- a/js/ui/environment.js
+++ b/js/ui/environment.js
@@ -1,14 +1,21 @@
 /* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
 
+imports.gi.versions.Clutter = '1.0';
+imports.gi.versions.Gio = '2.0';
+imports.gi.versions.Gdk = '3.0';
+imports.gi.versions.GdkPixbuf = '2.0';
+imports.gi.versions.Gtk = '3.0';
+
 const Clutter = imports.gi.Clutter;;
 const GLib = imports.gi.GLib;
+const Gtk = imports.gi.Gtk;
 const Shell = imports.gi.Shell;
 const St = imports.gi.St;
-const Gettext_gtk30 = imports.gettext.domain('gtk30');
 
-const Tweener = imports.ui.tweener;
+// We can't import shell JS modules yet, because they may have
+// variable initializations, etc, that depend on init() already having
+// been run.
 
-const Format = imports.misc.format;
 
 // "monkey patch" in some varargs ClutterContainer methods; we need
 // to do this per-container class since there is no representation
@@ -61,26 +68,16 @@ function _blockMethod(method, replacement, reason) {
 }
 
 function init() {
-    Tweener.init();
-    String.prototype.format = Format.format;
-
-    // Work around https://bugzilla.mozilla.org/show_bug.cgi?id=508783
-    Date.prototype.toLocaleFormat = function(format) {
-        return Shell.util_format_date(format, this.getTime());
-    };
+    // Add some bindings to the global JS namespace; (gjs keeps the web
+    // browser convention of having that namespace be called 'window'.)
+    window.global = Shell.Global.get();
 
     // Set the default direction for St widgets (this needs to be done before any use of St)
-    if (Gettext_gtk30.gettext('default:LTR') == 'default:RTL') {
+    if (Gtk.Widget.get_default_direction() == Gtk.TextDirection.RTL) {
         St.Widget.set_default_direction(St.TextDirection.RTL);
     }
 
-    let slowdownEnv = GLib.getenv('GNOME_SHELL_SLOWDOWN_FACTOR');
-    if (slowdownEnv) {
-        let factor = parseFloat(slowdownEnv);
-        if (!isNaN(factor) && factor > 0.0)
-            St.set_slow_down_factor(factor);
-    }
-
+    // Miscellaneous monkeypatching
     _patchContainerClass(St.BoxLayout);
     _patchContainerClass(St.Table);
 
@@ -97,9 +94,6 @@ function init() {
             return base;
     };
 
-    if (window.global === undefined) // test environment
-        return;
-
     _blockMethod('Clutter.Event.get_state', 'Shell.get_event_state',
                  'gjs\'s handling of Clutter.ModifierType is broken. See bug 597292.');
     _blockMethod('Gdk.Window.get_device_position', 'global.get_pointer',
@@ -110,4 +104,23 @@ function init() {
     // Shell.Global.prototype itself is read-only.
     global.set_property_mutable('imports.gi.Shell.Global.prototype', 'set_property_mutable', true);
     Shell.Global.prototype.set_property_mutable = undefined;
+
+    // Work around https://bugzilla.mozilla.org/show_bug.cgi?id=508783
+    Date.prototype.toLocaleFormat = function(format) {
+        return Shell.util_format_date(format, this.getTime());
+    };
+
+    let slowdownEnv = GLib.getenv('GNOME_SHELL_SLOWDOWN_FACTOR');
+    if (slowdownEnv) {
+        let factor = parseFloat(slowdownEnv);
+        if (!isNaN(factor) && factor > 0.0)
+            St.set_slow_down_factor(factor);
+    }
+
+    // OK, now things are initialized enough that we can import shell JS
+    const Format = imports.misc.format;
+    const Tweener = imports.ui.tweener;
+
+    Tweener.init();
+    String.prototype.format = Format.format;
 }
diff --git a/js/ui/main.js b/js/ui/main.js
index d02a22d..2845670 100644
--- a/js/ui/main.js
+++ b/js/ui/main.js
@@ -1,11 +1,5 @@
 /* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
 
-imports.gi.versions.Clutter = '1.0';
-imports.gi.versions.Gio = '2.0';
-imports.gi.versions.Gdk = '3.0';
-imports.gi.versions.GdkPixbuf = '2.0';
-imports.gi.versions.Gtk = '3.0';
-
 const Clutter = imports.gi.Clutter;
 const DBus = imports.dbus;
 const Gdk = imports.gi.Gdk;
@@ -75,12 +69,7 @@ let _cssStylesheet = null;
 let background = null;
 
 function start() {
-    // Add a binding for 'global' in the global JS namespace; (gjs
-    // keeps the web browser convention of having that namespace be
-    // called 'window'.)
-    window.global = Shell.Global.get();
-
-    // Now monkey patch utility functions into the global proxy;
+    // Monkey patch utility functions into the global proxy;
     // This is easier and faster than indirecting down into global
     // if we want to call back up into JS.
     global.logError = _logError;
@@ -102,8 +91,6 @@ function start() {
     // not loading any events until the user presses the clock
     global.launch_calendar_server();
 
-    Environment.init();
-
     // Ensure ShellWindowTracker and ShellAppUsage are initialized; this will
     // also initialize ShellAppSystem first.  ShellAppSystem
     // needs to load all the .desktop files, and ShellWindowTracker
diff --git a/src/gnome-shell-plugin.c b/src/gnome-shell-plugin.c
index 1b2ccf6..4708e0a 100644
--- a/src/gnome-shell-plugin.c
+++ b/src/gnome-shell-plugin.c
@@ -178,7 +178,8 @@ gnome_shell_plugin_start (MetaPlugin *plugin)
   gjs_context = _shell_global_get_gjs_context (shell_plugin->global);
 
   if (!gjs_context_eval (gjs_context,
-                         "const Main = imports.ui.main; Main.start();",
+                         "imports.ui.environment.init();"
+                         "imports.ui.main.start();",
                          -1,
                          "<main>",
                          &status,
diff --git a/src/run-js-test.c b/src/run-js-test.c
index 27cd400..41fb37c 100644
--- a/src/run-js-test.c
+++ b/src/run-js-test.c
@@ -24,15 +24,19 @@
  * IN THE SOFTWARE.
  */
 
-#include <config.h>
-#include <gtk/gtk.h>
-#include <gdk/gdkx.h>
-#include <clutter/x11/clutter-x11.h>
-#include <string.h>
-#include <stdlib.h>
+#include "config.h"
+
 #include <locale.h>
+#include <stdlib.h>
+#include <string.h>
 
+#include <clutter/x11/clutter-x11.h>
+#include <gdk/gdkx.h>
 #include <gjs/gjs.h>
+#include <gtk/gtk.h>
+
+#include "shell-global.h"
+#include "shell-global-private.h"
 
 static char **include_path = NULL;
 static char *command = NULL;
@@ -59,10 +63,10 @@ event_filter (GdkXEvent *xevent,
 int
 main(int argc, char **argv)
 {
-  char *command_line;
   GOptionContext *context;
   ClutterActor *stage;
   GError *error = NULL;
+  ShellGlobal *global;
   GjsContext *js_context;
   char *script;
   const char *filename;
@@ -94,12 +98,8 @@ main(int argc, char **argv)
   setlocale (LC_ALL, "");
   g_type_init ();
 
-  command_line = g_strjoinv (" ", argv);
-  g_debug ("Command line: %s", command_line);
-  g_free (command_line);
-
-  g_debug ("Creating new context to eval console script");
-  js_context = gjs_context_new_with_search_path (include_path);
+  global = shell_global_get ();
+  js_context = _shell_global_get_gjs_context (global);
 
   /* prepare command line arguments */
   if (!gjs_context_define_string_array (js_context, "ARGV",



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