Skip to content

Commit

Permalink
fix: automatically change the mouse mode when the camera is fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
flekschas committed Dec 11, 2024
1 parent 7b6b1f4 commit 4962fe1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
8 changes: 8 additions & 0 deletions jscatter/jscatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ def __init__(
self._camera_view = None
self._camera_is_fixed = False
self._mouse_mode = 'panZoom'
self._mouse_mode_prev = self._mouse_mode
self._axes = True
self._axes_grid = False
self._axes_labels = False
Expand Down Expand Up @@ -3194,6 +3195,12 @@ def camera(

if is_fixed is not UNDEF:
self._camera_is_fixed = is_fixed
if is_fixed:
self._mouse_mode_prev = self._mouse_mode
self._mouse_mode = 'lasso'
else:
self._mouse_mode = self._mouse_mode_prev
self.update_widget('mouse_mode', self._mouse_mode)
self.update_widget('camera_is_fixed', self._camera_is_fixed)

if any_not([target, distance, rotation, view, is_fixed], UNDEF):
Expand Down Expand Up @@ -3529,6 +3536,7 @@ def mouse(self, mode: Optional[Union[MouseModes, Undefined]] = UNDEF):
if mode is not UNDEF:
try:
self._mouse_mode = mode
self._mouse_mode_prev = mode
self.update_widget('mouse_mode', mode)
except:
pass
Expand Down
6 changes: 6 additions & 0 deletions jscatter/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ def show(self):
icon='arrows',
tooltip='Activate pan & zoom',
)
button_pan_zoom.disabled = self.camera_is_fixed
button_lasso = self.create_mouse_mode_toggle_button(
mouse_mode='lasso',
icon='crosshairs',
Expand Down Expand Up @@ -507,6 +508,11 @@ def show(self):
children=[self], layout=widgets.Layout(flex='1', width='auto')
)

def camera_is_fixed_change_handler(change):
button_pan_zoom.disabled = change['new']

self.observe(camera_is_fixed_change_handler, names=['camera_is_fixed'])

return widgets.HBox([buttons, plots])


Expand Down
24 changes: 20 additions & 4 deletions tests/test_jscatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,16 +569,32 @@ def test_point_size_scale(df: pd.DataFrame):

def test_camera_is_fixed(df: pd.DataFrame):
scatter = Scatter(data=df, x='a', y='b')
pan_zoom_button = scatter.show().children[0].children[0]

# Check that by default, the camera is not fixed
assert scatter.widget.camera_is_fixed == False
assert scatter.widget.mouse_mode == 'panZoom'
assert pan_zoom_button.disabled == False

# Test fixing the camera
scatter.camera(is_fixed=True)
assert scatter.widget.camera_is_fixed == True
assert scatter.widget.mouse_mode == 'lasso'
assert pan_zoom_button.disabled == True

# Test unfixing the camera
scatter.camera(is_fixed=False)
assert scatter.widget.camera_is_fixed == False
assert scatter.widget.mouse_mode == 'panZoom'
assert pan_zoom_button.disabled == False

scatter.mouse(mode='lasso')
scatter.camera(is_fixed=True)
scatter.camera(is_fixed=False)
assert scatter.widget.mouse_mode == 'lasso'

scatter_b = Scatter(data=df, x='a', y='b', camera_is_fixed=True)

# Test initializing a Scatter with a fixed camera
assert (
Scatter(data=df, x='a', y='b', camera_is_fixed=True).widget.camera_is_fixed
== True
)
assert scatter_b.widget.camera_is_fixed == True
assert scatter_b.widget.mouse_mode == 'lasso'

0 comments on commit 4962fe1

Please sign in to comment.