Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcuts: Add open new note intent #1160

Merged
merged 7 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 5 additions & 1 deletion IntentsExtension/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
<dict>
<key>IntentsRestrictedWhileLocked</key>
<array/>
<key>IntentsSupported</key>
<key>IntentsRestrictedWhileProtectedDataUnavailable</key>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL!

<array/>
<key>IntentsSupported</key>
<array>
<string>OpenNewNoteIntent</string>
</array>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.intents-service</string>
Expand Down
12 changes: 6 additions & 6 deletions IntentsExtension/IntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import Intents

class IntentHandler: INExtension {

override func handler(for intent: INIntent) -> Any {
// This is the default implementation. If you want different objects to handle different intents,
// you can override this and return the handler you want for that particular intent.

return self
switch intent {
case is OpenNewNoteIntent:
return OpenNewNoteIntentHandler()
default:
return self
}
}

}
15 changes: 15 additions & 0 deletions IntentsExtension/IntentHandlers/OpenNewNoteIntentHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// OpenNewNoteIntentHandler.swift
// Simplenote
//
// Created by Charlie Scheer on 5/23/24.
// Copyright © 2024 Simperium. All rights reserved.
//

import Intents

class OpenNewNoteIntentHandler: NSObject, OpenNewNoteIntentHandling {
func handle(intent: OpenNewNoteIntent, completion: @escaping (OpenNewNoteIntentResponse) -> Void) {
completion(OpenNewNoteIntentResponse(code: .continueInApp, userActivity: nil))
}
}
22 changes: 22 additions & 0 deletions Simplenote.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@
BAB2616B2BFFD0AF009A98D7 /* IntentsExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = BAB261622BFFD0AF009A98D7 /* IntentsExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
BAB261712BFFD0F4009A98D7 /* ShortcutIntents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = BAB261702BFFD0F4009A98D7 /* ShortcutIntents.intentdefinition */; };
BAB261722BFFD0F4009A98D7 /* ShortcutIntents.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = BAB261702BFFD0F4009A98D7 /* ShortcutIntents.intentdefinition */; };
BAB261752BFFD319009A98D7 /* OpenNewNoteIntentHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB261742BFFD319009A98D7 /* OpenNewNoteIntentHandler.swift */; };
BAB261762BFFD319009A98D7 /* OpenNewNoteIntentHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB261742BFFD319009A98D7 /* OpenNewNoteIntentHandler.swift */; };
BAB261782BFFD38D009A98D7 /* ShortcutHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB261772BFFD38D009A98D7 /* ShortcutHandler.swift */; };
BAB2617B2BFFD40B009A98D7 /* ActivityType.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAB2617A2BFFD40B009A98D7 /* ActivityType.swift */; };
BAD4ECC026E6FC0A00881CC4 /* markdown-light.css in Resources */ = {isa = PBXBuildFile; fileRef = BAF8D5DB26AE3BE800CA9383 /* markdown-light.css */; };
BAE66CAA26AF647500398FF3 /* Remote.swift in Sources */ = {isa = PBXBuildFile; fileRef = BA938CEB26ACFF4A00BE5A1D /* Remote.swift */; };
BAF1B0C32BC9B1C500B55F73 /* NoteWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = BAF1B0C12BC9B1C500B55F73 /* NoteWindow.swift */; };
Expand Down Expand Up @@ -669,6 +673,9 @@
BAB261672BFFD0AF009A98D7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
BAB261682BFFD0AF009A98D7 /* IntentsExtension.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = IntentsExtension.entitlements; sourceTree = "<group>"; };
BAB261702BFFD0F4009A98D7 /* ShortcutIntents.intentdefinition */ = {isa = PBXFileReference; lastKnownFileType = file.intentdefinition; path = ShortcutIntents.intentdefinition; sourceTree = "<group>"; };
BAB261742BFFD319009A98D7 /* OpenNewNoteIntentHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpenNewNoteIntentHandler.swift; sourceTree = "<group>"; };
BAB261772BFFD38D009A98D7 /* ShortcutHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutHandler.swift; sourceTree = "<group>"; };
BAB2617A2BFFD40B009A98D7 /* ActivityType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityType.swift; sourceTree = "<group>"; };
BAF1B0C12BC9B1C500B55F73 /* NoteWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoteWindow.swift; sourceTree = "<group>"; };
BAF1B0C72BCDDA9600B55F73 /* NoteWindowControllersManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoteWindowControllersManager.swift; sourceTree = "<group>"; };
BAF8D5DB26AE3BE800CA9383 /* markdown-light.css */ = {isa = PBXFileReference; lastKnownFileType = text.css; path = "markdown-light.css"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1205,6 +1212,8 @@
isa = PBXGroup;
children = (
B574CA72242D552100F8D02F /* TextViewInputHandler.swift */,
BAB261772BFFD38D009A98D7 /* ShortcutHandler.swift */,
BAB2617A2BFFD40B009A98D7 /* ActivityType.swift */,
);
name = Handlers;
sourceTree = "<group>";
Expand Down Expand Up @@ -1492,13 +1501,22 @@
isa = PBXGroup;
children = (
BA39C4552C0134180004B2A9 /* IntentsExtensionDebug.entitlements */,
BAB261732BFFD2F6009A98D7 /* IntentHandlers */,
BAB261652BFFD0AF009A98D7 /* IntentHandler.swift */,
BAB261672BFFD0AF009A98D7 /* Info.plist */,
BAB261682BFFD0AF009A98D7 /* IntentsExtension.entitlements */,
);
path = IntentsExtension;
sourceTree = "<group>";
};
BAB261732BFFD2F6009A98D7 /* IntentHandlers */ = {
isa = PBXGroup;
children = (
BAB261742BFFD319009A98D7 /* OpenNewNoteIntentHandler.swift */,
);
path = IntentHandlers;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -1864,8 +1882,10 @@
A6C1E21525E010140076ADF7 /* SPApplication.swift in Sources */,
466FFEB417CC10A800399652 /* DateTransformer.m in Sources */,
BA71EC252BC88FFC00F42CB1 /* NSManagedObjectContext+Simplenote.swift in Sources */,
BAB261752BFFD319009A98D7 /* OpenNewNoteIntentHandler.swift in Sources */,
B5132FA923C4B9760065DD80 /* NSTextStorage+Simplenote.swift in Sources */,
375D293721E033D1007AB25A /* document.c in Sources */,
BAB261782BFFD38D009A98D7 /* ShortcutHandler.swift in Sources */,
376EE3EC202B748E00E3812E /* SPAboutTextField.swift in Sources */,
B542FE5725D42EC700A3582D /* Storage.m in Sources */,
B59EA98224AA5EFA008ABE4B /* NoteMetrics.swift in Sources */,
Expand Down Expand Up @@ -1922,6 +1942,7 @@
B586C063245CCD35009508EC /* SplitViewController.swift in Sources */,
B5E196C2230F5F5300F5658A /* SPCredentials.swift in Sources */,
B51374F71AC0591900825FCC /* SPConstants.m in Sources */,
BAB2617B2BFFD40B009A98D7 /* ActivityType.swift in Sources */,
B52197872541C65C00182928 /* NumberFormatter+Simplenote.swift in Sources */,
B5E086392448EE9D00DEF476 /* NSTableView+Simplenote.swift in Sources */,
3712FC871FE1ACAA008544AC /* Element.swift in Sources */,
Expand Down Expand Up @@ -2024,6 +2045,7 @@
buildActionMask = 2147483647;
files = (
BAB261722BFFD0F4009A98D7 /* ShortcutIntents.intentdefinition in Sources */,
BAB261762BFFD319009A98D7 /* OpenNewNoteIntentHandler.swift in Sources */,
BAB261662BFFD0AF009A98D7 /* IntentHandler.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
13 changes: 13 additions & 0 deletions Simplenote/ActivityType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ActivityType.swift
// Simplenote
//
// Created by Charlie Scheer on 5/23/24.
// Copyright © 2024 Simperium. All rights reserved.
//

import Foundation

enum ActivityType: String {
case newNoteShortcut = "OpenNewNoteIntent"
}
1 change: 1 addition & 0 deletions Simplenote/NoteEditorViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)didReceiveNewContent;
- (void)willReceiveNewContent;
- (void)fixChecklistColoring;
- (void)createNoteFromNote:(nullable Note *)oldNote;
- (IBAction)deleteAction:(id)sender;
- (IBAction)printAction:(id)sender;
- (IBAction)adjustFontSizeAction:(id)sender;
Expand Down
37 changes: 37 additions & 0 deletions Simplenote/ShortcutHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ShortcutHandler.swift
// Simplenote
//
// Created by Charlie Scheer on 5/23/24.
// Copyright © 2024 Simperium. All rights reserved.
//

import Foundation

class ShortcutsHandler: NSObject {
@objc
static var shared = ShortcutsHandler()

/// Is User authenticated?
///
private var isAuthenticated: Bool {
return SimplenoteAppDelegate.shared().simperium.user?.authenticated() == true
}

/// Handles a UserActivity instance. Returns true on success.
///
@objc
func handleUserActivity(_ userActivity: NSUserActivity) -> Bool {
guard let type = ActivityType(rawValue: userActivity.activityType),
isAuthenticated else {
return false
}

switch type {
case .newNoteShortcut:
SimplenoteAppDelegate.shared().noteEditorViewController.createNote(from: nil)
}

return true
}
charliescheer marked this conversation as resolved.
Show resolved Hide resolved
}
75 changes: 69 additions & 6 deletions Simplenote/ShortcutIntents.intentdefinition
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,74 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>INIntents</key>
<array>
</array>
<key>INEnums</key>
<array>
</array>
<key>INEnums</key>
<array/>
<key>INIntentDefinitionModelVersion</key>
<string>1.2</string>
<key>INIntentDefinitionNamespace</key>
<string>ihfyvu</string>
<key>INIntentDefinitionSystemVersion</key>
<string>23E214</string>
<key>INIntentDefinitionToolsBuildVersion</key>
<string>15F31d</string>
<key>INIntentDefinitionToolsVersion</key>
<string>15.4</string>
<key>INIntents</key>
<array>
<dict>
<key>INIntentCategory</key>
<string>information</string>
<key>INIntentConfigurable</key>
<true/>
<key>INIntentDescription</key>
<string>Open Simplenote and create a new note</string>
<key>INIntentDescriptionID</key>
<string>DasQmf</string>
<key>INIntentIneligibleForSuggestions</key>
<true/>
<key>INIntentManagedParameterCombinations</key>
<dict>
<key></key>
<dict>
<key>INIntentParameterCombinationSupportsBackgroundExecution</key>
<true/>
<key>INIntentParameterCombinationTitle</key>
<string>Open a new note in Simplenote</string>
<key>INIntentParameterCombinationTitleID</key>
<string>1njn3I</string>
<key>INIntentParameterCombinationUpdatesLinked</key>
<true/>
</dict>
</dict>
<key>INIntentName</key>
<string>OpenNewNote</string>
<key>INIntentResponse</key>
<dict>
<key>INIntentResponseCodes</key>
<array>
<dict>
<key>INIntentResponseCodeName</key>
<string>success</string>
<key>INIntentResponseCodeSuccess</key>
<true/>
</dict>
<dict>
<key>INIntentResponseCodeName</key>
<string>failure</string>
</dict>
</array>
</dict>
<key>INIntentTitle</key>
<string>Open New Note</string>
<key>INIntentTitleID</key>
<string>3SWPX6</string>
<key>INIntentType</key>
<string>Custom</string>
<key>INIntentVerb</key>
<string>Open</string>
</dict>
</array>
<key>INTypes</key>
<array/>
</dict>
</plist>
8 changes: 6 additions & 2 deletions Simplenote/Simplenote-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
</array>
<key>CFBundleVersion</key>
<string>$BUILD_NUMBER</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSApplicationCategoryType</key>
<string>public.app-category.productivity</string>
<key>LSMinimumSystemVersion</key>
Expand All @@ -52,7 +54,9 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>SPApplication</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>NSUserActivityTypes</key>
<array>
<string>OpenNewNoteIntent</string>
</array>
</dict>
</plist>
2 changes: 1 addition & 1 deletion Simplenote/SimplenoteAppDelegate+Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ extension SimplenoteAppDelegate {
return true
}

return false
return ShortcutsHandler().handleUserActivity(userActivity)
}
}

Expand Down
Loading