From 78a9f9afe96460a8fb8b6bd39644ef11d758c2f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Mon, 16 May 2011 00:17:37 +0100 Subject: [PATCH] Let Keyboard handlers know which view should receive keyboard focus. --- src/document/DocumentView.js | 16 +++++++++++----- src/ui/Key.js | 9 +++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/document/DocumentView.js b/src/document/DocumentView.js index 777d4936..90d57da2 100644 --- a/src/document/DocumentView.js +++ b/src/document/DocumentView.js @@ -24,6 +24,7 @@ var DocumentView = this.DocumentView = Base.extend({ // To go with the convention of never passing document to constructors, // in all items, associate the view with the currently active document. this._document = paper.document; + this._scope = this._document._scope; // Push it onto document.views and set index: this._index = this._document.views.push(this) - 1; // Handle canvas argument @@ -74,6 +75,9 @@ var DocumentView = this.DocumentView = Base.extend({ this._zoom = 1; this._events = this._createEvents(); DomEvent.add(this._canvas, this._events); + // Make sure the first view is focused for keyboard input straight away + if (!DocumentView.focused) + DocumentView.focused = this; }, getDocument: function() { @@ -143,8 +147,9 @@ var DocumentView = this.DocumentView = Base.extend({ var res = Base.splice(this._document.views, null, this._index, 1); // Uninstall event handlers again for this view. DomEvent.remove(this._canvas, this._events); + this._document = this._scope = this._canvas = this._events = null; // Clearing _onFrame makes the frame handler stop automatically. - this._document = this._canvas = this._events = that._onFrame = null; + this._onFrame = null; return !!res.length; }, @@ -199,7 +204,7 @@ var DocumentView = this.DocumentView = Base.extend({ return; } // Set the global paper object to the current scope - paper = that._document._scope; + paper = that._scope; // Request next frame already DomEvent.requestAnimationFrame(frame, that._canvas); running = true; @@ -222,7 +227,6 @@ var DocumentView = this.DocumentView = Base.extend({ _createEvents: function() { var that = this, - scope = this._document._scope, tool, timer, curPoint, @@ -233,7 +237,9 @@ var DocumentView = this.DocumentView = Base.extend({ } function mousedown(event) { - if (!(tool = scope.tool)) + // Tell the Key class which view should receive keyboard input. + DocumentView.focused = that; + if (!(tool = that._scope.tool)) return; curPoint = viewToArtwork(event); tool.onHandleEvent('mousedown', curPoint, event); @@ -245,7 +251,7 @@ var DocumentView = this.DocumentView = Base.extend({ } function mousemove(event) { - if (!(tool = scope.tool)) + if (!(tool = that._scope.tool)) return; // If the event was triggered by a touch screen device, prevent the // default behaviour, as it will otherwise scroll the page: diff --git a/src/ui/Key.js b/src/ui/Key.js index 6e2b2a08..8dc2f304 100644 --- a/src/ui/Key.js +++ b/src/ui/Key.js @@ -64,17 +64,18 @@ var Key = this.Key = new function() { function handleKey(down, keyCode, charCode, event) { var character = String.fromCharCode(charCode), key = keys[keyCode] || character.toLowerCase(), - handler = down ? 'onKeyDown' : 'onKeyUp'; + handler = down ? 'onKeyDown' : 'onKeyUp', + scope = DocumentView.focused && DocumentView.focused._scope, + tool = scope && scope.tool; keyMap[key] = down; - if (paper.tool && paper.tool[handler]) { + if (tool && tool[handler]) { // Call the onKeyDown or onKeyUp handler if present // When the handler function returns false, prevent the // default behaviour of the key event: // PORT: Add to Sg var keyEvent = new KeyEvent(down, key, character, event); - if (paper.tool[handler](keyEvent) === false) { + if (tool[handler](keyEvent) === false) keyEvent.preventDefault(); - } } }