aboutsummaryrefslogblamecommitdiffstats
path: root/XMonad/Actions/ConstrainedResize.hs
blob: 3dd15698ab53ef313bb9dfcf444c38ce2e9b3c9d (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

                                                                             
                                                  

                                          
  




                                                    
                                                    




                                                                             
                                         

                  
                                                          


             
                        



                               

                                                                              
  
                                                             


                                                                
                                                                                               
                                                                                               
  





                                                                    














                                                                                                
-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Actions.ConstrainedResize
-- Copyright   :  (c) Dougal Stanton
-- License     :  BSD3-style (see LICENSE)
--
-- Maintainer  :  <dougal@dougalstanton.net>
-- Stability   :  unstable
-- Portability :  unportable
--
-- Lets you constrain the aspect ratio of a floating
-- window (by, say, holding shift while you resize).
--
-- Useful for making a nice circular XClock window.
--
-----------------------------------------------------------------------------

module XMonad.Actions.ConstrainedResize (
	-- * Usage
	-- $usage
	XMonad.Actions.ConstrainedResize.mouseResizeWindow
) where

import XMonad
import XMonad.Operations
import Graphics.X11.Xlib
import Graphics.X11.Xlib.Extras

-- $usage
--
-- You can use this module with the following in your @~\/.xmonad\/xmonad.hs@:
--
-- > import qualified XMonad.Actions.ConstrainedResize as Sqr
--
-- Then add something like the following to your mouse bindings:
--
-- >     , ((modMask, button3),               (\w -> focus w >> Sqr.mouseResizeWindow w False))
-- >     , ((modMask .|. shiftMask, button3), (\w -> focus w >> Sqr.mouseResizeWindow w True ))
--
-- The line without the shiftMask replaces the standard mouse resize
-- function call, so it's not completely necessary but seems neater
-- this way.
--
-- For detailed instructions on editing your mouse bindings, see
-- "XMonad.Doc.Extending#Editing_mouse_bindings".

-- | Resize (floating) window with optional aspect ratio constraints.
mouseResizeWindow :: Window -> Bool -> X ()
mouseResizeWindow w c = whenX (isClient w) $ withDisplay $ \d -> do
    io $ raiseWindow d w
    wa <- io $ getWindowAttributes d w
    sh <- io $ getWMNormalHints d w
    io $ warpPointer d none w 0 0 0 0 (fromIntegral (wa_width wa)) (fromIntegral (wa_height wa))
    mouseDrag (\ex ey -> do
                 let x = ex - fromIntegral (wa_x wa)
                     y = ey - fromIntegral (wa_y wa)
                     sz = if c then (max x y, max x y) else (x,y)
                 io $ resizeWindow d w `uncurry`
                    applySizeHints sh sz)
              (float w)