{-# LANGUAGE TypeSynonymInstances, MultiParamTypeClasses, PatternGuards #-}
----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Layout.BorderResize
-- Description :  Resize windows by dragging their borders with the mouse.
-- Copyright   :  (c) Jan Vornberger 2009
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  jan.vornberger@informatik.uni-oldenburg.de
-- Stability   :  unstable
-- Portability :  not portable
--
-- This layout modifier will allow to resize windows by dragging their
-- borders with the mouse. However, it only works in layouts or modified
-- layouts that react to the 'SetGeometry' message.
-- "XMonad.Layout.WindowArranger" can be used to create such a setup,
-- but it is probably must useful in a floating layout such as
-- "XMonad.Layout.PositionStoreFloat" with which it has been mainly tested.
-- See the documentation of PositionStoreFloat for a typical usage example.
--
-----------------------------------------------------------------------------

module XMonad.Layout.BorderResize
    ( -- * Usage
      -- $usage
      borderResize
    , borderResizeNear
    , BorderResize (..)
    , RectWithBorders, BorderInfo,
    ) where

import XMonad
import XMonad.Layout.Decoration
import XMonad.Layout.WindowArranger
import XMonad.Util.XUtils
import XMonad.Prelude(when)
import qualified Data.Map as M

-- $usage
-- You can use this module with the following in your
-- @xmonad.hs@:
--
-- > import XMonad.Layout.BorderResize
-- > myLayout = borderResize (... layout setup that reacts to SetGeometry ...)
-- > main = xmonad def { layoutHook = myLayout }
--

type BorderBlueprint = (Rectangle, Glyph, BorderType)

data BorderType = RightSideBorder
                    | LeftSideBorder
                    | TopSideBorder
                    | BottomSideBorder
                    deriving (Int -> BorderType -> ShowS
[BorderType] -> ShowS
BorderType -> String
(Int -> BorderType -> ShowS)
-> (BorderType -> String)
-> ([BorderType] -> ShowS)
-> Show BorderType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BorderType -> ShowS
showsPrec :: Int -> BorderType -> ShowS
$cshow :: BorderType -> String
show :: BorderType -> String
$cshowList :: [BorderType] -> ShowS
showList :: [BorderType] -> ShowS
Show, ReadPrec [BorderType]
ReadPrec BorderType
Int -> ReadS BorderType
ReadS [BorderType]
(Int -> ReadS BorderType)
-> ReadS [BorderType]
-> ReadPrec BorderType
-> ReadPrec [BorderType]
-> Read BorderType
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS BorderType
readsPrec :: Int -> ReadS BorderType
$creadList :: ReadS [BorderType]
readList :: ReadS [BorderType]
$creadPrec :: ReadPrec BorderType
readPrec :: ReadPrec BorderType
$creadListPrec :: ReadPrec [BorderType]
readListPrec :: ReadPrec [BorderType]
Read, BorderType -> BorderType -> Bool
(BorderType -> BorderType -> Bool)
-> (BorderType -> BorderType -> Bool) -> Eq BorderType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BorderType -> BorderType -> Bool
== :: BorderType -> BorderType -> Bool
$c/= :: BorderType -> BorderType -> Bool
/= :: BorderType -> BorderType -> Bool
Eq)
data BorderInfo = BI { BorderInfo -> Word64
bWin :: Window,
                        BorderInfo -> Rectangle
bRect :: Rectangle,
                        BorderInfo -> BorderType
bType :: BorderType
                     } deriving (Int -> BorderInfo -> ShowS
[BorderInfo] -> ShowS
BorderInfo -> String
(Int -> BorderInfo -> ShowS)
-> (BorderInfo -> String)
-> ([BorderInfo] -> ShowS)
-> Show BorderInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BorderInfo -> ShowS
showsPrec :: Int -> BorderInfo -> ShowS
$cshow :: BorderInfo -> String
show :: BorderInfo -> String
$cshowList :: [BorderInfo] -> ShowS
showList :: [BorderInfo] -> ShowS
Show, ReadPrec [BorderInfo]
ReadPrec BorderInfo
Int -> ReadS BorderInfo
ReadS [BorderInfo]
(Int -> ReadS BorderInfo)
-> ReadS [BorderInfo]
-> ReadPrec BorderInfo
-> ReadPrec [BorderInfo]
-> Read BorderInfo
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS BorderInfo
readsPrec :: Int -> ReadS BorderInfo
$creadList :: ReadS [BorderInfo]
readList :: ReadS [BorderInfo]
$creadPrec :: ReadPrec BorderInfo
readPrec :: ReadPrec BorderInfo
$creadListPrec :: ReadPrec [BorderInfo]
readListPrec :: ReadPrec [BorderInfo]
Read)

type RectWithBorders = (Rectangle, [BorderInfo])

data BorderResize a = BR
  { forall a. BorderResize a -> Word32
brBorderSize  :: !Dimension
  -- ^ Still resize when this number of pixels around the border.
  , forall a. BorderResize a -> Map Word64 RectWithBorders
brWrsLastTime :: !(M.Map Window RectWithBorders)
  }
  deriving (Int -> BorderResize a -> ShowS
[BorderResize a] -> ShowS
BorderResize a -> String
(Int -> BorderResize a -> ShowS)
-> (BorderResize a -> String)
-> ([BorderResize a] -> ShowS)
-> Show (BorderResize a)
forall a. Int -> BorderResize a -> ShowS
forall a. [BorderResize a] -> ShowS
forall a. BorderResize a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Int -> BorderResize a -> ShowS
showsPrec :: Int -> BorderResize a -> ShowS
$cshow :: forall a. BorderResize a -> String
show :: BorderResize a -> String
$cshowList :: forall a. [BorderResize a] -> ShowS
showList :: [BorderResize a] -> ShowS
Show, ReadPrec [BorderResize a]
ReadPrec (BorderResize a)
Int -> ReadS (BorderResize a)
ReadS [BorderResize a]
(Int -> ReadS (BorderResize a))
-> ReadS [BorderResize a]
-> ReadPrec (BorderResize a)
-> ReadPrec [BorderResize a]
-> Read (BorderResize a)
forall a. ReadPrec [BorderResize a]
forall a. ReadPrec (BorderResize a)
forall a. Int -> ReadS (BorderResize a)
forall a. ReadS [BorderResize a]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: forall a. Int -> ReadS (BorderResize a)
readsPrec :: Int -> ReadS (BorderResize a)
$creadList :: forall a. ReadS [BorderResize a]
readList :: ReadS [BorderResize a]
$creadPrec :: forall a. ReadPrec (BorderResize a)
readPrec :: ReadPrec (BorderResize a)
$creadListPrec :: forall a. ReadPrec [BorderResize a]
readListPrec :: ReadPrec [BorderResize a]
Read)

borderResize :: l a -> ModifiedLayout BorderResize l a
borderResize :: forall (l :: * -> *) a. l a -> ModifiedLayout BorderResize l a
borderResize = Word32 -> l a -> ModifiedLayout BorderResize l a
forall (l :: * -> *) a.
Word32 -> l a -> ModifiedLayout BorderResize l a
borderResizeNear Word32
2

-- | Like 'borderResize', but takes the number of pixels near the border
-- up to which dragging still resizes a window.
borderResizeNear :: Dimension -> l a -> ModifiedLayout BorderResize l a
borderResizeNear :: forall (l :: * -> *) a.
Word32 -> l a -> ModifiedLayout BorderResize l a
borderResizeNear Word32
borderSize = BorderResize a -> l a -> ModifiedLayout BorderResize l a
forall (m :: * -> *) (l :: * -> *) a.
m a -> l a -> ModifiedLayout m l a
ModifiedLayout (Word32 -> Map Word64 RectWithBorders -> BorderResize a
forall a. Word32 -> Map Word64 RectWithBorders -> BorderResize a
BR Word32
borderSize Map Word64 RectWithBorders
forall k a. Map k a
M.empty)

instance LayoutModifier BorderResize Window where
    redoLayout :: BorderResize Word64
-> Rectangle
-> Maybe (Stack Word64)
-> [(Word64, Rectangle)]
-> X ([(Word64, Rectangle)], Maybe (BorderResize Word64))
redoLayout BorderResize Word64
_       Rectangle
_ Maybe (Stack Word64)
Nothing  [(Word64, Rectangle)]
wrs = ([(Word64, Rectangle)], Maybe (BorderResize Word64))
-> X ([(Word64, Rectangle)], Maybe (BorderResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Word64, Rectangle)]
wrs, Maybe (BorderResize Word64)
forall a. Maybe a
Nothing)
    redoLayout (BR Word32
borderSize Map Word64 RectWithBorders
wrsLastTime) Rectangle
_ Maybe (Stack Word64)
_ [(Word64, Rectangle)]
wrs = do
            let correctOrder :: [Word64]
