Fix warning when opening file

set/clear text should count as a single user action.
github/fork/yochananmarqos/patch-1
Gonçalo Silva 2019-07-25 00:13:26 +01:00
parent 128ce54761
commit 3bb813895e
2 changed files with 10 additions and 7 deletions

View File

@ -445,16 +445,15 @@ class MainWindow(StyledWindow):
self.text_view.clear()
try:
if os.path.exists(filename):
current_file = codecs.open(filename, encoding="utf-8", mode='r')
self.text_view.set_text(current_file.read())
current_file.close()
with codecs.open(filename, encoding="utf-8", mode='r') as current_file:
self.text_view.set_text(current_file.read())
else:
dialog = Gtk.MessageDialog(self,
Gtk.DialogFlags.MODAL | Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.WARNING,
Gtk.ButtonsType.CLOSE,
_("The file you tried to open doesn't exist.\
\nA new file will be created in its place when you save the current one")
\nA new file will be created in its place when you save the current one.")
)
dialog.run()
dialog.destroy()

View File

@ -1,5 +1,6 @@
import gi
from uberwriter.helpers import user_action
from uberwriter.inline_preview import InlinePreview
from uberwriter.text_view_drag_drop_handler import DragDropHandler, TARGET_URI, TARGET_TEXT
from uberwriter.text_view_format_inserter import FormatInserter
@ -124,8 +125,12 @@ class TextView(Gtk.TextView):
return text_buffer.get_text(start_iter, end_iter, False)
def set_text(self, text):
"""Set text and clear undo history"""
text_buffer = self.get_buffer()
text_buffer.set_text(text)
with user_action(text_buffer):
text_buffer.set_text(text)
self.undo_redo.clear()
def can_scroll(self):
return self.scroller.can_scroll()
@ -247,8 +252,7 @@ class TextView(Gtk.TextView):
def clear(self):
"""Clear text and undo history"""
self.get_buffer().set_text('')
self.undo_redo.clear()
self.set_text('')
def smooth_scroll_to(self, mark=None):
"""Scrolls if needed to ensure mark is visible.