Skip to content

Commit

Permalink
Refactored note windows to be managed by the window controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
charliescheer committed Apr 15, 2024
1 parent 95a7d61 commit 4fc3b11
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Simplenote.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@
BAE66CAA26AF647500398FF3 /* Remote.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA938CEB26ACFF4A00BE5A1D /* Remote.swift */; };
BAF1B0C22BC9B1C500B55F73 /* NoteWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAF1B0C12BC9B1C500B55F73 /* NoteWindow.swift */; };
BAF1B0C32BC9B1C500B55F73 /* NoteWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAF1B0C12BC9B1C500B55F73 /* NoteWindow.swift */; };
BAF1B0C82BCDDA9600B55F73 /* NoteWindowManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAF1B0C72BCDDA9600B55F73 /* NoteWindowManager.swift */; };
BAF8D5DC26AE3BE800CA9383 /* markdown-light.css in Resources */ = {isa = PBXBuildFile; fileRef = BAF8D5DB26AE3BE800CA9383 /* markdown-light.css */; };
BAFB545026CCA7F1006E037C /* NSProgressIndicator+Simplenote.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFB544F26CCA7F1006E037C /* NSProgressIndicator+Simplenote.swift */; };
BAFB545126CCA7F1006E037C /* NSProgressIndicator+Simplenote.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAFB544F26CCA7F1006E037C /* NSProgressIndicator+Simplenote.swift */; };
Expand Down Expand Up @@ -879,6 +880,7 @@
BAA0A88626B9F8B40006260E /* Date+Simplenote.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Date+Simplenote.swift"; sourceTree = "<group>"; };
BAA4854925D5E22000F3BDB9 /* SearchQuery+Simplenote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SearchQuery+Simplenote.swift"; sourceTree = "<group>"; };
BAF1B0C12BC9B1C500B55F73 /* NoteWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoteWindow.swift; sourceTree = "<group>"; };
BAF1B0C72BCDDA9600B55F73 /* NoteWindowManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoteWindowManager.swift; sourceTree = "<group>"; };
BAF8D5DB26AE3BE800CA9383 /* markdown-light.css */ = {isa = PBXFileReference; lastKnownFileType = text.css; path = "markdown-light.css"; sourceTree = "<group>"; };
BAFB544F26CCA7F1006E037C /* NSProgressIndicator+Simplenote.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSProgressIndicator+Simplenote.swift"; sourceTree = "<group>"; };
F998F3EA22853C29008C2B59 /* CrashLogging.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CrashLogging.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1339,6 +1341,7 @@
B53BF1A324AC38C100938C34 /* VersionsController.swift */,
B53BF19F24AC1FB800938C34 /* Version.swift */,
BAA0A88126B9F8970006260E /* AccountDeletionController.swift */,
BAF1B0C72BCDDA9600B55F73 /* NoteWindowManager.swift */,
);
name = Controllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -2132,6 +2135,7 @@
B57CB87B244DEC4B00BA7969 /* MigrationsHandler.swift in Sources */,
B535014B249D5908003837C8 /* HorizontalScrollView.swift in Sources */,
2614F1E51405A0C60031AE94 /* Simplenote.xcdatamodeld in Sources */,
BAF1B0C82BCDDA9600B55F73 /* NoteWindowManager.swift in Sources */,
B5A2F1F02432DE54003B29C6 /* NSRange+Simplenote.swift in Sources */,
B58A71BE258422CC00601641 /* NSBezierPath+Simplenote.swift in Sources */,
B557A0E325BB0DC000E5313E /* AccountVerificationCoordinator.swift in Sources */,
Expand Down
6 changes: 2 additions & 4 deletions Simplenote/NoteListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -876,10 +876,8 @@ extension NoteListViewController {
return
}

let window = NoteWindow()
let windowController = NSWindowController(window: window)
window.show(note)
windowController.showWindow(window)
let controller = SimplenoteAppDelegate.shared().noteWindowManager.prepareWindowController(for: note)
controller.showWindow(nil)

// We don't want to be able to display the note in more than one editor at a time
// So when double tapped we open into a new window and close the editor in the main window
Expand Down
9 changes: 7 additions & 2 deletions Simplenote/NoteWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class NoteWindow: NSWindow {
editor.note?.simperiumKey
}

convenience init(note: Note) {
self.init()
load(note)
}

init() {
let storyboard = NSStoryboard(name: .main, bundle: nil)
self.editor = storyboard.instantiateViewController(ofType: NoteEditorViewController.self)
Expand All @@ -28,12 +33,12 @@ class NoteWindow: NSWindow {
private func setupWindow() {
contentViewController = editor
editor.metadataCache = SimplenoteAppDelegate.shared().noteEditorMetadataCache
editor.toolbarView.sidebarButton.isHidden = true
}

// MARK: Show Note
//
func show(_ note: Note) {
editor.toolbarView.sidebarButton.isHidden = true
func load(_ note: Note) {
editor.displayNote(note)
title = note.titlePreview
}
Expand Down
42 changes: 42 additions & 0 deletions Simplenote/NoteWindowManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// NoteWindowManager.swift
// Simplenote
//
// Created by Charlie Scheer on 4/15/24.
// Copyright © 2024 Simperium. All rights reserved.
//

import Foundation

class NoteWindowController: NSWindowController {
init(note: Note) {
let window = NoteWindow(note: note)
super.init(window: window)
}

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

class NoteWindowManager: NSObject {
var controllers = Set<NSWindowController>()

func prepareWindowController(for note: Note) -> NSWindowController {
let windowController = NoteWindowController(note: note)
windowController.window?.delegate = self

controllers.insert(windowController)

return windowController
}
}

extension NoteWindowManager: NSWindowDelegate {
func windowWillClose(_ notification: Notification) {
if let window = notification.object as? NSWindow,
let controller = window.windowController {
controllers.remove(controller)
}
}
}
5 changes: 5 additions & 0 deletions Simplenote/SimplenoteAppDelegate+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ extension SimplenoteAppDelegate {
splitViewController.insertSplitViewStatusBar(breadcrumbsViewController)
}

@objc
func configureNoteWindowManager() {
noteWindowManager = NoteWindowManager()
}

@objc
var window: Window {
// TODO: Temporary workaround. Let's get rid of this? please? 🔥🔥🔥
Expand Down
2 changes: 2 additions & 0 deletions Simplenote/SimplenoteAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@class VersionsController;
@class NoteEditorMetadataCache;
@class AccountDeletionController;
@class NoteWindowManager;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -48,6 +49,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nullable, strong, nonatomic) AccountDeletionController *accountDeletionController;

@property (strong, nonatomic) NoteEditorMetadataCache *noteEditorMetadataCache;
@property (strong, nonatomic, nonnull) NoteWindowManager *noteWindowManager;

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

Expand Down
1 change: 1 addition & 0 deletions Simplenote/SimplenoteAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ - (void)applicationWillFinishLaunching:(NSNotification *)notification
[self configureVerificationCoordinator];
[self configureVersionsController];
[self configureAccountDeletionController];
[self configureNoteWindowManager];

[self refreshStatusController];

Expand Down

0 comments on commit 4fc3b11

Please sign in to comment.