Skip to content

Commit

Permalink
Refactored management of creating windows for notes
Browse files Browse the repository at this point in the history
  • Loading branch information
charliescheer committed Apr 12, 2024
1 parent bfb0138 commit 1884fed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
30 changes: 30 additions & 0 deletions Simplenote/NoteWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@
import Foundation

class NoteWindow: NSWindow {
private let editor: NoteEditorViewController

init() {
let storyboard = NSStoryboard(name: .main, bundle: nil)
self.editor = storyboard.instantiateViewController(ofType: NoteEditorViewController.self)

let rect = NSRect(x: 100, y: 100, width: 100, height: 100)
super.init(contentRect: rect, styleMask: [.titled, .closable, .miniaturizable, .resizable], backing: .buffered, defer: false)

setupWindow()
}

private func setupWindow() {
contentViewController = editor

let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let fileURL = URL(fileURLWithPath: documentsDirectory, isDirectory: true).appendingPathComponent(".editor-metadata-cache")
let editorCache = NoteEditorMetadataCache(storage: FileStorage(fileURL: fileURL))
editor.metadataCache = editorCache
}

// The note windows are stored in the windows manager and need to be removed when they close
// This override cleans up the windows in the manager
override func close() {
Expand All @@ -19,4 +40,13 @@ class NoteWindow: NSWindow {
noteWindowsManager.windowControllers.remove(at: index)
}
}


//MARK: Show Note
//
func show(_ note: Note) {
editor.toolbarView.sidebarButton.isHidden = true
editor.displayNote(note)
title = note.titlePreview
}
}
22 changes: 2 additions & 20 deletions Simplenote/NoteWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,17 @@
import Foundation

class NoteWindowController: NSWindowController {
private let editor: NoteEditorViewController
private let editorCache: NoteEditorMetadataCache

init() {
let storyboard = NSStoryboard(name: .main, bundle: nil)
let noteEditor = storyboard.instantiateViewController(ofType: NoteEditorViewController.self)

let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let fileURL = URL(fileURLWithPath: documentsDirectory, isDirectory: true).appendingPathComponent(".editor-metadata-cache")
self.editorCache = NoteEditorMetadataCache(storage: FileStorage(fileURL: fileURL))

noteEditor.metadataCache = editorCache

let window = NoteWindow(contentViewController: noteEditor)

self.editor = noteEditor

super.init(window: window)
super.init(window: NoteWindow())
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

func show(_ note: Note) {
editor.toolbarView.sidebarButton.isHidden = true
editor.displayNote(note)
window?.title = note.titlePreview

(window as? NoteWindow)?.show(note)
showWindow(window)
}
}

0 comments on commit 1884fed

Please sign in to comment.