correctOrder = ((Word64, Rectangle) -> Word64)
-> [(Word64, Rectangle)] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map (Word64, Rectangle) -> Word64
forall a b. (a, b) -> a
fst [(Word64, Rectangle)]
wrs
                wrsCurrent :: Map Word64 Rectangle
wrsCurrent = [(Word64, Rectangle)] -> Map Word64 Rectangle
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [(Word64, Rectangle)]
wrs
                wrsGone :: Map Word64 RectWithBorders
wrsGone = Map Word64 RectWithBorders
-> Map Word64 Rectangle -> Map Word64 RectWithBorders
forall k a b. Ord k => Map k a -> Map k b -> Map k a
M.difference Map Word64 RectWithBorders
wrsLastTime Map Word64 Rectangle
wrsCurrent
                wrsAppeared :: Map Word64 Rectangle
wrsAppeared = Map Word64 Rectangle
-> Map Word64 RectWithBorders -> Map Word64 Rectangle
forall k a b. Ord k => Map k a -> Map k b -> Map k a
M.difference Map Word64 Rectangle
wrsCurrent Map Word64 RectWithBorders
wrsLastTime
                wrsStillThere :: Map Word64 (Maybe Rectangle, RectWithBorders)
wrsStillThere = (RectWithBorders
 -> Rectangle -> (Maybe Rectangle, RectWithBorders))
-> Map Word64 RectWithBorders
-> Map Word64 Rectangle
-> Map Word64 (Maybe Rectangle, RectWithBorders)
forall k a b c.
Ord k =>
(a -> b -> c) -> Map k a -> Map k b -> Map k c
M.intersectionWith RectWithBorders -> Rectangle -> (Maybe Rectangle, RectWithBorders)
forall {a} {b}. Eq a => (a, b) -> a -> (Maybe a, (a, b))
testIfUnchanged Map Word64 RectWithBorders
wrsLastTime Map Word64 Rectangle
wrsCurrent
            Map Word64 RectWithBorders -> X ()
handleGone Map Word64 RectWithBorders
wrsGone
            wrsCreated <- Word32 -> Map Word64 Rectangle -> X (Map Word64 RectWithBorders)
handleAppeared Word32
borderSize Map Word64 Rectangle
wrsAppeared
            let wrsChanged = Word32
-> Map Word64 (Maybe Rectangle, RectWithBorders)
-> Map Word64 RectWithBorders
handleStillThere Word32
borderSize Map Word64 (Maybe Rectangle, RectWithBorders)
wrsStillThere
                wrsThisTime = Map Word64 RectWithBorders
-> Map Word64 RectWithBorders -> Map Word64 RectWithBorders
forall k a. Ord k => Map k a -> Map k a -> Map k a
M.union Map Word64 RectWithBorders
wrsChanged Map Word64 RectWithBorders
wrsCreated
            return (compileWrs wrsThisTime correctOrder, Just $ BR borderSize wrsThisTime)
            -- What we return is the original wrs with the new border
            -- windows inserted at the correct positions - this way, the core
            -- will restack the borders correctly.
            -- We also return information about our borders, so that we
            -- can handle events that they receive and destroy them when
            -- they are no longer needed.
        where
            testIfUnchanged :: (a, b) -> a -> (Maybe a, (a, b))
testIfUnchanged entry :: (a, b)
entry@(a
rLastTime, b
_) a
rCurrent =
                if a
rLastTime a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
rCurrent
                    then (Maybe a
forall a. Maybe a
Nothing, (a, b)
entry)
                    else (a -> Maybe a
forall a. a -> Maybe a
Just a
rCurrent, (a, b)
entry)

    handleMess :: BorderResize Word64
-> SomeMessage -> X (Maybe (BorderResize Word64))
handleMess (BR Word32
borderSize Map Word64 RectWithBorders
wrsLastTime) SomeMessage
m
        | Just Event
e <- SomeMessage -> Maybe Event
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m :: Maybe Event =
            [(Word64, (BorderType, Word64, Rectangle))] -> Event -> X ()
