Added files
This commit is contained in:
parent
56daa2423b
commit
5d286f98d7
7 changed files with 172 additions and 10 deletions
91
src/swingbox_clj/core.clj
Normal file
91
src/swingbox_clj/core.clj
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
(ns swingbox-clj.core
|
||||
(:import java.awt.GraphicsConfiguration
|
||||
java.io.File
|
||||
java.net.URI
|
||||
javax.swing.JFrame
|
||||
org.fit.cssbox.swingbox.BrowserPane)
|
||||
(:require [hiccup.core :refer [html]]
|
||||
[markdown.core :as md]
|
||||
))
|
||||
|
||||
(defn window?
|
||||
"True if `x` is a window within the meaning used in this file, i.e.
|
||||
it is a map containing at least the keys `:frame` and `:browser`. such that
|
||||
* The value of `:frame` shall be a top-level JFrame.
|
||||
* The value of `:browser` shall be a BrowserPane."
|
||||
[x]
|
||||
(and
|
||||
(map? x)
|
||||
(instance? javax.swing.JFrame (:frame x))
|
||||
(instance? org.fit.cssbox.swingbox.BrowserPane (:browser x))))
|
||||
|
||||
(defn create-window
|
||||
"Create (but do not show) a top level JFrame window, with this `title` if
|
||||
supplied. The created window will contain a browser pane. Returns a map
|
||||
containing two keys, `:frame` and `:browser`."
|
||||
([] (create-window nil))
|
||||
([title]
|
||||
(let [frame (JFrame. (if title (str title) "Untitled Window"))
|
||||
browser (BrowserPane.)]
|
||||
(.setText browser "") ;; window won't open if content is not added to the
|
||||
;; browser before the browser is added to the frame.
|
||||
;; you can change the content later.
|
||||
(.add frame browser)
|
||||
(.pack frame)
|
||||
{:frame frame
|
||||
:browser browser})))
|
||||
|
||||
(defn set-content
|
||||
"Set the content of the browser in this `window` to this `content,` which may
|
||||
be
|
||||
* a URL or string representation of a URL
|
||||
* a file (java.io.File)
|
||||
* a [hiccup]() structure representing HTML
|
||||
* literally anything else.
|
||||
Returns the `window`."
|
||||
[window content]
|
||||
(if (window? window)
|
||||
(let [browser (:browser window)
|
||||
url (try (.toURL (URI/create (str content))) (catch Exception _ nil))
|
||||
html (try (html content) (catch Exception _ nil))]
|
||||
(cond
|
||||
url
|
||||
(.setPage browser url)
|
||||
html
|
||||
(.setText browser html)
|
||||
:else
|
||||
(case (type content)
|
||||
java.lang.String (.setText browser content)
|
||||
clojure.lang.PersistentVector (.setText browser (html content))
|
||||
java.io.File (.setPage browser (.toURL content))
|
||||
(.setText browser (str content))))
|
||||
window)))
|
||||
|
||||
(defn set-content-markdown
|
||||
"Set the content of this `window` to the markdown read from this `filename`.
|
||||
Return the `window`."
|
||||
[window filename]
|
||||
(set-content window (md/md-to-html-string (slurp filename)))
|
||||
window)
|
||||
|
||||
(defn set-visible
|
||||
"Set this `window` to be visible if `visible?` is `true` or not supplied;
|
||||
else false. Returns the `window`."
|
||||
([window]
|
||||
(set-visible window true))
|
||||
([window visible?]
|
||||
(if
|
||||
(window? window)
|
||||
(.setVisible
|
||||
(:frame window)
|
||||
(true? visible?)))
|
||||
window))
|
||||
|
||||
(defn show-in-window
|
||||
"Show this `content`, in a window with this `title`. Return the window as
|
||||
discussed above."
|
||||
[title content]
|
||||
(set-visible (set-content (create-window title) content)))
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue