Class for the "squeezed" text buttons used by Squeezer These buttons are displayed inside a Tk Text widget in place of text. A user can then use the button to replace it with the original text, copy the original text to the clipboard or view the original text in a separate window.
| 82 | |
| 83 | |
| 84 | class ExpandingButton(tk.Button): |
| 85 | """Class for the "squeezed" text buttons used by Squeezer |
| 86 | |
| 87 | These buttons are displayed inside a Tk Text widget in place of text. A |
| 88 | user can then use the button to replace it with the original text, copy |
| 89 | the original text to the clipboard or view the original text in a separate |
| 90 | window. |
| 91 | |
| 92 | Each button is tied to a Squeezer instance, and it knows to update the |
| 93 | Squeezer instance when it is expanded (and therefore removed). |
| 94 | """ |
| 95 | def __init__(self, s, tags, numoflines, squeezer): |
| 96 | self.s = s |
| 97 | self.tags = tags |
| 98 | self.numoflines = numoflines |
| 99 | self.squeezer = squeezer |
| 100 | self.editwin = editwin = squeezer.editwin |
| 101 | self.text = text = editwin.text |
| 102 | # The base Text widget is needed to change text before iomark. |
| 103 | self.base_text = editwin.per.bottom |
| 104 | |
| 105 | line_plurality = "lines" if numoflines != 1 else "line" |
| 106 | button_text = f"Squeezed text ({numoflines} {line_plurality})." |
| 107 | tk.Button.__init__(self, text, text=button_text, |
| 108 | background="#FFFFC0", activebackground="#FFFFE0") |
| 109 | |
| 110 | button_tooltip_text = ( |
| 111 | "Double-click to expand, right-click for more options." |
| 112 | ) |
| 113 | Hovertip(self, button_tooltip_text, hover_delay=80) |
| 114 | |
| 115 | self.bind("<Double-Button-1>", self.expand) |
| 116 | if macosx.isAquaTk(): |
| 117 | # AquaTk defines <2> as the right button, not <3>. |
| 118 | self.bind("<Button-2>", self.context_menu_event) |
| 119 | else: |
| 120 | self.bind("<Button-3>", self.context_menu_event) |
| 121 | self.selection_handle( # X windows only. |
| 122 | lambda offset, length: s[int(offset):int(offset) + int(length)]) |
| 123 | |
| 124 | self.is_dangerous = None |
| 125 | self.after_idle(self.set_is_dangerous) |
| 126 | |
| 127 | def set_is_dangerous(self): |
| 128 | dangerous_line_len = 50 * self.text.winfo_width() |
| 129 | self.is_dangerous = ( |
| 130 | self.numoflines > 1000 or |
| 131 | len(self.s) > 50000 or |
| 132 | any( |
| 133 | len(line_match.group(0)) >= dangerous_line_len |
| 134 | for line_match in re.finditer(r'[^\n]+', self.s) |
| 135 | ) |
| 136 | ) |
| 137 | |
| 138 | def expand(self, event=None): |
| 139 | """expand event handler |
| 140 | |
| 141 | This inserts the original text in place of the button in the Text |
no outgoing calls
searching dependent graphs…