----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Util.PositionStore
-- Description :  A utility module to store information about position and size of a window.
-- Copyright   :  (c) Jan Vornberger 2009
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  jan.vornberger@informatik.uni-oldenburg.de
-- Stability   :  unstable
-- Portability :  not portable
--
-- A utility module to store information about position and size of a window.
-- See "XMonad.Layout.PositionStoreFloat" for a layout that makes use of this.
--
-----------------------------------------------------------------------------

module XMonad.Util.PositionStore (
        getPosStore,
        modifyPosStore,

        posStoreInsert,
        posStoreMove,
        posStoreQuery,
        posStoreRemove,
        PositionStore,
    ) where

import XMonad
import qualified XMonad.Util.ExtensibleState as XS
import qualified Data.Map as M

-- Store window positions relative to the upper left screen edge
-- and windows sizes as well as positions as fractions of the screen size.
-- This way windows can be easily relocated and scaled when switching screens.

newtype PositionStore = PS (M.Map Window PosStoreRectangle)
                            deriving (ReadPrec [PositionStore]
ReadPrec PositionStore
Int -> ReadS PositionStore
ReadS [PositionStore]
(Int -> ReadS PositionStore)
-> ReadS [PositionStore]
-> ReadPrec PositionStore
-> ReadPrec [PositionStore]
-> Read PositionStore
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS PositionStore
readsPrec :: Int -> ReadS PositionStore
$creadList :: ReadS [PositionStore]
readList :: ReadS [PositionStore]
$creadPrec :: ReadPrec PositionStore
readPrec :: ReadPrec PositionStore
$creadListPrec :: ReadPrec [PositionStore]
readListPrec :: ReadPrec [PositionStore]
Read,Int -> PositionStore -> ShowS
[PositionStore] -> ShowS
PositionStore -> String
(Int -> PositionStore -> ShowS)
-> (PositionStore -> String)
-> ([PositionStore] -> ShowS)
-> Show PositionStore
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PositionStore -> ShowS
showsPrec :: Int -> PositionStore -> ShowS
$cshow :: PositionStore -> String
show :: PositionStore -> String
$cshowList :: [PositionStore] -> ShowS
showList :: [PositionStore] -> ShowS
Show)
data PosStoreRectangle = PSRectangle Double Double Double Double
                            deriving (ReadPrec [PosStoreRectangle]
ReadPrec PosStoreRectangle
Int -> ReadS PosStoreRectangle
ReadS [PosStoreRectangle]
(Int -> ReadS PosStoreRectangle)
-> ReadS [PosStoreRectangle]
-> ReadPrec PosStoreRectangle
-> ReadPrec [PosStoreRectangle]
-> Read PosStoreRectangle
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS PosStoreRectangle
readsPrec :: Int -> ReadS PosStoreRectangle
$creadList :: ReadS [PosStoreRectangle]
readList :: ReadS [PosStoreRectangle]
$creadPrec :: ReadPrec PosStoreRectangle
readPrec :: ReadPrec PosStoreRectangle
$creadListPrec :: ReadPrec [PosStoreRectangle]
readListPrec :: ReadPrec [PosStoreRectangle]
Read,Int -> PosStoreRectangle -> ShowS
[PosStoreRectangle] -> ShowS
PosStoreRectangle -> String
(Int -> PosStoreRectangle -> ShowS)
-> (PosStoreRectangle -> String)
-> ([PosStoreRectangle] -> ShowS)
-> Show PosStoreRectangle
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PosStoreRectangle -> ShowS
showsPrec :: Int -> PosStoreRectangle -> ShowS
$cshow :: PosStoreRectangle -> String
show :: PosStoreRectangle -> String
$cshowList :: [PosStoreRectangle] -> ShowS
showList :: [PosStoreRectangle] -> ShowS
Show)

instance ExtensionClass PositionStore where
  initialValue :: PositionStore
initialValue = Map Word64 PosStoreRectangle -> PositionStore
PS Map Word64 PosStoreRectangle
forall k a. Map k a
M.empty
  extensionType :: PositionStore -> StateExtension
extensionType = PositionStore -> StateExtension
forall a. (Read a, Show a, ExtensionClass a) => a -> StateExtension
PersistentExtension

getPosStore :: X PositionStore
getPosStore :: X PositionStore
getPosStore = X PositionStore
forall a (m :: * -> *). (ExtensionClass a, XLike m) => m a
XS.get

