File Management & Bookmarks
Relevant source files
The following files were used as context for generating this wiki page:
The File Management & Bookmarks system in Writer is the core infrastructure responsible for handling the distraction-free editor's document lifecycle. It encompasses directory navigation, file CRUD (Create, Read, Update, Delete) operations, and persistent access to the local file system through macOS security-scoped bookmarks.
The system is designed around a central @Observable store that manages a hierarchical file tree, handles autosave logic, and ensures that user-selected folders remain accessible across application restarts. This architecture allows for a seamless "writing folder" experience where the user focuses on content while the application manages the underlying file system interactions.
Sources: CLAUDE.md:12-14, Writer/EditorStore.swift:14-25
Architecture and Core Components
The file management system utilizes a centralized state management pattern, primarily driven by the EditorStore class. It leverages the Observation framework to propagate file system changes to the SwiftUI user interface.
EditorStore
The EditorStore is the central hub for all file-related logic. It maintains the fileTree, which is an array of FileNode structures representing the directory hierarchy. It also tracks the selectedFileURL and manages the documentText currently being edited.
Sources: Writer/EditorStore.swift:13-50, CLAUDE.md:18-20
Security-Scoped Bookmarks
To maintain access to user-selected folders without repeated permission prompts, the application uses macOS security-scoped bookmarks. These are stored in UserDefaults and resolved upon application launch.
The diagram shows the logic for maintaining persistent folder access across app launches using security-scoped bookmarks.
Sources: Writer/EditorStore.swift:154-205, CLAUDE.md:38
File System Operations
The system supports standard file operations including creation, deletion, renaming, and move/copy-paste functionality.
File Tree Navigation
The file tree is built recursively using FileNode objects. Supported file types include .md, .markdown, .txt, and .text.
Sources: Writer/EditorStore.swift:23-28, CLAUDE.md:41
| Component | Type | Description |
|---|---|---|
rootURL | URL? | The base directory selected by the user. |
fileTree | [FileNode] | Hierarchical representation of the filesystem. |
FileNode | struct | Data structure containing url, isDirectory, and children. |
isEditableFile | func | Filters for .md, .markdown, .txt, and .text. |
| Sources: Writer/EditorStore.swift:16-320, CLAUDE.md:41 |
CRUD Logic
- Creation: New files and folders are created relative to the
newFileDirectoryURL, which defaults to the root or the currently active directory. - Renaming: Uses
FileManager.default.moveItemto rename files while maintaining the current selection if the active file is renamed. - Deletion: Moves items to the system trash using
trashItemrather than immediate deletion. - Autosave: Implemented via a debounced
Taskthat persists changes to disk 350ms after the last edit.
Sources: Writer/EditorStore.swift:75-152, Writer/EditorStore.swift:250-270
Sequence: Document Loading & Autosave
This sequence illustrates the flow from initial file selection to the automatic persistence of changes.
Sources: Writer/EditorStore.swift:231-270, Writer/ContentView.swift:34-36
UI Integration
The ContentView provides the primary interface for file management through a NavigationSplitView sidebar.
Sidebar and Context Menus
The sidebar renders the fileTree using DisclosureGroup for folders. Each node (file or folder) provides a context menu for operations like "Show in Finder," "Duplicate," "Copy," "Cut," and "Paste."
Sources: Writer/ContentView.swift:156-250
Drag and Drop
The application supports internal drag and drop for moving files.
- Source:
onDragprovides theNSURLof the file. - Target:
dropDestinationidentifies the target directory. - Conflict Resolution: If a file exists at the destination, the user is prompted to replace or skip.
Sources: Writer/ContentView.swift:301-344
Summary
The File Management & Bookmarks system provides the backbone for Writer's document-centric workflow. By combining the Observation framework for state management with macOS-specific features like security-scoped bookmarks and NSSavePanel, it ensures that user data is handled securely and efficiently. The hierarchical FileNode structure and recursive loading logic allow the application to handle complex folder structures while maintaining a clean, distraction-free interface.
Sources: Writer/EditorStore.swift:1-320, Writer/ContentView.swift:1-350