App Entry Point & Commands
Relevant source files
The following files were used as context for generating this wiki page:
The Writer application is a distraction-free macOS text editor built using the SwiftUI framework and the Observation state management system. The entry point of the application is defined in WriterApp.swift, which initializes the primary state stores and configures the main window scene, including global menu commands and export logic.
The architecture centers around several @Observable stores that are injected into the environment, allowing for a decoupled UI that reacts to changes in the underlying file system, document state, and layout preferences.
Sources: Writer/WriterApp.swift:10-15, CLAUDE.md:12-14
Application Architecture & State Management
The application lifecycle is managed by the WriterApp struct. Upon startup, it initializes three core stores: EditorStore, ThemeStore, and LayoutStore. These are passed down through the SwiftUI environment to ContentView, ensuring all subviews have access to the global state.
The diagram above illustrates the distribution of state stores from the entry point to the main user interface components.
Sources: Writer/WriterApp.swift:11-15, Writer/WriterApp.swift:80-83, CLAUDE.md:16-30
Core Components
| Component | Responsibility | Source File |
|---|---|---|
WriterApp | Entry point, scene configuration, global menu commands, and export coordination. | WriterApp.swift |
EditorStore | Manages file system nodes, document text, security-scoped bookmarks, and autosave. | EditorStore.swift |
ThemeStore | Manages preview typography and color palettes. | ThemeStore.swift |
LayoutStore | Handles UI state such as the visibility of the preview pane. | WriterApp.swift (State) |
Sources: Writer/WriterApp.swift:11-15, CLAUDE.md:16-30
Menu Commands & Keyboard Shortcuts
The application defines a custom set of menu commands using the .commands modifier. These are organized into "File" and "View" menus, providing access to file management, formatting, and export functionalities.
File Menu Commands
The File menu handles the creation of new entities and the conversion of Markdown documents into various formats.
- New File (⌘N): Triggers the
pendingCreationstate inEditorStoreto prompt for a new.txtfile. - New Folder (⌘⇧N): Prompts for the creation of a new directory within the active path.
- Export to HTML (⌘⇧E): Invokes the
exportToHTML()private method. - Export to PDF (⌘⇧P): Invokes the
exportToPDF()private method. - Export to RTF (⌘⇧R): Invokes the
exportToRTF()private method. - Format Tables (⌘⇧F): Executes
editorStore.formatAllTables()to align Markdown tables.
Sources: Writer/WriterApp.swift:111-163, CLAUDE.md:46-51
View Menu Commands
The View menu controls the application's layout state.
- Show/Hide Preview (⌘⌥P): Toggles the
layoutStore.showPreviewboolean, which affects the visibility of theWebViewin the detail pane.
Sources: Writer/WriterApp.swift:165-170, Writer/ContentView.swift:125-127
Export Pipeline
The WriterApp serves as the coordinator for exporting document content. It utilizes specialized exporter structs to transform the raw Markdown text from the EditorStore into styled documents using themes from the ThemeStore.
The sequence above details the interaction between the application entry point and the export utilities.
Sources: Writer/WriterApp.swift:17-74, Writer/HTMLExporter.swift:23-38, Writer/PDFExporter.swift:25-45
Export Implementation Details
The export functions are asynchronous and handle potential errors, specifically ignoring NSUserCancelledError to prevent unnecessary alerts when a user cancels a save dialog.
- HTML Export: Uses
HTMLExporterto wrap Markdown rendered as HTML inside a document with inline CSS. - PDF Export: Uses
PDFExporter, which leverages a hiddenWKWebViewto render the content before capturing it as a PDF data stream. - RTF Export: Uses
RTFExporterto convert HTML strings intoNSAttributedStringbefore generating RTF data.
Sources: Writer/WriterApp.swift:17-74, Writer/HTMLExporter.swift:13-14, Writer/PDFExporter.swift:12-14, Writer/RTFExporter.swift:11-13
Initialization & Persistence
During the EditorStore initialization within the App entry point, the system attempts to restore access to the user's previously selected writing folder. This is achieved through Security-Scoped Bookmarks, which allow the app to persist sandbox-friendly access to external directories across launches.
- Check for Bookmark: The app looks for
forthewriting_folder_bookmarkinUserDefaults. - Resolve Bookmark: If found, it resolves the data to a URL with security scope.
- Access Resource: It calls
startAccessingSecurityScopedResource()to gain file system permissions. - Refresh Tree: The
fileTreeis populated by scanning the root URL.
Sources: Writer/EditorStore.swift:138-160, Writer/EditorStore.swift:207-210, CLAUDE.md:21-23
Conclusion
The WriterApp entry point establishes the foundation for the application by managing global state, window configuration, and high-level command orchestration. By delegating file operations to the EditorStore and rendering tasks to dedicated exporters, the entry point maintains a clean separation of concerns while providing the user with a comprehensive set of tools for distraction-free writing and multi-format document distribution.