module Manatee.Plugin.Anything.Main where
import Control.Monad.Trans
import DBus.Client hiding (Signal)
import Data.Text.Lazy (Text)
import GHC.Conc
import Graphics.UI.Gtk hiding (Window, windowNew, Frame, frameNew, Signal, Frame, Variant, Action, get)
import Manatee.Core.DBus
import Manatee.Core.Types
import Manatee.Plugin.Anything.AnythingView
import Manatee.Plugin.Anything.Types
import Manatee.Toolkit.General.DBus
import Manatee.Toolkit.General.STM
import Manatee.Toolkit.Gtk.Struct
import System.Posix.Process
import qualified Data.Map as M
anythingMain :: AnythingInteractiveType -> [String] -> IO ()
anythingMain iType names = do
processId <- getProcessID
putStrLn $ "Anyting process (" ++ show processId ++ ") startup."
anythingCache <- newTVarIO M.empty
let clientName = mkRenderClientName processId
client <- mkSessionClientWithName clientName
plug <- plugNew Nothing
plugId <- plugGetId plug
view <- anythingViewNew iType names
plug `containerAdd` avNotebook view
plug `on` deleteEvent $ tryEvent $ liftIO $ do
putStrLn $ "Anything plug disconnect from daemon process, exit process (" ++ show processId ++ ")."
mainQuit
widgetShowAll plug
mkAnythingClient client view
mkRenderMatchRuleUnsafe client (AnythingViewKeyPress, anythingViewHandleKeyPress client view anythingCache)
mkDaemonSignal client NewAnythingProcessConfirm (NewAnythingProcessConfirmArgs (GWindowId plugId) processId)
anythingViewCompletion client anythingCache view "" 0
mainGUI
mkAnythingClient :: Client -> AnythingView -> IO ()
mkAnythingClient client view =
mkRenderMatchRules client
[(AnythingViewChangeCandidate, anythingViewHandleChangeCandidate view)
,(AnythingViewChangeInteractiveType, anythingViewHandleChangeInteractiveType view)]
anythingViewHandleChangeInteractiveType :: AnythingView -> RenderSignalArgs -> IO ()
anythingViewHandleChangeInteractiveType view (AnythingViewChangeInteractiveTypeArgs iType) =
writeTVarIO (avInteractiveType view) iType
anythingViewHandleChangeCandidate :: AnythingView -> RenderSignalArgs -> IO ()
anythingViewHandleChangeCandidate view (AnythingViewChangeCandidateArgs names) = do
candidateTab <- anythingCandidateTabNew names
writeTVarIO (avCandidateTab view) candidateTab
anythingViewHandleKeyPress :: Client -> AnythingView -> TVar AnythingSearchCache -> RenderSignalArgs -> IO ()
anythingViewHandleKeyPress client view anythingCache
(AnythingViewKeyPressArgs
keystoke
allText
unselectText
keyPressId
isChanged
) = do
let input = AnythingViewInput allText unselectText
env = AnythingViewEnvironment view client anythingCache input keyPressId
anythingAction keymap =
case M.lookup keystoke keymap of
Just action ->
postGUIAsync $ anythingViewRunAction env action
Nothing ->
if isChanged
then anythingViewCompletion client anythingCache view unselectText keyPressId
else postGUIAsync $ do
boxHeight <- anythingViewGetBoxHeight view
mkDaemonSignal client AnythingViewOutput (AnythingViewOutputArgs
unselectText
(drop (length unselectText) allText)
boxHeight
keyPressId)
focusCandidateView <- anythingViewIsFocusCandidateTab view
anythingAction $ if focusCandidateView
then anythingCandidateViewKeymap
else anythingCommandViewKeymap
anythingViewRunAction :: AnythingViewEnvironment -> AnythingViewAction -> IO ()
anythingViewRunAction env (AnythingViewAction {anythingViewActionFun = fun}) =
anythingViewExecute env >>= fun
anythingCandidateViewKeymap :: AnythingViewKeymap
anythingCandidateViewKeymap =
M.fromList
["M-j" <=> anythingViewFocusNextCandidate
,"M-k" <=> anythingViewFocusPrevCandidate
,"Down" <=> anythingViewFocusNextCandidate
,"Up" <=> anythingViewFocusPrevCandidate
,"M-J" <=> anythingViewFocusNextCandidateView
,"M-K" <=> anythingViewFocusPrevCandidateView
,"M-m" <=> anythingViewRunFirstCommand
,"Return" <=> anythingViewRunFirstCommand
,"M-/" <=> anythingViewToggleTab
,"M-N" <=> anythingViewExpandSelectedCandidate
]
anythingCommandViewKeymap :: AnythingViewKeymap
anythingCommandViewKeymap =
M.fromList
["M-j" <=> anythingViewFocusNextCommand
,"M-k" <=> anythingViewFocusPrevCommand
,"Down" <=> anythingViewFocusNextCommand
,"Up" <=> anythingViewFocusPrevCommand
,"M-m" <=> anythingViewRunSelectedCommand
,"Return" <=> anythingViewRunSelectedCommand
,"M-/" <=> anythingViewToggleTab
,"M-N" <=> anythingViewExpandSelectedCandidate
]
(<=>) :: forall a . AnythingViewActionArgs a => Text -> (a -> IO ()) -> (Text, AnythingViewAction)
key <=> command = (key, AnythingViewAction command)