From ffd0ec06c9176c38962b1841ef2d1c3d5a806922 Mon Sep 17 00:00:00 2001 From: Bobby Date: Sun, 6 Nov 2022 01:15:27 -0400 Subject: feat: basic text editor and preferences --- src/texty.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ texty.prefs | 1 + 2 files changed, 64 insertions(+) create mode 100644 texty.prefs diff --git a/src/texty.py b/src/texty.py index e9be7d1..4f7f460 100644 --- a/src/texty.py +++ b/src/texty.py @@ -2,4 +2,67 @@ # text editor focused on simplicity and ease of use while being powerful and # extensible. It is a work in progress and is not yet ready for use. +import json +import os import tkinter as tk + +PREFERENCES_FILE = "texty.prefs" +DEFAULT_PREFS = { + "width": 800, + "height": 600, + "x_pos": 0, + "y_pos": 0, +} + + +class Texty(tk.Tk): + def __init__(self): + super().__init__() + self.title("Texty") + self.load_prefs() + self.geometry("{}x{}".format(self.prefs["width"], self.prefs["height"])) + self.minsize(400, 300) + + # set position + if self.prefs["x_pos"] > 0 and self.prefs["y_pos"] > 0: + self.geometry("+{}+{}".format(self.prefs["x_pos"], self.prefs["y_pos"])) + else: + self.geometry( + "+{}+{}".format( + self.winfo_screenwidth() // 2 - self.prefs["width"] // 2, + self.winfo_screenheight() // 2 - self.prefs["height"] // 2, + ) + ) + + self.draw_window() + + def load_prefs(self): + self.prefs = DEFAULT_PREFS + if os.path.exists(PREFERENCES_FILE): + with open(PREFERENCES_FILE) as f: + self.prefs = json.load(f) + else: + with open(PREFERENCES_FILE, "w") as f: + json.dump(DEFAULT_PREFS, f) + + def save_prefs(self): + with open(PREFERENCES_FILE, "w") as f: + json.dump(self.prefs, f) + + def draw_window(self): + self.text = tk.Text(self) + self.text.pack(fill="both", expand=True) + + def on_close(self): + self.prefs["width"] = self.winfo_width() + self.prefs["height"] = self.winfo_height() + self.prefs["x_pos"] = self.winfo_x() + self.prefs["y_pos"] = self.winfo_y() + self.save_prefs() + self.destroy() + + +if __name__ == "__main__": + app = Texty() + app.protocol("WM_DELETE_WINDOW", app.on_close) + app.mainloop() diff --git a/texty.prefs b/texty.prefs new file mode 100644 index 0000000..cbac23a --- /dev/null +++ b/texty.prefs @@ -0,0 +1 @@ +{"width": 1032, "height": 790, "x_pos": 391, "y_pos": 124} \ No newline at end of file -- cgit v1.2.3