From ca8b1bae6757387fabeec3559ccdfd440af247a6 Mon Sep 17 00:00:00 2001 From: Mario Lang Date: Fri, 25 Jun 2021 02:22:52 +0200 Subject: [PATCH] Implement support for the new PositionOnly constructor The API putCursor will request positioning but keep cursor invisible. --- brick.cabal | 2 +- src/Brick/Main.hs | 7 +++++-- src/Brick/Types/Internal.hs | 3 +++ src/Brick/Widgets/Core.hs | 12 +++++++++++- src/Brick/Widgets/FileBrowser.hs | 2 +- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/brick.cabal b/brick.cabal index deda4f8..f36409a 100644 --- a/brick.cabal +++ b/brick.cabal @@ -115,7 +115,7 @@ library Brick.Widgets.Internal build-depends: base < 4.16.0.0, - vty >= 5.31, + vty >= 5.33, transformers, data-clist >= 0.1, directory >= 1.2.5.0, diff --git a/src/Brick/Main.hs b/src/Brick/Main.hs index aff708b..de8c6be 100644 --- a/src/Brick/Main.hs +++ b/src/Brick/Main.hs @@ -441,8 +441,11 @@ renderApp vty app appState rs = do rs picWithCursor = case theCursor of Nothing -> pic { picCursor = NoCursor } - Just cloc -> pic { picCursor = AbsoluteCursor (cloc^.locationColumnL) - (cloc^.locationRowL) + Just cloc -> pic { picCursor = (if cursorLocationVisible cloc + then AbsoluteCursor + else PositionOnly True) + (cloc^.locationColumnL) + (cloc^.locationRowL) } update vty picWithCursor diff --git a/src/Brick/Types/Internal.hs b/src/Brick/Types/Internal.hs index ceea1ca..df53c90 100644 --- a/src/Brick/Types/Internal.hs +++ b/src/Brick/Types/Internal.hs @@ -19,6 +19,7 @@ module Brick.Types.Internal , CursorLocation(..) , cursorLocationL , cursorLocationNameL + , cursorLocationVisibleL , Context(..) , EventState(..) , EventRO(..) @@ -164,6 +165,8 @@ data CursorLocation n = -- ^ The location , cursorLocationName :: !(Maybe n) -- ^ The name of the widget associated with the location + , cursorLocationVisible :: !Bool + -- ^ Weather the cursor should actually be visible } deriving (Read, Show, Generic, NFData) diff --git a/src/Brick/Widgets/Core.hs b/src/Brick/Widgets/Core.hs index 86a2a39..f9da257 100644 --- a/src/Brick/Widgets/Core.hs +++ b/src/Brick/Widgets/Core.hs @@ -59,6 +59,7 @@ module Brick.Widgets.Core -- * Cursor placement , showCursor + , putCursor -- * Naming , Named(..) @@ -994,7 +995,16 @@ showCursor :: n -> Location -> Widget n -> Widget n showCursor n cloc p = Widget (hSize p) (vSize p) $ do result <- render p - return $ result & cursorsL %~ (CursorLocation cloc (Just n):) + return $ result & cursorsL %~ (CursorLocation cloc (Just n) True:) + +-- | When rendering the specified widget, also register a cursor +-- positioning request using the specified name and location. +-- The cursor will only be positioned but not made visible. +putCursor :: n -> Location -> Widget n -> Widget n +putCursor n cloc p = + Widget (hSize p) (vSize p) $ do + result <- render p + return $ result & cursorsL %~ (CursorLocation cloc (Just n) False:) hRelease :: Widget n -> Maybe (Widget n) hRelease p = diff --git a/src/Brick/Widgets/FileBrowser.hs b/src/Brick/Widgets/FileBrowser.hs index f62b222..8a54959 100644 --- a/src/Brick/Widgets/FileBrowser.hs +++ b/src/Brick/Widgets/FileBrowser.hs @@ -798,7 +798,7 @@ renderFileInfo foc maxLen selFiles n listSel info = addAttr = maybe id (withDefAttr . attrForFileType) (fileInfoFileType info) body = addAttr (hLimit (maxLen + 1) $ padRight Max $ - (if foc && listSel then showCursor n (Location (0,0)) else id) $ + (if foc && listSel then putCursor n (Location (0,0)) else id) $ str $ fileInfoSanitizedFilename info <> suffix) suffix = (if fileInfoFileType info == Just Directory then "/" else "") <> (if sel then "*" else "")