clojurecember

My attempt to learn at least a little bit of Clojure each day in December
git clone https://git.sr.ht/~jbauer/clojurecember
Log | Files | Refs | README | LICENSE

commit ec4a8508a08d242df70d76f05c719e71f640f704
parent f2a9de18ab36ce30de4c644663fc46edf93c0bda
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Tue,  6 Dec 2022 19:24:51 -0500

Progress!

Diffstat:
Mtext-editor/src/text_editor/core.clj | 106++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 100 insertions(+), 6 deletions(-)

diff --git a/text-editor/src/text_editor/core.clj b/text-editor/src/text_editor/core.clj @@ -1,14 +1,108 @@ (ns text-editor.core - (:use seesaw.core)) + (:use seesaw.core + seesaw.chooser + [clojure.java.io :only [file]])) ; Make the window look like a native app (native!) +(def current-file (atom (file "New Document.txt"))) + +(when-not (.exists @current-file) (spit @current-file "")) + +(def editor + (editor-pane :id "editor" :text (slurp @current-file))) + +(defn set-current-file [f] + (swap! current-file (constantly f))) + +(defn select-file [type] + (choose-file editor :type type)) + +(defn do-new [event] + (text! editor "")) + +(def new-action + (action + :handler (fn [event] (text! editor "")) + :name "New" + :key "menu N" + :tip "Open a new file.")) + +(defn do-open [event] + (let [selected (select-file :open)] + (set-current-file selected)) + (text! editor (slurp @current-file))) + +(def open-action + (action + :handler do-open + :name "Open" + :key "menu O" + :tip "Open a file.")) + +(defn do-save [event] + (spit @current-file (text editor))) + +(def save-action + (action + :handler do-save + :name "Save" + :key "menu S" + :tip "Save your work.")) + +(def quit-action + (action + :handler (fn [event] (dispose! event)) + :name "Quit" + :key "menu Q" + :tip "Exit the program.")) + +(def copy-action + (action + :handler (fn [event] (.copy editor)) + :name "Copy" + :key "menu C" + :tip "Copy selected text to the clipboard.")) + +(def cut-action + (action + :handler (fn [event] (.cut editor)) + :name "Cut" + :key "menu X" + :tip "Cut selected text from the document into the clipboard.")) + +(def paste-action + (action + :handler (fn [event] (.paste editor)) + :name "Paste" + :key "menu V" + :tip "Paste text from the clipboard into the document.")) + +(def about-action + (action + :handler (fn [event] (alert "Text Editor v0.1.0-SNAPSHOT")) + :name "About" + :key "menu A" + :tip "More information about this program.")) + +(def topbar + (menubar :items + [(menu :text "File" :items [new-action open-action save-action quit-action]) + (menu :text "Edit" :items [copy-action cut-action paste-action]) + (menu :text "Help" :items [about-action])])) + +(defn filemenu [] + (value (select menubar [:file]) + println "File clicked!" + println value)) + (defn -main [& args] - (invoke-later ; Executes the body sometime later on the Swing UI thread - (-> (frame :title "Text Editor", ; Frame makes a new window - :content (editor-pane) ; editor-pane is a JEditorPane + (invoke-later + (-> (frame :title "Text Editor", + :menubar topbar + :content editor :minimum-size [640 :by 480] :on-close :exit) - pack! ; Resize window to fit contents - show!))) ; Show the window + pack! + show!)))