     #   Modes
     #
 
+    switchMode: (mode, callback) ->
+        if mode isnt @mode
+            log 'Panel', "mode: #{@mode} => #{mode}"
+            switch mode
+                when HIDE
+                    @hide()
+                when OUTPUT
+                    @show()
+                    @content.show()
+                    @inputBox.hide()
+                when QUERY
+                    @show()
+                    @content.hide()
+                    @inputBox.show()
+                else
+            callback?()
+            @mode = mode
+
+
     query: ->
-        if @mode isnt QUERY
-            @mode = QUERY
-
-            log 'Panel', "query mode"
-            @show()
-            @content.hide()
-            @inputBox.show()
-
+        @switchMode QUERY, =>
             @inputBox.focus()
             @promise = Q.Promise (resolve, reject, notify) =>
                 # confirm

HIDE = 0
OUTPUT = 1
QUERY = 2
IM = 3

class PanelView extends View

    mode: HIDE

    @content: ->
        @div outlet: 'agdaPanel', class: 'agda-panel tool-panel panel-bottom', =>

        return @

    output: ->
        @switchMode OUTPUT
        return @

    activateIM: ->
        if not @IM
            @IM = true
            log 'Panel', "IM on"
            @title.hide()
            @inputMethod.show()
        return @

    deactivateIM: ->
        if @IM
            @IM = false
            log 'Panel', "IM off"
            @title.show()
            @inputMethod.hide()
        return @
