Repos / mcross / 6b30a998a7
commit 6b30a998a7ce4896a7947f258e08dcdce8620a49
Author: Bùi Thành Nhân <hi@imnhan.com>
Date:   Thu May 14 09:53:13 2020 +0700

    text widget styling and un-break ctrl+c, ctrl+v

diff --git a/README.md b/README.md
index 0d2a282..d346404 100644
--- a/README.md
+++ b/README.md
@@ -5,6 +5,12 @@ # WIP
 
 It currently looks like this: https://junk.imnhan.com/beans.mp4
 
+# Deps
+
+- python3.7+
+- idlelib (it's supposed to be in the standard lib but Ubuntu for example
+  splits it into a separate package)
+
 # Server bugs/surprises
 
 ## Forces gemini:// in request
diff --git a/gui.py b/gui.py
index c357d57..b0ea23b 100644
--- a/gui.py
+++ b/gui.py
@@ -1,13 +1,29 @@
 import sys
-from tkinter import Text, Tk, ttk
+from tkinter import Text, Tk, font, ttk
 
 import client
+from gui_widgets import ReadOnlyText
 
 
 class Model:
     plaintext = "Nothing to see here... yet."
 
 
+def pick_font(names):
+    available = font.families()
+    picked = None
+    for name in names:
+        if name in available:
+            picked = name
+            break
+
+    if not picked:
+        picked = "TkTextFont"
+
+    print("Picked font:", picked)
+    return picked
+
+
 class View:
     model: Model
     address_bar: ttk.Entry
@@ -51,10 +67,28 @@ def __init__(self, root: Tk, model: Model):
         viewport.pack(fill="both", expand=True)
 
         # Viewport content: just do text for now
-        text = Text(viewport)
+        text = ReadOnlyText(viewport)
         self.text = text
         self.render_page()
-        text.pack(fill="both", expand=True)
+        text_font = pick_font(
+            [
+                "Charis SIL",
+                "Source Serif Pro",
+                "Cambria",
+                "Georgia",
+                "DejaVu Serif",
+                "Times New Roman",
+                "Times",
+            ]
+        )
+        text.config(
+            font=(text_font, 13), bg="#fff8dc", fg="black", padx=5, pady=5,
+        )
+        text.pack(side="left", fill="both", expand=True)
+
+        text_scrollbar = ttk.Scrollbar(viewport, command=text.yview)
+        text["yscrollcommand"] = text_scrollbar.set
+        text_scrollbar.pack(side="left", fill="y")
 
         style = ttk.Style()
         if sys.platform == "win32":
@@ -69,10 +103,8 @@ def _on_go(self, ev=None):
             self.go_callback("gemini://" + self.address_bar.get())
 
     def render_page(self):
-        self.text.config(state="normal")
         self.text.delete("1.0", "end")
         self.text.insert("end", self.model.plaintext)
-        self.text.config(state="disabled")
 
 
 class Controller:
diff --git a/gui_widgets.py b/gui_widgets.py
new file mode 100644
index 0000000..c9a8b5a
--- /dev/null
+++ b/gui_widgets.py
@@ -0,0 +1,14 @@
+from idlelib.redirector import WidgetRedirector
+from tkinter import Text
+
+
+# Can't just use a Text widget with state='disabled' because that would
+# also disable Ctrl+C / Ctrl+V.
+# Further low-level reading:
+# https://wiki.tcl-lang.org/page/Read%2Donly+text+widget
+class ReadOnlyText(Text):
+    def __init__(self, *args, **kwargs):
+        Text.__init__(self, *args, **kwargs)
+        self.redirector = WidgetRedirector(self)
+        self.insert = self.redirector.register("insert", lambda *args, **kw: "break")
+        self.delete = self.redirector.register("delete", lambda *args, **kw: "break")