Skip to content
Open
16 changes: 12 additions & 4 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,25 @@ Scan flow: User picks folder → **ScanCoordinator** launches **FileScanner**
- **App/** — Entry point (`MacDirStatApp`) and `AppState` central state management
- **Scanning/** — `FileScanner` (parallel POSIX traversal with inode dedup, skips symlinks and other volumes, allocated size via blocks×512, thread-scoped materialization opt-out, cancellation propagation, explicit incomplete-folder reporting), `ScanCoordinator` (async orchestration, throttling, cancellation), `FileNode` (tree model with weak parent refs to avoid retain cycles, recursive aggregate computation)
- **Categorization/** — `FileCategory` (10 categories with colors/SF Symbols) and `FileExtensionMap` (~190 extension→category mappings; each extension belongs to one category, keys are lowercase)
- **Treemap/** — `TreemapLayoutEngine` in `TreemapLayout.swift` (Squarify algorithm, max 12 depth levels), `TreemapRenderer` (Canvas-based with depth-darkened category colors), `TreemapView` (hit testing; click/double-click drill-down/hover/context menu interactions), `ZoomPanOverlay` (scroll, pinch, and middle-drag zoom/pan)
- **Views/** — `ContentView` (NavigationSplitView: sidebar tree + center treemap + inspector), `WelcomeView` (volume list with usage bars, Full Disk Access banner), `ScanProgressView`, `DirectoryTreeView` (List of DisclosureGroups whose expansion lives in AppState, so a treemap selection can expand and scroll the sidebar), `DetailPanelView` (metadata + category breakdown), `SizeSparkline` (sidebar bar: share of the parent folder, split by category)
- **Treemap/**
- `TreemapLayout.swift` — `TreemapLayoutEngine`. Squarify layout. Emits `TreemapItem`s: files, open folders as titled frames, closed folders, and "N smaller items" blocks. Folders open by `detailLevel` and room, or by hand via `folderOverrides`, to depth 32. The shown folder itself is not emitted. Without `showsFolders`, every folder opens as an undrawn `group` and only files and blocks are drawn
- `TreemapRenderer.swift` — Canvas drawing. System category colors on the window background, graphite folder frames, rounded pixel-snapped shapes; color by type or by folder branch
- `TreemapView.swift` — Hit testing; click opens a closed folder in place, ▾ closes, double click drills in; hover and context menu
- `TreemapTransition.swift` — 250 ms move and fade between layouts on open, close and navigation
- `TreemapSelection.swift` — selection overlay: accent ring and shadow, a scrim outside a selected folder; travels between items of one layout
- `TreemapTooltip.swift` — hover card: size, path, folder makeup
- `ZoomPanOverlay.swift` — scroll, pinch, and middle-drag zoom/pan
- **Views/** — `ContentView` (NavigationSplitView: sidebar tree + center treemap + inspector), `WelcomeView` (volume list with usage bars, Full Disk Access banner), `ScanProgressView`, `DirectoryTreeView` (List of DisclosureGroups whose expansion lives in AppState, so treemap navigation can expand and scroll the sidebar), `DetailPanelView` (metadata + category breakdown), `SizeSparkline` (sidebar bar: share of the parent folder, split by category)
- **Utilities/** — `ByteFormatter` (human-readable sizes), `FolderPicker` (shared NSOpenPanel), `FullDiskAccess` (permission probe and System Settings link), `FileActions` (Reveal in Finder and Copy Path for the inspector, sidebar and treemap menus)

### Key Patterns

- All data types crossing async boundaries are `Sendable`
- FileNode uses weak parent references to break retain cycles
- FileScanner deduplicates hard links and firmlinks by tracking seen inodes
- Menu commands reach the frontmost window through `focusedSceneValue` actions (see `OpenFolderCommands` and the zoom commands), not notifications
- Menu commands never use notifications. `TreemapCommands` (View menu) calls the shared `AppState` directly for detail, color, Show Folders, Back and the inspector; only per-window actions (Open Folder, zoom, Rescan) reach the frontmost window through `focusedSceneValue`. Tree controls in the menu and toolbar disable on `AppState.hasTree` (through the `can*` helpers), so the welcome screen leaves them off
- The sidebar expands to a folder only on navigation (drill down, breadcrumb, Back, or a sidebar pick outside the shown folder); selecting or opening in the treemap leaves it alone
- Local SwiftPM builds record SDK 15.0 while CI links the macOS 26 SDK, which changes AppKit/SwiftUI behavior; test release builds from CI (or stamp a local build with `vtool -set-build-version`) before trusting a local result
- TreemapView uses Canvas for rendering (not individual SwiftUI views) for performance
- Toggle the inspector without animation (`withTransaction(\.disablesAnimations, true)`): on macOS 27, animating `.inspector` while the treemap has a `.contextMenu` loops AppKit's constraint pass until it throws. Any new menu command or shortcut that shows or hides the inspector must do the same
- Toggle the inspector without animation (`withTransaction(\.disablesAnimations, true)`): on macOS 27, animating `.inspector` while the treemap has a `.contextMenu` loops AppKit's constraint pass until it throws. `AppState.toggleInspector()` does this; any new control that shows or hides the inspector must call it
- macOS APIs used: `NSWorkspace` (Reveal in Finder), `NSPasteboard` (clipboard), `NSOpenPanel` (folder picker)
45 changes: 31 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,39 @@ To uninstall, quit MacDirStat and drag it from Applications to the Trash, or run
1. **Open MacDirStat.** The start screen lists your drives, with a bar showing how full each one is.
2. **Pick what to scan.** Click your startup disk (usually **Macintosh HD**) to scan the whole Mac, click an external drive, or click **Choose a Custom Folder...** to scan a single folder.
3. **Wait for the scan.** MacDirStat shows the number of files and the allocated disk space found so far. You can cancel at any time.
4. **Look for the biggest rectangles.** They are the files using the most space. Hover over any rectangle to see its full path and size in the status bar at the bottom.
5. **Zoom in on crowded areas** with the scroll wheel, a trackpad pinch, or the zoom buttons in the toolbar.
6. **Drill into a folder.** Double-click any rectangle to show just the folder it's in. The breadcrumb bar above the treemap, or the **Back** button, takes you back up.
7. **Check folder totals.** The sidebar lists folders sorted largest first. Select a folder to see its size, file count, and breakdown by file type in the inspector on the right, or click a file in the treemap to see its details.
8. **Act on what you find.** Right-click a file or folder and choose **Move to Trash…**, or **Reveal in Finder** to look at it first. The totals update without a rescan. After changing a folder in Finder, right-click it in the sidebar and choose **Rescan Folder**, or click **Rescan** in the toolbar to scan everything again.
4. **Look for the biggest rectangles.** Colored blocks are files; gray frames with a name and size along the top are folders. Hover over any rectangle for a card with its size, its path, and what a folder holds by file type.
5. **Open folders in place.** The treemap starts with folders opened three levels deep. Click a closed folder to open it where it is, and click the **▾** in its title to close it. **More Detail** and **Less Detail** in the toolbar or the **View** menu (⌘+ and ⌘−) open or close one more level everywhere, and **View → Collapse All** undoes the folders you opened or closed by hand. Files and folders too small to see on their own are gathered into a striped "smaller items" block.
6. **Drill into a folder.** Double-click a folder to show just that folder, or a file to show the folder it's in. The breadcrumb bar above the treemap, or the **Back** button, takes you back up.
7. **Zoom in on crowded areas** with the scroll wheel or a trackpad pinch. Drag with the middle mouse button to pan, and press ⌘0 to go back to actual size.
8. **Check folder totals.** The sidebar lists folders sorted largest first. Select a folder to see its size, file count, and breakdown by file type in the inspector on the right, or click a file in the treemap to see its details.
9. **Act on what you find.** Right-click a file or folder and choose **Move to Trash…**, or **Reveal in Finder** to look at it first. The totals update without a rescan. After changing files in Finder, click **Rescan** in the toolbar to rescan the folder the treemap shows, or right-click a folder in the sidebar and choose **Rescan Folder**. Hold Option while clicking **Rescan** to scan everything again.

### Keyboard shortcuts

| Shortcut | Action |
| --- | --- |
| ⌘O | Open a folder to scan |
| ⌘+ (or ⌘=) / ⌘− | More or less detail: open or close one more level of folders |
| ⌘0 | Actual size (undo zoom) |
| ⌘[ | Back to the parent folder |
| ⌘R | Rescan the folder the treemap shows |
| ⇧⌘R | Rescan everything |
| ⌥⌘I | Show or hide the inspector |

The **View** menu also has **Collapse All**, **Show Folders**, and **Color by Type** / **Color by Folder**.

MacDirStat only moves items to the Trash, after you confirm, so you can restore them from there. It never deletes files permanently, and it never moves cloud-only iCloud items.

## Features

- **Interactive treemap.** Every file is drawn as a rectangle sized by its disk usage, nested inside its folders, using the squarified treemap layout. Large files and folders stand out at a glance, and bigger rectangles are labeled with their name and size.
- **Color-coded file types.** Files are colored by category: documents, images, video, audio, code, archives, applications, system, caches, and other.
- **Interactive treemap.** Every file is drawn as a rectangle sized by its disk usage, nested inside titled folder frames, using the squarified treemap layout. Large files and folders stand out at a glance, and bigger rectangles are labeled with their name and size.
- **Open folders in place.** Click a folder to open it inside the treemap, choose how many levels open on their own with More Detail and Less Detail, and see a folder's contents by type on its closed frame. Items too small to see are gathered into one "smaller items" block per folder.
- **Show Folders.** Turn it off for the classic look: no frames, just every file down to the bottom.
- **Color by type or by folder.** By type, files are colored by category: documents, images, video, audio, code, archives, applications, system, caches, and other. By folder, everything inside each top-level folder shares that folder's color.
- **Folder tree sidebar.** Browse the folder hierarchy, sorted largest first, with each folder's size.
- **Inspector panel.** Shows size, allocated size, file and folder counts, last-modified date, and a bar chart of what each folder contains by file type.
- **Drill down.** Double-click a rectangle to focus the treemap on its folder, then go back up with the breadcrumb bar or the Back button.
- **Zoom and pan.** Zoom with the scroll wheel, a trackpad pinch, or the toolbar. Pan by dragging with the middle mouse button.
- **Drill down.** Double-click a rectangle to focus the treemap on its folder, then go back up with the breadcrumb bar or the Back button. Opening, closing, and drilling animate, unless Reduce Motion is on.
- **Zoom and pan.** Zoom with the scroll wheel or a trackpad pinch. Pan by dragging with the middle mouse button.
- **File Size or Allocated Size.** Allocated Size is the default. Switch to File Size to see logical content sizes, including online-only files.
- **Cloud-aware scanning.** File Provider placeholders (including OneDrive, synced SharePoint libraries, iCloud Drive, and other providers using macOS dataless files) are measured without downloading their contents. Cloud-only folders are skipped and marked as incomplete.
- **Drive overview.** The start screen shows your mounted drives with their used and available space, or you can scan any folder.
Expand Down Expand Up @@ -148,7 +165,7 @@ MacDirStat adds up the files it can read, which can differ from the numbers macO

### What's the difference between File Size and Allocated Size?

File Size is the size of a file's contents; Allocated Size is the disk space the file actually occupies. Allocated Size can be smaller for compressed or sparse files, such as some virtual machine disk images, and is often slightly larger for small files, because disk space is allocated in whole blocks. Allocated Size is the default for the treemap, folder ordering, and category breakdown. Scan progress always shows allocated space. Switch between them with the toggle in the toolbar.
File Size is the size of a file's contents; Allocated Size is the disk space the file actually occupies. Allocated Size can be smaller for compressed or sparse files, such as some virtual machine disk images, and is often slightly larger for small files, because disk space is allocated in whole blocks. Allocated Size is the default for the treemap, folder ordering, and category breakdown. Scan progress always shows allocated space. Switch between them with the Size menu in the toolbar.

Online-only cloud files can have a large File Size and little or no Allocated Size. A logical total above your drive capacity does not mean those bytes are stored on your Mac.

Expand All @@ -160,7 +177,7 @@ If a parent folder of the selected scan path is cloud-only, the scan may fail wi

### What do the colors in the treemap mean?

Colors show file type, based on the file extension. Rectangles get slightly darker the deeper they're nested.
With **Color by Type**, the default, colors show file type, based on the file extension. Folders are gray frames, and a closed folder shows a thin bar of what it holds by type. With **Color by Folder**, each folder in the view gets its own color, shared by everything inside it.

| Color | Category | Examples |
| --- | --- | --- |
Expand All @@ -171,9 +188,9 @@ Colors show file type, based on the file extension. Rectangles get slightly dark
| Purple | Code | Swift, Python, JavaScript, HTML, JSON |
| Teal | Archives | ZIP, DMG, ISO, PKG, TAR |
| Pink | Applications | DYLIB, SO, DLL, EXE, APK |
| Light gray | System | PLIST, STRINGS, .DS_Store |
| Gray | System | PLIST, STRINGS, .DS_Store |
| Brown | Caches | CACHE, TMP, O, PYC, CLASS |
| Gray | Other | Everything else, including files without an extension |
| Gray (a second shade) | Other | Everything else, including files without an extension |

### Can MacDirStat scan external drives?

Expand Down Expand Up @@ -230,7 +247,7 @@ Sources/MacDirStat/
├── App/ # Entry point, AppState
├── Scanning/ # FileScanner (parallel POSIX traversal), ScanCoordinator, FileNode tree model
├── Categorization/ # FileCategory definitions, ~190 file extension mappings
├── Treemap/ # Squarify layout engine, hit testing, Canvas renderer, zoom and pan
├── Treemap/ # Squarify layout engine, Canvas renderer, hit testing, tooltip, transitions, zoom and pan
├── Views/ # ContentView, WelcomeView, ScanProgressView, DirectoryTreeView, DetailPanelView
└── Utilities/ # ByteFormatter, FullDiskAccess, FolderPicker
```
Expand Down
57 changes: 50 additions & 7 deletions Sources/MacDirStat/App/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ enum ScanStatus: Equatable {
}
}

enum TreemapColorMode: String, CaseIterable, Sendable {
case type, folder
}

enum SizeMetric: String, CaseIterable, Sendable {
case fileSize = "File Size"
case allocatedSize = "Allocated Size"
Expand All @@ -29,12 +33,19 @@ final class AppState {
var scanStatus: ScanStatus = .idle
var rootNode: FileNode?
var treemapRoot: FileNode?
var selectedNode: FileNode? {
didSet { if let selectedNode { revealInTree(selectedNode) } }
}
var selectedNode: FileNode?
var breadcrumbs: [FileNode] = []
var sizeMetric: SizeMetric = .allocatedSize
var showInspector: Bool = true
/// How many folder levels the treemap opens on its own.
var detailLevel = 3
static let detailRange = 1...8
var colorMode: TreemapColorMode = .type
/// Off draws only files and smaller-items blocks, every folder open to the bottom.
/// The detail level and hand-opened folders then don't apply, but are kept for when it's back on.
var showsFolders = true
/// Folders the user opened or closed by hand in the treemap, by inode. Wins over the detail level.
private(set) var folderOverrides: [UInt64: Bool] = [:]
private(set) var expandedIDs: Set<UInt64> = []
/// Bumped when part of the tree is replaced in place, so views showing its totals refresh.
private(set) var treeVersion = 0
Expand All @@ -54,17 +65,41 @@ final class AppState {
return false
}

var hasData: Bool {
rootNode != nil
/// A scanned tree on screen, not the welcome screen over one: what the treemap controls act on.
var hasTree: Bool {
scanStatus == .completed && treemapRoot != nil
}

// Any write to an @Observable property notifies observers, even when nothing changes.
func setFolderOpen(_ node: FileNode, _ open: Bool) {
if folderOverrides[node.id] != open { folderOverrides[node.id] = open }
}

func collapseAll() { if !folderOverrides.isEmpty { folderOverrides = [:] } }

var canCollapseAll: Bool { hasTree && showsFolders && !folderOverrides.isEmpty }

var canMoreDetail: Bool { hasTree && showsFolders && detailLevel < Self.detailRange.upperBound }

var canLessDetail: Bool { hasTree && showsFolders && detailLevel > Self.detailRange.lowerBound }

func moreDetail() { detailLevel = min(detailLevel + 1, Self.detailRange.upperBound) }

func lessDetail() { detailLevel = max(detailLevel - 1, Self.detailRange.lowerBound) }

func toggleInspector() {
// On macOS 27, animating the inspector beside the treemap's .contextMenu
// loops AppKit's constraint pass until it throws. Toggle without animation.
withTransaction(\.disablesAnimations, true) { showInspector.toggle() }
}

var selectedDirectory: FileNode? {
guard let node = selectedNode else { return nil }
return node.isDirectory ? node : node.parent
}

/// Selection made in the sidebar. The treemap keeps its zoom unless the folder is outside
/// the folder it shows; then it moves to the folder's parent.
/// Selection made in the sidebar or the skipped-folders list. The treemap keeps its zoom unless
/// the folder is outside the folder it shows; then it moves to the folder's parent.
func selectFromTree(_ node: FileNode) {
selectedNode = node
if let current = treemapRoot, !node.isInSubtree(of: current) {
Expand All @@ -73,6 +108,7 @@ final class AppState {
rebuildBreadcrumbs()
}
}
revealInTree(node)
}

func setExpanded(_ node: FileNode, _ expanded: Bool) {
Expand Down Expand Up @@ -156,6 +192,9 @@ final class AppState {
selectedNode = node
rebuildBreadcrumbs()
}
revealInTree(node)
// The treemap now shows this folder's children, so the sidebar opens it too.
if !expandedIDs.contains(node.id) { expandedIDs.insert(node.id) }
}

func navigateTo(breadcrumb node: FileNode) {
Expand All @@ -164,6 +203,7 @@ final class AppState {
selectedNode = node
rebuildBreadcrumbs()
}
revealInTree(node)
}

func navigateUp() {
Expand All @@ -173,13 +213,15 @@ final class AppState {
selectedNode = parent
rebuildBreadcrumbs()
}
revealInTree(parent)
}

func setScanCompleted(root: FileNode) {
rootNode = root
isIncompleteBannerDismissed = false
treemapRoot = root
selectedNode = root
folderOverrides = [:]
skippedFolders = Self.collectSkippedFolders(in: root)
scanStatus = .completed
rebuildBreadcrumbs()
Expand All @@ -192,6 +234,7 @@ final class AppState {
selectedNode = nil
breadcrumbs = []
expandedIDs = []
folderOverrides = [:]
skippedFolders = []
}

Expand Down
Loading
Loading