{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE ScopedTypeVariables #-}
module XMonad.Actions.EasyMotion (
selectWindow
, EasyMotionConfig(..)
, ChordKeys(..)
, def
, fullSize
, fixedSize
, textSize
, proportional
, bar
) where
import XMonad
import XMonad.Prelude
import qualified XMonad.StackSet as W
import XMonad.Util.Font (releaseXMF, initXMF, Align(AlignCenter), XMonadFont(..), textExtentsXMF)
import XMonad.Util.XUtils (createNewWindow, paintAndWrite, deleteWindow, showWindow)
import Control.Arrow ((&&&))
import qualified Data.Map.Strict as M (Map, elems, map, mapWithKey)
data OverlayWindow =
OverlayWindow { OverlayWindow -> Word64
win :: !Window
, OverlayWindow -> WindowAttributes
attrs :: !WindowAttributes
, OverlayWindow -> Word64
overlay :: !Window
, OverlayWindow -> Rectangle
rect :: !Rectangle
}
data Overlay =
Overlay { Overlay -> OverlayWindow
overlayWin :: !OverlayWindow
, Overlay -> [Word64]
chord :: ![KeySym]
}
data ChordKeys = AnyKeys ![KeySym]
| PerScreenKeys !(M.Map ScreenId [KeySym])
data EasyMotionConfig =
EMConf { EasyMotionConfig -> WorkspaceId
txtCol :: !String
, EasyMotionConfig -> WorkspaceId
bgCol :: !String
, EasyMotionConfig -> Int32 -> Rectangle -> Rectangle
overlayF :: !(Position -> Rectangle -> Rectangle)
, EasyMotionConfig -> WorkspaceId
borderCol :: !String
, EasyMotionConfig -> ChordKeys
sKeys :: !ChordKeys
, EasyMotionConfig -> Word64
cancelKey :: !KeySym
, EasyMotionConfig -> WorkspaceId
emFont :: !String
, EasyMotionConfig -> Int
borderPx :: !Int
, EasyMotionConfig -> Int
maxChordLen :: !Int
}
instance Default EasyMotionConfig where
def :: EasyMotionConfig
def =
EMConf { txtCol :: WorkspaceId
txtCol = WorkspaceId
"#ffffff"
, bgCol :: WorkspaceId
bgCol = WorkspaceId
"#000000"
, overlayF :: Int32 -> Rectangle -> Rectangle
overlayF = Double -> Int32 -> Rectangle -> Rectangle
forall f. RealFrac f => f -> Int32 -> Rectangle -> Rectangle
proportional (Double
0.3::Double)
, borderCol :: WorkspaceId
borderCol = WorkspaceId
"#ffffff"
, sKeys :: ChordKeys
sKeys = [Word64] -> ChordKeys
AnyKeys [Word64
xK_s, Word64
xK_d, Word64
xK_f, Word64
xK_j, Word64
xK_k, Word64
xK_l]
, cancelKey :: Word64
cancelKey = Word64
xK_q
, borderPx :: Int
borderPx = Int
1
, maxChordLen :: Int
maxChordLen = Int
0
#ifdef XFT
, emFont = "xft:Sans-100"
#else
, emFont :: WorkspaceId
emFont = WorkspaceId
"-misc-fixed-*-*-*-*-200-*-*-*-*-*-*-*"
#endif
}
fullSize :: Position -> Rectangle -> Rectangle
fullSize :: Int32 -> Rectangle -> Rectangle
fullSize Int32
_ = Rectangle -> Rectangle
forall a. a -> a
id
proportional :: RealFrac f => f -> Position -> Rectangle -> Rectangle
proportional :: forall f. RealFrac f => f -> Int32 -> Rectangle -> Rectangle
proportional f
f Int32
th Rectangle
r = Rectangle { rect_width :: Word32
rect_width = Word32
newW
, rect_height :: Word32
rect_height = Word32
newH
, rect_x :: Int32
rect_x = Rectangle -> Int32
rect_x Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_width Rectangle
r Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
newW) Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`div` Int32
2
, rect_y :: Int32
rect_y = Rectangle -> Int32
rect_y Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_height Rectangle
r Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
newH) Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`div` Int32
2 }
where
newH :: Word32
newH = Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
max (Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int32
th) (f -> Word32
forall b. Integral b => f -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (f -> Word32) -> f -> Word32
forall a b. (a -> b) -> a -> b
$ f
f f -> f -> f
forall a. Num a => a -> a -> a
* Word32 -> f
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_height Rectangle
r))
newW :: Word32
newW = Word32
newH
fixedSize :: (Integral a, Integral b) => a -> b -> Position -> Rectangle -> Rectangle
fixedSize :: forall a b.
(Integral a, Integral b) =>
a -> b -> Int32 -> Rectangle -> Rectangle
fixedSize a
w b
h Int32
th Rectangle
r = Rectangle { rect_width :: Word32
rect_width = Word32
rw
, rect_height :: Word32
rect_height = Word32
rh
, rect_x :: Int32
rect_x = Rectangle -> Int32
rect_x Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_width Rectangle
r Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
rw) Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`div` Int32
2
, rect_y :: Int32
rect_y = Rectangle -> Int32
rect_y Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_height Rectangle
r Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
- Word32
rh) Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`div` Int32
2 }
where
rw :: Word32
rw = Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
max (a -> Word32
forall a b. (Integral a, Num b) => a -> b
fi a
w) (Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int32
th)
rh :: Word32
rh = Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
max (b -> Word32
forall a b. (Integral a, Num b) => a -> b
fi b
h) (Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int32
th)
textSize :: Position -> Rectangle -> Rectangle
textSize :: Int32 -> Rectangle -> Rectangle
textSize Int32
th Rectangle
r = Rectangle { rect_width :: Word32
rect_width = Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int32
th
, rect_height :: Word32
rect_height = Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int32
th
, rect_x :: Int32
rect_x = Rectangle -> Int32
rect_x Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_width Rectangle
r) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int32
th) Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`div` Int32
2
, rect_y :: Int32
rect_y = Rectangle -> Int32
rect_y Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_height Rectangle
r) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int32
th) Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`div` Int32
2 }
bar :: RealFrac f => f -> Position -> Rectangle -> Rectangle
bar :: forall f. RealFrac f => f -> Int32 -> Rectangle -> Rectangle
bar f
f Int32
th Rectangle
r = Rectangle { rect_width :: Word32
rect_width = Rectangle -> Word32
rect_width Rectangle
r
, rect_height :: Word32
rect_height = Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int32
th
, rect_x :: Int32
rect_x = Rectangle -> Int32
rect_x Rectangle
r
, rect_y :: Int32
rect_y = Rectangle -> Int32
rect_y Rectangle
r Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ f -> Int32
forall b. Integral b => f -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (f
f' f -> f -> f
forall a. Num a => a -> a -> a
* (Word32 -> f
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_height Rectangle
r) f -> f -> f
forall a. Num a => a -> a -> a
- Int32 -> f
forall a b. (Integral a, Num b) => a -> b
fi Int32
th)) }
where
f' :: f
f' = f -> f -> f
forall a. Ord a => a -> a -> a
min f
0.0 (f -> f) -> f -> f
forall a b. (a -> b) -> a -> b
$ f -> f -> f
forall a. Ord a => a -> a -> a
max f
f f
1.0
handleSelectWindow :: EasyMotionConfig -> X (Maybe Window)
handleSelectWindow :: EasyMotionConfig -> X (Maybe Word64)
handleSelectWindow EMConf { sKeys :: EasyMotionConfig -> ChordKeys
sKeys = AnyKeys [] } = Maybe Word64 -> X (Maybe Word64)
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Word64
forall a. Maybe a
Nothing
handleSelectWindow EasyMotionConfig
c = do
f <- WorkspaceId -> X XMonadFont
initXMF (WorkspaceId -> X XMonadFont) -> WorkspaceId -> X XMonadFont
forall a b. (a -> b) -> a -> b
$ EasyMotionConfig -> WorkspaceId
emFont EasyMotionConfig
c
th <- (\(Int32
asc, Int32
dsc) -> Int32
asc Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
dsc Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
2) <$> textExtentsXMF f (concatMap keysymToString (allKeys . sKeys $ c))
XConf { theRoot = rw, display = dpy } <- ask
XState { mapped = mappedWins, windowset = ws } <- get
overlays :: [Overlay] <- case sKeys c of
AnyKeys [Word64]
ks -> [Word64] -> [OverlayWindow] -> [Overlay]
buildOverlays [Word64]
ks ([OverlayWindow] -> [Overlay]) -> X [OverlayWindow] -> X [Overlay]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> X [OverlayWindow]
sortedOverlayWindows
where
visibleWindows :: [Window]
visibleWindows :: [Word64]
visibleWindows = Set Word64 -> [Item (Set Word64)]
forall l. IsList l => l -> [Item l]
toList Set Word64
mappedWins
sortedOverlayWindows :: X [OverlayWindow]
sortedOverlayWindows :: X [OverlayWindow]
sortedOverlayWindows = [OverlayWindow] -> [OverlayWindow]
sortOverlayWindows ([OverlayWindow] -> [OverlayWindow])
-> X [OverlayWindow] -> X [OverlayWindow]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int32 -> [Word64] -> X [OverlayWindow]
buildOverlayWindows Int32
th [Word64]
visibleWindows
PerScreenKeys Map ScreenId [Word64]
m ->
([[Overlay]] -> [Overlay]) -> X [[Overlay]] -> X [Overlay]
forall a b. (a -> b) -> X a -> X b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [[Overlay]] -> [Overlay]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
(X [[Overlay]] -> X [Overlay]) -> X [[Overlay]] -> X [Overlay]
forall a b. (a -> b) -> a -> b
$ [X [Overlay]] -> X [[Overlay]]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence
([X [Overlay]] -> X [[Overlay]]) -> [X [Overlay]] -> X [[Overlay]]
forall a b. (a -> b) -> a -> b
$ Map ScreenId (X [Overlay]) -> [X [Overlay]]
forall k a. Map k a -> [a]
M.elems
(Map ScreenId (X [Overlay]) -> [X [Overlay]])
-> Map ScreenId (X [Overlay]) -> [X [Overlay]]
forall a b. (a -> b) -> a -> b
$ (ScreenId -> [Word64] -> X [Overlay])
-> Map ScreenId [Word64] -> Map ScreenId (X [Overlay])
forall k a b. (k -> a -> b) -> Map k a -> Map k b
M.mapWithKey (\ScreenId
sid [Word64]
ks -> [Word64] -> [OverlayWindow] -> [Overlay]
buildOverlays [Word64]
ks ([OverlayWindow] -> [Overlay]) -> X [OverlayWindow] -> X [Overlay]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ScreenId -> X [OverlayWindow]
sortedOverlayWindows ScreenId
sid) Map ScreenId [Word64]
m
where
screenById :: ScreenId -> Maybe WindowScreen
screenById :: ScreenId -> Maybe WindowScreen
screenById ScreenId
sid = (WindowScreen -> Bool) -> [WindowScreen] -> Maybe WindowScreen
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((ScreenId -> ScreenId -> Bool
forall a. Eq a => a -> a -> Bool
== ScreenId
sid) (ScreenId -> Bool)
-> (WindowScreen -> ScreenId) -> WindowScreen -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowScreen -> ScreenId
forall i l a sid sd. Screen i l a sid sd -> sid
W.screen) (WindowSet -> [WindowScreen]
forall i l a s sd. StackSet i l a s sd -> [Screen i l a s sd]
W.screens WindowSet
ws)
visibleWindowsOnScreen :: ScreenId -> [Window]
visibleWindowsOnScreen :: ScreenId -> [Word64]
visibleWindowsOnScreen ScreenId
sid = (Word64 -> Bool) -> [Word64] -> [Word64]
forall a. (a -> Bool) -> [a] -> [a]
filter (Word64 -> [Word64] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` Set Word64 -> [Item (Set Word64)]
forall l. IsList l => l -> [Item l]
toList Set Word64
mappedWins) ([Word64] -> [Word64]) -> [Word64] -> [Word64]
forall a b. (a -> b) -> a -> b
$ Maybe (Stack Word64) -> [Word64]
forall a. Maybe (Stack a) -> [a]
W.integrate' (Maybe (Stack Word64) -> [Word64])
-> Maybe (Stack Word64) -> [Word64]
forall a b. (a -> b) -> a -> b
$ ScreenId -> Maybe WindowScreen
screenById ScreenId
sid Maybe WindowScreen
-> (WindowScreen -> Maybe (Stack Word64)) -> Maybe (Stack Word64)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Workspace WorkspaceId (Layout Word64) Word64
-> Maybe (Stack Word64)
forall i l a. Workspace i l a -> Maybe (Stack a)
W.stack (Workspace WorkspaceId (Layout Word64) Word64
-> Maybe (Stack Word64))
-> (WindowScreen -> Workspace WorkspaceId (Layout Word64) Word64)
-> WindowScreen
-> Maybe (Stack Word64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WindowScreen -> Workspace WorkspaceId (Layout Word64) Word64
forall i l a sid sd. Screen i l a sid sd -> Workspace i l a
W.workspace
sortedOverlayWindows :: ScreenId -> X [OverlayWindow]
sortedOverlayWindows :: ScreenId -> X [OverlayWindow]
sortedOverlayWindows ScreenId
sid = [OverlayWindow] -> [OverlayWindow]
sortOverlayWindows ([OverlayWindow] -> [OverlayWindow])
-> X [OverlayWindow] -> X [OverlayWindow]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int32 -> [Word64] -> X [OverlayWindow]
buildOverlayWindows Int32
th (ScreenId -> [Word64]
visibleWindowsOnScreen ScreenId
sid)
status <- io $ grabKeyboard dpy rw True grabModeAsync grabModeAsync currentTime
if status == grabSuccess
then do
resultWin <- handleKeyboard dpy (displayOverlay f) (cancelKey c) overlays []
io $ ungrabKeyboard dpy currentTime
mapM_ (deleteWindow . overlay . overlayWin) overlays
io $ sync dpy False
releaseXMF f
case resultWin of
Selected Overlay
o -> Maybe Word64 -> X (Maybe Word64)
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Word64 -> X (Maybe Word64))
-> (Overlay -> Maybe Word64) -> Overlay -> X (Maybe Word64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Maybe Word64
forall a. a -> Maybe a
Just (Word64 -> Maybe Word64)
-> (Overlay -> Word64) -> Overlay -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OverlayWindow -> Word64
win (OverlayWindow -> Word64)
-> (Overlay -> OverlayWindow) -> Overlay -> Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Overlay -> OverlayWindow
overlayWin (Overlay -> X (Maybe Word64)) -> Overlay -> X (Maybe Word64)
forall a b. (a -> b) -> a -> b
$ Overlay
o
HandleResult
_ -> Maybe Word64 -> (Word64 -> X ()) -> X ()
forall (m :: * -> *) a. Monad m => Maybe a -> (a -> m ()) -> m ()
whenJust (WindowSet -> Maybe Word64
forall i l a s sd. StackSet i l a s sd -> Maybe a
W.peek WindowSet
ws) ((WindowSet -> WindowSet) -> X ()
windows ((WindowSet -> WindowSet) -> X ())
-> (Word64 -> WindowSet -> WindowSet) -> Word64 -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> WindowSet -> WindowSet
forall s a i l sd.
(Eq s, Eq a, Eq i) =>
a -> StackSet i l a s sd -> StackSet i l a s sd
W.focusWindow) X () -> Maybe Word64 -> X (Maybe Word64)
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> Maybe Word64
forall a. Maybe a
Nothing
else releaseXMF f $> Nothing
where
allKeys :: ChordKeys -> [KeySym]
allKeys :: ChordKeys -> [Word64]
allKeys (AnyKeys [Word64]
ks) = [Word64]
ks
allKeys (PerScreenKeys Map ScreenId [Word64]
m) = [[Word64]] -> [Word64]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[Word64]] -> [Word64]) -> [[Word64]] -> [Word64]
forall a b. (a -> b) -> a -> b
$ Map ScreenId [Word64] -> [[Word64]]
forall k a. Map k a -> [a]
M.elems Map ScreenId [Word64]
m
buildOverlays :: [KeySym] -> [OverlayWindow] -> [Overlay]
buildOverlays :: [Word64] -> [OverlayWindow] -> [Overlay]
buildOverlays = Int -> [Word64] -> [OverlayWindow] -> [Overlay]
appendChords (EasyMotionConfig -> Int
maxChordLen EasyMotionConfig
c)
buildOverlayWindows :: Position -> [Window] -> X [OverlayWindow]
buildOverlayWindows :: Int32 -> [Word64] -> X [OverlayWindow]
buildOverlayWindows Int32
th = ([Maybe OverlayWindow] -> [OverlayWindow])
-> X [Maybe OverlayWindow] -> X [OverlayWindow]
forall a b. (a -> b) -> X a -> X b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([OverlayWindow] -> Maybe [OverlayWindow] -> [OverlayWindow]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [OverlayWindow] -> [OverlayWindow])
-> ([Maybe OverlayWindow] -> Maybe [OverlayWindow])
-> [Maybe OverlayWindow]
-> [OverlayWindow]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe OverlayWindow] -> Maybe [OverlayWindow]
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a. Applicative f => [f a] -> f [a]
sequenceA)
(X [Maybe OverlayWindow] -> X [OverlayWindow])
-> ([Word64] -> X [Maybe OverlayWindow])
-> [Word64]
-> X [OverlayWindow]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word64 -> X (Maybe OverlayWindow))
-> [Word64] -> X [Maybe OverlayWindow]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (Int32 -> Word64 -> X (Maybe OverlayWindow)
buildOverlayWin Int32
th)
sortOverlayWindows :: [OverlayWindow] -> [OverlayWindow]
sortOverlayWindows :: [OverlayWindow] -> [OverlayWindow]
sortOverlayWindows = (OverlayWindow -> (CInt, CInt))
-> [OverlayWindow] -> [OverlayWindow]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn ((WindowAttributes -> CInt
wa_x (WindowAttributes -> CInt)
-> (WindowAttributes -> CInt) -> WindowAttributes -> (CInt, CInt)
forall b c c'. (b -> c) -> (b -> c') -> b -> (c, c')
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& WindowAttributes -> CInt
wa_y) (WindowAttributes -> (CInt, CInt))
-> (OverlayWindow -> WindowAttributes)
-> OverlayWindow
-> (CInt, CInt)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OverlayWindow -> WindowAttributes
attrs)
makeRect :: WindowAttributes -> Rectangle
makeRect :: WindowAttributes -> Rectangle
makeRect WindowAttributes
wa = Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle (CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_x WindowAttributes
wa)) (CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_y WindowAttributes
wa)) (CInt -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_width WindowAttributes
wa)) (CInt -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (WindowAttributes -> CInt
wa_height WindowAttributes
wa))
buildOverlayWin :: Position -> Window -> X (Maybe OverlayWindow)
buildOverlayWin :: Int32 -> Word64 -> X (Maybe OverlayWindow)
buildOverlayWin Int32
th Word64
w = Word64 -> X (Maybe WindowAttributes)
safeGetWindowAttributes Word64
w X (Maybe WindowAttributes)
-> (Maybe WindowAttributes -> X (Maybe OverlayWindow))
-> X (Maybe OverlayWindow)
forall a b. X a -> (a -> X b) -> X b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Maybe WindowAttributes
Nothing -> Maybe OverlayWindow -> X (Maybe OverlayWindow)
forall a. a -> X a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe OverlayWindow
forall a. Maybe a
Nothing
Just WindowAttributes
wAttrs -> do
let r :: Rectangle
r = EasyMotionConfig -> Int32 -> Rectangle -> Rectangle
overlayF EasyMotionConfig
c Int32
th (Rectangle -> Rectangle) -> Rectangle -> Rectangle
forall a b. (a -> b) -> a -> b
$ WindowAttributes -> Rectangle
makeRect WindowAttributes
wAttrs
o <- Rectangle -> Maybe Word64 -> WorkspaceId -> Bool -> X Word64
createNewWindow Rectangle
r Maybe Word64
forall a. Maybe a
Nothing WorkspaceId
"" Bool
True
return . Just $ OverlayWindow { rect=r, overlay=o, win=w, attrs=wAttrs }
displayOverlay :: XMonadFont -> Overlay -> X ()
displayOverlay :: XMonadFont -> Overlay -> X ()
displayOverlay XMonadFont
f Overlay { overlayWin :: Overlay -> OverlayWindow
overlayWin = OverlayWindow { rect :: OverlayWindow -> Rectangle
rect = Rectangle
r, overlay :: OverlayWindow -> Word64
overlay = Word64
o }, chord :: Overlay -> [Word64]
chord = [Word64]
ch } = do
Word64 -> X ()
showWindow Word64
o
Word64
-> XMonadFont
-> Word32
-> Word32
-> Word32
-> WorkspaceId
-> WorkspaceId
-> WorkspaceId
-> WorkspaceId
-> [Align]
-> [WorkspaceId]
-> X ()
paintAndWrite Word64
o XMonadFont
f (Word32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_width Rectangle
r)) (Word32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Rectangle -> Word32
rect_height Rectangle
r)) (Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (EasyMotionConfig -> Int
borderPx EasyMotionConfig
c)) (EasyMotionConfig -> WorkspaceId
bgCol EasyMotionConfig
c) (EasyMotionConfig -> WorkspaceId
borderCol EasyMotionConfig
c) (EasyMotionConfig -> WorkspaceId
txtCol EasyMotionConfig
c) (EasyMotionConfig -> WorkspaceId
bgCol EasyMotionConfig
c) [Align
AlignCenter] [(Word64 -> WorkspaceId) -> [Word64] -> WorkspaceId
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Word64 -> WorkspaceId
keysymToString [Word64]
ch]
selectWindow :: EasyMotionConfig -> X (Maybe Window)
selectWindow :: EasyMotionConfig -> X (Maybe Word64)
selectWindow EasyMotionConfig
conf =
EasyMotionConfig -> X (Maybe Word64)
handleSelectWindow EasyMotionConfig
conf { sKeys = sanitiseKeys (sKeys conf) }
where
sanitise :: [KeySym] -> [KeySym]
sanitise :: [Word64] -> [Word64]
sanitise = [Word64] -> [Word64]
forall a. Eq a => [a] -> [a]
nub ([Word64] -> [Word64])
-> ([Word64] -> [Word64]) -> [Word64] -> [Word64]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word64 -> Bool) -> [Word64] -> [Word64]
forall a. (a -> Bool) -> [a] -> [a]
filter (Word64 -> [Word64] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Word64
xK_BackSpace, EasyMotionConfig -> Word64
cancelKey EasyMotionConfig
conf])
sanitiseKeys :: ChordKeys -> ChordKeys
sanitiseKeys :: ChordKeys -> ChordKeys
sanitiseKeys ChordKeys
cKeys =
case ChordKeys
cKeys of
AnyKeys [Word64]
ks -> [Word64] -> ChordKeys
AnyKeys ([Word64] -> ChordKeys)
-> ([Word64] -> [Word64]) -> [Word64] -> ChordKeys
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word64] -> [Word64]
sanitise ([Word64] -> ChordKeys) -> [Word64] -> ChordKeys
forall a b. (a -> b) -> a -> b
$ [Word64]
ks
PerScreenKeys Map ScreenId [Word64]
m -> Map ScreenId [Word64] -> ChordKeys
PerScreenKeys (Map ScreenId [Word64] -> ChordKeys)
-> Map ScreenId [Word64] -> ChordKeys
forall a b. (a -> b) -> a -> b
$ ([Word64] -> [Word64])
-> Map ScreenId [Word64] -> Map ScreenId [Word64]
forall a b k. (a -> b) -> Map k a -> Map k b
M.map [Word64] -> [Word64]
sanitise Map ScreenId [Word64]
m
appendChords :: Int -> [KeySym] -> [OverlayWindow] -> [Overlay]
appendChords :: Int -> [Word64] -> [OverlayWindow] -> [Overlay]
appendChords Int
_ [] [OverlayWindow]
_ = []
appendChords Int
maxUserSelectedLen [Word64]
ks [OverlayWindow]
overlayWins =
(OverlayWindow -> [Word64] -> Overlay)
-> [OverlayWindow] -> [[Word64]] -> [Overlay]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith OverlayWindow -> [Word64] -> Overlay
Overlay [OverlayWindow]
overlayWins [[Word64]]
chords
where
chords :: [[Word64]]
chords = Int -> [Word64] -> [[Word64]]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
chordLen [Word64]
ks
minCoverLen :: Int
minCoverLen = -((-([OverlayWindow] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [OverlayWindow]
overlayWins)) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` [Word64] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Word64]
ks)
chordLen :: Int
chordLen = if Int
maxUserSelectedLen Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 then Int
minCoverLen else Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
minCoverLen Int
maxUserSelectedLen
data HandleResult = Exit | Selected Overlay | Backspace
handleKeyboard :: Display -> (Overlay -> X()) -> KeySym -> [Overlay] -> [Overlay] -> X HandleResult
handleKeyboard :: Display
-> (Overlay -> X ())
-> Word64
-> [Overlay]
-> [Overlay]
-> X HandleResult
handleKeyboard Display
_ Overlay -> X ()
_ Word64
_ [] [Overlay]
_ = HandleResult -> X HandleResult
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return HandleResult
Exit
handleKeyboard Display
dpy Overlay -> X ()
drawFn Word64
cancel [Overlay]
selected [Overlay]
deselected = do
X [()]
redraw
ev <- IO Event -> X Event
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO Event -> X Event) -> IO Event -> X Event
forall a b. (a -> b) -> a -> b
$ (XEventPtr -> IO Event) -> IO Event
forall a. (XEventPtr -> IO a) -> IO a
allocaXEvent ((XEventPtr -> IO Event) -> IO Event)
-> (XEventPtr -> IO Event) -> IO Event
forall a b. (a -> b) -> a -> b
$ \XEventPtr
e -> do
Display -> Word64 -> XEventPtr -> IO ()
maskEvent Display
dpy (Word64
keyPressMask Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64
keyReleaseMask Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word64
buttonPressMask) XEventPtr
e
XEventPtr -> IO Event
getEvent XEventPtr
e
if | ev_event_type ev == keyPress -> do
s <- io $ keycodeToKeysym dpy (ev_keycode ev) 0
if | s == cancel -> return Exit
| s == xK_BackSpace -> return Backspace
| isNextOverlayKey s -> handleNextOverlayKey s
| otherwise -> handleKeyboard dpy drawFn cancel selected deselected
| ev_event_type ev == buttonPress -> do
io $ allowEvents dpy replayPointer currentTime
handleKeyboard dpy drawFn cancel selected deselected
| otherwise -> handleKeyboard dpy drawFn cancel selected deselected
where
redraw :: X [()]
redraw = ([Overlay] -> X ()) -> [[Overlay]] -> X [()]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((Overlay -> X ()) -> [Overlay] -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Overlay -> X ()
drawFn) [[Overlay]
selected, [Overlay]
deselected]
retryBackspace :: HandleResult -> X HandleResult
retryBackspace HandleResult
x =
case HandleResult
x of
HandleResult
Backspace -> X [()]
redraw X [()] -> X HandleResult -> X HandleResult
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Display
-> (Overlay -> X ())
-> Word64
-> [Overlay]
-> [Overlay]
-> X HandleResult
handleKeyboard Display
dpy Overlay -> X ()
drawFn Word64
cancel [Overlay]
selected [Overlay]
deselected
HandleResult
_ -> HandleResult -> X HandleResult
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return HandleResult
x
isNextOverlayKey :: Word64 -> Bool
isNextOverlayKey Word64
keySym = Maybe Overlay -> Bool
forall a. Maybe a -> Bool
isJust ((Overlay -> Bool) -> [Overlay] -> Maybe Overlay
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find ((Maybe Word64 -> Maybe Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64 -> Maybe Word64
forall a. a -> Maybe a
Just Word64
keySym) (Maybe Word64 -> Bool)
-> (Overlay -> Maybe Word64) -> Overlay -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word64] -> Maybe Word64
forall a. [a] -> Maybe a
listToMaybe ([Word64] -> Maybe Word64)
-> (Overlay -> [Word64]) -> Overlay -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Overlay -> [Word64]
chord) [Overlay]
selected)
handleNextOverlayKey :: Word64 -> X HandleResult
handleNextOverlayKey Word64
keySym =
case [Overlay]
fg of
[Overlay
x] -> HandleResult -> X HandleResult
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (HandleResult -> X HandleResult) -> HandleResult -> X HandleResult
forall a b. (a -> b) -> a -> b
$ Overlay -> HandleResult
Selected Overlay
x
[Overlay]
_ -> Display
-> (Overlay -> X ())
-> Word64
-> [Overlay]
-> [Overlay]
-> X HandleResult
handleKeyboard Display
dpy Overlay -> X ()
drawFn Word64
cancel ([Overlay] -> [Overlay]
trim [Overlay]
fg) ([Overlay] -> [Overlay]
clear [Overlay]
bg) X HandleResult
-> (HandleResult -> X HandleResult) -> X HandleResult
forall a b. X a -> (a -> X b) -> X b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= HandleResult -> X HandleResult
retryBackspace
where
([Overlay]
fg, [Overlay]
bg) = (Overlay -> Bool) -> [Overlay] -> ([Overlay], [Overlay])
forall a. (a -> Bool) -> [a] -> ([a], [a])
partition ((Maybe Word64 -> Maybe Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64 -> Maybe Word64
forall a. a -> Maybe a
Just Word64
keySym) (Maybe Word64 -> Bool)
-> (Overlay -> Maybe Word64) -> Overlay -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word64] -> Maybe Word64
forall a. [a] -> Maybe a
listToMaybe ([Word64] -> Maybe Word64)
-> (Overlay -> [Word64]) -> Overlay -> Maybe Word64
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Overlay -> [Word64]
chord) [Overlay]
selected
trim :: [Overlay] -> [Overlay]
trim = (Overlay -> Overlay) -> [Overlay] -> [Overlay]
forall a b. (a -> b) -> [a] -> [b]
map (\Overlay
o -> Overlay
o { chord = drop 1 $ chord o })
clear :: [Overlay] -> [Overlay]
clear = (Overlay -> Overlay) -> [Overlay] -> [Overlay]
forall a b. (a -> b) -> [a] -> [b]
map (\Overlay
o -> Overlay
o { chord = [] })