├── .gitignore ├── LICENSE ├── README.md ├── project.clj ├── src-java └── clj_thrust │ ├── NullOutputStream.java │ ├── RpcEvent.java │ ├── RpcListener.java │ └── RpcWriter.java └── src └── clj_thrust ├── core.clj ├── menu.clj ├── rpc.clj ├── util.clj └── window.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | .classpath 3 | .project 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | /lib/ 8 | /classes/ 9 | /target/ 10 | /checkouts/ 11 | .lein-deps-sum 12 | .lein-repl-history 13 | .lein-plugins/ 14 | .lein-failures 15 | /.idea/ 16 | *.iml 17 | .nrepl-port 18 | /.repl/ 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | clj-thrust 2 | ========== 3 | 4 | Clojure bindings for [Thrust](https://github.com/breach/thrust) 5 | 6 | Overview 7 | -------- 8 | 9 | clj-thrust allows you to write desktop GUI applications as if you were writing a web app ([Atom](https://github.com/atom/atom) and [Light Table](https://github.com/LightTable/LightTable) are 2 popular applications that follow a similar approach). 10 | 11 | By doing so, it becomes possible to target the desktop and web simultaneously with the same codebase without requiring too much extra effort (of course, this also depends on how you design your project and what kind of dependencies you have). 12 | 13 | Also, since clj-thrust is written in Clojure, it's possible to use it to write applications where both the server and client code are written entirely in Clojure and ClojureScript. And you can do all this with a REPL and live code reloading. 14 | 15 | There's also [Middlebrow](https://github.com/solicode/middlebrow), which exposes a common interface for multiple web-view containers (the outer shell which hosts your web app), allowing you to easily switch hosts/engines with minimal code changes. Middlebrow supports clj-thrust, as well as JavaFX and SWT with their built-in web-view components. 16 | 17 | Getting Started 18 | --------------- 19 | 20 | ### Installation 21 | 22 | Add the following dependency to your `project.clj` file: 23 | 24 | ```clojure 25 | [net.solicode/clj-thrust "0.1.0-SNAPSHOT"] 26 | ``` 27 | 28 | You will also need the Thrust runtime in order to run any programs that use Thrust. You can download it [here](https://github.com/breach/thrust/releases). By default, clj-thrust looks for the runtime in `$HOME/.thrust/`, but it's possible to specify a different location with: 29 | 30 | ```clojure 31 | (create-process :thrust-directory "/path/to/thrust/directory/") 32 | ``` 33 | 34 | ### Examples 35 | 36 | The simplest example illustrating how to use clj-thrust is the following: 37 | 38 | ```clojure 39 | (ns my-app.core 40 | (:require [clj-thrust.core :refer [create-process destroy-process]] 41 | [clj-thrust.window :as w])) 42 | 43 | (let [process (create-process) ; `create-process` also takes path to Thrust directory 44 | window (w/create-window process 45 | :root-url "http://localhost:8080" ; URL to your web app 46 | :size {:width 400 :height 300})] 47 | (w/listen-closed window 48 | (fn [e] 49 | (destroy-process process))) ; Optionally call `(System/exit 0)` here. 50 | (w/show window) 51 | (w/focus window true)) 52 | ``` 53 | 54 | ### Sample Project 55 | 56 | For a more complete example which includes both server and client code, you can take a look at [this sample project](https://github.com/solicode/clj-thrust-example-notepad). It's a very basic Notepad-like application which shows how one might structure an app that uses clj-thrust. 57 | 58 | License 59 | ------- 60 | 61 | Copyright © 2015 Solicode 62 | 63 | Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version. 64 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject net.solicode/clj-thrust "0.1.0-SNAPSHOT" 2 | :description "Clojure bindings for Thrust." 3 | :url "https://github.com/solicode/clj-thrust" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :java-source-paths ["src-java"] 7 | :javac-options ["-target" "1.6" "-source" "1.6"] 8 | :dependencies [[org.clojure/clojure "1.6.0"] 9 | [cheshire "5.5.0"]] 10 | :profiles {:dev {:dependencies [[criterium "0.4.3"]] 11 | :jvm-opts ["-XX:-OmitStackTraceInFastThrow"]} 12 | :1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]} 13 | :1.7 {:dependencies [[org.clojure/clojure "1.7.0-RC1"]]}}) 14 | -------------------------------------------------------------------------------- /src-java/clj_thrust/NullOutputStream.java: -------------------------------------------------------------------------------- 1 | package clj_thrust; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | 6 | /** 7 | * Used for discarding data written into output stream. You can essentially 8 | * think of this as /dev/null for Java output streams. 9 | */ 10 | public class NullOutputStream extends OutputStream { 11 | @Override 12 | public void write(int b) throws IOException { 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src-java/clj_thrust/RpcEvent.java: -------------------------------------------------------------------------------- 1 | package clj_thrust; 2 | 3 | public class RpcEvent { 4 | private String json; 5 | 6 | public RpcEvent(String json) { 7 | this.json = json; 8 | } 9 | 10 | public String getJson() { 11 | return json; 12 | } 13 | 14 | public void setJson(String json) { 15 | this.json = json; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src-java/clj_thrust/RpcListener.java: -------------------------------------------------------------------------------- 1 | package clj_thrust; 2 | 3 | public interface RpcListener { 4 | void received(RpcEvent e); 5 | } 6 | -------------------------------------------------------------------------------- /src-java/clj_thrust/RpcWriter.java: -------------------------------------------------------------------------------- 1 | package clj_thrust; 2 | 3 | import java.io.IOException; 4 | import java.io.Writer; 5 | 6 | public class RpcWriter extends Writer { 7 | public static final String BOUNDARY = "--(Foo)++__THRUST_SHELL_BOUNDARY__++(Bar)--"; 8 | 9 | private StringBuilder buffer; 10 | private RpcListener rpcListener; 11 | 12 | public RpcWriter() { 13 | this(null); 14 | } 15 | 16 | public RpcWriter(RpcListener rpcListener) { 17 | buffer = new StringBuilder(); 18 | lock = buffer; 19 | this.rpcListener = rpcListener; 20 | } 21 | 22 | @Override 23 | public void write(int c) { 24 | buffer.append((char)c); 25 | 26 | if(c == '\n') { 27 | tryDispatch(); 28 | } 29 | } 30 | 31 | @Override 32 | public void write(char[] cbuf, int off, int len) { 33 | if(off < 0 || off > cbuf.length || len < 0 34 | || (off + len) > cbuf.length || (off + len) < 0) { 35 | throw new IndexOutOfBoundsException(); 36 | } 37 | 38 | if(len == 0) { 39 | return; 40 | } 41 | 42 | buffer.append(cbuf, off, len); 43 | 44 | tryDispatch(); 45 | } 46 | 47 | @Override 48 | public void write(String s) { 49 | buffer.append(s); 50 | 51 | tryDispatch(); 52 | } 53 | 54 | @Override 55 | public void write(String s, int off, int len) { 56 | buffer.append(s, off, off + len); 57 | 58 | tryDispatch(); 59 | } 60 | 61 | private void tryDispatch() { 62 | if(rpcListener == null) { 63 | return; 64 | } 65 | 66 | int start = 0; 67 | int boundaryIndex = buffer.indexOf(BOUNDARY); 68 | 69 | while(boundaryIndex >= 0) { 70 | rpcListener.received(new RpcEvent(buffer.substring(start, boundaryIndex))); 71 | 72 | start = boundaryIndex + BOUNDARY.length(); 73 | boundaryIndex = buffer.indexOf(BOUNDARY, start); 74 | } 75 | 76 | if(start > 0) { 77 | buffer.setLength(0); 78 | } 79 | } 80 | 81 | @Override 82 | public void flush() { 83 | } 84 | 85 | @Override 86 | public void close() throws IOException { 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/clj_thrust/core.clj: -------------------------------------------------------------------------------- 1 | (ns clj-thrust.core 2 | (:require [clj-thrust.rpc :as r] 3 | [clj-thrust.util :refer [os]] 4 | [clojure.java.io :as io] 5 | [clojure.string :as s]) 6 | (:import [java.io FileNotFoundException] 7 | [clj_thrust NullOutputStream])) 8 | 9 | (def thrust-executable-name 10 | (case os 11 | :windows "thrust_shell.exe" 12 | :linux "thrust_shell" 13 | :mac "ThrustShell.app/Contents/MacOS/ThrustShell")) 14 | 15 | ; TODO: Check for Thrust shell in the other locations that Thrust bindings authors have chosen? 16 | ; https://github.com/breach/thrust/issues/264 17 | (defn locate-thrust-shell [thrust-directory] 18 | (let [locations [(io/file thrust-directory thrust-executable-name) 19 | (io/file (System/getProperty "user.home") ".thrust" thrust-executable-name)] 20 | found (first (filter #(when % (.exists %)) locations))] 21 | (if found 22 | (str found) 23 | (throw (FileNotFoundException. 24 | (str "Could not locate Thrust shell at locations:\n" 25 | (s/join "\n" (remove nil? locations)) 26 | \newline 27 | "If you do not have a Thrust runtime, you can download one here: https://github.com/breach/thrust/releases")))))) 28 | 29 | (defn create-process [& {:keys [thrust-directory verbose redirect-output-to] :as opts}] 30 | (let [thrust-shell-path (locate-thrust-shell thrust-directory) 31 | thrust-shell (-> (ProcessBuilder. [thrust-shell-path]) (.start)) 32 | pending-requests (atom {}) 33 | current-id (atom 0) 34 | event-handlers (atom {}) 35 | process {:shell thrust-shell 36 | :pending-requests pending-requests 37 | :current-id current-id 38 | :event-handlers event-handlers}] 39 | (future 40 | (io/copy (.getInputStream thrust-shell) (r/rpc-output process))) 41 | 42 | (future 43 | (if verbose 44 | (io/copy (.getErrorStream thrust-shell) (or redirect-output-to *out*)) 45 | ; If you're wondering why this is necessary, this comes straight from the documentation for 46 | ; Java's Process class: 47 | ; 48 | ; "Because some native platforms only provide limited buffer size for standard input and 49 | ; output streams, failure to promptly write the input stream or read the output stream of 50 | ; the subprocess may cause the subprocess to block, or even deadlock." 51 | ; 52 | ; And I was indeed seeing these deadlocks before this line was added. 53 | (io/copy (.getErrorStream thrust-shell) (NullOutputStream.)))) 54 | process)) 55 | 56 | (defn destroy-process [process] 57 | (.destroy (:shell process))) -------------------------------------------------------------------------------- /src/clj_thrust/menu.clj: -------------------------------------------------------------------------------- 1 | (ns clj-thrust.menu 2 | (:require [clj-thrust.rpc :as r])) 3 | 4 | (defn create-menu [process] 5 | (r/execute process 6 | (r/->create :menu) 7 | {:menu-item-id (atom 0)})) 8 | 9 | (defn add-check-item [menu command-id label] 10 | (r/call-get-result :add_check_item menu 11 | {:command-id command-id 12 | :label label})) 13 | 14 | (defn add-radio-item [menu command-id label] 15 | (r/call-get-result :add_radio_item menu 16 | {:command-id command-id 17 | :label label})) 18 | 19 | (defn add-separator [menu] 20 | (r/call-get-result :add_separator menu)) 21 | 22 | (defn set-checked [menu command-id value] 23 | (r/call-get-result :set_checked menu 24 | {:command-id command-id 25 | :value value})) 26 | 27 | (defn set-enabled [menu command-id value] 28 | (r/call-get-result :set_enabled menu 29 | {:command-id command-id 30 | :value value})) 31 | 32 | (defn set-visible [menu command-id value] 33 | (r/call-get-result :set_visible menu 34 | {:command-id command-id 35 | :value value})) 36 | 37 | (defn set-accelerator [menu command-id accelerator] 38 | (r/call-get-result :set_accelerator menu 39 | {:command-id command-id 40 | :accelerator accelerator})) 41 | 42 | (defn add-submenu [menu menu-id label] 43 | (let [menu-item-id (:menu-item-id menu)] 44 | (swap! menu-item-id inc) 45 | (r/call-get-result :add_submenu menu 46 | {:menu-id menu-id 47 | :label label 48 | :command-id @menu-item-id}) 49 | @menu-item-id)) 50 | 51 | (defn clear [menu] 52 | (r/call-get-result :clear menu)) 53 | 54 | (defn popup [menu window-id] 55 | (r/call-get-result :popup menu {:window-id window-id})) 56 | 57 | (defn set-application-menu [menu] 58 | (r/call-get-result :set_application_menu menu)) 59 | 60 | (defn add-item [menu label & {:keys [accelerator]}] 61 | (let [menu-item-id (:menu-item-id menu)] 62 | (swap! menu-item-id inc) 63 | (r/call :add_item menu 64 | {:command-id @menu-item-id 65 | :label label}) 66 | (when accelerator 67 | (set-accelerator menu @menu-item-id accelerator)) 68 | @menu-item-id ; TODO: Possibly return a map here, like {:id id, etc} 69 | )) 70 | 71 | (defn listen-execute [menu handler] 72 | (let [target (:target menu) 73 | k [target "execute"]] 74 | (swap! (get-in menu [:process :event-handlers]) assoc k handler))) -------------------------------------------------------------------------------- /src/clj_thrust/rpc.clj: -------------------------------------------------------------------------------- 1 | (ns clj-thrust.rpc 2 | (:require [cheshire.core :as c] 3 | [clojure.java.io :as io]) 4 | (:import [clj_thrust RpcWriter RpcListener RpcEvent] 5 | [java.io StringReader])) 6 | 7 | (defn rpc-output [process] 8 | (let [{:keys [pending-requests]} process] 9 | (RpcWriter. 10 | (proxy [RpcListener] [] 11 | (received [^RpcEvent rpc-event] 12 | (let [event (c/parse-string (.getJson rpc-event) true)] 13 | (case (:_action event) 14 | "reply" 15 | (let [id (:_id event) 16 | original-request (get @pending-requests id)] 17 | (deliver (:promise original-request) 18 | (merge 19 | (:context original-request) 20 | {:target (get-in event [:_result :_target]) 21 | :process process 22 | :id (:_id event) 23 | :result (:_result event) 24 | :error (:_error event)})) 25 | (swap! pending-requests dissoc id)) 26 | 27 | "event" 28 | (let [target (:_target event) 29 | event-type (:_type event) 30 | k [target event-type] 31 | handler (get @(:event-handlers process) k) 32 | ] 33 | (when handler (handler (assoc (:_event event) :target target))))))))))) 34 | 35 | (defn ->create [type & [args]] 36 | {:_action "create" 37 | :_type (name type) 38 | :_args args}) 39 | 40 | (defn ->call [method target & [args]] 41 | {:_target target 42 | :_action "call" 43 | :_method method 44 | :_args args}) 45 | 46 | (defn execute-async 47 | "Executes an RPC request asynchronously, returning a promise which 48 | will have the response populated when it's ready." 49 | [process request & [context]] 50 | (let [id (swap! (:current-id process) inc) 51 | p {:promise (promise) 52 | :context context} 53 | request (assoc request :_id id)] 54 | (io/copy (StringReader. 55 | (str 56 | (c/generate-string request {:key-fn #(clojure.string/replace (name %) "-" "_")}) 57 | \newline 58 | RpcWriter/BOUNDARY 59 | \newline)) 60 | (.getOutputStream (:shell process))) 61 | (swap! (:pending-requests process) assoc id p) 62 | p)) 63 | 64 | (defn execute [process request & [context]] 65 | @(:promise (execute-async process request context))) 66 | 67 | (defn call [method object & [args]] 68 | (execute (:process object) (->call method (:target object) args))) 69 | 70 | (defn call-get-result [method object & [args]] 71 | (get-in (call method object args) [:response :_result])) 72 | -------------------------------------------------------------------------------- /src/clj_thrust/util.clj: -------------------------------------------------------------------------------- 1 | (ns clj-thrust.util 2 | (:import [java.util Locale])) 3 | 4 | (defonce os 5 | (let [os-name (.toLowerCase (System/getProperty "os.name" "") Locale/ENGLISH)] 6 | (cond 7 | (or (.contains os-name "mac") (.contains os-name "darwin")) 8 | :mac 9 | (.contains os-name "win") 10 | :windows 11 | (.contains os-name "linux") 12 | :linux 13 | :else 14 | :unknown))) 15 | 16 | (defn os? [target-os] 17 | (= os target-os)) -------------------------------------------------------------------------------- /src/clj_thrust/window.clj: -------------------------------------------------------------------------------- 1 | (ns clj-thrust.window 2 | (:require [clj-thrust.rpc :as r])) 3 | 4 | (defn create-window [process & {:keys [root-url size title icon-path has-frame session-id] 5 | :or {root-url "about:blank" 6 | size {:width 400 :height 300} 7 | title "Untitled"}}] 8 | (r/execute process 9 | (r/->create :window 10 | {:root-url root-url 11 | :size size 12 | :title title 13 | :icon-path icon-path 14 | :has-frame has-frame 15 | :session-id session-id}))) 16 | 17 | (defn listen-closed [window handler] 18 | (let [target (:target window) 19 | k [target "closed"]] 20 | (swap! (get-in window [:process :event-handlers]) assoc k handler))) 21 | 22 | (defn listen-blur [window handler] 23 | (let [target (:target window) 24 | k [target "blur"]] 25 | (swap! (get-in window [:process :event-handlers]) assoc k handler))) 26 | 27 | (defn listen-focus [window handler] 28 | (let [target (:target window) 29 | k [target "focus"]] 30 | (swap! (get-in window [:process :event-handlers]) assoc k handler))) 31 | 32 | (defn listen-unresponsive [window handler] 33 | (let [target (:target window) 34 | k [target "unresponsive"]] 35 | (swap! (get-in window [:process :event-handlers]) assoc k handler))) 36 | 37 | (defn listen-responsive [window handler] 38 | (let [target (:target window) 39 | k [target "responsive"]] 40 | (swap! (get-in window [:process :event-handlers]) assoc k handler))) 41 | 42 | (defn listen-worker-crashed [window handler] 43 | (let [target (:target window) 44 | k [target "worker_crashed"]] 45 | (swap! (get-in window [:process :event-handlers]) assoc k handler))) 46 | 47 | (defn listen-remote [window handler] 48 | (let [target (:target window) 49 | k [target "remote"]] 50 | (swap! (get-in window [:process :event-handlers]) assoc k handler))) 51 | 52 | (defn show [window] 53 | (r/call-get-result :show window)) 54 | 55 | (defn close [window] 56 | (r/call-get-result :close window)) 57 | 58 | (defn focus [window focus] 59 | (r/call-get-result :focus window {:focus focus})) 60 | 61 | (defn maximize [window] 62 | (r/call-get-result :maximize window)) 63 | 64 | (defn minimize [window] 65 | (r/call-get-result :minimize window)) 66 | 67 | (defn restore [window] 68 | (r/call-get-result :restore window)) 69 | 70 | (defn set-title [window title] 71 | (r/call-get-result :set_title window {:title title})) 72 | 73 | (defn set-fullscreen [window fullscreen] 74 | (r/call-get-result :set_fullscreen window {:fullscreen fullscreen})) 75 | 76 | (defn set-kiosk [window kiosk] 77 | (r/call-get-result :set_kiosk window {:kiosk kiosk})) 78 | 79 | (defn open-devtools [window] 80 | (r/call-get-result :open_devtools window)) 81 | 82 | (defn close-devtools [window] 83 | (r/call-get-result :close_devtools window)) 84 | 85 | (defn move [window x y] 86 | (r/call-get-result :move window {:x x :y y})) 87 | 88 | (defn resize [window width height] 89 | (r/call-get-result :resize window {:width width :height height})) 90 | 91 | (defn remote [window message] 92 | (r/call-get-result :remote window {:message message})) 93 | 94 | (defn closed? [window] 95 | (r/call-get-result :is_closed window)) 96 | 97 | (defn size [window] 98 | (r/call-get-result :size window)) 99 | 100 | (defn position [window] 101 | (r/call-get-result :position window)) 102 | 103 | (defn maximized? [window] 104 | (r/call-get-result :is_maximized window)) 105 | 106 | (defn minimized? [window] 107 | (r/call-get-result :is_minimized window)) 108 | 109 | (defn fullscreen? [window] 110 | (r/call-get-result :is_fullscreen window)) 111 | 112 | (defn kiosked? [window] 113 | (r/call-get-result :is_kiosked window)) 114 | 115 | (defn devtools-opened? [window] 116 | (r/call-get-result :is_devtools_opened window)) --------------------------------------------------------------------------------