From 064f3e05d38d9253379156a59f216b08c0046b37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=BCrg=20Lehni?= <juerg@scratchdisk.com>
Date: Sat, 14 May 2011 20:08:14 +0100
Subject: [PATCH] Simplify paper scope switching code for now, as key handling
 code depends on paper object to always point to a valid one. This is a
 workaround, rethinking of how key handling should be distributed across
 multiple instances within one page is required.

---
 src/core/PaperScript.js | 13 ++++---------
 src/tool/ToolHandler.js |  3 +--
 2 files changed, 5 insertions(+), 11 deletions(-)

diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js
index 13b6b93d..2c2e19e9 100644
--- a/src/core/PaperScript.js
+++ b/src/core/PaperScript.js
@@ -130,8 +130,8 @@ var PaperScript = this.PaperScript = new function() {
 	}
 
 	function run(code, scope) {
-		try { with (scope) { // Safe one indentation by grouping try and with
-			PaperScope.set(scope);
+		with (scope) { // Safe one indentation by grouping try and with
+			paper = scope;
 			var doc = scope.document;
 				// TODO: Add support for multiple tools
 			var tool = scope.tool =
@@ -158,7 +158,7 @@ var PaperScript = this.PaperScript = new function() {
 				var totalTime = 0;
 				function frame(dontSwitch) {
 					if (!dontSwitch)
-						PaperScope.set(scope);
+						paper = scope;
 					// Request next frame already
 					DomEvent.requestAnimationFrame(frame, doc && doc.canvas);
 					var time = Date.now() / 1000;
@@ -174,8 +174,6 @@ var PaperScript = this.PaperScript = new function() {
 					if (doc)
 						doc.redraw();
 					lastTime = time;
-					if (!dontSwitch)
-						PaperScope.restore();
 				};
 				// Call the onFrame handler and redraw the document:
 				frame(true);
@@ -185,8 +183,6 @@ var PaperScript = this.PaperScript = new function() {
 					doc.redraw();
 			}
 			return res;
-		} } finally {
-			PaperScope.restore();
 		}
 	}
 
@@ -223,9 +219,8 @@ var PaperScript = this.PaperScript = new function() {
 				var canvas = script.getAttribute('canvas');
 				if (canvas = canvas && document.getElementById(canvas)) {
 					// Create a Document for this canvas, using the right scope
-					PaperScope.set(scope);
+					paper = scope;
 					new Document(canvas);
-					PaperScope.restore();
 				}
 				if (script.src) {
 					request(script.src, scope);
diff --git a/src/tool/ToolHandler.js b/src/tool/ToolHandler.js
index 8a3510b2..f0db61a9 100644
--- a/src/tool/ToolHandler.js
+++ b/src/tool/ToolHandler.js
@@ -121,7 +121,7 @@ var ToolHandler = this.ToolHandler = Base.extend({
 	},
 
 	onHandleEvent: function(type, pt, event) {
-		PaperScope.set(this._scope);
+		paper = this._scope;
 		switch (type) {
 		case 'mousedown':
 			this.updateEvent(type, pt, null, null, true, false, false);
@@ -178,6 +178,5 @@ var ToolHandler = this.ToolHandler = Base.extend({
 			}
 			break;
 		}
-		PaperScope.restore();
 	}
 });