handleResize (Map Word64 RectWithBorders
-> [(Word64, (BorderType, Word64, Rectangle))]
createBorderLookupTable Map Word64 RectWithBorders
wrsLastTime) Event
e X ()
-> X (Maybe (BorderResize Word64))
-> X (Maybe (BorderResize Word64))
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe (BorderResize Word64) -> X (Maybe (BorderResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (BorderResize Word64)
forall a. Maybe a
Nothing
        | Just LayoutMessages
_ <- SomeMessage -> Maybe LayoutMessages
forall m. Message m => SomeMessage -> Maybe m
fromMessage SomeMessage
m :: Maybe LayoutMessages =
            Map Word64 RectWithBorders -> X ()
handleGone Map Word64 RectWithBorders
wrsLastTime X ()
-> X (Maybe (BorderResize Word64))
-> X (Maybe (BorderResize Word64))
forall a b. X a -> X b -> X b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe (BorderResize Word64) -> X (Maybe (BorderResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return (BorderResize Word64 -> Maybe (BorderResize Word64)
forall a. a -> Maybe a
Just (BorderResize Word64 -> Maybe (BorderResize Word64))
-> BorderResize Word64 -> Maybe (BorderResize Word64)
forall a b. (a -> b) -> a -> b
$ Word32 -> Map Word64 RectWithBorders -> BorderResize Word64
forall a. Word32 -> Map Word64 RectWithBorders -> BorderResize a
BR Word32
borderSize Map Word64 RectWithBorders
forall k a. Map k a
M.empty)
    handleMess BorderResize Word64
_ SomeMessage
_ = Maybe (BorderResize Word64) -> X (Maybe (BorderResize Word64))
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (BorderResize Word64)
forall a. Maybe a
Nothing

compileWrs :: M.Map Window RectWithBorders -> [Window] -> [(Window, Rectangle)]
compileWrs :: Map Word64 RectWithBorders -> [Word64] -> [(Word64, Rectangle)]
compileWrs Map Word64 RectWithBorders
wrsThisTime [Word64]
correctOrder = let wrs :: [(Word64, RectWithBorders)]
wrs = [(Word64, RectWithBorders)]
-> [Word64] -> [(Word64, RectWithBorders)]
forall a b. Eq a => [(a, b)] -> [a] -> [(a, b)]
reorder (Map Word64 RectWithBorders -> [(Word64, RectWithBorders)]
forall k a. Map k a -> [(k, a)]
M.toList Map Word64 RectWithBorders
wrsThisTime) [Word64]
correctOrder
                                      in ((Word64, RectWithBorders) -> [(Word64, Rectangle)])
-> [(Word64, RectWithBorders)] -> [(Word64, Rectangle)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Word64, RectWithBorders) -> [(Word64, Rectangle)]
compileWr [(Word64, RectWithBorders)]
wrs

compileWr :: (Window, RectWithBorders) -> [(Window, Rectangle)]
compileWr :: (Word64, RectWithBorders) -> [(Word64, Rectangle)]
compileWr (Word64
w, (Rectangle
r, [BorderInfo]
borderInfos)) =
    let borderWrs :: [(Word64, Rectangle)]
borderWrs = [BorderInfo]
-> (BorderInfo -> (Word64, Rectangle)) -> [(Word64, Rectangle)]
forall a b. [a] -> (a -> b) -> [b]
for [BorderInfo]
borderInfos ((BorderInfo -> (Word64, Rectangle)) -> [(Word64, Rectangle)])
-> (BorderInfo -> (Word64, Rectangle)) -> [(Word64, Rectangle)]
forall a b. (a -> b) -> a -> b
$ \BorderInfo
bi -> (BorderInfo -> Word64
bWin BorderInfo
bi, BorderInfo -> Rectangle
bRect BorderInfo
bi)
    in [(Word64, Rectangle)]
borderWrs [(Word64, Rectangle)]
-> [(Word64, Rectangle)] -> [(Word64, Rectangle)]
forall a. [a] -> [a] -> [a]
++ [(Word64
w, Rectangle
r)]

handleGone :: M.Map Window RectWithBorders -> X ()
handleGone :: Map Word64 RectWithBorders -> X ()
handleGone Map Word64 RectWithBorders
wrsGone = (Word64 -> X ()) -> [Word64] -> X ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Word64 -> X ()
deleteWindow [Word64]
borderWins
    where
        borderWins :: [Word64]
borderWins = (BorderInfo -> Word64) -> [BorderInfo] -> [Word64]
forall a b. (a -> b) -> [a] -> [b]
map BorderInfo -> Word64
bWin ([BorderInfo] -> [Word64])
-> (Map Word64 RectWithBorders -> [BorderInfo])
-> Map Word64 RectWithBorders
-> [Word64]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RectWithBorders -> [BorderInfo])
-> [RectWithBorders] -> [BorderInfo]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap RectWithBorders -> [BorderInfo]
forall a b. (a, b) -> b
snd ([RectWithBorders] -> [BorderInfo])
-> (Map Word64 RectWithBorders -> [RectWithBorders])
-> Map Word64 RectWithBorders
-> [BorderInfo]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map Word64 RectWithBorders -> [RectWithBorders]
forall k a. Map k a -> [a]
M.elems (Map Word64 RectWithBorders -> [Word64])
-> Map Word64 RectWithBorders -> [Word64]
forall a b. (a -> b) -> a -> b
$ Map Word64 RectWithBorders
wrsGone

handleAppeared :: Dimension -> M.Map Window Rectangle -> X (M.Map Window RectWithBorders)
handleAppeared :: Word32 -> Map Word64 Rectangle -> X (Map Word64 RectWithBorders)
handleAppeared Word32
borderSize Map Word64 Rectangle
wrsAppeared = do
    let wrs :: [(Word64, Rectangle)]
wrs = Map Word64 Rectangle -> [(Word64, Rectangle)]
forall k a. Map k a -> [(k, a)]
M.toList Map Word64 Rectangle
wrsAppeared
    wrsCreated <- ((Word64, Rectangle) -> X (Word64, RectWithBorders))
-> [(Word64, Rectangle)] -> X [(Word64, RectWithBorders)]
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 (Word32 -> (Word64, Rectangle) -> X (Word64, RectWithBorders)
handleSingleAppeared Word32
borderSize) [(Word64, Rectangle)]
wrs
    return $ M.fromList wrsCreated

handleSingleAppeared :: Dimension ->(Window, Rectangle) -> X (Window, RectWithBorders)
handleSingleAppeared :: Word32 -> (Word64, Rectangle) -> X (Word64, RectWithBorders)
handleSingleAppeared Word32
borderSize (Word64
w, Rectangle
r) = do
    let borderBlueprints :: [BorderBlueprint]
borderBlueprints = Word32 -> Rectangle -> [BorderBlueprint]
prepareBorders Word32
borderSize Rectangle
r
    borderInfos <- (BorderBlueprint -> X BorderInfo)
-> [BorderBlueprint] -> X [BorderInfo]
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 BorderBlueprint -> X BorderInfo
createBorder [BorderBlueprint]
borderBlueprints
    return (w, (r, borderInfos))

handleStillThere :: Dimension -> M.Map Window (Maybe Rectangle, RectWithBorders) -> M.Map Window RectWithBorders
handleStillThere :: Word32
-> Map Word64 (Maybe Rectangle, RectWithBorders)
-> Map Word64 RectWithBorders
handleStillThere Word32
borderSize = ((Maybe Rectangle, RectWithBorders) -> RectWithBorders)
-> Map Word64 (Maybe Rectangle, RectWithBorders)
-> Map Word64 RectWithBorders
forall a b k. (a -> b) -> Map k a -> Map k b
M.map (Word32 -> (Maybe Rectangle, RectWithBorders) -> RectWithBorders
handleSingleStillThere Word32
borderSize)

handleSingleStillThere :: Dimension -> (Maybe Rectangle, RectWithBorders) -> RectWithBorders
handleSingleStillThere :: Word32 -> (Maybe Rectangle, RectWithBorders) -> RectWithBorders
handleSingleStillThere Word32
_            (Maybe Rectangle
Nothing, RectWithBorders
entry)                  = RectWithBorders
entry
handleSingleStillThere Word32
borderSize (Just Rectangle
rCurrent, (Rectangle
_, [BorderInfo]
borderInfos)) = (Rectangle
rCurrent, [BorderInfo]
updatedBorderInfos)
    where
        changedBorderBlueprints :: [BorderBlueprint]
changedBorderBlueprints = Word32 -> Rectangle -> [BorderBlueprint]
prepareBorders Word32
borderSize Rectangle
rCurrent
        updatedBorderInfos :: [BorderInfo]
updatedBorderInfos = (BorderInfo -> BorderBlueprint -> BorderInfo)
-> [BorderInfo] -> [BorderBlueprint] -> [BorderInfo]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (((BorderInfo, BorderBlueprint) -> BorderInfo)
-> BorderInfo -> BorderBlueprint -> BorderInfo
forall a b c. ((a, b) -> c) -> a -> b -> c
curry (BorderInfo, BorderBlueprint) -> BorderInfo
updateBorderInfo) [BorderInfo]
borderInfos [BorderBlueprint]
changedBorderBlueprints
          -- assuming that the four borders are always in the same order

updateBorderInfo :: (BorderInfo, BorderBlueprint) -> BorderInfo
updateBorderInfo :: (BorderInfo, BorderBlueprint) -> BorderInfo
updateBorderInfo (BorderInfo
borderInfo, (Rectangle
r, Glyph
_, BorderType
_)) = BorderInfo
borderInfo { bRect = r }

createBorderLookupTable :: M.Map Window RectWithBorders -> [(Window, (BorderType, Window, Rectangle))]
createBorderLookupTable :: Map Word64 RectWithBorders
-> [(Word64, (BorderType, Word64, Rectangle))]
createBorderLookupTable Map Word64 RectWithBorders
wrsLastTime = ((Word64, RectWithBorders)
 -> [(Word64, (BorderType, Word64, Rectangle))])
-> [(Word64, RectWithBorders)]
-> [(Word64, (BorderType, Word64, Rectangle))]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Word64, RectWithBorders)
-> [(Word64, (BorderType, Word64, Rectangle))]
processSingleEntry (Map Word64 RectWithBorders -> [(Word64, RectWithBorders)]
forall k a. Map k a -> [(k, a)]
M.toList Map Word64 RectWithBorders
wrsLastTime)
    where
        processSingleEntry :: (Window, RectWithBorders) -> [(Window, (BorderType, Window, Rectangle))]
        processSingleEntry :: (Word64, RectWithBorders)
-> [(Word64, (BorderType, Word64, Rectangle))]
processSingleEntry (Word64
w, (Rectangle
r, [BorderInfo]
borderInfos)) = [BorderInfo]
-> (BorderInfo -> (Word64, (BorderType, Word64, Rectangle)))
-> [(Word64, (BorderType, Word64, Rectangle))]
forall a b. [a] -> (a -> b) -> [b]
for [BorderInfo]
borderInfos ((BorderInfo -> (Word64, (BorderType, Word64, Rectangle)))
 -> [(Word64, (BorderType, Word64, Rectangle))])
-> (BorderInfo -> (Word64, (BorderType, Word64, Rectangle)))
-> [(Word64, (BorderType, Word64, Rectangle))]
forall a b. (a -> b) -> a -> b
$ \BorderInfo
bi -> (BorderInfo -> Word64
bWin BorderInfo
bi, (BorderInfo -> BorderType
bType BorderInfo
bi, Word64
w, Rectangle
r))

prepareBorders :: Dimension -> Rectangle -> [BorderBlueprint]
prepareBorders :: Word32 -> Rectangle -> [BorderBlueprint]
prepareBorders Word32
borderSize (Rectangle Int32
x Int32
y Word32
wh Word32
ht) =
    [(Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle (Int32
x Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
wh Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
borderSize) Int32
y Word32
borderSize Word32
ht, Glyph
xC_right_side , BorderType
RightSideBorder),
     (Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
x Int32
y Word32
borderSize Word32
ht                          , Glyph
xC_left_side  , BorderType
LeftSideBorder),
     (Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
x Int32
y Word32
wh Word32
borderSize                          , Glyph
xC_top_side   , BorderType
TopSideBorder),
     (Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
x (Int32
y Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
ht Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
borderSize) Word32
wh Word32
borderSize, Glyph
xC_bottom_side, BorderType
BottomSideBorder)
    ]

handleResize :: [(Window, (BorderType, Window, Rectangle))] -> Event -> X ()
handleResize :: [(Word64, (BorderType, Word64, Rectangle))] -> Event -> X ()
handleResize [(Word64, (BorderType, Word64, Rectangle))]
borders ButtonEvent { ev_window :: Event -> Word64
ev_window = Word64
ew, ev_event_type :: Event -> Word32
ev_event_type = Word32
et }
    | Word32
et Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
buttonPress, Just (BorderType, Word64, Rectangle)
edge <- Word64
-> [(Word64, (BorderType, Word64, Rectangle))]
-> Maybe (BorderType, Word64, Rectangle)
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Word64
ew [(Word64, (BorderType, Word64, Rectangle))]
borders =
    case (BorderType, Word64, Rectangle)
edge of
        (BorderType
RightSideBorder, Word64
hostWin, Rectangle Int32
hx Int32
hy Word32
_ Word32
hht) ->
            (Int32 -> Int32 -> X ()) -> X () -> X ()
mouseDrag (\Int32
x Int32
_ -> do
                            let nwh :: Word32
nwh = Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
max Word32
1 (Word32 -> Word32) -> Word32 -> Word32
forall a b. (a -> b) -> a -> b
$ Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Int32
x Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
hx)
                                rect :: Rectangle
rect = Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
hx Int32
hy Word32
nwh Word32
hht
                            Word64 -> X ()
focus Word64
hostWin
                            Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int32
x Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
hx Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
> Int32
0) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ WindowArrangerMsg -> X ()
forall a. Message a => a -> X ()
sendMessage (Rectangle -> WindowArrangerMsg
SetGeometry Rectangle
rect)) (Word64 -> X ()
focus Word64
hostWin)
        (BorderType
LeftSideBorder, Word64
hostWin, Rectangle Int32
hx Int32
hy Word32
hwh Word32
hht) ->
            (Int32 -> Int32 -> X ()) -> X () -> X ()
