Skip to content

Commit 4ec9d8e

Browse files
committed
Fixed the fileStats thread to work with the new popup window from master. Changed a lot of the API for the popup window to make it more thread safe
1 parent bb915ba commit 4ec9d8e

File tree

5 files changed

+97
-35
lines changed

5 files changed

+97
-35
lines changed

app/buffer_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def loadTextBuffer(self, relPath, view):
9393
textBuffer = app.text_buffer.TextBuffer()
9494
textBuffer.view = view
9595
self.renameBuffer(textBuffer, fullPath)
96+
textBuffer.fileStats.setPopupWindow(view.popupWindow)
9697
textBuffer.fileLoad()
9798
self.buffers.append(textBuffer)
9899
if 0:

app/ci_program.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def commandLoop(self):
131131
self.exiting = True
132132
return
133133
if frame[0] == 'popup':
134-
self.changeFocusTo(self.popupWindow)
134+
self.changeFocusTo(self.inputWindow.popupWindow)
135135
callerSemaphore.release()
136136
else:
137137
self.refresh(frame[0], frame[1])
@@ -224,7 +224,7 @@ def commandLoop(self):
224224
self.exiting = True
225225
return
226226
if frame[0] == 'popup':
227-
self.changeFocusTo(self.popupWindow)
227+
self.changeFocusTo(self.inputWindow.popupWindow)
228228
callerSemaphore.release()
229229
else:
230230
self.refresh(frame[0], frame[1])
@@ -295,7 +295,6 @@ def startup(self):
295295
self.debugWindow = None
296296
self.debugUndoWindow = None
297297
self.logWindow = None
298-
self.popupWindow = app.window.PopupWindow(self)
299298
self.paletteWindow = app.window.PaletteWindow(self)
300299
self.inputWindow = app.window.InputWindow(self)
301300
self.zOrder.append(self.inputWindow)

app/cu_editor.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -434,23 +434,9 @@ def reloadBuffer(self):
434434
TODO: Make this reloading a new change that can be appended
435435
to the redo chain so user can undo out of a reloadBuffer call.
436436
"""
437-
mainBuffer = self.view.host.inputWindow.textBuffer
437+
mainBuffer = self.view.host.textBuffer
438438
mainBuffer.fileLoad()
439-
self.changeToMainWindow()
440-
441-
def setOptions(self, options):
442-
"""
443-
This function is used to change the options that are displayed in the
444-
popup window as well as their functions.
445-
446-
Args:
447-
options (dict): A dictionary mapping keys (ints) to its
448-
corresponding action.
449-
450-
Returns;
451-
None.
452-
"""
453-
self.commandSet = options
439+
self.changeToInputWindow()
454440

455441
class PaletteDialogController(app.controller.Controller):
456442
"""."""

app/file_stats.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
# limitations under the License.
1414

1515
import app.background
16+
import app.curses_util
1617
import app.log
1718
import app.prefs
1819
import os
1920
import time
2021
import threading
2122
import app.window
2223

23-
2424
class FileTracker(threading.Thread):
2525
def __init__(self, *args, **keywords):
2626
threading.Thread.__init__(self, *args, **keywords)
@@ -66,10 +66,12 @@ def run(self):
6666
redraw = False
6767
waitOnSemaphore = False
6868
if program:
69-
if self.fileContentChangedSinceCheck():
70-
program.popupWindow.setMessage(
71-
"The file on disk has changed.\nReload file?")
72-
program.popupWindow.controller.callerSemaphore = self.thread.semaphore
69+
if self.fileContentChangedSinceCheck() and self.__popupWindow:
70+
self.__popupWindow.setUpWindow(
71+
message="The file on disk has changed.\nReload file?",
72+
displayOptions=self.__popupDisplayOptions,
73+
controllerOptions=self.__popupControllerOptions)
74+
self.__popupWindow.controller.callerSemaphore = self.thread.semaphore
7375
app.background.bg.put((program, 'popup', self.thread.semaphore))
7476
self.thread.semaphore.acquire() # Wait for popup to load
7577
redraw = True
@@ -143,6 +145,29 @@ def getCurrentFileInfo(self):
143145
"""
144146
return self.currentFileInfo
145147

148+
def setPopupWindow(self, popupWindow):
149+
"""
150+
Sets the file stat's object's reference to the popup window that
151+
it will use to notify the user of any changes.
152+
153+
Args:
154+
popupWindow (PopupWindow): The popup window that this object will use.
155+
156+
Returns:
157+
None.
158+
"""
159+
# The keys that the user can press to respond to the popup window.
160+
self.__popupControllerOptions = {
161+
ord('Y'): popupWindow.controller.reloadBuffer,
162+
ord('y'): popupWindow.controller.reloadBuffer,
163+
ord('N'): popupWindow.controller.changeToInputWindow,
164+
ord('n'): popupWindow.controller.changeToInputWindow,
165+
app.curses_util.KEY_ESCAPE: popupWindow.controller.changeToInputWindow,
166+
}
167+
# The options that will be displayed on the popup window.
168+
self.__popupDisplayOptions = ['Y', 'N']
169+
self.__popupWindow = popupWindow
170+
146171
def setTextBuffer(self, textBuffer):
147172
self.textBuffer = textBuffer
148173

