add gtk stuff to show completions as user types

main
Daniel Barlow 2022-12-30 17:44:53 +00:00
parent 36edd12c6e
commit e4ed51e137
3 changed files with 44 additions and 7 deletions

View File

@ -34,4 +34,9 @@
(tset buffers name b)
b))
:find (fn [term] (. buffers term))
;; will rename this to "find" once we've got rid of the
;; only remaining call to the existing Buffer.find
:match (fn [s] (collect [name buffer (pairs buffers)]
(if (string.find name s)
(values name buffer))))
})

View File

@ -23,8 +23,10 @@
(define-command
"visit-location"
[[:buffer #[$1] (fn [f] f.buffer.name)]
[:url #[$1] #(do "http://www.example.com")]
[[:buffer
Buffer.match
#"main"]
[:url #(doto {} (tset $1 $1)) #(do "http://www.example.com")]
]
(fn [{:url url :buffer buffer}]
(let [b (Buffer.find buffer)] (: b :visit url))))
@ -98,11 +100,30 @@
:default (and param (param.default self.frame))
}))
(fn update-widget-state [{ : entry : prompt} result]
(fn update-widget-state [{ : entry : completions-widget : prompt} result]
(set prompt.label (or result.prompt ""))
(set entry.sensitive result.active)
(if (not result.active)
(completions-widget:hide))
(set entry.text (or result.default result.error "")))
(fn on-input [self str]
(match self.state
{:command c :this-param param-name}
(let [parent self.completions-widget
{ : completer} (. c.params param-name)
completions (completer str)]
(parent:foreach #(parent:remove $1))
(each [text _w (pairs completions)]
(parent:add (Gtk.Button {
:label text
:on_clicked
#(update-widget-state self (self:on-activate text))
})))
(parent:show_all)
)))
(fn activate [{: state : entry : prompt}]
(tset state :active true)
(set entry.sensitive true)
@ -130,21 +151,32 @@
(let [entry (Gtk.Entry {:sensitive false })
prompt (Gtk.Label { :label ""})
box (Gtk.Box {
:orientation Gtk.Orientation.VERTICAL
})
hbox (Gtk.Box {
:orientation Gtk.Orientation.HORIZONTAL
})
completions (Gtk.FlowBox)
self {
:state default-state
: activate
:active? (fn [self] self.state.active)
: on-input
: on-activate
: invoke-interactively
: entry
:widget box
: prompt
: frame
:completions-widget completions
}]
(box:pack_start prompt false false 15)
(box:pack_start entry true true 5)
(hbox:pack_start prompt false false 15)
(hbox:pack_start entry true true 5)
(box:pack_start hbox true false 0)
(box:pack_start completions true true 0)
(tset entry :on_changed
(fn [event]
(self:on-input event.text)))
(tset entry :on_activate
(fn [event]
(let [result (self:on-activate event.text)]

View File

@ -12,8 +12,8 @@
(Command.define-command
"multiply"
[[:a #[$1] #(do "3")]
[:b #[$1] #(do "2")]]
[[:a #{$1 $1} #"3"]
[:b #{$1 $1} #"2"]]
(fn [{: a : b }] (set happened (* (tonumber a) (tonumber b)))))
(before)