mouseDrag (\Int32
x Int32
_ -> do
                            let nx :: Int32
nx = Int32 -> Int32 -> Int32
forall a. Ord a => a -> a -> a
max Int32
0 (Int32 -> Int32) -> Int32 -> Int32
forall a b. (a -> b) -> a -> b
$ Int32 -> Int32 -> Int32
forall a. Ord a => a -> a -> a
min (Int32
hx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
hwh) Int32
x
                                nwh :: Word32
nwh = Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
max Word32
1 (Word32 -> Word32) -> Word32 -> Word32
forall a b. (a -> b) -> a -> b
$ Word32
hwh Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Int32
hx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
x)
                                rect :: Rectangle
rect = Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
nx Int32
hy Word32
nwh Word32
hht
                            Word64 -> X ()
focus Word64
hostWin
                            Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int32
x Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
< Int32
hx Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
hwh) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ WindowArrangerMsg -> X ()
forall a. Message a => a -> X ()
sendMessage (Rectangle -> WindowArrangerMsg
SetGeometry Rectangle
rect)) (Word64 -> X ()
focus Word64
hostWin)
        (BorderType
TopSideBorder, Word64
hostWin, Rectangle Int32
hx Int32
hy Word32
hwh Word32
hht) ->
            (Int32 -> Int32 -> X ()) -> X () -> X ()