app/window.py

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import app.text_buffer
2525
import app.vi_editor
2626
import bisect
27+
import threading
2728
import os
2829
import sys
2930
import curses
@@ -1193,26 +1194,30 @@ def __init__(self, host):
11931194
self.host = host
11941195
self.controller = app.cu_editor.PopupController(self)
11951196
self.setTextBuffer(app.text_buffer.TextBuffer())
1196-
self.longestLineLength = 0
11971197
self.__message = []
11981198
self.showOptions = True
11991199
# This will be displayed and should contain the keys that respond to user
12001200
# input. This should be updated if you change the controller's command set.
1201-
self.options = []
1201+
self.__displayOptions = []
1202+
# Prevent sync issues from occuring when setting options.
1203+
self.__optionsLock = threading.Lock()
12021204

12031205
def render(self):
12041206
"""
12051207
Display a box of text in the center of the window.
12061208
"""
12071209
maxRows, maxCols = self.host.rows, self.host.cols
1208-
cols = min(self.longestLineLength + 6, maxCols)
1210+
longestLineLength = 0
1211+
if len(self.__message):
1212+
longestLineLength = len(max(self.__message, key=len))
1213+
cols = min(longestLineLength + 6, maxCols)
12091214
rows = min(len(self.__message) + 4, maxRows)
12101215
self.resizeTo(rows, cols)
12111216
self.moveTo(maxRows / 2 - rows / 2, maxCols / 2 - cols / 2)
12121217
color = app.color.get('popup_window')
12131218
for row in range(rows):
12141219
if row == rows - 2 and self.showOptions:
1215-
message = '/'.join(self.options)
1220+
message = '/'.join(self.__displayOptions)
12161221
elif row == 0 or row >= rows - 3:
12171222
self.addStr(row, 0, ' ' * cols, color)
12181223
continue
@@ -1223,27 +1228,73 @@ def render(self):
12231228
spacing2 = cols - lineLength - spacing1
12241229
self.addStr(row, 0, ' ' * spacing1 + message + ' ' * spacing2, color)
12251230

1226-
def setMessage(self, message):
1231+
def __setMessage(self, message):
12271232
"""
12281233
Sets the Popup window's message to the given message.
1229-
message (str): A string that you want to display.
1234+
This function should only be called by setUpWindow.
1235+
1236+
Args:
1237+
message (str): A string that you want to display.
12301238
12311239
Returns:
12321240
None.
12331241
"""
12341242
self.__message = message.split("\n")
1235-
self.longestLineLength = max([len(line) for line in self.__message])
12361243

1237-
def setOptionsToDisplay(self, options):
1244+
def __setDisplayOptions(self, displayOptions):
12381245
"""
12391246
This function is used to change the options that are displayed in the
12401247
popup window. They will be separated by a '/' character when displayed.
1248+
This function should only be called by setUpWindow.
1249+
1250+
Args:
1251+
displayOptions (list): A list of possible keys which the user can press
1252+
and should be responded to by the controller.
1253+
"""
1254+
self.__displayOptions = displayOptions
1255+
1256+
def __setControllerOptions(self, controllerOptions):
1257+
"""
1258+
This function is used to change the options that are displayed in the
1259+
popup window as well as their functions. This function should only be
1260+
called by setUpWindow.
1261+
1262+
Args:
1263+
controllerOptions (dict): A dictionary mapping keys (ints) to its
1264+
corresponding action.
1265+
1266+
Returns;
1267+
None.
1268+
"""
1269+
self.controller.commandSet = controllerOptions
1270+
1271+
def setUpWindow(self, message=None, displayOptions=None,
1272+
controllerOptions=None):
1273+
"""
1274+
Sets up the popup window. You should pass in the following arguments in
1275+
case of any sync issues, even if the values seem to already be set. By
1276+
default, the values will be set to None, meaning that the values will
1277+
not be changed. However, if you wish to set each value to be empty,
1278+
message should be an empty string, displayOptions should be an empty list,
1279+
and controllerOptions should be an empty dictionary.
12411280
12421281
Args:
1243-
options (list): A list of possible keys which the user can press and
1244-
should be responded to by the controller.
1282+
message (str): The string that you want the popup window to display.
1283+
displayOptions (list): The list of strings representing the options
1284+
that will be displayed on the popup window.
1285+
controllerOptions (dict): The mapping of user keypresses to functions.
1286+
1287+
Returns:
1288+
None.
12451289
"""
1246-
self.options = options
1290+
self.__optionsLock.acquire()
1291+
if message is not None:
1292+
self.__setMessage(message)
1293+
if displayOptions is not None:
1294+
self.__setDisplayOptions(displayOptions)
1295+
if controllerOptions is not None:
1296+
self.__setControllerOptions(controllerOptions)
1297+
self.__optionsLock.release()
12471298

12481299
def setTextBuffer(self, textBuffer):
12491300
Window.setTextBuffer(self, textBuffer)

0 commit comments

Comments
 (0)