Commits

Brian McKenna  committed 8a47b06

Sync OS X's and XMonad's focused window

  • Participants
  • Parent commits e8935bc

Comments (0)

Files changed (4)

File OSXMonad/Core.hs

 updateWindow window = do
   with window setWindow
 
+focusWindow :: Window -> IO ()
+focusWindow window = do
+  with window setWindowFocused
+
 rectangleWindow :: Rectangle -> Window -> Window
 rectangleWindow (Rectangle x y w h) win =
     win { pos = pos', size = size' }
   (rectangles, _) <- C.runLayout (S.workspace . S.current $ ws) rect
 
   let namedWindowsById = zip wids namedWindows
+      focusedWindow = S.peek newStack >>= flip lookup namedWindowsById
       windows' = Maybe.catMaybes
                  $ map (\(i, r) ->
                         fmap (rectangleWindow r) (lookup i namedWindowsById)
                        ) rectangles
 
+  maybe (return ()) (XM.io . focusWindow) focusedWindow
+
   if null namedWindows
      then return ()
      else XM.io $ mapM_ updateWindow windows'

File OSXMonad/Window.hsc

 foreign import ccall "ApplicationServices/ApplicationServices.h AXAPIEnabled"
   axAPIEnabled :: IO Bool
 
+foreign import ccall "utils.h setWindowFocused"
+  setWindowFocused :: Ptr Window -> IO ()
+
 foreign import ccall "utils.h setWindow"
   setWindow :: Ptr Window -> IO ()
 
 int getWindows(Windows *);
 void freeWindows(Windows *);
 void setWindow(Window *);
+void setWindowFocused(Window *);
 void getFrame(CGPoint *, CGSize *);
 void setupEventCallback();
 void collectEvent();
     setWindowSize(window->size, window->uiElement);
 }
 
+void setWindowFocused(Window *window) {
+    AXUIElementSetAttributeValue(window->uiElement, kAXMainAttribute, kCFBooleanTrue);
+
+    AXUIElementRef application;
+    AXUIElementCopyAttributeValue(window->uiElement, kAXParentAttribute, &application);
+    AXUIElementSetAttributeValue(application, kAXFrontmostAttribute, kCFBooleanTrue);
+}
+
 void addWindows(CFArrayRef windows, Windows *context, int *count) {
     int j;
     for(j = 0; j < CFArrayGetCount(windows) && *count < WINDOWS_ELEMENTS_LENGTH; j++) {