style: align notes controls with action pills
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import AppKit
|
||||
import ConjureKit
|
||||
import UniformTypeIdentifiers
|
||||
import Highlighter
|
||||
|
||||
@MainActor
|
||||
final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
private let notePicker = NSPopUpButton()
|
||||
private let editor = NotesTextView()
|
||||
private let createButton = NSButton(title: "New Note", target: nil, action: nil)
|
||||
private let actionsButton = NSButton(title: "Actions", target: nil, action: nil)
|
||||
private let noteMenu = NSMenu(title: "Notes")
|
||||
private let editor = NSTextView()
|
||||
private let status = NSTextField(labelWithString: "")
|
||||
private let defaults: UserDefaults
|
||||
private var store: NotesStore?
|
||||
@@ -29,16 +30,19 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
|
||||
override func loadView() {
|
||||
view = NSView()
|
||||
notePicker.target = self
|
||||
notePicker.action = #selector(switchNote)
|
||||
notePicker.setAccessibilityLabel("Choose a note")
|
||||
let create = NSButton(title: "New Note", target: self, action: #selector(newNote))
|
||||
create.keyEquivalent = "n"
|
||||
create.keyEquivalentModifierMask = .command
|
||||
let image = NSButton(title: "Insert Image…", target: self, action: #selector(insertImage))
|
||||
let header = NSStackView(views: [notePicker, create, image])
|
||||
header.spacing = 8
|
||||
notePicker.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
|
||||
createButton.target = self
|
||||
createButton.action = #selector(newNote)
|
||||
createButton.toolTip = "Create a note (Command-N)"
|
||||
actionsButton.target = self
|
||||
actionsButton.action = #selector(showActions)
|
||||
actionsButton.setAccessibilityLabel("Choose a note")
|
||||
actionsButton.toolTip = "Choose a note (Command-K)"
|
||||
actionsButton.menu = noteMenu
|
||||
let pill = ActionPill(primaryButton: createButton, actionsButton: actionsButton, primaryCommand: "n")
|
||||
let footer = NSStackView(views: [status, NSView(), pill])
|
||||
footer.orientation = .horizontal
|
||||
footer.alignment = .centerY
|
||||
footer.spacing = 12
|
||||
|
||||
editor.delegate = self
|
||||
editor.isRichText = true
|
||||
@@ -70,26 +74,24 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
status.textColor = SunsetPalette.secondaryText
|
||||
status.setAccessibilityLabel("Notes save status")
|
||||
status.lineBreakMode = .byTruncatingTail
|
||||
for control in [header, scroll, status] {
|
||||
status.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
|
||||
for control in [scroll, footer] {
|
||||
control.translatesAutoresizingMaskIntoConstraints = false
|
||||
view.addSubview(control)
|
||||
}
|
||||
NSLayoutConstraint.activate([
|
||||
header.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
|
||||
header.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
|
||||
header.topAnchor.constraint(equalTo: view.topAnchor),
|
||||
notePicker.widthAnchor.constraint(greaterThanOrEqualToConstant: 180),
|
||||
scroll.topAnchor.constraint(equalTo: header.bottomAnchor, constant: 10),
|
||||
scroll.leadingAnchor.constraint(equalTo: header.leadingAnchor),
|
||||
scroll.trailingAnchor.constraint(equalTo: header.trailingAnchor),
|
||||
scroll.bottomAnchor.constraint(equalTo: status.topAnchor, constant: -8),
|
||||
status.leadingAnchor.constraint(equalTo: header.leadingAnchor),
|
||||
status.trailingAnchor.constraint(equalTo: header.trailingAnchor),
|
||||
status.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -12),
|
||||
scroll.topAnchor.constraint(equalTo: view.topAnchor),
|
||||
scroll.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
|
||||
scroll.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
|
||||
scroll.bottomAnchor.constraint(equalTo: footer.topAnchor, constant: -8),
|
||||
footer.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
|
||||
footer.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8),
|
||||
footer.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8),
|
||||
footer.heightAnchor.constraint(equalToConstant: 36),
|
||||
])
|
||||
guard let store else {
|
||||
editor.isEditable = false
|
||||
for control in [notePicker, create, image] { control.isEnabled = false }
|
||||
for control in [createButton, actionsButton] { control.isEnabled = false }
|
||||
showError(loadError!)
|
||||
return
|
||||
}
|
||||
@@ -117,8 +119,8 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
do {
|
||||
try store.update(id: selectedID, contents: editor.attributedString())
|
||||
isDirty = false
|
||||
updatePicker()
|
||||
status.stringValue = "Saved locally · Markdown · Paste images or use Insert Image"
|
||||
updateActions()
|
||||
status.stringValue = "Saved locally · Markdown · Paste or drop images"
|
||||
status.textColor = SunsetPalette.secondaryText
|
||||
return true
|
||||
} catch {
|
||||
@@ -138,9 +140,8 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
catch { showError(error) }
|
||||
}
|
||||
|
||||
@objc private func switchNote() {
|
||||
guard let value = notePicker.selectedItem?.representedObject as? UUID else { return }
|
||||
guard save() else { updatePicker(); return }
|
||||
@objc private func switchNote(_ sender: NSMenuItem) {
|
||||
guard let value = sender.representedObject as? UUID, save() else { return }
|
||||
select(value)
|
||||
focusEditor()
|
||||
}
|
||||
@@ -154,17 +155,18 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
editor.isEditable = true
|
||||
editor.setSelectedRange(NSRange(location: editor.string.utf16.count, length: 0))
|
||||
MarkdownHighlighting.apply(to: editor)
|
||||
updatePicker()
|
||||
status.stringValue = "Saved locally · Markdown · Paste images or use Insert Image"
|
||||
updateActions()
|
||||
status.stringValue = "Saved locally · Markdown · Paste or drop images"
|
||||
status.textColor = SunsetPalette.secondaryText
|
||||
}
|
||||
|
||||
private func updatePicker() {
|
||||
notePicker.removeAllItems()
|
||||
private func updateActions() {
|
||||
noteMenu.removeAllItems()
|
||||
for note in store?.notes ?? [] {
|
||||
notePicker.menu?.addItem(withTitle: note.title, action: nil, keyEquivalent: "")
|
||||
notePicker.lastItem?.representedObject = note.id
|
||||
if note.id == selectedID { notePicker.select(notePicker.lastItem) }
|
||||
let item = noteMenu.addItem(withTitle: note.title, action: #selector(switchNote(_:)), keyEquivalent: "")
|
||||
item.target = self
|
||||
item.representedObject = note.id
|
||||
item.state = note.id == selectedID ? .on : .off
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,30 +176,9 @@ final class NotesController: NSViewController, NSTextViewDelegate {
|
||||
status.textColor = .systemRed
|
||||
}
|
||||
|
||||
@objc private func insertImage() {
|
||||
guard let window = view.window, editor.isEditable else { return }
|
||||
let panel = NSOpenPanel()
|
||||
panel.allowedContentTypes = [.image]
|
||||
panel.canChooseDirectories = false
|
||||
panel.beginSheetModal(for: window) { [weak self] response in
|
||||
guard response == .OK, let self, let url = panel.url else { return }
|
||||
do { try self.editor.insertImage(at: url); self.focusEditor() }
|
||||
catch { self.showError(error) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
final class NotesTextView: NSTextView {
|
||||
func insertImage(at url: URL) throws {
|
||||
guard let image = NSImage(contentsOf: url) else {
|
||||
throw CocoaError(.fileReadCorruptFile)
|
||||
}
|
||||
let attachment = NSTextAttachment()
|
||||
attachment.attachmentCell = NSTextAttachmentCell(imageCell: image)
|
||||
attachment.fileWrapper = try FileWrapper(url: url, options: .immediate)
|
||||
let contents = NSAttributedString(attachment: attachment)
|
||||
insertText(contents, replacementRange: selectedRange())
|
||||
@objc private func showActions() {
|
||||
noteMenu.popUp(positioning: nil, at: NSPoint(x: 0, y: actionsButton.bounds.maxY + 4), in: actionsButton)
|
||||
focusEditor()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user