← Carl Osborne

macOS Tiling Window Manager

A tiling window manager for macOS built on a binary space partitioning tree.

Window Manager Spoon

A high-performance Hammerspoon spoon for macOS window management using Binary Space Partitioning (BSP) trees. Built for rapid keyboard-only navigation and automatic tiling that works seamlessly with native macOS features.

Table of Contents

Key Features

Automatic BSP Tiling

Multi-Space Architecture

Advanced Window Operations

Intelligent Window Filtering

Keyboard Bindings

All keyboard shortcuts use the Hyper key combination (Cmd + Alt + Ctrl + Shift). The window manager provides comprehensive keyboard control for all operations.

Navigation & Focus

Key Action Description
Hyper + A Focus Left Move focus to the window to the left
Hyper + S Focus Down Move focus to the window below
Hyper + D Focus Up Move focus to the window above
Hyper + F Focus Right Move focus to the window to the right
Hyper + Space Next Window Rotate through windows in the current stack

Window Swapping

Key Action Description
Hyper + Q Swap Left Exchange positions with the window to the left
Hyper + W Swap Down Exchange positions with the window below
Hyper + E Swap Up Exchange positions with the window above
Hyper + R Swap Right Exchange positions with the window to the right

Dynamic Resizing

Key Action Description
Hyper + Z Resize Left Continuously resize window to the left (hold key)
Hyper + X Resize Down Continuously resize window downward (hold key)
Hyper + C Resize Up Continuously resize window upward (hold key)
Hyper + V Resize Right Continuously resize window to the right (hold key)

Tree Operations

Key Action Description
Hyper + G Rotate Left Rotate the tree structure to the left
Hyper + B Rotate Right Rotate the tree structure to the right
Hyper + T Reflect Switch between horizontal and vertical splits in parent nodes

Node Management

Key Action Description
Hyper + H Gather Nodes Combine multiple child nodes into a single parent node
Hyper + N Explode Node Split a multi-window node into separate individual nodes

System Control

Key Action Description
Hyper + Y Toggle Shutdown/Restart First press: Shutdown window manager
Second press: Restart window manager

Mouse Behavior

The window manager provides intelligent mouse-based window management that works seamlessly with keyboard operations and supports multi-screen setups.

Window Movement

Drag and Drop Operations

Cross-Screen Operations

Window Resizing

Mouse-Based Resizing

Resize Behavior by Direction

Mouse Integration Features

User vs System Moves

Multi-Screen Behavior

Performance Optimizations

Event Handling

Architecture

Core Components

Tree Structure

Each macOS Space maintains an independent BSP tree with:

Component Description Properties
Root Node Contains all windows in the space with full screen dimensions position, size, leaf, windows
Internal Nodes Split the space horizontally or vertically with configurable ratios split_type, split_ratio, child1, child2
Leaf Nodes Contain actual windows, supporting multiple windows per leaf (stacking) windows[], leaf = true
Selected Node Currently active leaf node for operations Tracks current focus state
Focused Window The window that receives focus when switching spaces Frontmost window in selected node

Event System

The window manager subscribes to multiple Hammerspoon events:

Event Type Trigger Action
Window Creation New window appears Automatically adds manageable windows to current space
Window Focus Window gains focus Updates selected node and maintains focus tracking
Window Movement Window is dragged or moved Handles drag-and-drop operations and space transitions
Window Destruction Window is closed Removes windows and collapses empty nodes
Space Changes User switches spaces Switches between space trees and refreshes layouts
Window Maximization Window goes full-screen Clears focus tracking for full-screen windows
Window Minimization Window is minimized Removes from management temporarily
Window Restoration Window is unminimized Adds back to management

Performance Optimizations

Algorithm Details

Window Placement Algorithm

When a window is moved or created:

  1. Position Analysis: Calculate mouse position relative to existing windows
  2. Edge Detection: Determine which edge of a window the mouse is closest to
  3. Split Decision: Create horizontal or vertical splits based on edge proximity
  4. Stack Management: Add to existing stack if mouse is in center area
  5. Tree Update: Rebuild tree structure and apply new layout

Resize Algorithm

For dynamic window resizing:

  1. Parent Detection: Find the appropriate split parent in the tree hierarchy
  2. Ratio Calculation: Update split ratios based on window position changes
  3. Continuous Updates: Apply changes in real-time during resize operations
  4. Boundary Enforcement: Maintain valid split ratios between 0.0 and 1.0

Space Management Algorithm

For multi-space support:

  1. Space Detection: Identify the current space using macOS APIs
  2. Tree Selection: Switch to the appropriate tree for the current space
  3. Window Migration: Handle windows moving between spaces
  4. Focus Preservation: Maintain focus state across space transitions

Layout Persistence Algorithm

