commit d823792ff47ce64aff36a14ff5dbc20be8d6930d
parent c2d9640d988b95672bd2467035a107e113976fa4
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Wed, 7 Dec 2022 22:48:36 -0500
Seventh day
Diffstat:
2 files changed, 62 insertions(+), 10 deletions(-)
diff --git a/README.md b/README.md
@@ -38,3 +38,10 @@ that spawns a window that lets you type text in.
Added a menu bar with some basic actions (File -> [New, Open, Save, Quit]),
(Edit -> [Copy, Cut, Paste]), and (Help -> [About]) to the text editor.
+
+## DEC07
+
+Improved the save features of the text editor: added a Save As and made the New
+option make an entirely new file, instead of just blanking the text area. Also
+added some labels for the current file and some feedback when a file was saved,
+opened, etc.
diff --git a/text-editor/src/text_editor/core.clj b/text-editor/src/text_editor/core.clj
@@ -1,17 +1,30 @@
(ns text-editor.core
(:use seesaw.core
+ seesaw.mig
seesaw.chooser
[clojure.java.io :only [file]]))
; Make the window look like a native app
(native!)
-(def current-file (atom (file "New Document.txt")))
+(def current-file
+ (atom (file "")))
-(when-not (.exists @current-file) (spit @current-file ""))
+(def current-file-label
+ (if (.exists @current-file)
+ (label :text @current-file)
+ (label :text "No file selected")))
+
+(def status-label
+ (label :text "Welcome!"))
+
+(defn set-status [& strings]
+ (text! status-label (apply str strings)))
(def editor
- (editor-pane :id "editor" :text (slurp @current-file)))
+ (if (.exists @current-file)
+ (editor-pane :id "editor" :text (slurp @current-file))
+ (editor-pane :id "editor" :text "")))
(defn set-current-file [f]
(swap! current-file (constantly f)))
@@ -20,11 +33,13 @@
(choose-file editor :type type))
(defn do-new [event]
- (text! editor ""))
+ (set-current-file (file ""))
+ (text! editor "")
+ (set-status "New file"))
(def new-action
(action
- :handler (fn [event] (text! editor ""))
+ :handler do-new
:name "New"
:key "menu N"
:tip "Open a new file."))
@@ -32,7 +47,8 @@
(defn do-open [event]
(let [selected (select-file :open)]
(set-current-file selected))
- (text! editor (slurp @current-file)))
+ (text! editor (slurp @current-file))
+ (set-status "File opened"))
(def open-action
(action
@@ -41,15 +57,32 @@
:key "menu O"
:tip "Open a file."))
+(defn do-save-as [event]
+ (when-let [selected (select-file :save)]
+ (set-current-file selected)
+ (spit @current-file (text editor))
+ (set-status "File saved")))
+
(defn do-save [event]
- (spit @current-file (text editor)))
+ (if-not (.exists @current-file)
+ (do-save-as event)
+ (do
+ (spit @current-file (text editor))
+ (set-status "File saved"))))
(def save-action
(action
:handler do-save
:name "Save"
:key "menu S"
- :tip "Save your work."))
+ :tip "Save the document."))
+
+(def save-as-action
+ (action
+ :handler do-save-as
+ :name "Save As"
+ :key "menu shift S"
+ :tip "Save the document with a different name. "))
(def quit-action
(action
@@ -88,20 +121,32 @@
(def topbar
(menubar :items
- [(menu :text "File" :items [new-action open-action save-action quit-action])
+ [(menu :text "File" :items [new-action open-action save-action save-as-action quit-action])
(menu :text "Edit" :items [copy-action cut-action paste-action])
(menu :text "Help" :items [about-action])]))
+(def main-panel
+ (mig-panel
+ :constraints ["fill, ins 0"]
+ :items [[(scrollable editor) "grow"]
+ [current-file-label "dock south"]
+ [(separator) "dock south"]
+ [status-label "dock south"]]))
+
(defn filemenu []
(value (select menubar [:file])
println "File clicked!"
println value))
(defn -main [& args]
+ (add-watch
+ current-file
+ nil
+ (fn [key ref old new] (text! current-file-label (str new))))
(invoke-later
(-> (frame :title "Text Editor",
:menubar topbar
- :content editor
+ :content main-panel
:minimum-size [640 :by 480]
:on-close :exit)
pack!