1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
module Rahm.Desktop.Dragging where
import Control.Arrow ((&&&))
import qualified Control.Exception as C
import Control.Monad (filterM, forM_, when)
import Control.Monad.Trans (lift)
import Control.Monad.Trans.Maybe (MaybeT (MaybeT))
import qualified Data.Foldable as Foldable
import Data.IORef (newIORef, readIORef, writeIORef)
import Data.Maybe (fromMaybe, isJust, mapMaybe)
import qualified Data.Set as Set
import Graphics.X11.Xlib.Extras
import Rahm.Desktop.BorderColors (BorderColor (BorderColor), setBorderColor)
import Rahm.Desktop.Common (floatAll, pointerLocation, pointerWindow, runMaybeT_)
import Rahm.Desktop.Layout.Hole (addHoleForWindow, removeHoleForWindow, resetHole)
import Rahm.Desktop.Layout.PinWindow (isWindowPinned, pinWindow, unpinWindow)
import Rahm.Desktop.Logger
import Rahm.Desktop.Marking (setAlternateWindows)
import qualified Rahm.Desktop.StackSet as W
import XMonad (X, io)
import qualified XMonad as X
import XMonad.Util.WindowProperties (getProp32s)
import Rahm.Desktop.Workspaces (accompanyingWorkspace)
-- Action which happens after a dragging event.
--
-- The window is the window under
newtype AfterDragAction = AfterDragAction (X.Window -> X.Button -> X ())
instance Semigroup AfterDragAction where
(AfterDragAction f1) <> (AfterDragAction f2) =
AfterDragAction $ \w b -> f1 w b >> f2 w b
instance Monoid AfterDragAction where
mappend = (<>)
mempty = AfterDragAction $ \_ _ -> return ()
dragBorderColor = BorderColor "#00ffff" "#80a0a0"
afterDrag :: X () -> X ()
afterDrag action = do
X.modify $ \case
st@(X.XState {X.dragging = Just (fn, cleanup)}) ->
st {X.dragging = Just (fn, cleanup >> action)}
s -> s
isDragging :: X Bool
isDragging = X.gets (isJust . X.dragging)
finishDrag :: X ()
finishDrag = runMaybeT_ $ do
(_, cleanup) <- MaybeT $ X.gets X.dragging
X.modify $ \s -> s {X.dragging = Nothing}
lift cleanup
windowsUnderCursor :: X [X.Window]
windowsUnderCursor = do
(dpy, root) <- X.asks $ (,) <$> X.display <*> X.theRoot
aw <- X.withWindowSet (return . W.allVisibleWindows)
let fi :: (Integral a, Integral b) => a -> b
fi = fromIntegral
(cx, cy) <- pointerLocation
filterM
( \win -> do
(_, fi -> x, fi -> y, fi -> w, fi -> h, _, _) <- io $ X.getGeometry dpy win
return
( cx > x
&& cx < x + fi w
&& cy > y
&& cy < y + h
)
)
aw
sinkOnRelease :: AfterDragAction
sinkOnRelease = AfterDragAction $ \window _ -> sinkByWindowUnderCursor window
sinkByWindowUnderCursor :: X.Window -> X ()
sinkByWindowUnderCursor window = do
unpinWindow window
(cx, cy) <- pointerLocation
wins <- filter (/= window) <$> windowsUnderCursor
t <- fmap (W.tag . W.workspace) <$> X.pointScreen cx cy
X.windows $
W.focusWindow window
. case wins of
(h : _) -> W.sinkBy window h
_ -> W.sink window
. ( \ss -> case t of
Nothing -> ss
Just t' -> W.shiftWin t' window ss
)
where
fi = fromIntegral
ifReleased :: X.Button -> AfterDragAction -> AfterDragAction
ifReleased but (AfterDragAction act) = AfterDragAction $ \win b -> do
when (but == b) $ act win b
-- Like ifReleased, but with a function instead of an AfterDragAction
ifReleased' :: X.Button -> (X.Window -> X.Button -> X ()) -> AfterDragAction
ifReleased' but act = ifReleased but (AfterDragAction act)
mouseResizeWindowAndThen ::
(X.Window -> X ()) ->
AfterDragAction ->
X.Window ->
X ()
mouseResizeWindowAndThen beforeAction (AfterDragAction releaseAction) window = do
beforeAction window
windowPinned <- isWindowPinned window
unpinWindow window
tilePosition <- X.withWindowSet $ return . W.windowTilePosition window
mapM_ (X.broadcastMessage . flip addHoleForWindow window) tilePosition
X.mouseResizeWindow window
afterDrag $ do
X.broadcastMessage $ removeHoleForWindow window
curev <- X.asks X.currentEvent
when windowPinned $
pinWindow window
releaseAction
window
( maybe
0
( \case
X.ButtonEvent {X.ev_button = b} -> b
_ -> 0
)
curev
)
X.refresh
mouseMoveWindowsAndThen ::
(Foldable f) =>
(f X.Window -> X ()) ->
AfterDragAction ->
f X.Window ->
X ()
mouseMoveWindowsAndThen beforeAction (AfterDragAction releaseAction) windowsF = do
let windows = Foldable.toList windowsF
beforeAction windowsF
pinnedWindows <- Set.fromList <$> filterM isWindowPinned windows
mapM_ unpinWindow windows
tilePositions <- X.withWindowSet $
\ws -> return $ mapMaybe (\win -> (win,) <$> W.windowTilePosition win ws) windows
mapM_
( \(win, pos) ->
X.broadcastMessage $ addHoleForWindow pos win
)
tilePositions
case windows of
[win] -> X.mouseMoveWindow win
_ -> X.withDisplay $ \d -> do
floatAll windows
was <-
io $
mapM
( \w -> C.handle (\(C.SomeException _) -> return Nothing) $ do
wa <- X.getWindowAttributes d w
(_, _, _, fi -> ox, fi -> oy, _, _, _) <- io $ X.queryPointer d w
return (Just (w, wa, ox, oy))
)
windows
X.mouseDrag
( \ex ey -> do
forM_ was $ \case
Just (w, wa, ox, oy) -> do
io $
X.moveWindow
d
w
(fromIntegral (fromIntegral (wa_x wa) + (ex - ox)))
(fromIntegral (fromIntegral (wa_y wa) + (ey - oy)))
Nothing -> return ()
)
(floatAll windows)
afterDrag $ do
mapM_ pinWindow pinnedWindows
curev <- X.asks X.currentEvent
forM_ windows $ \window -> do
X.broadcastMessage $ removeHoleForWindow window
releaseAction
window
( maybe
0
( \case
X.ButtonEvent {X.ev_button = b} -> b
_ -> 0
)
curev
)
X.refresh
where
fi = fromIntegral
dragWorkspace :: X ()
dragWorkspace = do
(ox, oy) <- pointerLocation
X.mouseDrag (\_ _ -> return ()) $ do
(nx, ny) <- pointerLocation
runMaybeT_ $ do
(W.Screen (W.tag -> ws1) _ _) <- MaybeT $ X.pointScreen ox oy
(W.Screen (W.tag -> ws2) _ _) <- MaybeT $ X.pointScreen nx ny
lift $ X.windows $ W.switchWorkspaces ws1 ws2
dragAlternateWorkspace :: X ()
dragAlternateWorkspace = do
(ox, oy) <- pointerLocation
X.mouseDrag (\_ _ -> return ()) $ do
(nx, ny) <- pointerLocation
runMaybeT_ $ do
(W.Screen (W.tag -> ws1) _ _) <- MaybeT $ X.pointScreen ox oy
(W.Screen (W.tag -> ws2) _ _) <- MaybeT $ X.pointScreen nx ny
lift $ X.windows $ W.switchWorkspaces (accompanyingWorkspace ws1) ws2
dragWindow :: X ()
dragWindow = do
w <- pointerWindow
isDockOrRoot <- windowIsDockOrRoot w
if isDockOrRoot
then dragWorkspace
else do
cleanup <- setBorderColor dragBorderColor [w]
ref <- io $ newIORef (w, return ())
X.mouseDrag
( \_ _ -> do
w' <- pointerWindow
(ow, ocleanup) <- io $ readIORef ref
case () of
() | ow /= w' -> do
ocleanup
cleanup' <-
if w' == 0 || w' == w
then return (return ())
else setBorderColor dragBorderColor [w']
io $ writeIORef ref (w', cleanup')
() -> return ()
return ()
)
$ do
(nx, ny) <- pointerLocation
w' <- pointerWindow
(_, iocleanup) <- io $ readIORef ref
isDockOrRoot <- windowIsDockOrRoot w'
if isDockOrRoot
then runMaybeT_ $ do
(W.Screen (W.tag -> ws1) _ _) <- MaybeT $ X.pointScreen nx ny
lift $ do
setAlternateWindows [w]
X.windows $ W.sink w . W.shiftWin ws1 w
else do
X.windows $ W.focusWindow w . W.swapWindows [(w, w')]
setAlternateWindows [w, w']
iocleanup
cleanup
where
windowIsDockOrRoot 0 = return True
windowIsDockOrRoot w = do
dock <- X.getAtom "_NET_WM_WINDOW_TYPE_DOCK"
desk <- X.getAtom "_NET_WM_WINDOW_TYPE_DESKTOP"
mbr <-
if w == 0
then return Nothing
else getProp32s "_NET_WM_WINDOW_TYPE" w
return $ maybe False (any ((`elem` [dock, desk]) . fromIntegral)) mbr
|