-
Notifications
You must be signed in to change notification settings - Fork 1
Context menu 3d #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
4a3d27f
2086384
2fea63e
90e40a1
bb18c32
87fdb0a
84f7111
0a8d87a
4a60e59
45b30ef
5ed834c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -646,6 +646,14 @@ def full_screen_toggle(self): | |
| is_fullscreen = bool(self.window.attributes('-fullscreen')) | ||
| self.window.attributes('-fullscreen', not is_fullscreen) | ||
|
|
||
| def context_menu(self, event, labels=None, actions=None): | ||
| if labels is None or actions is None: | ||
| return | ||
| menu = tk.Menu(self.window, tearoff=0) | ||
| for label, action in zip(labels, actions): | ||
| menu.add_command(label=label, command=action) | ||
| menu.tk_popup(event.guiEvent.x_root, event.guiEvent.y_root) | ||
|
Comment on lines
+649
to
+655
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
|
|
||
| class NavigationToolbar2Tk(NavigationToolbar2, tk.Frame): | ||
| def __init__(self, canvas, window=None, *, pack_toolbar=True): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |
| from matplotlib.backend_bases import ( | ||
| _Backend, FigureCanvasBase, FigureManagerBase, NavigationToolbar2, | ||
| ResizeEvent, TimerBase, _allow_interrupt) | ||
| from Foundation import NSObject | ||
| import AppKit | ||
|
|
||
|
|
||
| class TimerMac(_macosx.Timer, TimerBase): | ||
|
|
@@ -145,6 +147,12 @@ def save_figure(self, *args): | |
| return filename | ||
|
|
||
|
|
||
| class MenuCallback(NSObject): | ||
| def action_(self, sender): | ||
| if hasattr(self, 'callback'): | ||
| self.callback() | ||
|
|
||
|
|
||
| class FigureManagerMac(_macosx.FigureManager, FigureManagerBase): | ||
| _toolbar2_class = NavigationToolbar2Mac | ||
|
|
||
|
|
@@ -161,6 +169,23 @@ def __init__(self, canvas, num): | |
| self.show() | ||
| self.canvas.draw_idle() | ||
|
|
||
| def context_menu(self, event, labels=None, actions=None): | ||
r3kste marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if labels is None or actions is None: | ||
| return | ||
| menu = AppKit.NSMenu.alloc().init() | ||
| self._menu_callbacks = [] | ||
| for label, action in zip(labels, actions): | ||
| target = MenuCallback.alloc().init() | ||
| target.callback = action | ||
| self._menu_callbacks.append(target) | ||
| item = AppKit.NSMenuItem.alloc().initWithTitle_action_keyEquivalent_( | ||
| label, "action:", "" | ||
| ) | ||
| item.setTarget_(target) | ||
| menu.addItem_(item) | ||
| mouse_loc = AppKit.NSEvent.mouseLocation() | ||
| menu.popUpMenuPositioningItem_atLocation_inView_(None, mouse_loc, None) | ||
|
Comment on lines
+186
to
+187
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For other backends, the position is extracted from |
||
|
|
||
| def _close_button_pressed(self): | ||
| Gcf.destroy(self) | ||
| self.canvas.flush_events() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| import textwrap | ||
| import warnings | ||
|
|
||
| import functools | ||
| import numpy as np | ||
|
|
||
| import matplotlib as mpl | ||
|
|
@@ -187,6 +188,8 @@ def __init__( | |
| pseudo_bbox = self.transLimits.inverted().transform([(0, 0), (1, 1)]) | ||
| self._pseudo_w, self._pseudo_h = pseudo_bbox[1] - pseudo_bbox[0] | ||
|
|
||
| self._mouse_moved = False | ||
|
|
||
| # mplot3d currently manages its own spines and needs these turned off | ||
| # for bounding box calculations | ||
| self.spines[:].set_visible(False) | ||
|
|
@@ -1357,6 +1360,7 @@ def clear(self): | |
|
|
||
| def _button_press(self, event): | ||
| if event.inaxes == self: | ||
| self._mouse_moved = False | ||
| self.button_pressed = event.button | ||
| self._sx, self._sy = event.xdata, event.ydata | ||
| toolbar = self.get_figure(root=True).canvas.toolbar | ||
|
|
@@ -1367,6 +1371,23 @@ def _button_press(self, event): | |
|
|
||
| def _button_release(self, event): | ||
| self.button_pressed = None | ||
|
|
||
| if event.button in self._zoom_btn and event.inaxes == self \ | ||
| and not self._mouse_moved: | ||
| canvas = self.get_figure(root=True).canvas | ||
|
|
||
| def draw_lambda(elev, azim): | ||
| self.view_init(elev=elev, azim=azim) | ||
| canvas.draw_idle() | ||
|
|
||
| canvas.manager.context_menu( | ||
| event, | ||
| labels=["XY", "YZ", "XZ"], | ||
| actions=[functools.partial(draw_lambda, elev=90, azim=-90), | ||
| functools.partial(draw_lambda, elev=0, azim=0), | ||
| functools.partial(draw_lambda, elev=0, azim=-90)], | ||
| ) | ||
|
|
||
| toolbar = self.get_figure(root=True).canvas.toolbar | ||
| # backend_bases.release_zoom and backend_bases.release_pan call | ||
| # push_current, so check the navigation mode so we don't call it twice | ||
|
|
@@ -1563,6 +1584,9 @@ def _on_move(self, event): | |
| w = self._pseudo_w | ||
| h = self._pseudo_h | ||
|
|
||
| if (dx**2 + dy**2) > 1e-6: | ||
| self._mouse_moved = True | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this threshold a bit too high?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This threshold is now not too high, but is still noticeable. If I move the cursor slightly, the plot is zoomed as well as the context menu is shown. |
||
| # Rotation | ||
| if self.button_pressed in self._rotate_btn: | ||
| # rotate viewing point | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.