am stuck, this approach is not working
This commit is contained in:
parent
712a657846
commit
268ce1fe79
@ -1,9 +1,10 @@
|
||||
(local lgi (require :lgi))
|
||||
(local { : Gtk : Gdk : WebKit2 : cairo } lgi)
|
||||
|
||||
(local inspect (require :inspect))
|
||||
|
||||
(local observable (require :observable))
|
||||
(local Gtk lgi.Gtk)
|
||||
(local Gdk lgi.Gdk)
|
||||
(local WebKit2 lgi.WebKit2)
|
||||
(local cairo lgi.cairo)
|
||||
|
||||
(local cache-dir (.. (os.getenv "HOME") "/.cache/just"))
|
||||
|
||||
@ -17,32 +18,6 @@
|
||||
(.. cache-dir "/cookies.db")
|
||||
WebKit2.CookiePersistentStorage.SQLITE))
|
||||
|
||||
(comment
|
||||
(local app-state
|
||||
{
|
||||
:panes [
|
||||
{
|
||||
:current-uri "https://ex.com"
|
||||
:uri-entry "https://ex.comsdfdg"
|
||||
:title "helo"
|
||||
:snapshot nil ; recent image snapshot for overview
|
||||
:load-progress nil ; between 0 and 1 if loading
|
||||
:index 1
|
||||
}
|
||||
]
|
||||
:visible-pane 1
|
||||
:completions []
|
||||
}))
|
||||
|
||||
(local app-state
|
||||
(observable.new {
|
||||
:panes { 1 {:title "blank"} }
|
||||
:visible-pane nil
|
||||
:completions []
|
||||
}))
|
||||
|
||||
(app-state:observe [] #(print (inspect (app-state:get []))))
|
||||
|
||||
(fn event-bus []
|
||||
(let [subscriptions {}
|
||||
vivify (fn [n v]
|
||||
@ -67,12 +42,12 @@
|
||||
(fn load-easylist-json [store cb]
|
||||
(print "loading easylist from json")
|
||||
(with-open [f (io.open "easylist_min_content_blocker.json" "r")]
|
||||
(let [blocks (f:read "*a")]
|
||||
(store:save "easylist"
|
||||
(lgi.GLib.Bytes blocks)
|
||||
nil
|
||||
(fn [self res]
|
||||
(cb (store:save_finish res)))))))
|
||||
(let [blocks (f:read "*a")]
|
||||
(store:save "easylist"
|
||||
(lgi.GLib.Bytes blocks)
|
||||
nil
|
||||
(fn [self res]
|
||||
(cb (store:save_finish res)))))))
|
||||
|
||||
(fn load-adblocks [content-manager store]
|
||||
(store:fetch_identifiers
|
||||
@ -105,31 +80,27 @@ progress, trough {
|
||||
))
|
||||
|
||||
|
||||
(fn handle-webview-properties [self index pspec bus]
|
||||
(fn handle-webview-properties [self pspec bus]
|
||||
(match pspec.name
|
||||
"uri"
|
||||
(do
|
||||
(app-state:update [:panes index :current-uri] self.uri)
|
||||
(bus:publish self :url-changed self.uri))
|
||||
(bus:publish self :url-changed self.uri)
|
||||
|
||||
"title"
|
||||
(when (> (self.title:len) 0)
|
||||
(app-state:update [:panes index :title] self.title)
|
||||
(bus:publish self :title-changed self.title))
|
||||
(if (> (self.title:len) 0)
|
||||
(bus:publish self :title-changed self.title))
|
||||
|
||||
"estimated-load-progress"
|
||||
(let [progress self.estimated_load_progress]
|
||||
(app-state:update [:panes index :load-progress] progress)
|
||||
(bus:publish self :loading-progress progress))
|
||||
(bus:publish self :loading-progress self.estimated_load_progress)
|
||||
|
||||
"is-loading"
|
||||
(let [loading? self.is_loading]
|
||||
(bus:publish self (if loading? :start-loading :stop-loading))
|
||||
(if (not loading?) (app-state:update [:panes index :load-progress] nil)))
|
||||
(bus:publish self (if self.is_loading :start-loading :stop-loading))
|
||||
))
|
||||
|
||||
(fn new-webview [bus]
|
||||
(let [webview (WebKit2.WebView { })]
|
||||
(let [webview (WebKit2.WebView {
|
||||
:on_notify
|
||||
#(handle-webview-properties $1 $2 bus)
|
||||
})]
|
||||
(load-adblocks webview.user_content_manager content-filter-store)
|
||||
webview))
|
||||
|
||||
@ -228,16 +199,9 @@ progress, trough {
|
||||
(tset tabs i v)
|
||||
(v:show)
|
||||
(set widget.page i)
|
||||
(values v i)))
|
||||
v))
|
||||
new-tab (fn [self]
|
||||
(let [(v i) (add-page (new-webview bus))]
|
||||
(app-state:update [:panes i] {
|
||||
:index i
|
||||
:title "blank"
|
||||
})
|
||||
(tset v
|
||||
:on_notify
|
||||
#(handle-webview-properties $1 i $2 bus))
|
||||
(let [v (add-page (new-webview bus))]
|
||||
(v:load_uri "about:blank")
|
||||
v))
|
||||
tab-overview (Gtk.ScrolledWindow)
|
||||
@ -254,7 +218,6 @@ progress, trough {
|
||||
(bus:subscribe :switch-tab
|
||||
(fn [sender index]
|
||||
(widget:set_current_page index)
|
||||
(app-state:update [:current-pane] index)
|
||||
(let [tab (. tabs index)]
|
||||
(when (and tab tab.uri tab.title)
|
||||
(bus:publish tab :url-changed tab.uri)
|
||||
@ -273,7 +236,6 @@ progress, trough {
|
||||
:current-tab current
|
||||
:widget widget
|
||||
:show-tab-overview (fn []
|
||||
(app-state:update [:current-pane] nil)
|
||||
(widget:set_current_page 0)
|
||||
(bus:publish tab-overview :url-changed false)
|
||||
(bus:publish tab-overview :title-changed "Open tabs"))
|
||||
|
@ -1,76 +0,0 @@
|
||||
(local fennel (require :fennel))
|
||||
(local observable (require :observable))
|
||||
|
||||
(macro expect [form]
|
||||
`(do
|
||||
(assert ,form ,(view [:failed form]))
|
||||
(print ,(view [:passed form]))))
|
||||
|
||||
(macro expect-error [form]
|
||||
`(let [(ok?# msg#) (pcall (fn [] ,form))]
|
||||
(assert (not ok?#) msg#)
|
||||
(print ,(view [:passed (list :error form)]))))
|
||||
|
||||
(local app-state
|
||||
(observable.new
|
||||
{
|
||||
:foo 27
|
||||
:bar 54
|
||||
:nest {
|
||||
:name "beatles"
|
||||
:artists [:john :paul: :ringo :george ]
|
||||
}
|
||||
}))
|
||||
|
||||
|
||||
(expect (= (app-state:get [:foo]) 27))
|
||||
(expect (= (app-state:get [:nest :name]) "beatles"))
|
||||
|
||||
(let [s (observable.new {:foo 43})]
|
||||
;; s:get on a non-leaf returns the subtree. use table.concat
|
||||
;; for comparison of values
|
||||
(expect (= (table.concat (s:get [])) (table.concat {:foo 43}))))
|
||||
|
||||
(let [s (observable.new {:foo 43})]
|
||||
;; update existing entry
|
||||
(s:update [:foo] 84)
|
||||
(expect (= (s:get [:foo]) 84))
|
||||
|
||||
;; create new entry
|
||||
(s:update [:baz] 48)
|
||||
(expect (= (s:get [:baz]) 48))
|
||||
|
||||
;; doesn't create new nested keys
|
||||
(expect-error (s:update [:nonexistent :path] 22)))
|
||||
|
||||
(let [s (observable.new {:foo {:bar 43}})]
|
||||
(var win false)
|
||||
;; observer on leaf
|
||||
(s:observe [:foo :bar] #(set win true))
|
||||
(s:update [:foo :bar] 42)
|
||||
(expect (and win)))
|
||||
|
||||
(let [s (observable.new {:foo {:bar 43}})]
|
||||
(var win false)
|
||||
;; observer on subtree
|
||||
(s:observe [:foo] #(set win true))
|
||||
(s:update [:foo :bar] 42)
|
||||
(expect (and true win)))
|
||||
|
||||
|
||||
|
||||
(let [s (observable.new {:foo {:bar {:baz 43}}})]
|
||||
(var win 0)
|
||||
;; observers on ancestor trees are called after child trees
|
||||
(s:observe [:foo] #(set win (/ win 2)))
|
||||
(s:observe [:foo :bar] #(set win 4))
|
||||
(s:update [:foo :bar :baz] 42)
|
||||
(expect (= win 2)))
|
||||
|
||||
(let [s (observable.new {:foo {:bar {:baz 43}}})]
|
||||
(var win 0)
|
||||
;; multiple observers can live on same subtree
|
||||
(s:observe [:foo :bar] #(set win (+ win 1)))
|
||||
(s:observe [:foo :bar] #(set win (+ win 1)))
|
||||
(s:update [:foo :bar :baz] 42)
|
||||
(expect (= win 2)))
|
@ -1,42 +0,0 @@
|
||||
(local {: view} (require :fennel))
|
||||
|
||||
(fn concat [dest src]
|
||||
(table.move dest 1 (# dest) (# src) src))
|
||||
|
||||
(fn update [data path value vivify?]
|
||||
(let [[first & rest] path]
|
||||
(if (next rest)
|
||||
(do
|
||||
(if (and vivify? (not (. data first)))
|
||||
(tset data first {}))
|
||||
(update (. data first) rest value))
|
||||
(tset data first value))))
|
||||
|
||||
(fn new [data]
|
||||
(let [observers {}
|
||||
key [:obs]]
|
||||
(fn get [data path]
|
||||
(let [[first & rest] path]
|
||||
(if (not first) data
|
||||
(next rest) (get (. data first) rest)
|
||||
(. (or data {}) first))))
|
||||
|
||||
(fn publish [observers path]
|
||||
(let [[first & rest] path
|
||||
os (. (or observers {}) first)]
|
||||
(if (and (next rest) (next os)) (publish os rest))
|
||||
(match os
|
||||
{key list} (each [_ f (pairs list)] (f)))))
|
||||
|
||||
(fn observe [observers path fun]
|
||||
(let [el (get observers path)]
|
||||
(if el
|
||||
(table.insert (. el key) fun)
|
||||
(update observers path {key [fun]} true))))
|
||||
{
|
||||
:observe #(observe observers $2 $3)
|
||||
:update #(do (update data $2 $3) (publish observers $2))
|
||||
:get #(get data $2)
|
||||
}))
|
||||
|
||||
{: new }
|
Loading…
Reference in New Issue
Block a user