From 6ad789149036a9e97a9c66860828892efa432bd4 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 26 Dec 2024 00:17:53 +0530 Subject: Add c-hjkl for movement keys --- src/Chelleport.hs | 14 +++++++++----- src/Chelleport/AppState.hs | 11 +++++++---- src/Chelleport/Control.hs | 19 +++++++++++++------ src/Chelleport/Types.hs | 4 ++++ 4 files changed, 33 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/Chelleport.hs b/src/Chelleport.hs index 4b3ed42..be4f3ac 100644 --- a/src/Chelleport.hs +++ b/src/Chelleport.hs @@ -3,8 +3,8 @@ module Chelleport where import Chelleport.AppShell (setupAppShell) import qualified Chelleport.AppState as AppState import Chelleport.Context (initializeContext) -import Chelleport.Control (anyAlphanumeric, anyDigit, checkKey, ctrl, eventToKeycode, key, pressed, released, shift) -import Chelleport.KeySequence (keycodeToInt) +import Chelleport.Control (anyAlphanumeric, anyDigit, checkKey, ctrl, eventToKeycode, hjklDirection, key, pressed, released, shift) +import Chelleport.KeySequence (keycodeToInt, toKeyChar) import Chelleport.Types import Chelleport.Utils ((<||>)) import qualified Chelleport.View @@ -28,7 +28,8 @@ run = do runAppWithCtx ctx = (`runReaderT` ctx) . runAppM eventHandler :: State -> SDL.Event -> Maybe AppAction -eventHandler state event = +eventHandler state event = do + let hjkl = key SDL.KeycodeH <||> key SDL.KeycodeJ <||> key SDL.KeycodeK <||> key SDL.KeycodeL case SDL.eventPayload event of SDL.QuitEvent -> Just ShutdownApp SDL.KeyboardEvent ev @@ -36,11 +37,14 @@ eventHandler state event = | checkKey [key SDL.KeycodeEscape, pressed] ev -> Just ShutdownApp -- : Enable search mode | checkKey [ctrl, key SDL.KeycodeS, pressed] ev -> Just $ SetMode defaultSearchMode - -- : Enable hints mode - | checkKey [ctrl, key SDL.KeycodeH, pressed] ev -> Just $ SetMode defaultHintsMode + -- : Enable hints mode + | checkKey [ctrl, key SDL.KeycodeT, pressed] ev -> Just $ SetMode defaultHintsMode -- , : Search increment next/prev | checkKey [ctrl, key SDL.KeycodeN, pressed] ev -> Just $ IncrementHighlightIndex (stateRepetition state) | checkKey [ctrl, key SDL.KeycodeP, pressed] ev -> Just $ IncrementHighlightIndex (-1 * stateRepetition state) + -- : Movement + | checkKey [ctrl, hjkl, pressed] ev -> + MoveMouseInDirection . hjklDirection <$> toKeyChar (eventToKeycode ev) -- Space / Shift+Space : Left click/chain left click | checkKey [key SDL.KeycodeSpace, pressed] ev -> if shift ev diff --git a/src/Chelleport/AppState.hs b/src/Chelleport/AppState.hs index 25575f1..7687257 100644 --- a/src/Chelleport/AppState.hs +++ b/src/Chelleport/AppState.hs @@ -1,7 +1,7 @@ module Chelleport.AppState (initialState, update) where import Chelleport.AppShell (MonadAppShell (hideWindow, showWindow, shutdownApp)) -import Chelleport.Control (MonadControl (..), directionalIncrement) +import Chelleport.Control (MonadControl (..), directionalIncrement, hjklDirection) import Chelleport.Draw (MonadDraw (windowPosition, windowSize), pointerPositionIncrement, screenPositionFromCellPosition, wordPosition) import Chelleport.KeySequence (findMatchPosition, generateGrid, nextChars, toKeyChar) import Chelleport.OCR (MonadOCR (captureScreenshot), getWordsInImage) @@ -36,9 +36,7 @@ update state@(State {stateMode = ModeHints}) (HandleKeyInput keycode) = do case (toKeyChar keycode, validNextKeys) of (Just keyChar, Just validChars') | stateIsMatched state && keyChar `elem` ("HJKL" :: String) -> do - incr <- pointerPositionIncrement state - let action = IncrementMouseCursor $ directionalIncrement incr keyChar - pure (state, Just action) + pure (state, Just $ MoveMouseInDirection $ hjklDirection keyChar) | keyChar `elem` validChars' -> do let newKeySequence = stateKeySequence state ++ [keyChar] let matchPosition = findMatchPosition newKeySequence $ stateGrid state @@ -115,6 +113,11 @@ update state MouseDragToggle | stateIsDragging state = pure (state {stateIsDragging = False}, Just MouseDragEnd) | otherwise = do pure (state {stateIsDragging = True}, Just MouseDragStart) +-- Apply movement in given direction +update state (MoveMouseInDirection direction) = do + incr <- pointerPositionIncrement state + pure (state, Just $ IncrementMouseCursor $ directionalIncrement incr direction) + -- Move mouse to given position update state (MoveMousePosition (x, y)) = do moveMousePointer (intToCInt x) (intToCInt y) diff --git a/src/Chelleport/Control.hs b/src/Chelleport/Control.hs index 48bdcb2..e7bd418 100644 --- a/src/Chelleport/Control.hs +++ b/src/Chelleport/Control.hs @@ -94,10 +94,17 @@ anyDigit = isKeycodeDigit . eventToKeycode anyAlphanumeric :: SDL.KeyboardEventData -> Bool anyAlphanumeric = isValidKey . eventToKeycode -directionalIncrement :: (CInt, CInt) -> Char -> (Int, Int) +hjklDirection :: Char -> Direction +hjklDirection = \case + 'H' -> DirLeft + 'L' -> DirRight + 'K' -> DirUp + 'J' -> DirDown + _ -> DirUp + +directionalIncrement :: (CInt, CInt) -> Direction -> (Int, Int) directionalIncrement (incX, incY) = \case - 'H' -> (-cIntToInt incX, 0) - 'L' -> (cIntToInt incX, 0) - 'K' -> (0, -cIntToInt incY) - 'J' -> (0, cIntToInt incY) - _ -> undefined + DirLeft -> (-cIntToInt incX, 0) + DirRight -> (cIntToInt incX, 0) + DirUp -> (0, -cIntToInt incY) + DirDown -> (0, cIntToInt incY) diff --git a/src/Chelleport/Types.hs b/src/Chelleport/Types.hs index f526fe8..89b2e3d 100644 --- a/src/Chelleport/Types.hs +++ b/src/Chelleport/Types.hs @@ -62,6 +62,9 @@ defaultAppState = stateMode = ModeHints } +data Direction = DirUp | DirDown | DirLeft | DirRight + deriving (Show, Eq) + data AppAction = ChainMouseClick MouseButtonType | HandleKeyInput SDL.Keycode @@ -70,6 +73,7 @@ data AppAction | MouseDragEnd | MouseDragStart | MouseDragToggle + | MoveMouseInDirection Direction | MoveMousePosition (Int, Int) | ResetKeys | SetMode Mode -- cgit v1.3.1