Skip to content

Commit 252f44c

Browse files
committed
Bugfix on font customization and theme selection.
Also optimisation of the code and
1 parent ebe42c8 commit 252f44c

File tree

10 files changed

+605
-117
lines changed

10 files changed

+605
-117
lines changed

Application/Base.lproj/Main.storyboard

Lines changed: 166 additions & 39 deletions
Large diffs are not rendered by default.
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
//
2+
// CustomTypeViewController.swift
3+
// Syntax Highlight
4+
//
5+
// Created by Sbarex on 15/11/2019.
6+
// Copyright © 2019 sbarex. All rights reserved.
7+
//
8+
//
9+
// This file is part of SourceCodeSyntaxHighlight.
10+
// SourceCodeSyntaxHighlight is free software: you can redistribute it and/or modify
11+
// it under the terms of the GNU General Public License as published by
12+
// the Free Software Foundation, either version 3 of the License, or
13+
// (at your option) any later version.
14+
//
15+
// SourceCodeSyntaxHighlight is distributed in the hope that it will be useful,
16+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
// GNU General Public License for more details.
19+
//
20+
// You should have received a copy of the GNU General Public License
21+
// along with SourceCodeSyntaxHighlight. If not, see <http://www.gnu.org/licenses/>.
22+
23+
import Cocoa
24+
import Syntax_Highlight_XPC_Service
25+
26+
class DropView: NSTextField {
27+
weak var dropDelegate: DropViewDelegate?
28+
29+
var acceptableTypes: [NSPasteboard.PasteboardType] { return [.fileURL] }
30+
31+
override init(frame frameRect: NSRect) {
32+
super.init(frame: frameRect)
33+
setup()
34+
}
35+
36+
required init?(coder: NSCoder) {
37+
super.init(coder: coder)
38+
setup()
39+
}
40+
41+
func setup() {
42+
registerForDraggedTypes(acceptableTypes)
43+
self.wantsLayer = true
44+
self.layer?.cornerRadius = 12
45+
self.layer?.borderColor = NSColor.gridColor.cgColor
46+
self.layer?.borderWidth = 4
47+
}
48+
49+
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
50+
self.layer?.borderColor = NSColor.selectedControlColor.cgColor
51+
return .every
52+
}
53+
54+
override func draggingExited(_ sender: NSDraggingInfo?) {
55+
self.layer?.borderColor = NSColor.gridColor.cgColor
56+
}
57+
58+
override func draggingEnded(_ sender: NSDraggingInfo) {
59+
if let fileUrl = sender.draggingPasteboard.pasteboardItems?.first?.propertyList(forType: .fileURL) as? String, let url = URL(string: fileUrl) {
60+
61+
do {
62+
let v = try url.resourceValues(forKeys: [URLResourceKey.typeIdentifierKey])
63+
if let uti = v.typeIdentifier {
64+
let s = NSMutableAttributedString()
65+
66+
let u = UTI(uti)
67+
dropDelegate?.setUTI(u)
68+
69+
var label: String = u.description
70+
if u.extensions.count > 0 {
71+
label += (label.isEmpty ? "" : " ") + "[.\(u.extensions.joined(separator: ", ."))]"
72+
}
73+
74+
let style = NSMutableParagraphStyle()
75+
style.alignment = .center
76+
77+
if !label.isEmpty {
78+
s.append(NSAttributedString(string: label + "\n", attributes: [NSAttributedString.Key.paragraphStyle: style]))
79+
}
80+
s.append(NSAttributedString(string: uti, attributes: [NSAttributedString.Key.font : NSFont.systemFont(ofSize: NSFont.systemFontSize(for: NSControl.ControlSize.small)), NSAttributedString.Key.paragraphStyle: style]))
81+
82+
self.attributedStringValue = s
83+
}
84+
} catch {
85+
dropDelegate?.setUTI(nil)
86+
}
87+
} else {
88+
dropDelegate?.setUTI(nil)
89+
}
90+
self.layer?.borderColor = NSColor.gridColor.cgColor
91+
}
92+
}
93+
94+
protocol DropViewDelegate: class {
95+
func setUTI(_ type: UTI?)
96+
}
97+
98+
class CustomTypeViewController: NSViewController, DropViewDelegate {
99+
@IBOutlet weak var dropView: DropView!
100+
@IBOutlet weak var warningLabel: NSTextField!
101+
@IBOutlet weak var warningImage: NSImageView!
102+
@IBOutlet weak var saveButton: NSButton!
103+
104+
var service: SCSHXPCServiceProtocol? {
105+
return (NSApplication.shared.delegate as? AppDelegate)?.service
106+
}
107+
var settings: SCSHSettings? {
108+
return (self.presentingViewController as? PreferencesViewController)?.settings
109+
}
110+
111+
var UTI: UTI? {
112+
didSet {
113+
warningLabel.isHidden = true
114+
warningImage.isHidden = true
115+
saveButton.isEnabled = false
116+
117+
if let uti = self.UTI {
118+
if let p = presentingViewController as? PreferencesViewController {
119+
if let _ = p.allFileTypes.first(where: { $0.uti == uti }) {
120+
self.warningLabel.stringValue = "Format already recognized."
121+
self.warningImage.image = NSImage(named: NSImage.statusAvailableName)
122+
123+
self.warningLabel.isHidden = false
124+
self.warningImage.isHidden = false
125+
self.saveButton.isEnabled = false
126+
return
127+
} else {
128+
self.warningLabel.stringValue = "Not supported by highlight."
129+
self.warningImage.image = NSImage(named: NSImage.statusPartiallyAvailableName)
130+
}
131+
}
132+
service?.areSomeSyntaxSupported(uti.extensions, overrideSettings: nil, reply: { (state) in
133+
self.warningLabel.isHidden = state
134+
self.warningImage.isHidden = state
135+
self.saveButton.isEnabled = true
136+
})
137+
}
138+
}
139+
}
140+
141+
override func viewDidLoad() {
142+
self.dropView.dropDelegate = self
143+
144+
self.warningLabel.isHidden = true
145+
self.warningImage.isHidden = true
146+
self.saveButton.isEnabled = false
147+
}
148+
149+
func setUTI(_ type: UTI?) {
150+
self.UTI = type
151+
}
152+
153+
@IBAction func doSave(_ sender: Any) {
154+
if let p = presentingViewController as? PreferencesViewController, let url = p.getQLAppexUrl(), let bundle = Bundle(url: url) {
155+
156+
let alert = NSAlert()
157+
alert.messageText = "Warning"
158+
alert.informativeText = "Modifiy the supported format break code signature!\nDo you want to continue?"
159+
alert.addButton(withTitle: "Continue")
160+
alert.addButton(withTitle: "Cancel")
161+
alert.alertStyle = .critical
162+
163+
guard alert.runModal() == .alertFirstButtonReturn else {
164+
return
165+
}
166+
167+
let u = bundle.bundleURL.appendingPathComponent("Contents/info.plist")
168+
if let d = NSMutableDictionary(contentsOf: u) {
169+
if let NSExtension = d["NSExtension"] as? NSMutableDictionary, let NSExtensionAttributes = NSExtension["NSExtensionAttributes"] as? NSMutableDictionary, let QLSupportedContentTypes = NSExtensionAttributes["QLSupportedContentTypes"] as? NSMutableArray, let UTI = self.UTI?.UTI {
170+
QLSupportedContentTypes.add(UTI)
171+
print(QLSupportedContentTypes)
172+
173+
do {
174+
try d.write(to: u)
175+
} catch {
176+
let alert = NSAlert()
177+
alert.messageText = "Unable to apply the changes!"
178+
alert.informativeText = error.localizedDescription
179+
alert.addButton(withTitle: "Close")
180+
alert.alertStyle = .critical
181+
alert.runModal()
182+
}
183+
}
184+
}
185+
print(u)
186+
}
187+
dismiss(sender)
188+
}
189+
}

0 commit comments

Comments
 (0)