X.U.DynamicScratchpads: Make any window a scratchpad

This commit is contained in:
mephory 2020-05-29 15:47:46 +02:00
parent 28e29fa238
commit ff0ab6f977
No known key found for this signature in database
GPG Key ID: A4E5D6261AC4BC85
3 changed files with 102 additions and 0 deletions

View File

@ -42,6 +42,11 @@
conditions on a window, basis. Useful for creating bindings that are conditions on a window, basis. Useful for creating bindings that are
excluded or exclusive for some windows. excluded or exclusive for some windows.
* `XMonad.Util.DynamicScratchpads`
Declare any window as a scratchpad on the fly. Once declared, the
scratchpad behaves like `XMonad.Util.NamedScratchpad`.
### Bug Fixes and Minor Changes ### Bug Fixes and Minor Changes
* `XMonad.Util.Run` * `XMonad.Util.Run`

View File

@ -0,0 +1,96 @@
-----------------------------------------------------------------------------
-- |
-- Module : XMonad.Util.DynamicScratchpads
-- Copyright : (c) Robin Oberschweiber <mephory@mephory.com>
-- License : BSD-style (see LICENSE)
--
-- Maintainer : Robin Obercshweiber <mephory@mephory.com>
-- Stability : unstable
-- Portability : unportable
--
-- Dynamically declare any window as a scratchpad.
--
-----------------------------------------------------------------------------
module XMonad.Util.DynamicScratchpads (
-- * Usage
-- $usage
makeDynamicSP,
spawnDynamicSP
) where
import Graphics.X11.Types
import XMonad.Core
import XMonad.Operations
import qualified Data.Map as M
import qualified XMonad.StackSet as W
import qualified XMonad.Util.ExtensibleState as XS
-- $usage
-- Allows you to dynamically declare windows as scratchpads. You can bind a key
-- to make a window start/stop being a scratchpad, and another key to
-- spawn/hide that scratchpad.
--
-- Like with XMonad.Util.NamedScratchpad, you have to have a workspace called
-- NSP, where hidden scratchpads will be moved to.
--
-- You can declare dynamic scrachpads in your xmonad.hs like so:
--
-- import XMonad.Util.DynamicScratchpads
--
-- , ((modm .|. shiftMask, xK_a), withFocused $ makeDynamicSP "dyn1")
-- , ((modm .|. shiftMask, xK_b), withFocused $ makeDynamicSP "dyn2")
-- , ((modm , xK_a), spawnDynamicSP "dyn1")
-- , ((modm , xK_b), spawnDynamicSP "dyn2")
-- | Stores dynamic scratchpads as a map of name to window
data SPStorage = SPStorage (M.Map String Window)
deriving (Typeable,Read,Show)
instance ExtensionClass SPStorage where
initialValue = SPStorage $ M.fromList []
extensionType = PersistentExtension
-- | Makes a window a dynamic scratchpad with the given name, or stop a window
-- | from being a dynamic scratchpad, if it already is.
makeDynamicSP :: String -- ^ Scratchpad name
-> Window -- ^ Window to be made a scratchpad
-> X ()
makeDynamicSP s w = do
(SPStorage m) <- XS.get
case M.lookup s m of
Nothing -> addDynamicSP s w
Just ow -> if w == ow
then removeDynamicSP s
else (showWindow ow >> addDynamicSP s w)
-- | Spawn the specified dynamic scratchpad
spawnDynamicSP :: String -- ^ Scratchpad name
-> X ()
spawnDynamicSP s = do
(SPStorage m) <- XS.get
case M.lookup s m of
Nothing -> mempty
Just w -> spawnDynamicSP' w
spawnDynamicSP' :: Window -> X ()
spawnDynamicSP' w = withWindowSet $ \s -> do
let matchingWindows = filter (== w) ((maybe [] W.integrate . W.stack . W.workspace . W.current) s)
case matchingWindows of
[] -> showWindow w
_ -> hideWindow w
-- | Make a window a dynamic scratchpad
addDynamicSP :: String -> Window -> X ()
addDynamicSP s w = XS.modify $ alterSPStorage (\_ -> Just w) s
-- | Make a window stop being a dynamic scratchpad
removeDynamicSP :: String -> X ()
removeDynamicSP s = XS.modify $ alterSPStorage (\_ -> Nothing) s
-- | Moves window to the scratchpad workspace, effectively hiding it
hideWindow :: Window -> X ()
hideWindow = windows . W.shiftWin "NSP"
-- vim:ts=4:shiftwidth=4:softtabstop=4:expandtab:

View File

@ -319,6 +319,7 @@ library
XMonad.Util.CustomKeys XMonad.Util.CustomKeys
XMonad.Util.DebugWindow XMonad.Util.DebugWindow
XMonad.Util.Dmenu XMonad.Util.Dmenu
XMonad.Util.DynamicScratchpads
XMonad.Util.Dzen XMonad.Util.Dzen
XMonad.Util.EZConfig XMonad.Util.EZConfig
XMonad.Util.ExclusiveScratchpads XMonad.Util.ExclusiveScratchpads