diff options
| author | Akshay Nair <phenax5@gmail.com> | 2024-12-26 00:17:53 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2024-12-26 00:20:47 +0530 |
| commit | 6ad789149036a9e97a9c66860828892efa432bd4 (patch) | |
| tree | b509812fc69f1af890c1c4ddbcda79f81bc9ab43 /src | |
| parent | d9b2256047669b5a5dbac4baec7140f18a5b6eff (diff) | |
| download | chelleport-6ad789149036a9e97a9c66860828892efa432bd4.tar.gz chelleport-6ad789149036a9e97a9c66860828892efa432bd4.zip | |
Add c-hjkl for movement keys
Diffstat (limited to 'src')
| -rw-r--r-- | src/Chelleport.hs | 14 | ||||
| -rw-r--r-- | src/Chelleport/AppState.hs | 11 | ||||
| -rw-r--r-- | src/Chelleport/Control.hs | 19 | ||||
| -rw-r--r-- | src/Chelleport/Types.hs | 4 |
4 files changed, 33 insertions, 15 deletions
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 -- <C-s>: Enable search mode | checkKey [ctrl, key SDL.KeycodeS, pressed] ev -> Just $ SetMode defaultSearchMode - -- <C-h>: Enable hints mode - | checkKey [ctrl, key SDL.KeycodeH, pressed] ev -> Just $ SetMode defaultHintsMode + -- <C-t>: Enable hints mode + | checkKey [ctrl, key SDL.KeycodeT, pressed] ev -> Just $ SetMode defaultHintsMode -- <C-n>, <C-p>: 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) + -- <C-hjkl>: 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 |
