@@ -24,34 +24,55 @@ import Cocoa
2424import Sparkle
2525import Syntax_Highlight_XPC_Service
2626
27- typealias ExampleItem = ( url: URL , title: String , uti: String )
27+ typealias ExampleItem = ( url: URL , title: String , uti: String , standalone : Bool )
2828
2929@NSApplicationMain
3030class AppDelegate : NSObject , NSApplicationDelegate , NSMenuItemValidation {
31+ @IBOutlet weak var advancedSettingsMenu : NSMenuItem !
32+
3133 var userDriver : SPUStandardUserDriver ?
3234 var updater : SPUUpdater ?
3335
34- lazy var connection : NSXPCConnection = {
35- let connection = NSXPCConnection ( serviceName: " org.sbarex.SourceCodeSyntaxHighlight.XPCService " )
36- connection. remoteObjectInterface = NSXPCInterface ( with: SCSHXPCServiceProtocol . self)
37- connection. resume ( )
38- return connection
39- } ( )
36+ var isAdvancedSettingsVisible : Bool = false {
37+ didSet {
38+ advancedSettingsMenu. state = isAdvancedSettingsVisible ? . on : . off
39+
40+ guard oldValue != isAdvancedSettingsVisible else {
41+ return
42+ }
43+ UserDefaults . standard. setValue ( isAdvancedSettingsVisible, forKey: " advanced-settings " )
44+ NotificationCenter . default. post ( name: . AdvancedSettings, object: isAdvancedSettingsVisible)
45+ }
46+ }
4047
41- lazy var service : SCSHXPCServiceProtocol ? = {
42- let service = self . connection . synchronousRemoteObjectProxyWithErrorHandler { error in
43- print ( " Received error: " , error )
44- } as? SCSHXPCServiceProtocol
45-
46- return service
47- } ( )
48+ @ IBAction func handleAdvancedSettings ( _ sender : Any ) {
49+ isAdvancedSettingsVisible = !isAdvancedSettingsVisible
50+ }
51+
52+ func applicationShouldTerminate ( _ sender : NSApplication ) -> NSApplication . TerminateReply {
53+ return SCSHWrapper . shared . applicationShouldTerminate ( )
54+ }
4855
4956 func applicationDidFinishLaunching( _ aNotification: Notification ) {
57+ let utis = handledUTIs
58+ DispatchQueue . global ( qos: . userInitiated) . async ( ) {
59+ for uti in utis {
60+ uti. initLazyVars ( async : false )
61+ uti. fetchIcon ( async : false )
62+ }
63+ }
64+
5065 // Insert code here to initialize your application
5166 if #available( OSX 10 . 12 . 2 , * ) {
5267 NSApplication . shared. isAutomaticCustomizeTouchBarMenuItemEnabled = true
5368 }
5469
70+ if let state = UserDefaults . standard. object ( forKey: " advanced-settings " ) as? Bool {
71+ isAdvancedSettingsVisible = state
72+ } else {
73+ isAdvancedSettingsVisible = false
74+ }
75+
5576 let hostBundle = Bundle . main
5677 let applicationBundle = hostBundle;
5778
@@ -86,7 +107,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
86107
87108 func applicationWillTerminate( _ aNotification: Notification ) {
88109 // Insert code here to tear down your application
89- self . connection. invalidate ( )
110+ SCSHWrapper . connection. invalidate ( )
90111 }
91112
92113 func applicationShouldTerminateAfterLastWindowClosed( _ sender: NSApplication ) -> Bool {
@@ -111,16 +132,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
111132 return nil
112133 }
113134
114- /// Get all handled UTIs.
115- func fetchHandledUTIs( ) -> [ UTIDesc ] {
135+ lazy var handledUTIs : [ UTI ] = {
116136 // Get the list of all uti supported by the quicklook extension.
117137 guard let url = getQLAppexUrl ( ) , let bundle = Bundle ( url: url) , let extensionInfo = bundle. object ( forInfoDictionaryKey: " NSExtension " ) as? [ String : Any ] , let attributes = extensionInfo [ " NSExtensionAttributes " ] as? [ String : Any ] , let supportedTypes = attributes [ " QLSupportedContentTypes " ] as? [ String ] else {
118138 return [ ]
119139 }
120140
121- var fileTypes : [ UTIDesc ] = [ ]
141+ var fileTypes : [ UTI ] = [ ]
122142 for type in supportedTypes {
123- let uti = UTIDesc ( UTI: type)
143+ let uti = UTI ( type)
124144 if uti. isValid {
125145 fileTypes. append ( uti)
126146 } else {
@@ -130,14 +150,18 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
130150
131151 // Sort alphabetically.
132152 fileTypes. sort { ( a, b) -> Bool in
133- return a. description < b. description
153+ return a. description. lowercased ( ) < b. description. lowercased ( )
134154 }
135155
136156 return fileTypes
137- }
157+ } ( )
138158
159+ fileprivate var allExamples : [ ExampleItem ] ?
139160 /// Get the list of available source file example.
140161 func getAvailableExamples( ) -> [ ExampleItem ] {
162+ if let allExamples = self . allExamples {
163+ return allExamples
164+ }
141165 // Populate the example files list.
142166 var examples : [ ExampleItem ] = [ ]
143167 if let examplesDirURL = Bundle . main. url ( forResource: " examples " , withExtension: nil ) {
@@ -147,10 +171,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
147171 let title : String
148172 if let uti = UTI ( URL: file) {
149173 title = uti. description + " (. " + file. pathExtension + " ) "
150- examples. append ( ( url: file, title: title, uti: uti. UTI) )
174+ examples. append ( ( url: file, title: title, uti: uti. UTI, standalone : true ) )
151175 } else {
152176 title = file. lastPathComponent
153- examples. append ( ( url: file, title: title, uti: " " ) )
177+ examples. append ( ( url: file, title: title, uti: " " , standalone : true ) )
154178 }
155179
156180 }
@@ -159,11 +183,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
159183 }
160184 }
161185 }
186+ self . allExamples = examples
162187 return examples
163188 }
164189
165190 @IBAction func openApplicationSupportFolder( _ sender: Any ) {
166- service? . getApplicationSupport ( reply: { ( url) in
191+ SCSHWrapper . service? . getApplicationSupport ( reply: { ( url) in
167192 if let u = url, FileManager . default. fileExists ( atPath: u. path) {
168193 // Open the Finder to the application support folder.
169194 NSWorkspace . shared. activateFileViewerSelecting ( [ u] )
@@ -179,5 +204,23 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuItemValidation {
179204 }
180205 } )
181206 }
207+
208+ @IBAction func selectSettingsFile( _ sender: Any ) {
209+ SCSHWrapper . service? . getSettingsURL ( reply: { ( url) in
210+ if let u = url, FileManager . default. fileExists ( atPath: u. path) {
211+ // Open the Finder to the settings file.
212+ NSWorkspace . shared. activateFileViewerSelecting ( [ u] )
213+ } else {
214+ let alert = NSAlert ( )
215+ alert. window. title = " Attention "
216+ alert. messageText = " Settings not found "
217+ alert. informativeText = " You probably haven't customize the standard settings. "
218+ alert. addButton ( withTitle: " Close " )
219+ alert. alertStyle = . informational
220+
221+ alert. runModal ( )
222+ }
223+ } )
224+ }
182225}
183226
0 commit comments