diff --git a/Nasal/canvas/gui.nas b/Nasal/canvas/gui.nas index 3d1c03d04..c1f0cf5a4 100644 --- a/Nasal/canvas/gui.nas +++ b/Nasal/canvas/gui.nas @@ -68,7 +68,6 @@ var Window = { _ghost: ghost, _node: props.wrapNode(ghost._node_ghost), _focused: 0, - _focused_widget: nil, _widgets: [] }; @@ -155,6 +154,8 @@ var Window = { canvas_.addPlacement({type: "window", "id": me.get("id")}); me['_canvas'] = canvas_; + + canvas_._focused_widget = nil; canvas_.data("focused", me._focused); # prevent resizing if canvas is placed from somewhere else diff --git a/Nasal/canvas/gui/Widget.nas b/Nasal/canvas/gui/Widget.nas index 0ec18e096..ab65bc27b 100644 --- a/Nasal/canvas/gui/Widget.nas +++ b/Nasal/canvas/gui/Widget.nas @@ -1,10 +1,4 @@ gui.Widget = { -# enum FocusPolicy: - NoFocus: 0, - TabFocus: 1, - ClickFocus: 2, - StrongFocus: 1 + 2, - # new: func(derived) { @@ -82,7 +76,7 @@ gui.Widget = { return me; var canvas = me.getCanvas(); - if( canvas._focused_widget != nil ) + if( canvas._impl['_focused_widget'] != nil ) canvas._focused_widget.clearFocus(); if( !me._enabled ) @@ -118,7 +112,13 @@ gui.Widget = { onRemove: func { if( me._view != nil ) + { me._view._root.del(); + me._view = nil; + } + + if( me._focused ) + me.getCanvas()._focused_widget = nil; }, # protected: _MAX_SIZE: 32768, # size for "no size-limit" @@ -148,7 +148,7 @@ gui.Widget = { me._onStateChange(); }); root.addEventListener("mousedown", func { - if( bits.test(me._focus_policy, me.ClickFocus / 2) ) + if( me._focus_policy & me.ClickFocus ) me.setFocus(); }); root.addEventListener("mouseleave", func { @@ -159,9 +159,10 @@ gui.Widget = { }, _trigger: func(type, data = nil) { - me._view._root.dispatchEvent( - canvas.CustomEvent.new("cb." ~ type, {detail: data}) - ); + if( me._view != nil ) + me._view._root.dispatchEvent( + canvas.CustomEvent.new("cb." ~ type, {detail: data}) + ); return me; }, _windowFocus: func @@ -170,3 +171,10 @@ gui.Widget = { return canvas != nil ? canvas.data("focused") : 0; } }; + +# enum FocusPolicy: +gui.Widget.NoFocus = 0; +gui.Widget.TabFocus = 1; +gui.Widget.ClickFocus = 2; +gui.Widget.StrongFocus = gui.Widget.TabFocus + | gui.Widget.ClickFocus;