mouseDrag (\Int32
_ Int32
y -> do
                            let ny :: Int32
ny = Int32 -> Int32 -> Int32
forall a. Ord a => a -> a -> a
max Int32
0 (Int32 -> Int32) -> Int32 -> Int32
forall a b. (a -> b) -> a -> b
$ Int32 -> Int32 -> Int32
forall a. Ord a => a -> a -> a
min (Int32
hy Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
hht) Int32
y
                                nht :: Word32
nht = Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
max Word32
1 (Word32 -> Word32) -> Word32 -> Word32
forall a b. (a -> b) -> a -> b
$ Word32
hht Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Int32
hy Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
y)
                                rect :: Rectangle
rect = Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
hx Int32
ny Word32
hwh Word32
nht
                            Word64 -> X ()
focus Word64
hostWin
                            Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int32
y Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
< Int32
hy Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
hht) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ WindowArrangerMsg -> X ()
forall a. Message a => a -> X ()
sendMessage (Rectangle -> WindowArrangerMsg
SetGeometry Rectangle
rect)) (Word64 -> X ()
focus Word64
hostWin)
        (BorderType
BottomSideBorder, Word64
hostWin, Rectangle Int32
hx Int32
hy Word32
hwh Word32
_) ->
            (Int32 -> Int32 -> X ()) -> X () -> X ()
