Skip to content

Commit

Permalink
measuring, but no changes yet
Browse files Browse the repository at this point in the history
  • Loading branch information
saikyun committed Aug 11, 2021
1 parent 71b3df3 commit ab868d3
Show file tree
Hide file tree
Showing 12 changed files with 597 additions and 222 deletions.
69 changes: 69 additions & 0 deletions freja/default-layout.janet
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(use ./init-text-areas)
(import ./editor :as e :fresh true)
(import freja/theme :as t)
(import freja/hiccup :as h)
(import freja/events :as e)
(import freja/state)
(use freja/defonce)

(defonce editor-state @{})

(defn text-area-hc
[props & _]

(unless (props :left-state)
(put props :left-state @{}))

(unless (props :right-state)
(put props :right-state @{}))

[:background {:color (t/comp-cols :background)}
[:padding {:left 0 :top 30}
[:row {}
[:block {:weight 1}
[e/editor {:state (props :left-state)
:id :left
:initial-path "measure-stuff.janet"
:open (props :left-open)
:set-open |(e/put! props :left-open $)}]]
[:block {:width 2}]
[:block {:weight 1}
[e/editor @{:state (props :right-state)
:id :right
:initial-path "freja/render_new_gap_buffer.janet"
:open (props :right-open)
:set-open |(e/put! props :right-open $)}]]

#
]]])

(comment
# old way
(defn text-area-hc
[props & _]

[:background {:color (t/comp-cols :background)}
[:padding {:left 0 :top 30}
[:row {}
[:block {:weight 1}
[text-area {:state text-area-state}]]]]
#
])
#
)

(defn init
[]
(def c (h/new-layer
:text-area
text-area-hc
editor-state))

#(e/put! state/focus123 :focus )
)

#
# this will only be true when running load-file inside freja
(when ((curenv) :freja/loading-file)
(print "reiniting :)")
(init))
156 changes: 156 additions & 0 deletions freja/editor.janet
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
(import ./textarea :as ta :fresh true)
(import ./theme :as t)
(import freja/input :as i)
(import freja/events :as e)
(import freja/state)
(import freja/file-handling :as fh)
(import freja/new_gap_buffer :as gb)

(use profiling/profile)

(varfn search
[props]
(p :search
(let [search-term (string (gb/content props))
gb (props :search-target)]
(gb/put-caret gb (if (gb :selection)
(max (gb :selection)
(gb :caret))
(gb :caret)))
(print "searching :O")
(when-let [i (gb/gb-find-forward! gb search-term)]
(-> gb
(gb/put-caret i)
(put :selection (gb/gb-find-backward! gb search-term))
(put :changed-selection true))))))

(varfn search-backwards
[props]
(p :search-back
(let [search-term (string (gb/content props))
gb (props :search-target)]
(gb/put-caret gb (if (gb :selection)
(min (gb :selection)
(gb :caret))
(gb :caret)))
(print "searching bbbb :O")
(when-let [i (gb/gb-find-backward! gb search-term)]
(-> gb
(gb/put-caret i)
(put :selection (gb/gb-find-forward! gb search-term))
(put :changed-selection true))))))


(def file-open-binds
(-> @{}
(table/setproto i/file-open-binds)
(merge-into
@{:escape (fn [props] (:escape props))
:enter (fn [props] (:enter props))})))

(def search-binds
(-> @{}
(table/setproto i/global-keys)
(merge-into
@{:escape |(:escape $)
:enter |(:search $)
:control @{:f |(:search $)
:b |(:search-backwards $)}})))

(defn editor
[props & children]
(def {:open open
:set-open set-open
:state state
:initial-path initial-path
:id id} props)

(assert state "Must define :state")

(unless (state :file-open)
(put state :file-open (ta/default-textarea-state :binds file-open-binds)))

(unless (state :search)
(put state :search (ta/default-textarea-state :binds search-binds)))

(unless (state :editor)
(put state :editor (ta/default-textarea-state))

(when initial-path
(fh/load-file (state :editor) initial-path)))

(def {:file-open file-open
:search search-state
:editor editor-state} state)

(when id
(put editor-state :id id))

(put-in editor-state [:gb :open-file]
(fn [_]
(set-open :file-open)
(e/put! state/focus123 :focus file-open)))

(put-in editor-state [:gb :search]
(fn [_]
(set-open :search)
(e/put! state/focus123 :focus search-state)))

(put-in file-open [:gb :escape]
(fn [props]
(set-open false)
(e/put! state/focus123 :focus editor-state)))

(put-in file-open [:gb :enter]
(fn [props]
(set-open false)
(fh/load-file editor-state (string ((gb/commit! props) :text)))
(e/put! state/focus123 :focus editor-state)))

(put-in search-state [:gb :search-target] (editor-state :gb))

(put-in search-state [:gb :escape]
(fn [props]
(print "ESCAPE!")
(set-open false)
(e/put! state/focus123 :focus editor-state)))

(put-in search-state [:gb :search] search)
(put-in search-state [:gb :search-backwards] search-backwards)

[:block {}
(when-let [c (tracev (props :open))]
[:background {:color :purple}
[:padding {:all 4}
(case c
:file-open
[:row {}
[:text {:size 22
:text "Open: "}]
[ta/textarea {:weight 1
:text/size 22
:height 28
:state file-open}]]

:search
[:row {}
[:text {:size 22
:text "Search: "}]
[ta/textarea {:weight 1
:text/size 22
:height 14
:state search-state}]])]]
#
)

[:background {:color (t/colors :background)}
[:padding {:left 6 :top 6}
[ta/textarea {:text/spacing 0.5
:text/size 20
:text/font "MplusCode"
:text/color (t/colors :text)
:state editor-state
:show-line-numbers true}]]]

#
])
8 changes: 1 addition & 7 deletions freja/frp.janet
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ Emits events when rerendering is needed.
(var delay-left @{})


