Browse Source

Rudimentary skeleton code for a key binding system

master
Peter J. Jones 4 years ago
parent
commit
2337166ffc

+ 2
- 0
clockdown.cabal View File

@@ -32,6 +32,7 @@ library
Clockdown.Core.Properties
Clockdown.Core.Window
Clockdown.UI.Common.Action
Clockdown.UI.Common.Binding
Clockdown.UI.Term.Draw
Clockdown.UI.Term.Run

@@ -45,6 +46,7 @@ library

build-depends: async >= 2.0 && < 2.1
, base >= 4.7 && < 5.0
, containers >= 0.5 && < 0.6
, text >= 0.11 && < 1.3
, time >= 1.5 && < 1.6
, vty >= 5.2 && < 5.3

+ 10
- 0
src/Clockdown/UI/Common/Action.hs View File

@@ -12,13 +12,23 @@ the LICENSE file.
--------------------------------------------------------------------------------
module Clockdown.UI.Common.Action
( Action (..)
, parseAction
) where

--------------------------------------------------------------------------------
-- Library imports:
import Data.Text (Text)
import Data.Time

--------------------------------------------------------------------------------
-- | Actions which can be triggered by the system or the user.
data Action = Tick UTCTime
-- ^ Update the clock.

| Quit
-- ^ Quit the application.
deriving (Show, Read)

--------------------------------------------------------------------------------
parseAction :: Text -> Either String Action
parseAction = undefined

+ 70
- 0
src/Clockdown/UI/Common/Binding.hs View File

@@ -0,0 +1,70 @@
{-# LANGUAGE OverloadedStrings #-}

{-

This file is part of the package clockdown. It is subject to the
license terms in the LICENSE file found in the top-level directory of
this distribution and at git://pmade.com/clockdown/LICENSE. No part of
the clockdown package, including this file, may be copied, modified,
propagated, or distributed except according to the terms contained in
the LICENSE file.

-}

--------------------------------------------------------------------------------
-- | Functions and data types for key bindings.
module Clockdown.UI.Common.Binding
( KeyCode (..)
, KeyModifier (..)
, KeyMap
, defaultKeyMap
, parseKeys
, processKey
) where

--------------------------------------------------------------------------------
-- Library imports:
import Data.Bits
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Text (Text)
import Data.Word

--------------------------------------------------------------------------------
-- Local imports:
import Clockdown.UI.Common.Action

--------------------------------------------------------------------------------
data KeyCode = RawKey Char -- ^ A normal character key.
| Escape -- ^ The escape key.
deriving (Eq, Ord)

--------------------------------------------------------------------------------
data KeyModifier = Shift -- ^ The shift key.
| Control -- ^ The control key.
| Meta -- ^ The meta/alt/option key.
deriving (Eq, Enum)

--------------------------------------------------------------------------------
newtype KeyMap = KeyMap {unMap :: Map (Word8, KeyCode) Action}

--------------------------------------------------------------------------------
defaultKeyMap :: Either String KeyMap
defaultKeyMap = parseKeys defaultKeys
where
defaultKeys :: [(Text, Text)]
defaultKeys = [ ("q", "quit")
, ("<ESC>", "quit")
]

--------------------------------------------------------------------------------
parseKeys :: [(Text, Text)] -> Either String KeyMap
parseKeys = undefined

--------------------------------------------------------------------------------
processKey :: [KeyModifier] -> KeyCode -> KeyMap -> Maybe Action
processKey mods key = Map.lookup (modsToWord mods, key) . unMap

--------------------------------------------------------------------------------
modsToWord :: [KeyModifier] -> Word8
modsToWord = foldl (.|.) 0 . map (setBit 0 . fromEnum)

+ 1
- 0
src/Clockdown/UI/Term/Run.hs View File

@@ -50,6 +50,7 @@ drawThread vty channel = forever $ do
display = windowDigitalDisplay clock t
region <- displayBounds (outputIface vty)
update vty $ picForImage (centerImage region $ drawDisplay display)
Quit -> return ()

--------------------------------------------------------------------------------
-- | Temporary function for testing the drawing functions.

Loading…
Cancel
Save