Skip to content

Commit a1196c9

Browse files
committed
Change focusing behavior: Try getting the view from the current event target in mousemove event, and temporarily focus views so keyboard events are handled too.
1 parent 07cfaf8 commit a1196c9

2 files changed

Lines changed: 40 additions & 24 deletions

File tree

src/ui/Key.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var Key = this.Key = new function() {
6969
var character = String.fromCharCode(charCode),
7070
key = keys[keyCode] || character.toLowerCase(),
7171
handler = down ? 'onKeyDown' : 'onKeyUp',
72-
view = View.focused,
72+
view = View._focused,
7373
scope = view && view.isVisible() && view._scope,
7474
tool = scope && scope.tool;
7575
keyMap[key] = down;

src/ui/View.js

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ var View = this.View = Base.extend({
101101
this._events = this._createEvents();
102102
DomEvent.add(this._canvas, this._events);
103103
// Make sure the first view is focused for keyboard input straight away
104-
if (!View.focused)
105-
View.focused = this;
104+
if (!View._focused)
105+
View._focused = this;
106106
// As soon as a new view is added we need to mark the redraw as not
107107
// motified, so the next call loops through all the views again.
108108
this._scope._redrawNotified = false;
@@ -243,8 +243,9 @@ var View = this.View = Base.extend({
243243
remove: function() {
244244
if (this._index == null)
245245
return false;
246-
if (View.focused == this)
247-
View.focused = null;
246+
// Clear focus if removed view had it
247+
if (View._focused == this)
248+
View._focused = null;
248249
delete View._views[this._id];
249250
Base.splice(this._scope.views, null, this._index, 1);
250251
// Uninstall event handlers again for this view.
@@ -370,15 +371,45 @@ var View = this.View = Base.extend({
370371
var tool,
371372
timer,
372373
curPoint,
374+
tempFocus,
373375
dragging = false;
374376

375377
function viewToArtwork(view, event) {
376378
return view.viewToArtwork(DomEvent.getOffset(event, view._canvas));
377379
}
378380

381+
function updateFocus() {
382+
if (!View._focused || !View._focused.isVisible()) {
383+
// Find the first visible view in all scopes
384+
PaperScope.each(function(scope) {
385+
for (var i = 0, l = scope.views.length; i < l; i++) {
386+
var view = scope.views[i];
387+
if (view.isVisible()) {
388+
View._focused = tempFocus = view;
389+
throw Base.stop;
390+
}
391+
}
392+
});
393+
}
394+
}
395+
379396
function mousemove(event) {
380-
var view = View.focused;
381-
if (!view || !(tool = view._scope.tool))
397+
var view;
398+
if (!dragging) {
399+
// See if we can get the view from the current event target, and
400+
// handle the mouse move over it.
401+
view = View._views[DomEvent.getTarget(event).getAttribute('id')];
402+
if (view) {
403+
// Temporarily focus this view without making it sticky, so
404+
// Key events are handled too during the mouse over
405+
View._focused = tempFocus = view;
406+
} else if (tempFocus && tempFocus == View._focused) {
407+
// Clear temporary focus again and update it.
408+
View._focused = null;
409+
updateFocus();
410+
}
411+
}
412+
if (!(view = view || View._focused) || !(tool = view._scope.tool))
382413
return;
383414
var point = event && viewToArtwork(view, event);
384415
var onlyMove = !!(!tool.onMouseDrag && tool.onMouseMove);
@@ -398,7 +429,7 @@ var View = this.View = Base.extend({
398429
}
399430

400431
function mouseup(event) {
401-
var view = View.focused;
432+
var view = View._focused;
402433
if (!view || !dragging)
403434
return;
404435
dragging = false;
@@ -420,21 +451,6 @@ var View = this.View = Base.extend({
420451
DomEvent.stop(event);
421452
}
422453

423-
function updateFocus() {
424-
if (!View.focused || View.focused.isInvisible()) {
425-
// Find the first visible view in all scopes
426-
PaperScope.each(function(scope) {
427-
for (var i = 0, l = scope.views.length; i < l; i++) {
428-
var view = scope.views[i];
429-
if (view.isVisible()) {
430-
View.focused = view;
431-
throw Base.stop;
432-
}
433-
}
434-
});
435-
}
436-
}
437-
438454
// mousemove and mouseup events need to be installed on document, not the
439455
// view canvas, since we want to catch the end of drag events even outside
440456
// our view. Only the mousedown events are installed on the view, as handled
@@ -459,7 +475,7 @@ var View = this.View = Base.extend({
459475

460476
function mousedown(event) {
461477
// Tell the Key class which view should receive keyboard input.
462-
View.focused = view;
478+
View._focused = view;
463479
if (!(tool = view._scope.tool))
464480
return;
465481
curPoint = viewToArtwork(view, event);

0 commit comments

Comments
 (0)