modifyPosStore :: (PositionStore -> PositionStore) -> X ()
modifyPosStore :: (PositionStore -> PositionStore) -> X ()
modifyPosStore = (PositionStore -> PositionStore) -> X ()
forall a (m :: * -> *).
(ExtensionClass a, XLike m) =>
(a -> a) -> m ()
XS.modify

posStoreInsert :: PositionStore -> Window -> Rectangle -> Rectangle -> PositionStore
posStoreInsert :: PositionStore -> Word64 -> Rectangle -> Rectangle -> PositionStore
posStoreInsert (PS Map Word64 PosStoreRectangle
posStoreMap) Word64
w (Rectangle Int32
x Int32
y Word32
wh Word32
ht) (Rectangle Int32
srX Int32
srY Word32
srWh Word32
srHt) =
    let offsetX :: Int32
offsetX = Int32
x Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
srX
        offsetY :: Int32
offsetY = Int32
y Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
srY
    in Map Word64 PosStoreRectangle -> PositionStore
PS (Map Word64 PosStoreRectangle -> PositionStore)
-> Map Word64 PosStoreRectangle -> PositionStore
forall a b. (a -> b) -> a -> b
$ Word64
-> PosStoreRectangle
-> Map Word64 PosStoreRectangle
-> Map Word64 PosStoreRectangle
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Word64
w (Double -> Double -> Double -> Double -> PosStoreRectangle
PSRectangle (Int32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
offsetX Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
srWh)
                                               (Int32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
offsetY Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
srHt)
                                               (Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
wh Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
srWh)
                                               (Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
ht Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
srHt)) Map Word64 PosStoreRectangle
posStoreMap

posStoreRemove :: PositionStore -> Window -> PositionStore
posStoreRemove :: PositionStore -> Word64 -> PositionStore
posStoreRemove (PS Map Word64 PosStoreRectangle
posStoreMap) Word64
w = Map Word64 PosStoreRectangle -> PositionStore
PS (Map Word64 PosStoreRectangle -> PositionStore)
-> Map Word64 PosStoreRectangle -> PositionStore
forall a b. (a -> b) -> a -> b
$ Word64
-> Map Word64 PosStoreRectangle -> Map Word64 PosStoreRectangle
forall k a. Ord k => k -> Map k a -> Map k a
M.delete Word64
w Map Word64 PosStoreRectangle
posStoreMap

posStoreQuery :: PositionStore -> Window -> Rectangle -> Maybe Rectangle
posStoreQuery :: PositionStore -> Word64 -> Rectangle -> Maybe Rectangle
posStoreQuery (PS Map Word64 PosStoreRectangle
posStoreMap) Word64
w (Rectangle Int32
srX Int32
srY Word32
srWh Word32
srHt) = do
    (PSRectangle x y wh ht) <- Word64 -> Map Word64 PosStoreRectangle -> Maybe PosStoreRectangle
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Word64
w Map Word64 PosStoreRectangle
posStoreMap
    let realWh = Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
srWh Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
wh
        realHt = Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
srHt Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
ht
        realOffsetX = Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
srWh Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
x
        realOffsetY = Word32 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
srHt Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
y
    return (Rectangle (srX + round realOffsetX) (srY + round realOffsetY)
                        (round realWh) (round realHt))

posStoreMove :: PositionStore -> Window -> Position -> Position -> Rectangle -> Rectangle -> PositionStore
posStoreMove :: PositionStore
-> Word64
-> Int32
-> Int32
-> Rectangle
-> Rectangle
-> PositionStore
posStoreMove PositionStore
posStore Word64
w Int32
x Int32
y Rectangle
oldSr Rectangle
newSr =
    case PositionStore -> Word64 -> Rectangle -> Maybe Rectangle
posStoreQuery PositionStore
posStore Word64
w Rectangle
oldSr of
        Maybe Rectangle
Nothing -> PositionStore
posStore     -- not in store, can't move -> do nothing
        Just (Rectangle Int32
_ Int32
_ Word32
wh Word32
ht) -> PositionStore -> Word64 -> Rectangle -> Rectangle -> PositionStore
posStoreInsert PositionStore
posStore Word64
w (Int32 -> Int32 -> Word32 -> Word32 -> Rectangle
Rectangle Int32
x Int32
y Word32
wh Word32
ht) Rectangle
newSr