add invoke-interactively, for use in keymaps
move commander widget to Command.widget
This commit is contained in:
parent
491f3225b0
commit
1c1608fe1f
46
command.fnl
46
command.fnl
@ -2,6 +2,7 @@
|
|||||||
(local { : view } (require :fennel))
|
(local { : view } (require :fennel))
|
||||||
|
|
||||||
(local commands {})
|
(local commands {})
|
||||||
|
|
||||||
(local Buffer (require :buffer))
|
(local Buffer (require :buffer))
|
||||||
|
|
||||||
(fn define-command [name function params]
|
(fn define-command [name function params]
|
||||||
@ -74,8 +75,12 @@
|
|||||||
(do
|
(do
|
||||||
(tset p k input-string)
|
(tset p k input-string)
|
||||||
(state-for-next-param c p))
|
(state-for-next-param c p))
|
||||||
_
|
|
||||||
(do (print "unexpected state " (view state))
|
{:command c :this-param nil :collected-params p}
|
||||||
|
(do
|
||||||
|
(state-for-next-param c p))
|
||||||
|
|
||||||
|
_ (do (print "unexpected state " (view state))
|
||||||
state)
|
state)
|
||||||
)))
|
)))
|
||||||
|
|
||||||
@ -90,13 +95,50 @@
|
|||||||
:default (and param (param))
|
:default (and param (param))
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
(fn update-widget-state [w result]
|
||||||
|
(set w.placeholder_text
|
||||||
|
(or result.prompt ""))
|
||||||
|
(set w.sensitive result.active)
|
||||||
|
(set w.text
|
||||||
|
(or result.default result.error "")))
|
||||||
|
|
||||||
|
(local widget
|
||||||
|
(let [w (Gtk.Entry {
|
||||||
|
:sensitive false
|
||||||
|
})]
|
||||||
|
(tset w :on_activate
|
||||||
|
(fn [event]
|
||||||
|
(update-widget-state w (on-input event.text))))
|
||||||
|
w))
|
||||||
|
|
||||||
(fn activate []
|
(fn activate []
|
||||||
(tset state :active true)
|
(tset state :active true)
|
||||||
|
(set widget.sensitive true)
|
||||||
|
(set widget.text "")
|
||||||
|
(widget:grab_focus)
|
||||||
state)
|
state)
|
||||||
|
|
||||||
|
(fn invoke-interactively [name params]
|
||||||
|
(let [c (find-command name)
|
||||||
|
s {
|
||||||
|
:active true
|
||||||
|
:command c
|
||||||
|
:collected-params params
|
||||||
|
}]
|
||||||
|
(set state s)
|
||||||
|
(activate)
|
||||||
|
(let [r (on-input nil)]
|
||||||
|
(update-widget-state widget r)
|
||||||
|
r)))
|
||||||
|
|
||||||
|
(fn active? [] state.active)
|
||||||
|
|
||||||
{
|
{
|
||||||
: activate
|
: activate
|
||||||
|
: active?
|
||||||
: define-command
|
: define-command
|
||||||
: on-input
|
: on-input
|
||||||
: reset-state
|
: reset-state
|
||||||
|
: invoke-interactively
|
||||||
|
: widget
|
||||||
}
|
}
|
||||||
|
22
frame.fnl
22
frame.fnl
@ -16,9 +16,6 @@
|
|||||||
container (Gtk.Box {
|
container (Gtk.Box {
|
||||||
:orientation Gtk.Orientation.VERTICAL
|
:orientation Gtk.Orientation.VERTICAL
|
||||||
})
|
})
|
||||||
commander (Gtk.Entry {
|
|
||||||
:sensitive false
|
|
||||||
})
|
|
||||||
progress-bar (Gtk.ProgressBar {
|
progress-bar (Gtk.ProgressBar {
|
||||||
:orientation Gtk.Orientation.HORIZONTAL
|
:orientation Gtk.Orientation.HORIZONTAL
|
||||||
:fraction 1.0
|
:fraction 1.0
|
||||||
@ -28,26 +25,28 @@
|
|||||||
:orientation Gtk.Orientation.VERTICAL
|
:orientation Gtk.Orientation.VERTICAL
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
(tset commander :on_activate
|
(tset Command.widget :on_activate
|
||||||
(fn [event]
|
(fn [event]
|
||||||
(let [result (Command.on-input event.text)]
|
(let [result (Command.on-input event.text)]
|
||||||
(set commander.placeholder_text
|
(set Command.widget.placeholder_text
|
||||||
(or result.prompt ""))
|
(or result.prompt ""))
|
||||||
(set commander.sensitive result.active)
|
(set Command.widget.sensitive result.active)
|
||||||
(set commander.text
|
(set Command.widget.text
|
||||||
(or result.default result.error "")))))
|
(or result.default result.error "")))))
|
||||||
|
|
||||||
(tset window :on_key_release_event
|
(tset window :on_key_release_event
|
||||||
(fn [window event]
|
(fn [window event]
|
||||||
|
(when (and (not (Command.active?))
|
||||||
|
(= event.keyval (string.byte "g")))
|
||||||
|
(Command.invoke-interactively
|
||||||
|
"visit-location"
|
||||||
|
{:buffer "main"}))
|
||||||
(when (and event.state.MOD1_MASK
|
(when (and event.state.MOD1_MASK
|
||||||
(= event.keyval (string.byte "x")))
|
(= event.keyval (string.byte "x")))
|
||||||
(set commander.sensitive true)
|
|
||||||
(set commander.text "")
|
|
||||||
(commander:grab_focus)
|
|
||||||
(Command.activate))))
|
(Command.activate))))
|
||||||
|
|
||||||
(doto container
|
(doto container
|
||||||
(: :pack_start commander false false vpad)
|
(: :pack_start Command.widget false false vpad)
|
||||||
(: :pack_start progress-bar false false vpad)
|
(: :pack_start progress-bar false false vpad)
|
||||||
(: :pack_start contentwidget true true vpad))
|
(: :pack_start contentwidget true true vpad))
|
||||||
(window:add container)
|
(window:add container)
|
||||||
@ -56,7 +55,6 @@
|
|||||||
{
|
{
|
||||||
:window window
|
:window window
|
||||||
:buffer nil
|
:buffer nil
|
||||||
:commander commander
|
|
||||||
:content contentwidget
|
:content contentwidget
|
||||||
:show-buffer (fn [self b]
|
:show-buffer (fn [self b]
|
||||||
(each [_ w (pairs (contentwidget:get_children))]
|
(each [_ w (pairs (contentwidget:get_children))]
|
||||||
|
@ -31,3 +31,14 @@
|
|||||||
(catch
|
(catch
|
||||||
x (values nil (view x))))]
|
x (values nil (view x))))]
|
||||||
(assert ok err))
|
(assert ok err))
|
||||||
|
|
||||||
|
(before)
|
||||||
|
(let [(ok err)
|
||||||
|
(match
|
||||||
|
(Command.invoke-interactively
|
||||||
|
"multiply"
|
||||||
|
{:a "7" :b "9"})
|
||||||
|
(where {:active false} (= happened 63)) true
|
||||||
|
x (values nil (.. "wrong answer " (view x) " " (view happened)))
|
||||||
|
nil (values nil "???"))]
|
||||||
|
(assert ok err))
|
||||||
|
Loading…
Reference in New Issue
Block a user