For saving and restoring window arrangements:

  1. Window Identification: Store window titles, app names, and indices for reconstruction
  2. Tree Serialization: Convert BSP tree structure to JSON format
  3. Lazy Reconstruction: Rebuild trees only when spaces become active
  4. Fallback Matching: Use multiple strategies to match saved windows to current windows

Configuration

Window Filtering

The window manager automatically excludes:

Window Type Examples Reason
Non-standard windows Dialogs, floating windows, system dialogs Not suitable for tiling
Minimized windows Hidden or minimized windows Not visible to user
Full-screen windows Maximized applications Create their own space
System applications Raycast, System Settings, Spotlight, Dock, Control Center, Notification Center System UI elements
Invalid windows Windows with nil IDs or invalid states Prevent errors

Performance Settings

Setting Value Purpose
Animation Duration 0.0 Instant window movements for responsiveness
Event Throttling 1 second Minimum time between window move events
Refresh Control Enabled Prevents recursive refresh calls during operations
Re-entrancy Protection Enabled Prevents layout application loops

Layout Persistence

Aspect Configuration Description
Save Path ~/.hammerspoon/window-manager.layout.json JSON file storing tree structures
Auto-Save Triggers Sleep, stop, window destruction Automatic saving on system events
Auto-Load Triggers Wake, startup, space changes Automatic loading on system events
Window Matching Multiple strategies ID, title+app, app+index, app-only fallback
Lazy Loading Per-space Only loads trees when spaces become active

Integration

macOS Compatibility

Feature Support Level Description
Native Space Support Full Works with macOS Spaces and Mission Control
Multi-Monitor Support Full Handles multiple displays and their spaces
System Integration Full Respects macOS window management conventions
Accessibility Compatible Works with VoiceOver and other accessibility features
Window States Full Handles minimized, full-screen, and normal windows
Drag and Drop Full Supports native macOS window dragging

Hammerspoon Integration

Component Integration Description
Spoon Architecture Native Follows Hammerspoon spoon conventions
Event System Native Uses Hammerspoon's window filter and space watcher APIs
Configuration Native Integrates with Hammerspoon's configuration system
Logging Native Uses Hammerspoon's logging system for debugging
Hotkey System Native Uses Hammerspoon's hotkey binding system
Timer System Native Uses Hammerspoon's timer system for throttling

Performance Characteristics

Memory Usage

Aspect Efficiency Details
Minimal Overhead High Only stores essential window and tree data
Efficient Storage High Uses UUIDs for node identification
Garbage Collection Automatic Automatic cleanup of orphaned references
Tree Size O(n) Linear with number of windows
Space Isolation Per-space Each space maintains independent trees

CPU Usage

Aspect Efficiency Details
Event-Driven High Only processes events when windows change
Throttled Operations High Prevents excessive processing during rapid changes
Lazy Evaluation High Defers expensive operations until necessary
Algorithm Complexity O(log n) BSP tree operations are logarithmic
Layout Application O(n) Linear with number of windows

Responsiveness

Aspect Performance Details
Instant Feedback Excellent Window operations provide immediate visual feedback
Smooth Animations Excellent Zero-duration animations for instant movement
Non-Blocking Excellent Operations don't block the main thread
Event Latency < 16ms Sub-frame timing for smooth operation
Memory Pressure Low Minimal memory footprint per window

Technical Implementation

Data Structures

Structure Purpose Key Properties
Node Class Represents tree nodes with position, size, and window data id, leaf, position, size, windows[], parent, child1, child2
Tree Objects Contain root node, selected node, and focused window root, selected
Space Mapping Maps space IDs to tree objects for multi-space support trees[space_id] = tree
Window Tracking Tracks window positions and states _lastWindowPositions[window_id]

Error Handling

Strategy Implementation Benefit
Robust Window Operations Uses pcall for safe window manipulation Prevents crashes from invalid windows
Graceful Degradation Continues operation even if individual windows fail Maintains system stability
State Validation Validates window and tree states before operations Prevents invalid state propagation
Exception Recovery Catches and logs errors without stopping execution Provides debugging information

Memory Management

Aspect Implementation Benefit
Reference Tracking Maintains proper parent-child relationships Prevents memory leaks
Cleanup Operations Removes orphaned nodes and invalid references Keeps memory usage low
Garbage Collection Leverages Lua's garbage collector for cleanup Automatic memory management
Lazy Loading Only loads trees when needed Reduces initial memory footprint

Key Algorithms

BSP Tree Operations

Window Management

Conclusion

This window manager represents a fast and feature-complete solution for macOS window management, providing both automatic tiling and powerful manual control while maintaining full compatibility with native macOS features. The implementation demonstrates:

This window manager, completed in four days, represents the a very fast and feature-complete solution for macOS window management, providing both automatic tiling and powerful manual control while maintaining full compatibility with native macOS features.