Skip to content

Commit

Permalink
Refactored to remove the need for the note windows manager
Browse files Browse the repository at this point in the history
  • Loading branch information
charliescheer committed Apr 12, 2024
1 parent 1884fed commit fe52a88
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
9 changes: 6 additions & 3 deletions Simplenote/NoteListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,12 @@ extension NoteListViewController {
guard let note = listController.note(at: tableView.selectedRow) else {
return
}
let windowController = NoteWindowController()
SimplenoteAppDelegate.shared().noteWindowsManager.windowControllers.append(windowController)
let window = NoteWindow()
let windowController = NSWindowController(window: window)

windowController.show(note)
window.show(note)

tableView.deselectRow(tableView.selectedRow)
refreshPresentedNote()
}
}
22 changes: 8 additions & 14 deletions Simplenote/NoteWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import Foundation

class NoteWindow: NSWindow {
private let editor: NoteEditorViewController
let editor: NoteEditorViewController

var selectedNoteID: String? {
editor.note?.simperiumKey
}

init() {
let storyboard = NSStoryboard(name: .main, bundle: nil)
Expand All @@ -30,23 +34,13 @@ class NoteWindow: NSWindow {
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() {
super.close()
let noteWindowsManager = SimplenoteAppDelegate.shared().noteWindowsManager
if let controller = windowController,
let index = noteWindowsManager.windowControllers.firstIndex(of: controller) {
noteWindowsManager.windowControllers.remove(at: index)
}
}


//MARK: Show Note
// MARK: Show Note
//
func show(_ note: Note) {
editor.toolbarView.sidebarButton.isHidden = true
editor.displayNote(note)
title = note.titlePreview

windowController?.showWindow(self)
}
}
30 changes: 23 additions & 7 deletions Simplenote/SimplenoteAppDelegate+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ extension SimplenoteAppDelegate {
noteEditorViewController.editorDelegate = self
}

@objc
func configureNoteWindowsManager() {
noteWindowsManager = NoteWindowsManager()
}

@objc
func configureEditorMetadataCache() {
let documentsDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
Expand Down Expand Up @@ -471,8 +466,13 @@ extension SimplenoteAppDelegate: NotesControllerDelegate {
}

func notesController(_ controller: NoteListViewController, didSelect note: Note) {
breadcrumbsViewController.notesControllerDidSelectNote(note)
noteEditorViewController.displayNote(note)
if let noteWindow = noteWindow(for: note) {
notesControllerDidSelectZeroNotes(controller)
noteWindow.makeKeyAndOrderFront(nil)
} else {
breadcrumbsViewController.notesControllerDidSelectNote(note)
noteEditorViewController.displayNote(note)
}
}

func notesController(_ controller: NoteListViewController, didSelect notes: [Note]) {
Expand Down Expand Up @@ -516,6 +516,22 @@ extension SimplenoteAppDelegate {
}
}

// MARK: - Window management
//
extension SimplenoteAppDelegate {
var windows: [NSWindow] {
NSApplication.shared.windows
}

var noteWindows: [NoteWindow] {
windows.compactMap({ $0 as? NoteWindow })
}

func noteWindow(for note: Note) -> NoteWindow? {
noteWindows.first(where: { $0.selectedNoteID == note.simperiumKey })
}
}

// MARK: - Constants
//
private struct Constants {
Expand Down
2 changes: 0 additions & 2 deletions Simplenote/SimplenoteAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
@class VersionsController;
@class NoteEditorMetadataCache;
@class AccountDeletionController;
@class NoteWindowsManager;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -51,7 +50,6 @@ NS_ASSUME_NONNULL_BEGIN
@property (strong, nonatomic) NoteEditorMetadataCache *noteEditorMetadataCache;

@property (nullable, strong, nonatomic) NSWindowController *preferencesWindowController;
@property (strong, nonatomic) NoteWindowsManager *noteWindowsManager;

+ (SimplenoteAppDelegate *)sharedDelegate;

Expand Down
1 change: 0 additions & 1 deletion Simplenote/SimplenoteAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ - (void)applicationWillFinishLaunching:(NSNotification *)notification
[self configureTagsController];
[self configureNotesController];
[self configureEditorController];
[self configureNoteWindowsManager];
[self configureVerificationCoordinator];
[self configureVersionsController];
[self configureAccountDeletionController];
Expand Down

0 comments on commit fe52a88

Please sign in to comment.