 TUPLE: editor < line-gadget
     caret-color
     caret mark
-    focused? blink blink-timer ;
+    focused? blink blink-timer
+    default-text ;
 
 <PRIVATE
 

M: model-field model-changed
    nip [ editor>> editor-string ] [ field-model>> ] bi set-model ;

TUPLE: action-editor < editor default-text ;

: <action-editor> ( -- editor )
    action-editor new-editor ;

<PRIVATE

: draw-default-text? ( editor -- ? )
    { [ default-text>> ] [ model>> doc-string empty? ] } 1&& ;

: draw-default-text ( editor -- )
    [ font>> clone line-color >>foreground ]
    [ default-text>> ] bi draw-text ;

PRIVATE>

M: action-editor draw-gadget*
    dup draw-default-text? [
        [ draw-default-text ] [ draw-caret ] bi
    ] [
        call-next-method
    ] if ;

M: action-editor pref-dim*
    dup draw-default-text? [
        [ font>> ] [ default-text>> ] bi text-dim { 1 0 } v+
    ] [
        call-next-method
    ] if ;

TUPLE: action-field < field quot ;

: <action-field> ( quot: ( string -- ) -- gadget )
    action-field [ <action-editor> ] dip new-border
        dup gadget-child >>editor
        field-theme
        swap >>quot ;

M: action-field default-text>> editor>> default-text>> ;

M: action-field default-text<< editor>> default-text<< ;

: invoke-action-field ( field -- )
    [ editor>> editor-string ]

    [ draw-selected-line ] [ nip draw-unselected-line ] if ;

M: editor draw-gadget*
    dup draw-default-text? [
        [ draw-default-text ] [ draw-caret ] bi
    ] [
        dup compute-selection selected-lines [
            [ draw-lines ] [ draw-caret ] bi
        ] with-variable
    ] if ;

M: editor pref-dim*
    ! Add some space for the caret.
    [ font>> ] keep dup draw-default-text?
    [ default-text>> ] [ control-value ] if
    text-dim { 1 0 } v+ ;

M: editor baseline font>> font-metrics ascent>> ;