mouseDrag (\Int32
_ Int32
y -> do
                            let nht :: Word32
nht = Word32 -> Word32 -> Word32
forall a. Ord a => a -> a -> a
max Word32
1 (Word32 -> Word32) -> Word32 -> Word32
forall a b. (a -> b) -> a -> b
$ Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Int32
y Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
hy)
                                rect :: Rectangle
rect = Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
hx Int32
hy Word32
hwh Word32
nht
                            Word64 -> X ()
focus Word64
hostWin
                            Bool -> X () -> X ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int32
y Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
hy Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
> Int32
0) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ WindowArrangerMsg -> X ()
forall a. Message a => a -> X ()
sendMessage (Rectangle -> WindowArrangerMsg
SetGeometry Rectangle
rect)) (Word64 -> X ()
focus Word64
hostWin)
handleResize [(Word64, (BorderType, Word64, Rectangle))]
_ Event
_ = () -> X ()
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

createBorder :: BorderBlueprint -> X BorderInfo
createBorder :: BorderBlueprint -> X BorderInfo
createBorder (Rectangle
borderRect, Glyph
borderCursor, BorderType
borderType) = do
    borderWin <- Glyph -> Rectangle -> X Word64
createInputWindow Glyph
borderCursor Rectangle
borderRect
    return BI { bWin = borderWin, bRect = borderRect, bType = borderType }

