Skip to content

Commit

Permalink
🚑 Fix widget crash when moving from Aladin Lite view
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed May 6, 2024
1 parent b5723a6 commit 1060451
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
8 changes: 4 additions & 4 deletions examples/6_Linked-widgets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 29 additions & 19 deletions js/models/event_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down Expand Up @@ -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");
Expand Down
6 changes: 2 additions & 4 deletions js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/ipyaladin/aladin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 1060451

Please sign in to comment.