Created
November 4, 2014 17:50
-
-
Save danlamanna/d9430099147136e6d417 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns vboxtray.core | |
(:import [java.awt Toolkit] | |
[java.awt.datatransfer StringSelection] | |
[java.net URLClassLoader URL] | |
[java.awt PopupMenu]) | |
(:require [clojure.java.shell :refer [sh]] | |
[clojure.string :only (split)]) | |
(:gen-class)) | |
(defn vm-output->map [vm-str] | |
"Takes a string in the form: | |
\"vm name\" {alphanu-13435-uuid} | |
And converts it to a mapping of the uuid -> vm name." | |
(apply hash-map | |
(reverse (rest (re-matches #"\"(.*)\"\ \{(.*)\}" (clojure.string/trim-newline vm-str)))))) | |
(defn all-vms [] | |
(let [cmdout (:out (sh "VBoxManage" "list" "vms")) | |
vm-strings (clojure.string/split cmdout #"\n")] | |
(zipmap | |
(into [] (map (fn [s] | |
(clojure.string/replace | |
(second (clojure.string/split s #"\ \{")) | |
"}" "")) vm-strings)) | |
(into [] (map #(first (clojure.string/split % #"\ \{")) vm-strings))))) ;; vm uuid | |
(defn add-to-popup-menu [popup-menu items] | |
(doseq [item items] | |
(let [string-or-key (first item) | |
func (last item)] | |
(if (= string-or-key :separator) | |
(.addSeparator popup-menu) | |
(let [menu-item (java.awt.MenuItem. string-or-key)] | |
(.addActionListener menu-item | |
(proxy [ActionListener] [] | |
(actionPerformed [evt] (func)))) | |
(.add popup-menu menu-item)))))) | |
(defn icon->image [icon] | |
(let [w (.getIconWidth icon) | |
h (.getIconHeight icon) | |
img (-> (java.awt.GraphicsEnvironment/getLocalGraphicsEnvironment) | |
(.getDefaultScreenDevice) | |
(.getDefaultConfiguration) | |
(.createCompatibleImage w h)) | |
gfx (.createGraphics img) | |
icon (.paintIcon icon nil gfx 0 0)] | |
(.dispose gfx) | |
img)) | |
(defn remove-all-tray-icons [] | |
(let [tray (java.awt.SystemTray/getSystemTray)] | |
(doseq [i (seq (.getTrayIcons tray))] | |
(.remove tray i)))) | |
(remove-all-tray-icons) | |
(def tray-icon | |
(let [icon (java.awt.TrayIcon. | |
(icon->image (.getIcon (javax.swing.UIManager/getDefaults) "FileView.computerIcon")) | |
#_(.getImage (java.awt.Toolkit/getDefaultToolkit) "tray.gif") | |
"REPL notifications" nil)] | |
(.add (java.awt.SystemTray/getSystemTray) icon) | |
icon)) | |
(defn create-image | |
[path description] | |
(-> (javax.swing.ImageIcon. (clojure.java.io/resource path) description) | |
(.getImage))) | |
(defn make-tray-icon [sys-tray description] | |
(let [tray-icon (java.awt.TrayIcon. icon-neutral description)] | |
(.setImageAutoSize tray-icon true) | |
(.add sys-tray tray-icon) | |
tray-icon)) | |
(def icon-running (create-image "icon.png" "Tray Icon")) | |
(def icon-error (create-image "bh-icon-32px-error.png" "Tray Icon")) | |
(def icon-neutral (create-image "bh-icon-32px-neutral.png" "Tray Icon")) | |
(defn notify | |
"Send a notification to the system tray." | |
[msg & [title type]] | |
(let [type (or type :none) | |
type (or (tray-msg-type-map type) (:none tray-msg-type-map))] | |
(.displayMessage tray-icon title msg type)) | |
msg) | |
(def sys-tray (java.awt.SystemTray/getSystemTray)) | |
(def popup-menu (java.awt.PopupMenu.)) | |
(defn create-system-tray-menu | |
"Take multiple vectors as menu-items. | |
[[\"Exit\" (fn [e] (exit))] | |
[:separator] | |
[\"Open\" (fn [e] (open))]]" | |
[&{:keys [items icons]}] | |
(def tray-icon (make-tray-icon sys-tray "BACnet Help - Stopped")) | |
(add-to-popup-menu popup-menu items) | |
(.setPopupMenu tray-icon popup-menu)) | |
(defn -main | |
"I don't do a whole lot ... yet." | |
[& args] | |
(println "Hello, World!")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment