From 07252ce0461d8746481881dbcc6ca07b71fd8553 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Sun, 24 Apr 2022 21:06:10 -0600 Subject: Roll Windows.hs into R.D.StackSet --- src/Rahm/Desktop/Common.hs | 1 - src/Rahm/Desktop/Keys.hs | 7 +-- src/Rahm/Desktop/Layout.hs | 1 - src/Rahm/Desktop/Layout/ConsistentMosaic.hs | 3 +- src/Rahm/Desktop/Layout/Hole.hs | 3 +- src/Rahm/Desktop/Marking.hs | 1 - src/Rahm/Desktop/StackSet.hs | 70 ++++++++++++++++++++++- src/Rahm/Desktop/SwapMaster.hs | 7 +-- src/Rahm/Desktop/Windows.hs | 86 ----------------------------- 9 files changed, 77 insertions(+), 102 deletions(-) delete mode 100644 src/Rahm/Desktop/Windows.hs (limited to 'src/Rahm') diff --git a/src/Rahm/Desktop/Common.hs b/src/Rahm/Desktop/Common.hs index 273984e..8790d84 100644 --- a/src/Rahm/Desktop/Common.hs +++ b/src/Rahm/Desktop/Common.hs @@ -23,7 +23,6 @@ import Rahm.Desktop.DMenu import Data.Ord (comparing) import qualified Rahm.Desktop.StackSet as S -import Rahm.Desktop.Windows -- A location is a workspace and maybe a window with that workspace. data Location = Location { diff --git a/src/Rahm/Desktop/Keys.hs b/src/Rahm/Desktop/Keys.hs index 0f61018..8cb2b76 100644 --- a/src/Rahm/Desktop/Keys.hs +++ b/src/Rahm/Desktop/Keys.hs @@ -61,7 +61,6 @@ import Rahm.Desktop.RebindKeys import Rahm.Desktop.Submap import Rahm.Desktop.Swallow import Rahm.Desktop.SwapMaster (swapMaster) -import Rahm.Desktop.Windows import Rahm.Desktop.Workspaces import qualified Rahm.Desktop.StackSet as W import Rahm.Desktop.History @@ -174,9 +173,9 @@ keymap = runKeys $ do -- focused. It's pretty annoying because it keeps me from doing some cool -- things all for BS security theater, but I guess there might be some way -- to do this via XTest? - shiftMod $ forAllWindows $ \w -> do + shiftMod $ withWindowSet $ mapM_ (\w -> do logs Info "Try send to %s" (show w) - sendKey (0, xK_a) w + sendKey (0, xK_a) w) . W.allWindows justMod $ doc "Print this documentation" @@ -888,7 +887,7 @@ windowBindings :: XConfig l -> XConfig l windowBindings xconfig = xconfig { startupHook = do - forAllWindows (runQuery doQuery) + withWindowSet $ mapM_ (runQuery doQuery) . W.allWindows startupHook xconfig, manageHook = (doQuery >> return (Endo id)) <> manageHook xconfig diff --git a/src/Rahm/Desktop/Layout.hs b/src/Rahm/Desktop/Layout.hs index ea80ba9..ad54d4a 100644 --- a/src/Rahm/Desktop/Layout.hs +++ b/src/Rahm/Desktop/Layout.hs @@ -26,7 +26,6 @@ import XMonad.Layout.NoBorders (smartBorders, noBorders) import Rahm.Desktop.Layout.CornerLayout (Corner(..)) import Rahm.Desktop.Layout.List -import Rahm.Desktop.Windows import Rahm.Desktop.Layout.ReinterpretMessage import Rahm.Desktop.Layout.Pop import Rahm.Desktop.Layout.Flip diff --git a/src/Rahm/Desktop/Layout/ConsistentMosaic.hs b/src/Rahm/Desktop/Layout/ConsistentMosaic.hs index 0a6215a..3dbc44c 100644 --- a/src/Rahm/Desktop/Layout/ConsistentMosaic.hs +++ b/src/Rahm/Desktop/Layout/ConsistentMosaic.hs @@ -13,7 +13,6 @@ import Data.Maybe (mapMaybe) import XMonad.Layout.MosaicAlt -import Rahm.Desktop.Windows import Rahm.Desktop.Logger @@ -40,7 +39,7 @@ instance (LayoutClass l a, Show a, Ord a, Enum a, Num a) => LayoutClass (MosaicW runLayout (W.Workspace t (MosaicWrap l) (id -> Just s)) rect = do let zs = zipStack [100..] s - s' = mapStack fst zs + s' = fmap fst zs m = Map.fromList (W.integrate zs) (rects, maybeNewLayout) <- runLayout (W.Workspace t l (Just s')) rect diff --git a/src/Rahm/Desktop/Layout/Hole.hs b/src/Rahm/Desktop/Layout/Hole.hs index f6632d5..fe48340 100644 --- a/src/Rahm/Desktop/Layout/Hole.hs +++ b/src/Rahm/Desktop/Layout/Hole.hs @@ -3,11 +3,10 @@ -- Delegates to a lower layout, but leaves a hole where the next window will go. module Rahm.Desktop.Layout.Hole (hole, toggleHole) where -import qualified Rahm.Desktop.StackSet as W import XMonad import Data.Maybe (mapMaybe) -import Rahm.Desktop.Windows +import qualified Rahm.Desktop.StackSet as W data Hole (l :: * -> *) (a :: *) = Hole Bool (l a) diff --git a/src/Rahm/Desktop/Marking.hs b/src/Rahm/Desktop/Marking.hs index 9bc2cb6..4da2a46 100644 --- a/src/Rahm/Desktop/Marking.hs +++ b/src/Rahm/Desktop/Marking.hs @@ -29,7 +29,6 @@ import Data.Sequence (Seq(..)) import Rahm.Desktop.Common import Rahm.Desktop.History import Rahm.Desktop.Hooks.WindowChange -import Rahm.Desktop.Windows (mapWindows, findWindow, getLocationWorkspace) import Rahm.Desktop.Workspaces import System.Environment import System.FilePath diff --git a/src/Rahm/Desktop/StackSet.hs b/src/Rahm/Desktop/StackSet.hs index 8db16c1..652dafe 100644 --- a/src/Rahm/Desktop/StackSet.hs +++ b/src/Rahm/Desktop/StackSet.hs @@ -7,6 +7,12 @@ module Rahm.Desktop.StackSet ( shiftWin, screenRotateBackward, screenRotateForward, + mapWindows, + swapWindows, + getLocationWorkspace, + WindowLocation(..), + windowMemberOfWorkspace, + findWindow, module W) where import Prelude hiding (head) @@ -15,7 +21,37 @@ import Data.List (find) import XMonad.StackSet as W hiding (greedyView, shiftWin) import qualified XMonad.StackSet import Data.Default -import Data.Maybe (fromMaybe) +import Data.Maybe (fromMaybe, catMaybes, listToMaybe) +import qualified Data.Map as Map + +data WindowLocation i l a s sd = + OnScreen (Screen i l a s sd) | + OnHiddenWorkspace (Workspace i l a) | + Floating + +getLocationWorkspace :: WindowLocation i l a s sd -> Maybe (Workspace i l a) +getLocationWorkspace (OnScreen (Screen w _ _)) = Just w +getLocationWorkspace (OnHiddenWorkspace w) = Just w +getLocationWorkspace _ = Nothing + +mapWindows :: (Ord a, Ord b) => (a -> b) -> StackSet i l a s sd -> StackSet i l b s sd +mapWindows fn (StackSet cur vis hid float) = + StackSet + (mapWindowsScreen cur) + (map mapWindowsScreen vis) + (map mapWindowsWorkspace hid) + (Map.mapKeys fn float) + where + mapWindowsScreen (Screen work a b) = Screen (mapWindowsWorkspace work) a b + mapWindowsWorkspace (Workspace t l stack) = + Workspace t l (fmap (fmap fn) stack) + +swapWindows :: (Ord a) => a -> a -> StackSet i l a s d -> StackSet i l a s d +swapWindows wa wb = mapWindows $ \w -> + case w of + _ | w == wa -> wb + _ | w == wb -> wa + _ -> w masterWindow :: StackSet i l a s sd -> Maybe a masterWindow = head . integrate' . stack . workspace . current @@ -67,3 +103,35 @@ screenRotateForward (W.StackSet current visible others floating) = do in W.StackSet current' visible' others floating where rcycle l = last l : l + +{- Finds a Window and returns the screen its on and the workspace its on. + - Returns nothing if the window doesn't exist. + - + - If the window is not a screen Just (Nothing, workspace) is returned. + - If the window is a floating window Just (Nothing, Nothing) is returned. -} +findWindow :: + (Eq a) => StackSet i l a s sd -> a -> Maybe (WindowLocation i l a s sd) +findWindow (StackSet cur vis hid float) win = + listToMaybe . catMaybes $ + map findWindowScreen (cur : vis) ++ + map findWindowWorkspace hid ++ + [findWindowFloat] + + where + findWindowScreen s@(Screen ws _ _) = + if windowMemberOfWorkspace ws win + then Just (OnScreen s) + else Nothing + + findWindowWorkspace w = + if windowMemberOfWorkspace w win + then Just (OnHiddenWorkspace w) + else Nothing + + findWindowFloat = + if win `elem` Map.keys float + then Just Floating + else Nothing + +windowMemberOfWorkspace :: (Eq a) => Workspace i l a -> a -> Bool +windowMemberOfWorkspace (Workspace _ _ s) w = w `elem` integrate' s diff --git a/src/Rahm/Desktop/SwapMaster.hs b/src/Rahm/Desktop/SwapMaster.hs index fd61a50..96417ed 100644 --- a/src/Rahm/Desktop/SwapMaster.hs +++ b/src/Rahm/Desktop/SwapMaster.hs @@ -3,7 +3,6 @@ module Rahm.Desktop.SwapMaster (swapMaster) where import qualified Rahm.Desktop.StackSet as W -import Rahm.Desktop.Windows (mapWindows, getMaster, swapWindows) import Control.Monad.Trans.Maybe import XMonad (Window, ExtensionClass(..), X(..), windows, windowset) import Control.Monad (void) @@ -28,13 +27,13 @@ swapMaster = void $ runMaybeT $ do ss <- gets windowset focused <- hoist $ W.peek ss - master <- hoist $ getMaster ss + master <- hoist $ W.masterWindow ss if focused == master then do lw <- MaybeT $ lastWindow <$> XS.get - lift $ windows (swapWindows focused lw) - else lift $ windows (swapWindows focused master) + lift $ windows (W.swapWindows focused lw) + else lift $ windows (W.swapWindows focused master) lift $ do XS.put (LastWindow $ Just master) diff --git a/src/Rahm/Desktop/Windows.hs b/src/Rahm/Desktop/Windows.hs deleted file mode 100644 index 2aa5995..0000000 --- a/src/Rahm/Desktop/Windows.hs +++ /dev/null @@ -1,86 +0,0 @@ -module Rahm.Desktop.Windows where - -import XMonad (windowset, X, Window, get) - -import Control.Applicative ((<|>)) -import Rahm.Desktop.StackSet (Stack(..), StackSet(..), Screen(..), Workspace(..), peek, integrate, integrate', allWindows) -import Data.Maybe (listToMaybe, catMaybes) -import qualified Data.Map as Map - -mapWindows :: (Ord a, Ord b) => (a -> b) -> StackSet i l a s sd -> StackSet i l b s sd -mapWindows fn (StackSet cur vis hid float) = - StackSet - (mapWindowsScreen cur) - (map mapWindowsScreen vis) - (map mapWindowsWorkspace hid) - (Map.mapKeys fn float) - where - mapWindowsScreen (Screen work a b) = Screen (mapWindowsWorkspace work) a b - mapWindowsWorkspace (Workspace t l stack) = - Workspace t l (fmap (mapStack fn) stack) - --- | What genius decided to hide the instances for the Stack type!!??? -mapStack :: (a -> b) -> Stack a -> Stack b -mapStack fn (Stack focus up down) = Stack (fn focus) (map fn up) (map fn down) - -getMaster :: StackSet i l a s sd -> Maybe a -getMaster (StackSet (Screen (Workspace _ _ ss) _ _) _ _ _) = - head . integrate <$> ss - -swapWindows :: (Ord a) => a -> a -> StackSet i l a s d -> StackSet i l a s d -swapWindows wa wb = mapWindows $ \w -> - case w of - _ | w == wa -> wb - _ | w == wb -> wa - _ -> w - -data WindowLocation i l a s sd = - OnScreen (Screen i l a s sd) | - OnHiddenWorkspace (Workspace i l a) | - Floating - -getLocationWorkspace :: WindowLocation i l a s sd -> Maybe (Workspace i l a) -getLocationWorkspace (OnScreen (Screen w _ _)) = Just w -getLocationWorkspace (OnHiddenWorkspace w) = Just w -getLocationWorkspace _ = Nothing - -workspaceMember :: (Eq a) => Workspace i l a -> a -> Bool -workspaceMember (Workspace _ _ s) w = w `elem` integrate' s - -forAllWindows :: (Window -> X ()) -> X () -forAllWindows fn = do - stackSet <- windowset <$> get - mapM_ fn (allWindows stackSet) - -getFocusedWindow :: X (Maybe Window) -getFocusedWindow = do - peek . windowset <$> get - -{- Finds a Window and returns the screen its on and the workspace its on. - - Returns nothing if the window doesn't exist. - - - - If the window is not a screen Just (Nothing, workspace) is returned. - - If the window is a floating window Just (Nothing, Nothing) is returned. -} -findWindow :: - (Eq a) => StackSet i l a s sd -> a -> Maybe (WindowLocation i l a s sd) -findWindow (StackSet cur vis hid float) win = - listToMaybe . catMaybes $ - map findWindowScreen (cur : vis) ++ - map findWindowWorkspace hid ++ - [findWindowFloat] - - where - findWindowScreen s@(Screen ws _ _) = - if workspaceMember ws win - then Just (OnScreen s) - else Nothing - - findWindowWorkspace w = - if workspaceMember w win - then Just (OnHiddenWorkspace w) - else Nothing - - findWindowFloat = - if win `elem` Map.keys float - then Just Floating - else Nothing -- cgit