From 1060451f85eef063234aa393f56123cb60a3b2e6 Mon Sep 17 00:00:00 2001 From: Xen0Xys Date: Fri, 3 May 2024 11:18:49 +0200 Subject: [PATCH] :ambulance: Fix widget crash when moving from Aladin Lite view --- examples/6_Linked-widgets.ipynb | 8 +++--- js/models/event_handler.js | 48 ++++++++++++++++++++------------- js/utils.js | 6 ++--- src/ipyaladin/aladin.py | 10 ------- 4 files changed, 35 insertions(+), 37 deletions(-) diff --git a/examples/6_Linked-widgets.ipynb b/examples/6_Linked-widgets.ipynb index f31212f9..1ce40493 100644 --- a/examples/6_Linked-widgets.ipynb +++ b/examples/6_Linked-widgets.ipynb @@ -27,12 +27,12 @@ "c = Aladin(layout=Layout(width=\"33.33%\"), survey=\"P/2MASS/color\", **cosmetic_options)\n", "\n", "# synchronize target between 3 widgets\n", - "widgets.jslink((a, \"shared_target\"), (b, \"shared_target\"))\n", - "widgets.jslink((b, \"shared_target\"), (c, \"shared_target\"))\n", + "widgets.jslink((a, \"_target\"), (b, \"_target\"))\n", + "widgets.jslink((b, \"_target\"), (c, \"_target\"))\n", "\n", "# synchronize FoV (zoom level) between 3 widgets\n", - "widgets.jslink((a, \"shared_fov\"), (b, \"shared_fov\"))\n", - "widgets.jslink((b, \"shared_fov\"), (c, \"shared_fov\"))\n", + "widgets.jslink((a, \"_fov\"), (b, \"_fov\"))\n", + "widgets.jslink((b, \"_fov\"), (c, \"_fov\"))\n", "\n", "items = [a, b, c]\n", "\n", diff --git a/js/models/event_handler.js b/js/models/event_handler.js index 40bac042..3c88e812 100644 --- a/js/models/event_handler.js +++ b/js/models/event_handler.js @@ -31,45 +31,55 @@ export default class EventHandler { // is also necessary for the field of view. /* Target control */ - let targetLock = new Lock(); + const jsTargetLock = new Lock(); + const pyTargetLock = new Lock(); // Event triggered when the user moves the map in Aladin Lite - this.aladin.on("positionChanged", () => { - if (targetLock.locked) { - targetLock.unlock(); + this.aladin.on("positionChanged", (position) => { + if (pyTargetLock.locked) { + pyTargetLock.unlock(); return; } - targetLock.lock(); - const raDec = this.aladin.getRaDec(); + jsTargetLock.lock(); + const raDec = [position.ra, position.dec]; + // const raDec = this.aladin.getRaDec(); this.model.set("_target", `${raDec[0]} ${raDec[1]}`); - this.model.set("shared_target", `${raDec[0]} ${raDec[1]}`); this.model.save_changes(); }); - // Event triggered when the target is changed from the Python side using jslink - this.model.on("change:shared_target", () => { - const target = this.model.get("shared_target"); + this.model.on("change:_target", () => { + if (jsTargetLock.locked) { + jsTargetLock.unlock(); + return; + } + pyTargetLock.lock(); + let target = this.model.get("_target"); const [ra, dec] = target.split(" "); this.aladin.gotoRaDec(ra, dec); }); /* Field of View control */ - let fovLock = new Lock(); + const jsFovLock = new Lock(); + const pyFovLock = new Lock(); this.aladin.on("zoomChanged", (fov) => { - if (fovLock.locked) { - fovLock.unlock(); + if (pyFovLock.locked) { + pyFovLock.unlock(); return; } - fovLock.lock(); + jsFovLock.lock(); // fov MUST be cast into float in order to be sent to the model this.model.set("_fov", parseFloat(fov.toFixed(5))); - this.model.set("shared_fov", parseFloat(fov.toFixed(5))); this.model.save_changes(); }); - this.model.on("change:shared_fov", () => { - let fov = this.model.get("shared_fov"); + this.model.on("change:_fov", () => { + if (jsFovLock.locked) { + jsFovLock.unlock(); + return; + } + pyFovLock.lock(); + let fov = this.model.get("_fov"); this.aladin.setFoV(fov); }); @@ -186,8 +196,8 @@ export default class EventHandler { * There is no need to unsubscribe from the Aladin Lite events. */ unsubscribeAll() { - this.model.off("change:shared_target"); - this.model.off("change:fov"); + this.model.off("change:_target"); + this.model.off("change:_fov"); this.model.off("change:height"); this.model.off("change:coo_frame"); this.model.off("change:survey"); diff --git a/js/utils.js b/js/utils.js index b78bfdd6..746ba336 100644 --- a/js/utils.js +++ b/js/utils.js @@ -28,18 +28,16 @@ class Lock { /** * Locks the object - * @returns {boolean} True if the object was locked, false otherwise */ unlock() { - return false; + this.locked = false; } /** * Unlocks the object - * @returns {boolean} True if the object was unlocked, false otherwise */ lock() { - return true; + this.locked = true; } } diff --git a/src/ipyaladin/aladin.py b/src/ipyaladin/aladin.py index bf13df64..330ea84f 100644 --- a/src/ipyaladin/aladin.py +++ b/src/ipyaladin/aladin.py @@ -45,22 +45,12 @@ class Aladin(anywidget.AnyWidget): " Its public version is the 'target' property that returns an " "`~astropy.coordinates.SkyCoord` object", ).tag(sync=True, init_option=True) - shared_target = Unicode( - "0 0", - help="A trait that can be used with `~ipywidgets.widgets.jslink`" - "to link two Aladin Lite widgets targets together", - ).tag(sync=True) _fov = Float( 60.0, help="A private trait that stores the current field of view of the widget." " Its public version is the 'fov' property that returns an " "`~astropy.units.Angle` object", ).tag(sync=True, init_option=True) - shared_fov = Float( - 60.0, - help="A trait that can be used with `~ipywidgets.widgets.jslink`" - "to link two Aladin Lite widgets field of view together", - ).tag(sync=True) survey = Unicode("https://alaskybis.unistra.fr/DSS/DSSColor").tag( sync=True, init_option=True )