Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X.L.MultiToggle: Add function to query toggle state #119

Merged
merged 1 commit into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@
pointer to match the focused window). Together these can be used to
ensure focus stays in sync with mouse.

- `XMonad.Layout.MultiToggle`

- Added `isToggleActive` for querying the toggle state of transformers.
Useful to show the state in a status bar.

## 0.16

### Breaking Changes
Expand Down
23 changes: 22 additions & 1 deletion XMonad/Layout/MultiToggle.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE ExistentialQuantification, Rank2Types, FunctionalDependencies, FlexibleInstances, FlexibleContexts, PatternGuards #-}
{-# LANGUAGE ExistentialQuantification, Rank2Types, FunctionalDependencies, FlexibleInstances, FlexibleContexts, PatternGuards, ScopedTypeVariables #-}

-----------------------------------------------------------------------------
-- |
Expand All @@ -25,6 +25,7 @@ module XMonad.Layout.MultiToggle (
single,
mkToggle,
mkToggle1,
isToggleActive,

HList,
HCons,
Expand All @@ -37,6 +38,7 @@ import XMonad.Prelude hiding (find)
import XMonad.StackSet (Workspace(..))

import Control.Arrow
import Data.IORef
import Data.Typeable

-- $usage
Expand Down Expand Up @@ -206,7 +208,26 @@ instance (Typeable a, Show ts, Typeable ts, HList ts a, LayoutClass l a) => Layo
currIndex = if cur then Nothing else i
}
where cur = i == currIndex mt
| Just (MultiToggleActiveQueryMessage t ref :: MultiToggleActiveQueryMessage a) <- fromMessage m
, i@(Just _) <- find (transformers mt) t
= Nothing <$ io (writeIORef ref (Just (i == currIndex mt)))
| otherwise
= case currLayout mt of
EL l det -> fmap (\x -> mt { currLayout = EL x det }) <$>
handleMessage l m

data MultiToggleActiveQueryMessage a = forall t. (Transformer t a) =>
MultiToggleActiveQueryMessage t (IORef (Maybe Bool))

instance (Typeable a) => Message (MultiToggleActiveQueryMessage a)

-- | Query the state of a 'Transformer' on a given workspace.
--
-- To query the current workspace, use something like this:
--
-- > withWindowSet (isToggleActive t . W.workspace . W.current)
isToggleActive :: Transformer t Window => t -> WindowSpace -> X (Maybe Bool)
isToggleActive t w = do
ref <- io $ newIORef Nothing
sendMessageWithNoRefresh (MultiToggleActiveQueryMessage t ref) w
io $ readIORef ref