Move repl / inspector to new imgui

* make views pin scrolling to bottom
* support multiline labels (no wordwrap yet)
* always expand groups (don't clear after populating form)
This commit is contained in:
Jeremy Penner 2021-12-25 13:26:00 -06:00
parent 45e78e298e
commit e6eee86a91
4 changed files with 59 additions and 79 deletions

View file

@ -27,7 +27,9 @@
:view self} :view self}
(or ?overrides {}))) (or ?overrides {})))
(fn view.end-scroll [self {: y : h}] (fn view.end-scroll [self {: y : h}]
(set self.scrollheight (- (+ y (or h 0) style.padding.y) (+ self.position.y style.padding.y (- self.scroll.y))))) (let [pin-to-bottom (>= self.scroll.to.y (- self.scrollheight self.size.y))]
(set self.scrollheight (- (+ y (or h 0) style.padding.y) (+ self.position.y style.padding.y (- self.scroll.y))))
(when pin-to-bottom (set self.scroll.to.y (- self.scrollheight self.size.y)))))
(fn view.draw [self] (fn view.draw [self]
(set self.cursor nil) (set self.cursor nil)
(self.__index.draw self) (self.__index.draw self)
@ -127,7 +129,10 @@
(fn [form coord-key size-key] (fn [form coord-key size-key]
(let [coord-group (. group coord-key) size-group (. group size-key) (let [coord-group (. group coord-key) size-group (. group size-key)
coord-form (. form coord-key) size-form (. form size-key)] coord-form (. form coord-key) size-form (. form size-key)]
(if (= coord-group nil) ; container takes on the size of its first item (if (= size-form nil) ; tried to add an unsized value to the group, ignore
nil
(= coord-group nil) ; container takes on the size of its first item
(do (tset group coord-key coord-form) (do (tset group coord-key coord-form)
(tset group size-key size-form)) (tset group size-key size-form))
@ -146,9 +151,7 @@
(update-dimensions ?form) (update-dimensions ?form)
(table.unpack result)) (table.unpack result))
[:table form] (update-dimensions form) [:table form] (update-dimensions form)
[:nil] (do (lume.extend orig-form group) [:nil] (lume.extend orig-form group)))))
(lume.clear group)
orig-form)))))
(fn horiz-wrapper [{:x orig-x :w orig-w}] (fn horiz-wrapper [{:x orig-x :w orig-w}]
(fn [{: x : y : w : h : xpad : ypad &as form} overrides] (fn [{: x : y : w : h : xpad : ypad &as form} overrides]
@ -164,14 +167,16 @@
(and (active? view tag) (= view.imstate.left :released) (mouse-inside x y w h))) (and (active? view tag) (= view.imstate.left :released) (mouse-inside x y w h)))
(fn label [form text] (fn label [form text]
(let [{: x : y : w : h : halign : valign : font : color} (let [(_ newlines) (text:gsub "\n" "\n")
text-height (fn [font] (* (font:get_height) (+ newlines 1)))
{: x : y : w : h : halign : valign : font : color}
(with-style form (with-style form
:w #($1.font:get_width text) :w #($1.font:get_width text)
:h #($1.font:get_height) :h #(text-height $1.font)
:halign :left :halign :left
:valign :center) :valign :center)
x (match halign :left x :center (+ x (/ (- w (font:get_width text)) 2)) :right (+ x w (- (font:get_width text)))) x (match halign :left x :center (+ x (/ (- w (font:get_width text)) 2)) :right (+ x w (- (font:get_width text))))
y (match valign :top y :center (+ y (/ (- h (font:get_height)) 2)) :bottom (+ y h (- (font:get_height))))] y (match valign :top y :center (+ y (/ (- h (text-height font)) 2)) :bottom (+ y h (- (text-height font))))]
(renderer.draw_text font text x y color))) (renderer.draw_text font text x y color)))
(fn textbutton [form label] (fn textbutton [form label]

View file

@ -2,23 +2,22 @@
(local fennel (require :lib.fennel)) (local fennel (require :lib.fennel))
(local style (require :core.style)) (local style (require :core.style))
(local lume (require :lib.lume)) (local lume (require :lib.lume))
(local {: textbutton} (util.require :editor.imstate)) (local {: textbutton : under : group-wrapper} (util.require :editor.imgui))
(local {: inspect} (util.require :inspector)) (local {: inspect} (util.require :inspector))
(local repl (util.hot-table ...)) (local repl (util.hot-table ...))
(fn repl.inspector [{: vals : states} view x y] (fn repl.inspector [{: w &as form} {: vals : states}]
(var h 0) (let [g (group-wrapper form)]
(each [i v (ipairs vals)] (each [i v (ipairs vals)]
(set h (+ h (inspect (. states i) v view x (+ y h) view.size.x)))) (g #(inspect $...) (under (g) {: w}) (. states i) v))
(+ h style.padding.y)) (g)))
(fn repl.notify [listeners line] (fn repl.notify [listeners line]
(each [_ listener (ipairs listeners)] (each [_ listener (ipairs listeners)]
(listener:append line))) (listener:append line)))
(fn repl.mk-result [vals] (fn repl.mk-result [vals]
(local inspector #(repl.inspector $...)) {:draw repl.inspector : vals :states (icollect [_ (ipairs vals)] {})})
{:draw inspector : vals :states (icollect [_ (ipairs vals)] {})})
(fn repl.run [{: listeners}] (fn repl.run [{: listeners}]
(fennel.repl {:readChunk coroutine.yield (fennel.repl {:readChunk coroutine.yield

View file

@ -1,5 +1,5 @@
(local util (require :lib.util)) (local util (require :lib.util))
(local {: attach-imstate : textbox : textbutton : mouse-inside} (util.require :editor.imstate)) (local {: attach-imstate : textbox : textbutton : label : under : reform : group-wrapper : mouse-inside} (util.require :editor.imgui))
(local View (require :core.view)) (local View (require :core.view))
(local style (require :core.style)) (local style (require :core.style))
@ -25,44 +25,34 @@
(fn ReplView.append [self line] (fn ReplView.append [self line]
(table.insert self.log line)) (table.insert self.log line))
(fn ReplView.draw-cmd [{: cmd} view x y iline] (fn ReplView.draw-cmd [{: x : y : w : view &as form} {: cmd} iline]
(renderer.draw_text style.font cmd x y style.text) (label form cmd)
(when (mouse-inside x y view.size.x (style.font:get_height)) (when (mouse-inside x y w form.h)
(when (textbutton view :X (+ x view.size.x -35) y) (when (textbutton (reform form {:x (+ x w -35) :into {}}) :X)
(table.remove view.log iline) (table.remove view.log iline)
(table.remove view.log iline)) (table.remove view.log iline))
(when (textbutton view :! (+ x view.size.x -60) y) (when (textbutton (reform form {:x (+ x w -60) :into {}}) :!)
(view:submit cmd))) (view:submit cmd))))
(+ (style.font:get_height) style.padding.y))
(fn ReplView.submit [self ?cmd] (fn ReplView.submit [self ?cmd]
(local cmd (or ?cmd self.cmd)) (local cmd (or ?cmd self.cmd))
(when (= ?cmd nil) (when (= ?cmd nil)
(set self.cmd "")) (set self.cmd ""))
(self:append {:draw #(self.draw-cmd $...) : cmd}) (self:append {:draw self.draw-cmd : cmd})
(self.conn:submit cmd)) (self.conn:submit cmd))
(fn ReplView.draw [self] (fn ReplView.draw [self]
(self:draw_background style.background) (self:draw_background style.background)
(self:draw_scrollbar) (self:draw_scrollbar)
(var x (- self.position.x self.scroll.x)) (let [{: w &as form} (self:form)
(var y (- self.position.y self.scroll.y)) g (group-wrapper form)]
(var rendered-h 0)
; todo: cache sizes and avoid drawing if offscreen? ; todo: cache sizes and avoid drawing if offscreen?
; note: then offscreen items can't be focussed without further effort ; note: then offscreen items can't be focussed without further effort
; todo: draw line numbers ; todo: draw line numbers
(each [i line (ipairs self.log)] (each [i line (ipairs self.log)]
(let [h (line:draw self x y i)] (g line.draw (under (g) {: w}) line i))
(set y (+ y h)) (set self.cmd (g textbox (under (g) {: w :tag :command}) self.cmd))
(set rendered-h (+ rendered-h h)))) (self:end-scroll (g))))
(set self.cmd (textbox self :command self.cmd x y self.size.x))
(local pin-to-bottom (>= self.scroll.to.y (- self.scrollheight self.size.y)))
(set self.scrollheight (+ rendered-h (style.font:get_height) 4))
(when pin-to-bottom
(set self.scroll.to.y (- self.scrollheight self.size.y))))
(fn ReplView.get_name [self] self.title) (fn ReplView.get_name [self] self.title)

View file

@ -1,7 +1,7 @@
(local util (require :lib.util)) (local util (require :lib.util))
(local style (require :core.style)) (local style (require :core.style))
(local {: defmulti : defmethod} (util.require :lib.multimethod)) (local {: defmulti : defmethod} (util.require :lib.multimethod))
(local {: textbutton} (util.require :editor.imstate)) (local {: textbutton : label : under : right-of : reform : group-wrapper } (util.require :editor.imgui))
(local inspector (util.hot-table ...)) (local inspector (util.hot-table ...))
@ -15,7 +15,7 @@
best-inspector) best-inspector)
(set inspector.inspect (set inspector.inspect
(defmulti (fn [state value view x y w] (defmulti (fn [form state value]
(when (= state.inspector nil) (when (= state.inspector nil)
(set state.inspector (inspector.best-inspector value))) (set state.inspector (inspector.best-inspector value)))
state.inspector) :inspect ...)) state.inspector) :inspect ...))
@ -26,43 +26,29 @@
(tset inspector.inspectors name {: predicate : priority :inspector inspect-func}) (tset inspector.inspectors name {: predicate : priority :inspector inspect-func})
(defmethod inspector.inspect name inspect-func)) (defmethod inspector.inspect name inspect-func))
(fn inspector.text-height [text ?font] (inspector.register :default 0 #true (fn [form state value]
(let [font (or ?font style.code_font) (label (reform form {:font style.code_font}) (fv value))))
(_ newlines) (text:gsub "\n" "\n")]
(* (font:get_height) (+ newlines 1))))
(fn inspector.draw-text [font text x y color]
(renderer.draw_text font text x y color)
(inspector.text-height text))
(inspector.register :default 0 #true (fn [state value view x y w]
(inspector.draw-text style.code_font (fv value) x y style.text)))
(inspector.register :table 10 (inspector.register :table 10
#(and (= (type $1) :table) (not= (next $1) nil)) #(and (= (type $1) :table) (not= (next $1) nil))
(fn [state tbl view x y w] (fn [form state tbl]
(local font style.code_font) (let [get-kstate (fn [tbl k state]
(var h 0)
; todo: state assumes an .inspector key
; todo: inspector swapping
; todo: edit in place?
(fn get-kstate [tbl k state]
(when (= nil state.keys) (set state.keys {})) (when (= nil state.keys) (set state.keys {}))
(when (= nil (?. state.keys k)) (when (= nil (?. state.keys k))
(util.nested-tset state [:keys k] {:collapsed (= (type (. tbl k)) :table) :children {}})) (util.nested-tset state [:keys k] {:collapsed (= (type (. tbl k)) :table) :children {}}))
(. state.keys k)) (. state.keys k))
g (group-wrapper form)]
(each [k v (pairs tbl)] (each [k v (pairs tbl)]
(let [kstate (get-kstate tbl k state) (let [kstate (get-kstate tbl k state)]
kstr (fv k) ; todo: state assumes an .inspector key
wk (font:get_width kstr) ; todo: inspector swapping
xoffset (+ wk style.padding.x) ; todo: edit in place?
toggle-collapse (textbutton view kstr x (+ y h)) (when (g textbutton (under form {:font style.code_font}) (fv k))
hv (if kstate.collapsed (set kstate.collapsed (not kstate.collapsed)))
(inspector.draw-text font "..." (+ x xoffset) (+ y h) style.syntax.comment) (if kstate.collapsed
(inspector.inspect kstate.children v view (+ x xoffset) (+ y h) (- w xoffset)))] (g label (right-of form {:color style.syntax.comment :into {}}) "...")
(when toggle-collapse (set kstate.collapsed (not kstate.collapsed))) (g #(inspector.inspect $...) (right-of form {:into {}}) kstate.children v))
(set h (+ h hv style.padding.y)))) (g))))))
h))
inspector.hot inspector.hot