createInputWindow :: Glyph -> Rectangle -> X Window
createInputWindow :: Glyph -> Rectangle -> X Word64
createInputWindow Glyph
cursorGlyph Rectangle
r = (Display -> X Word64) -> X Word64
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X Word64) -> X Word64)
-> (Display -> X Word64) -> X Word64
forall a b. (a -> b) -> a -> b
$ \Display
d -> do
    win <- Display -> Rectangle -> X Word64
mkInputWindow Display
d Rectangle
r
    io $ selectInput d win (exposureMask .|. buttonPressMask)
    cursor <- io $ createFontCursor d cursorGlyph
    io $ defineCursor d win cursor
    io $ freeCursor d cursor
    showWindow win
    return win

mkInputWindow :: Display -> Rectangle -> X Window
mkInputWindow :: Display -> Rectangle -> X Word64
mkInputWindow Display
d (Rectangle Int32
x Int32
y Word32
w Word32
h) = do
  rw <- (XConf -> Word64) -> X Word64
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks XConf -> Word64
theRoot
  let screen   = Display -> Screen
defaultScreenOfDisplay Display
d
      visual   = Screen -> Visual
defaultVisualOfScreen Screen
screen
      attrmask = Word64
cWOverrideRedirect
  io $ allocaSetWindowAttributes $
         \Ptr SetWindowAttributes
attributes -> do
           Ptr SetWindowAttributes -> Bool -> IO ()
set_override_redirect Ptr SetWindowAttributes
attributes Bool
True
           Display
-> Word64
-> Int32
-> Int32
-> Word32
-> Word32
-> CInt
-> CInt
-> CInt
-> Visual
-> Word64
-> Ptr SetWindowAttributes
-> IO Word64
createWindow Display
d Word64
rw Int32
x Int32
y Word32
w Word32
h CInt
0 CInt
0 CInt
inputOnly Visual
visual Word64
attrmask Ptr SetWindowAttributes
attributes

for :: [a] -> (a -> b) -> [b]
for :: forall a b. [a] -> (a -> b) -> [b]
for = ((a -> b) -> [a] -> [b]) -> [a] -> (a -> b) -> [b]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map

reorder :: (Eq a) => [(a, b)] -> [a] -> [(a, b)]
reorder :: forall a b. Eq a => [(a, b)] -> [a] -> [(a, b)]
reorder [(a, b)]
wrs [a]
order =
    let ordered :: [(a, b)]
ordered = (a -> [(a, b)]) -> [a] -> [(a, b)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ([(a, b)] -> a -> [(a, b)]
forall {a} {b}. Eq a => [(a, b)] -> a -> [(a, b)]
pickElem [(a, b)]
wrs) [a]
order
        rest :: [(a, b)]
rest = ((a, b) -> Bool) -> [(a, b)] -> [(a, b)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(a
w, b
_) -> a
w a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [a]
order) [(a, b)]
wrs
    in [(a, b)]
ordered [(a, b)] -> [(a, b)] -> [(a, b)]
forall a. [a] -> [a] -> [a]
++ [(a, b)]
rest
    where
        pickElem :: [(a, b)] -> a -> [(a, b)]
pickElem [(a, b)]
list a
e = case a -> [(a, b)] -> Maybe b
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup a
e [(a, b)]
list of
                                Just b
result -> [(a
e, b
result)]
                                Maybe b
Nothing -> []