(def mouse-events {:press :press
:drag :drag
:release :release
:double-click :double-click
:triple-click :triple-click})


(defn handle-keys
[dt]
Expand Down Expand Up @@ -257,7 +251,7 @@ Emits events when rerendering is needed.
(i/handle-scroll-event (self :gb) n)
(put self :event/changed true))))

['(mouse-events (first ev)) _]
['(i/mouse-events (first ev)) _]
(i/handle-mouse-event
(self :gb)
ev
Expand Down
118 changes: 62 additions & 56 deletions freja/hiccup.janet
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

(var children-on-event nil)

(use profiling/profile)

(defn elem-on-event
[e ev]
# traverse children first
Expand Down Expand Up @@ -120,63 +122,67 @@

(def default-hiccup-renderer
{:draw (fn [self dt]
(with-dyns [:text/get-font a/font]
((self :render)
(self :root))))
(when (= :text-area (self :name))
(p :render
(with-dyns [:text/get-font a/font]
((self :render)
(self :root))))))
:on-event (fn [self ev]
(try
(match ev
@{:screen/width w
:screen/height h}
(do
(put self :max-width w)
(put self :max-height h)

(put self :root
(compile-tree
(self :hiccup)
(self :props)
:tags (self :tags)
:max-width (self :max-width)
:max-height (self :max-height)
:text/font (self :text/font)
:text/size (self :text/size)
:old-root (self :root))))

[:dt dt]
(:draw self dt)

'(table? ev)
(do # (print "compiling tree!")
(put self :props ev)
(put self :root
(compile-tree
(self :hiccup)
ev
:tags (self :tags)
:max-width (self :max-width)
:max-height (self :max-height)
:text/font (self :text/font)
:text/size (self :text/size)
:old-root (self :root))))

(handle-ev (self :root) ev))
([err fib]
(print "Error during event:")
(pp ev)
#(print "Hiccup: ")
#(pp ((self :hiccup) (self :props)))
#(print "Full tree:")
#(pp (self :root))
#(if (self :root)
# (do
# (print "Tree: ")
# (ch/print-tree (self :root)))
# (print "(self :root) is nil"))
(debug/stacktrace fib err)
(when (self :remove-layer-on-error)
(print "Removing layer: " (self :name))
(remove-layer (self :name) (self :props))))))})
(p :on-event
(try
(match ev
@{:screen/width w
:screen/height h}
(do
(put self :max-width w)
(put self :max-height h)

(put self :root
(compile-tree
(self :hiccup)
(self :props)
:tags (self :tags)
:max-width (self :max-width)
:max-height (self :max-height)
:text/font (self :text/font)
:text/size (self :text/size)
:old-root (self :root))))

[:dt dt]
(:draw self dt)

'(table? ev)
(do # (print "compiling tree!")
(put self :props ev)
(put self :root
(compile-tree
(self :hiccup)
ev
:tags (self :tags)
:max-width (self :max-width)
:max-height (self :max-height)
:text/font (self :text/font)
:text/size (self :text/size)
:old-root (self :root))))

(p :handle-ev (handle-ev (self :root) ev)))

([err fib]
(print "Error during event:")
(pp ev)
#(print "Hiccup: ")
#(pp ((self :hiccup) (self :props)))
#(print "Full tree:")
#(pp (self :root))
#(if (self :root)
# (do
# (print "Tree: ")
# (ch/print-tree (self :root)))
# (print "(self :root) is nil"))
(debug/stacktrace fib err)
(when (self :remove-layer-on-error)
(print "Removing layer: " (self :name))
(remove-layer (self :name) (self :props)))))))})

(defn new-layer
[name
Expand Down
Loading

0 comments on commit ab868d3

Please sign in to comment.