Skip to content

Commit

Permalink
Implemented auto-save
Browse files Browse the repository at this point in the history
- Closed #72
- Closed #63
- Autosave is now implemented!
- Improved text in preferences for what the auto-save interval value means.
- Changed allowed maximum autosave delay to be 10 minutes.
- Changed increments from 15 to 5 seconds to allow more fine-grained control easily.
- Fixed an issue where if the user was selecting text it would cause issues when saving the file.
- Moved markdown auto-open preview to only run if the file was opened correctly.
- Autosave runs recursively with the specified delay on the current tab.
- Autosave interval value can be changed without needing to reopen the program to see the changes.
  • Loading branch information
WhenLifeHandsYouLemons committed Apr 9, 2024
1 parent 8508928 commit 316ec2d
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions Encryptext.pyw
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ class PreferenceWindow(tk.Toplevel):

# Auto-save interval number
self.selected_auto_save_interval_val = tk.IntVar(value=settings["otherSettings"]["autoSaveInterval"])
self.auto_save_interval_label = WrappedLabel(self.pref_window, text="Auto-save interval: ", font=(settings["otherSettings"]["fontStyle"], int(round(11*font_scale_factor))))
self.auto_save_interval_val = ttk.Spinbox(self.auto_save_interval_label, textvariable=self.selected_auto_save_interval_val, from_=1, to=300, increment=15, width=5, font=(settings["otherSettings"]["fontStyle"], int(round(11*font_scale_factor))))
self.auto_save_interval_label = WrappedLabel(self.pref_window, text="Auto-save interval (seconds): ", font=(settings["otherSettings"]["fontStyle"], int(round(11*font_scale_factor))))
self.auto_save_interval_val = ttk.Spinbox(self.auto_save_interval_label, textvariable=self.selected_auto_save_interval_val, from_=1, to=600, increment=5, width=5, font=(settings["otherSettings"]["fontStyle"], int(round(11*font_scale_factor))))

self.auto_save_interval_label.pack(side="top", fill="x", padx=5, anchor="nw")
self.auto_save_interval_val.pack(side="right", padx=20, pady=self.option_pady)
Expand Down Expand Up @@ -444,6 +444,11 @@ def updateTags():

tags_used = textboxes[current_tab].tag_names()
i = 0

# Convert the tuple into a list to remove the "sel" tag
# The "sel" tag caused issues when saving if there was text selected
tags_used = list(tags_used)
tags_used.remove("sel")
for tag in tags_used:
indices = textboxes[current_tab].tag_ranges(tag)
for start, end in zip(indices[::2], indices[1::2]):
Expand Down Expand Up @@ -691,13 +696,15 @@ def openFile(Event=None, current=False, file_path=None):
if len(recent_files) > settings["maxRecentFiles"]:
recent_files.pop()
createMenuBar()
if file_extensions[current_tab] == "md":
global preview_window
try:
preview_window.deiconify()
updatePreview()
except:
preview_window.__init__()

# Open the preview window if a markdown file is opened
if file_extensions[current_tab] == "md":
global preview_window
try:
preview_window.deiconify()
updatePreview()
except:
preview_window.__init__()
else:
text = textboxes[current_tab].get("1.0", tk.END)
textboxes[current_tab].delete("1.0", tk.END)
Expand Down Expand Up @@ -748,6 +755,16 @@ def newFile(Event=None):

updatePreview()

def autoSaveFile():
# Save the file if there is a tab open
current_tab = getCurrentTab()
if current_tab != -1:
saveFile(auto_save=True)

# Recursively run autoSaveFile until program is closed (sys.exit kills all processes)
# Delay time is in milliseconds
root.after(settings["otherSettings"]["autoSaveInterval"]*1000, autoSaveFile)

def saveFile(Event=None, auto_save=False):
current_tab = getCurrentTab()
if current_tab == -1:
Expand Down Expand Up @@ -1458,6 +1475,10 @@ addNewTab()
# The preview window was the focused one before
root.focus_force()

# Set the autosave to start working
if settings["otherSettings"]["autoSave"]:
autoSaveFile()

"""
Menu Bar
"""
Expand Down

0 comments on commit 316ec2d

Please sign in to comment.