-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.FlexibleResize
-- Description :  Resize floating windows from any corner.
-- Copyright   :  (c) Lukas Mai
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  <l.mai@web.de>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Resize floating windows from any corner.
--
-----------------------------------------------------------------------------

module XMonad.Actions.FlexibleResize (
        -- * Usage
        -- $usage
        XMonad.Actions.FlexibleResize.mouseResizeWindow,
        XMonad.Actions.FlexibleResize.mouseResizeEdgeWindow
) where

import XMonad
import XMonad.Prelude (fi)
import Foreign.C.Types

-- $usage
-- To use, first import this module into your @xmonad.hs@ file:
--
-- > import qualified XMonad.Actions.FlexibleResize as Flex
--
-- Then add an appropriate mouse binding:
--
-- >     , ((modm, button3), (\w -> focus w >> Flex.mouseResizeWindow w))
--
-- For detailed instructions on editing your mouse bindings, see
-- "XMonad.Doc.Extending#Editing_mouse_bindings".

-- | Resize a floating window from whichever corner the mouse is
--   closest to.
mouseResizeWindow
  :: Window -- ^ The window to resize.
  -> X ()
mouseResizeWindow :: Window -> X ()
mouseResizeWindow = Ratio Integer -> Window -> X ()
mouseResizeEdgeWindow Ratio Integer
0


-- | Resize a floating window from whichever corner or edge the mouse is
--   closest to.
mouseResizeEdgeWindow
  :: Rational -- ^ The size of the area where only one edge is resized.
  -> Window   -- ^ The window to resize.
  -> X ()
mouseResizeEdgeWindow :: Ratio Integer -> Window -> X ()
mouseResizeEdgeWindow Ratio Integer
edge Window
w = X Bool -> X () -> X ()
whenX (Window -> X Bool
isClient Window
w) (X () -> X ()) -> X () -> X ()
forall a b. (a -> b) -> a -> b
$ (Display -> X ()) -> X ()
forall a. (Display -> X a) -> X a
withDisplay ((Display -> X ()) -> X ()) -> (Display -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \Display
d ->
  Display -> Window -> (WindowAttributes -> X ()) -> X ()
withWindowAttributes Display
d Window
w ((WindowAttributes -> X ()) -> X ())
-> (WindowAttributes -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$ \WindowAttributes
wa -> do
    sh <- IO SizeHints -> X SizeHints
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO SizeHints -> X SizeHints) -> IO SizeHints -> X SizeHints
forall a b. (a -> b) -> a -> b
$ Display -> Window -> IO SizeHints
getWMNormalHints Display
d Window
w
    (_, _, _, _, _, ix, iy, _) <- io $ queryPointer d w
    let
        pos_x  = CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (CInt -> Int32) -> CInt -> Int32
forall a b. (a -> b) -> a -> b
$ WindowAttributes -> CInt
wa_x WindowAttributes
wa
        pos_y  = CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (CInt -> Int32) -> CInt -> Int32
forall a b. (a -> b) -> a -> b
$ WindowAttributes -> CInt
wa_y WindowAttributes
wa
        width  = CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (CInt -> Int32) -> CInt -> Int32
forall a b. (a -> b) -> a -> b
$ WindowAttributes -> CInt
wa_width WindowAttributes
wa
        height = CInt -> Int32
forall a b. (Integral a, Num b) => a -> b
fi (CInt -> Int32) -> CInt -> Int32
forall a b. (a -> b) -> a -> b
$ WindowAttributes -> CInt
wa_height WindowAttributes
wa
        west  = CInt -> Int32 -> Maybe Bool
findPos CInt
ix Int32
width
        north = CInt -> Int32 -> Maybe Bool
findPos CInt
iy Int32
height
        (cx, fx, gx) = mkSel west  width  pos_x
        (cy, fy, gy) = mkSel north height pos_y
    io $ warpPointer d none w 0 0 0 0 cx cy
    mouseDrag (\Int32
ex Int32
ey -> do let (Word32
nw,Word32
nh) = SizeHints -> (Word32, Word32) -> (Word32, Word32)
forall a. Integral a => SizeHints -> (a, a) -> (Word32, Word32)
applySizeHintsContents SizeHints
sh (Int32 -> Word32
gx Int32
ex, Int32 -> Word32
gy Int32
ey)
                            IO () -> X ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
io (IO () -> X ()) -> IO () -> X ()
forall a b. (a -> b) -> a -> b
$ Display -> Window -> Int32 -> Int32 -> Word32 -> Word32 -> IO ()
moveResizeWindow Display
d Window
w (Word32 -> Int32
fx Word32
nw) (Word32 -> Int32
fy Word32
nh) Word32
nw Word32
nh
                            Window -> X ()
float Window
w)
              (float w)
    where
    findPos :: CInt -> Position -> Maybe Bool
    findPos :: CInt -> Int32 -> Maybe Bool
findPos CInt
m Int32
s
      | Ratio Integer
p Ratio Integer -> Ratio Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Ratio Integer
0.5 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
- Ratio Integer
edgeRatio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/Ratio Integer
2 = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
      | Ratio Integer
p Ratio Integer -> Ratio Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Ratio Integer
0.5 Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Num a => a -> a -> a
+ Ratio Integer
edgeRatio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/Ratio Integer
2 = Maybe Bool
forall a. Maybe a
Nothing
      | Bool
otherwise = Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
False
      where
          p :: Ratio Integer
p = CInt -> Ratio Integer
forall a b. (Integral a, Num b) => a -> b
fi CInt
m Ratio Integer -> Ratio Integer -> Ratio Integer
forall a. Fractional a => a -> a -> a
/ Int32 -> Ratio Integer
forall a b. (Integral a, Num b) => a -> b
fi Int32
s
    mkSel :: Maybe Bool -> Position -> Position -> (Position, Dimension -> Position, Position -> Dimension)
    mkSel :: Maybe Bool
-> Int32 -> Int32 -> (Int32, Word32 -> Int32, Int32 -> Word32)
mkSel Maybe Bool
b Int32
k Int32
p = case Maybe Bool
b of
                      Just Bool
True ->  (Int32
0, (Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int32
k Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Int32
p Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
-)(Int32 -> Int32) -> (Word32 -> Int32) -> Word32 -> Int32
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi, (Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int32
k Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
+ Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int32
p Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
-)(Word32 -> Word32) -> (Int32 -> Word32) -> Int32 -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi)
                      Maybe Bool
Nothing ->    (Int32
k Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`div` Int32
2, Int32 -> Word32 -> Int32
forall a b. a -> b -> a
const Int32
p, Word32 -> Int32 -> Word32
forall a b. a -> b -> a
const (Word32 -> Int32 -> Word32) -> Word32 -> Int32 -> Word32
forall a b. (a -> b) -> a -> b
$ Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int32
k)
                      Just Bool
False -> (Int32
k, Int32 -> Word32 -> Int32
forall a b. a -> b -> a
const Int32
p, Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
subtract (Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Int32
p) (Word32 -> Word32) -> (Int32 -> Word32) -> Int32 -> Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi)