Skip to content

Commit 7f456f5

Browse files
committed
Version 2.0 with a redesigned interface, highlight 4.1 and much more.
1 parent c36bf96 commit 7f456f5

File tree

5,660 files changed

+1059560
-25376
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

5,660 files changed

+1059560
-25376
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ DerivedData/
2525

2626
## Gcc Patch
2727
/*.gcno
28+
29+
.idea/

.gitmodules

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[submodule "lua4swift"]
2-
path = lua4swift
3-
url = https://github.com/sbarex/lua4swift.git
1+
[submodule "highlight-wrapper/highlight"]
2+
path = highlight-wrapper/highlight
3+
url = https://gitlab.com/saalen/highlight.git
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
//
2+
// AboutViewController.swift
3+
// SyntaxHighlight
4+
//
5+
// Created by Sbarex on 07/01/21.
6+
//
7+
8+
import Cocoa
9+
import WebKit
10+
11+
class AboutViewController: NSViewController, WKNavigationDelegate {
12+
@IBOutlet weak var imageView: NSImageView!
13+
@IBOutlet weak var titleField: NSTextField!
14+
@IBOutlet weak var versionField: NSTextField!
15+
@IBOutlet weak var copyrightField: NSTextField!
16+
@IBOutlet weak var infoWebView: WKWebView!
17+
18+
override func viewDidLoad() {
19+
imageView.image = NSApplication.shared.applicationIconImage
20+
if let info = Bundle.main.infoDictionary {
21+
let version = info["CFBundleShortVersionString"] as? String ?? ""
22+
let build = info["CFBundleVersion"] as? String ?? ""
23+
24+
titleField.stringValue = info["CFBundleExecutable"] as? String ?? "Syntax Highlight"
25+
versionField.stringValue = "Version \(version) (\(build))"
26+
copyrightField.stringValue = (info["NSHumanReadableCopyright"] as? String ?? "").trimmingCharacters(in: CharacterSet(charactersIn: ". ")) + " with ❤️"
27+
} else {
28+
versionField.stringValue = ""
29+
copyrightField.stringValue = ""
30+
}
31+
32+
let fg_color = NSColor.textColor.toHexString() ?? "#000000"
33+
let bg_color = NSColor.textBackgroundColor.toHexString() ?? "#ffffff"
34+
let grid_color = NSColor.tertiaryLabelColor.toHexString() ?? "#cccccc"
35+
36+
let font_size = NSFont.smallSystemFontSize
37+
let head_font_size = NSFont.systemFontSize
38+
39+
let dos2unix: String
40+
if let c = Bundle.main.path(forResource: "dos2unix", ofType: nil), let ci = try? ShellTask.runTask(command: c, arguments: ["-V"]), ci.isSuccess, let s = String(data: ci.data, encoding: .utf8) {
41+
dos2unix = s.replacingOccurrences(of: "\n", with: "<br />\n")
42+
} else {
43+
dos2unix = ""
44+
}
45+
let info = HighlightWrapper.shared.getHighlightInfo()
46+
let html = """
47+
<html>
48+
<head>
49+
<title>About</title>
50+
<style type="text/css">
51+
:root {
52+
--backgroundColor: \(bg_color);
53+
--textColor: \(fg_color);
54+
}
55+
56+
html {
57+
padding: 0;
58+
margin: 0;
59+
-webkit-text-size-adjust: none;
60+
text-size-adjust: none;
61+
font-family: -apple-system;
62+
font-size: \(font_size)px;
63+
}
64+
65+
td, tr, table {
66+
font-family: -apple-system;
67+
font-size: \(font_size)px;
68+
}
69+
table {
70+
width: 100%;
71+
border-collapse: collapse;
72+
}
73+
td {
74+
vertical-align: top;
75+
}
76+
td:first-child {
77+
min-width: 10em;
78+
}
79+
h2 {
80+
font-size: \(head_font_size)px;
81+
margin-top: 1.5em;
82+
}
83+
84+
h2:first-child {
85+
margin-top: 0;
86+
}
87+
88+
h2 {
89+
font-size: \((head_font_size+font_size) / 2)px;
90+
}
91+
92+
body {
93+
margin: 0;
94+
padding: 1em;
95+
color: var(--textColor);
96+
background-color: var(--backgroundColor);
97+
text-align: center;
98+
}
99+
a {
100+
color: var(--textColor);
101+
}
102+
103+
table tr td {
104+
border-bottom: 1px solid \(grid_color);
105+
}
106+
table tr:last-child td {
107+
border-bottom: none;
108+
}
109+
hr {
110+
margin-top: 12pt;
111+
margin-bottom: 6pt;
112+
}
113+
</style>
114+
</head>
115+
<body>
116+
<h2>Developer</h2>
117+
<a href='https://github.com/sbarex/'>sbarex</a><br />
118+
<a href='https://github.com/sbarex/SourceCodeSyntaxHighlight'>https://github.com/sbarex/SourceCodeSyntaxHighlight</a><br />
119+
<hr />
120+
\(info)
121+
<hr />
122+
<h2>dos2unix</h2>
123+
\(dos2unix)
124+
</body>
125+
</html>
126+
"""
127+
128+
infoWebView.loadHTMLString(html, baseURL: nil)
129+
130+
//self.infoTextView.isHidden = true
131+
}
132+
133+
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
134+
if navigationAction.navigationType == .linkActivated {
135+
if let url = navigationAction.request.url {
136+
NSWorkspace.shared.open(url)
137+
decisionHandler(.cancel)
138+
} else {
139+
print("Open it locally")
140+
decisionHandler(.allow)
141+
}
142+
} else {
143+
print("not a user click")
144+
decisionHandler(.allow)
145+
}
146+
}
147+
148+
override func viewWillAppear() {
149+
super.viewWillAppear()
150+
view.window?.standardWindowButton(.zoomButton)?.isHidden = true
151+
view.window?.standardWindowButton(.miniaturizeButton)?.isHidden = true
152+
view.window?.titlebarAppearsTransparent = true
153+
if #available(OSX 11.0, *) {
154+
view.window?.toolbarStyle = .unifiedCompact
155+
view.window?.titlebarSeparatorStyle = .none
156+
}
157+
view.window?.hidesOnDeactivate = true
158+
}
159+
160+
@IBAction func cancel(_ sender: Any?) {
161+
self.dismiss(sender)
162+
}
163+
}

Application/AppDelegate.swift

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,55 @@ import Cocoa
2424
import Sparkle
2525
import 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
3030
class 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

Comments
 (0)