Skip to content

Commit

Permalink
use Carry lib
Browse files Browse the repository at this point in the history
  • Loading branch information
metametadata committed May 23, 2016
1 parent 67088c5 commit 4e530cd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 88 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ To package for Mac OS (experimental, requires pyinvoke):

## Architecture

- Model-View-Signal-Action architecture similar to the one from [reagent-mvsa](https://github.com/metametadata/reagent-mvsa)
- Unidirectional data flow using [Carry](https://github.com/metametadata/carry/)
- [Seesaw](https://github.com/daveray/seesaw) for UI
- [Reloaded workflow] (http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded) for easier running from REPL
- Seesaw for UI

## TODO
- feature: ! and !!! priority suffixes - e.g by pressing hotkey several times
Expand Down
1 change: 1 addition & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(defproject pickings "0.2.0-SNAPSHOT"
:description "Press global hotkey to append clipboard text into file."
:dependencies [[org.clojure/clojure "1.8.0"]
[carry "0.3.0"]
[seesaw "1.4.5"]
[com.stuartsierra/component "0.3.1"]
[org.clojars.houshuang/keymaster-clj "0.1.0"]
Expand Down
19 changes: 9 additions & 10 deletions src/pickings/core.clj
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
(ns pickings.core
(:require [pickings.keylistener :as keylistener]
[pickings.ui :as ui]
[pickings.logic :as logic]
[pickings.mvsa :as mvsa]
[pickings.spec :as spec]
[carry.core :as carry]
[com.stuartsierra.component :as component]
[seesaw.core :as sc]))

(def -app-spec {:init logic/init
:view ui/view
:control logic/control
:reconcile logic/reconcile})

; Shows/hides app window, it's kinda an adapter from MVSA to Component pattern
; Shows/hides app window, it's kinda an adapter from Carry to Component pattern
(defrecord App []
component/Lifecycle
(start [this]
(let [new-this (merge this
(mvsa/connect-seesaw (-> -app-spec mvsa/wrap-log) []))]
(let [app (carry/app spec/spec)
view (ui/view (:model app) (:dispatch-signal app))
new-this (-> this
(merge app)
(assoc :view view))]
((:dispatch-signal app) :on-start)
(sc/show! (:view new-this))
new-this))

Expand Down
57 changes: 0 additions & 57 deletions src/pickings/mvsa.clj

This file was deleted.

43 changes: 25 additions & 18 deletions src/pickings/logic.clj → src/pickings/spec.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(ns pickings.logic
(ns pickings.spec
(:require [clojure.core.match :refer [match]]
[clojure.java.io :as io])
(:import (java.io BufferedInputStream)
(javazoom.jl.player Player)))

;;; Model
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn -file-path
"App stores files in user's home folder."
[filename]
Expand All @@ -28,13 +28,11 @@
(spit -config-path (pr-str config))
config)

;;; Init
(defn init
[]
(def -initial-model
{:file (-file-path "pickings.txt")
:delimeter "\n--\n\n"})

;;; Control
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn -beep
"Inspired by code from https://github.com/technomancy/lein-play."
[]
Expand All @@ -43,37 +41,46 @@
Player.
.play))

(defn control
[model signal dispatch]
(defn -control
[model signal _dispatch-signal dispatch-action]
(println "signal =" (pr-str signal))
(match signal
:on-connect
:on-start
(when-let [loaded-config (-load-config)]
(dispatch [:reset loaded-config]))
(dispatch-action [:reset loaded-config]))

[:on-set-file file]
(-> (dispatch [:set-file file])
-save-config!)
(do
(dispatch-action [:set-file file])
(-save-config! @model))

:on-reveal-file
(->> (.getAbsoluteFile (clojure.java.io/file (:file model)))
(->> (.getAbsoluteFile (clojure.java.io/file (:file @model)))
.getParentFile
(.open (java.awt.Desktop/getDesktop)))

:on-open-file
(->> (.getAbsoluteFile (clojure.java.io/file (:file model)))
(->> (.getAbsoluteFile (clojure.java.io/file (:file @model)))
(.open (java.awt.Desktop/getDesktop)))

[:on-append text]
(let [{:keys [file delimeter]} model]
(let [{:keys [file delimeter]} @model]
(spit file (str (clojure.string/trim text) delimeter) :append true)
(-beep))))

;;; Reconcile
(defn reconcile
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn -reconcile
[model action]
(println " action =" (pr-str action))
(match action
[:reset new-model]
new-model

[:set-file file]
(assoc model :file file)))
(assoc model :file file)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def spec
{:initial-model -initial-model
:control -control
:reconcile -reconcile})
2 changes: 1 addition & 1 deletion src/pickings/ui.clj
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
;delimeter-label (sc/label)
hotkey-label (sc/label)
update-app-frame! (fn [model]
(.setToolTip tray-icon (str "Pickings\n" (:file model)))
(.setToolTip tray-icon (str "Pickings1\n" (:file model)))
(sc/value! file-label (:file model))
;(sc/value! delimeter-label (pr-str (:delimeter model)))
(sc/value! hotkey-label "OS X: command+shift+V; Windows: control+shift+V")
Expand Down

0 comments on commit 4e530cd

Please sign in to comment.