8000 refactored "start typing" and "stop typing" into their own functions … · matplotlib/matplotlib@6160864 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6160864

Browse files
committed
refactored "start typing" and "stop typing" into their own functions to aid readability
1 parent 07949f5 commit 6160864

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

lib/matplotlib/widgets.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ def __init__(self, ax, label, initial='',
680680
self.params_to_disable += [key]
681681

682682
self.text = initial
683-
self.label = ax.text(0.0, 0.5, label,
683+
self.label = ax.text(-0.01, 0.5, label,
684684
verticalalignment='center',
685685
10000 horizontalalignment='right',
686686
transform=ax.transAxes)
@@ -726,7 +726,7 @@ def _make_text_disp(self, string):
726726
def _rendercursor(self):
727727
# this is a hack to figure out where the cursor should go.
728728
# we draw the text up to where the cursor should go, measure
729-
# save its dimensions, draw the real text, then put the cursor
729+
# and save its dimensions, draw the real text, then put the cursor
730730
# at the saved dimensions
731731

732732
widthtext = self.text[:self.cursor_index]
@@ -798,38 +798,51 @@ def _keypress(self, event):
798798
func(self.text)
799799
if key == "enter":
800800
self._notify_submit_observers()
801+
802+
def begin_typing(x):
803+
self.capturekeystrokes = True
804+
#disable command keys so that the user can type without
805+
#command keys causing figure to be saved, etc
806+
self.reset_params = {}
807+
for key in self.params_to_disable:
808+
self.reset_params[key] = rcParams[key]
809+
rcParams[key] = []
810+
#now, we have to figure out where the cursor goes.
811+
#approximate it based on assuming all characters the same length
812+
print(x)
813+
self.cursor_index = len(self.text)
814+
self._rendercursor()
815+
816+
def stop_typing():
817+
notifysubmit = False
818+
# because _notify_submit_users might throw an error in the
819+
# user's code, we only want to call it once we've already done
820+
# our cleanup.
821+
if self.capturekeystrokes:
822+
#since the user is no longer typing,
823+
#reactivate the standard command keys
824+
for key in self.params_to_disable:
825+
rcParams[key] = self.reset_params[key]
826+
notifysubmit = True
827+
self.capturekeystrokes = False
828+
self.cursor.set_visible(False)
829+
self.ax.figure.canvas.draw()
830+
if notifysubmit:
831+
self._notify_submit_observers()
801832

833+
802834
def _click(self, event):
803835
if self.ignore(event):
804836
return
805837
if event.inaxes != self.ax:
806-
notifysubmit = False
807-
# because _notify_submit_users might throw an error in the
808-
# user's code, we only want to call it once we've already done
809-
# our cleanup.
810-
if self.capturekeystrokes:
811-
for key in self.params_to_disable:
812-
rcParams[key] = self.reset_params[key]
813-
notifysubmit = True
814-
self.capturekeystrokes = False
815-
self.cursor.set_visible(False)
816-
self.ax.figure.canvas.draw()
817-
818-
if notifysubmit:
819-
self._notify_submit_observers()
838+
self.stop_typing()
820839
return
821840
if not self.eventson:
822841
return
823842
if event.canvas.mouse_grabber != self.ax:
824843
event.canvas.grab_mouse(self.ax)
825844
if not(self.capturekeystrokes):
826-
self.capturekeystrokes = True
827-
self.reset_params = {}
828-
for key in self.params_to_disable:
829-
self.reset_params[key] = rcParams[key]
830-
rcParams[key] = []
831-
self.cursor_index = len(self.text)
832-
self._rendercursor()
845+
self.begin_typing(exent.x)
833846

834847
def _motion(self, event):
835848
if self.ignore(event):

0 commit comments

Comments
 (0)
0