Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Docs/RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Unreleased

No changes yet.
- Adds a File menu entry that registers Margin as the default editor for
Markdown (.md) files through Launch Services.

## 0.5.1 — native reading and review fixes

Expand Down
Binary file added Docs/screenshots/default-markdown-editor-menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions Sources/MarginApp/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AppKit
import MarginCore
import UniformTypeIdentifiers

protocol WorkspacePathRenameParticipating: AnyObject {
var documentURLForPathRename: URL? { get }
Expand Down Expand Up @@ -220,6 +221,25 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
}
}

@objc func setAsDefaultMarkdownEditor(_ sender: Any?) {
guard let markdownType = UTType(filenameExtension: "md") else { return }
let bundleURL = Bundle.main.bundleURL
let workspace = NSWorkspace.shared
workspace.setDefaultApplication(at: bundleURL, toOpen: markdownType) { error in
DispatchQueue.main.async {
let alert = NSAlert()
if error == nil {
alert.messageText = "Margin is now the default editor for Markdown files."
} else {
alert.messageText = "Margin could not be set as the default Markdown editor."
alert.informativeText = "You can set it manually: choose a .md file in Finder, "
+ "press Command-I, and select Margin as the default app."
}
alert.runModal()
}
}
}

@objc func newWindow(_ sender: Any?) {
makeWorkspaceWindow(for: nil)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/MarginApp/AppMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ enum AppMenu {
)
fileMenu.addItem(.separator())
fileMenu.addItem(item("Close Tab", action: #selector(NSWindow.performClose(_:)), key: "w"))
fileMenu.addItem(.separator())
fileMenu.addItem(
item(
"Set as Default Markdown Editor",
action: #selector(AppDelegate.setAsDefaultMarkdownEditor(_:)),
target: delegate
)
)
mainMenu.addItem(menuItem(title: "File", submenu: fileMenu))

let editMenu = NSMenu(title: "Edit")
Expand Down
10 changes: 10 additions & 0 deletions Tests/MarginAppTests/WorkspaceBehaviorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,16 @@ final class WorkspaceBehaviorTests: XCTestCase {
)
}

func testFileMenuOffersDefaultMarkdownEditorRegistration() {
let delegate = AppDelegate()
AppMenu.install(for: NSApplication.shared, delegate: delegate)

let item = menuItem(named: "Set as Default Markdown Editor")
XCTAssertNotNil(item)
XCTAssertEqual(item?.action, #selector(AppDelegate.setAsDefaultMarkdownEditor(_:)))
XCTAssertTrue(item?.target === delegate)
}

func testFileWatcherNeverBlocksTheInteractionThreadWhileOpening() {
let openerFinished = expectation(description: "Slow descriptor open finished")
let watcher = FileSystemWatcher(
Expand Down