-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathKeymanWebViewController.swift
1255 lines (1077 loc) · 45.3 KB
/
KeymanWebViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// KeymanWebViewController.swift
// KeymanEngine
//
// Created by Gabriel Wong on 2017-10-31.
// Copyright © 2017 SIL International. All rights reserved.
//
import UIKit
import WebKit
import AudioToolbox
import Sentry
import XCGLogger
private let keyboardChangeHelpText = NSLocalizedString("keyboard-help-change", bundle: engineBundle, comment: "")
private let subKeyColor = Colors.popupKey
private let subKeyColorHighlighted = Colors.popupKeyHighlighted
// We subclass for one critical reason - by default, WebViews may become first responder...
// a detail that is really, REALLY bad for a WebView in a keyboard.
//
// Minor reference here: https://stackoverflow.com/questions/39829863/can-a-uiwebview-handle-user-interaction-without-becoming-first-responder
//
// Confirmed issue existed within app during workaround for https://github.com/keymanapp/keyman/issues/2716
class KeymanWebView: WKWebView {
override public var canBecomeFirstResponder: Bool {
return false;
}
override public func becomeFirstResponder() -> Bool {
return false;
}
}
// MARK: - UIViewController
class KeymanWebViewController: UIViewController {
let storage: Storage
weak var delegate: KeymanWebDelegate?
private var useSpecialFont = false
private var userContentController = WKUserContentController()
private let keymanWebViewName: String = "keyman"
// Views
var webView: KeymanWebView?
var activeModel: Bool = false
private var helpBubbleView: PopoverView?
private var keyPreviewView: KeyPreviewView?
private var subKeysView: SubKeysView?
private var keyboardMenuView: KeyboardMenuView?
// Arrays
private var subKeyIDs: [String] = []
private var subKeyTexts: [String] = []
private var subKeys: [UIButton] = []
private var subKeyAnchor = CGRect.zero
/// Stores the keyboard view's current size.
private var kbSize: CGSize = CGSize.zero
/// Stores the current image for use by the Banner
/// when predictive text is not active
private var bannerImgPath: String = ""
var shouldReload: Bool = true
var isLoading: Bool = false
private var currentText: String = ""
private var currentCursorRange: NSRange? = nil
init(storage: Storage) {
self.storage = storage
super.init(nibName: nil, bundle: nil)
_ = view
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func fixLayout() {
view.setNeedsLayout()
view.layoutIfNeeded()
}
override func viewWillLayoutSubviews() {
// This method is called automatically during layout correction by iOS.
// It also has access to correct `view.bounds.size` values, unlike viewDidAppear.
// As a result, it's the correct place to perform OSK size adjustments.
//
// Problem - this is ALSO called automatically upon any touch-based interaction with the OSK! (Why!?)
// The `keyboardSize` property will filter out any such redundant size-change requests to prevent issues
// that would otherwise arise. (Important event handlers can trigger for the original OSK instance
// after it has been replaced by KMW's OSK resizing operation.)
keyboardSize = view.bounds.size
}
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
if Manager.shared.isKeymanHelpOn {
showHelpBubble(afterDelay: 1.5)
}
coordinator.animateAlongsideTransition(in: nil, animation: {
_ in
self.fixLayout()
}, completion: {
_ in
// When going from landscape to portrait, the value is often not properly set until the end of the call chain.
// A simple, ultra-short timer allows us to quickly rectify the value in these cases to correct the keyboard.
Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: #selector(self.fixLayout), userInfo: nil, repeats: false)
})
}
override func loadView() {
let config = WKWebViewConfiguration()
let prefs = WKPreferences()
prefs.javaScriptEnabled = true
config.preferences = prefs
config.suppressesIncrementalRendering = false
config.userContentController = self.userContentController
webView = KeymanWebView(frame: CGRect(origin: .zero, size: keyboardSize), configuration: config)
webView!.isOpaque = false
webView!.translatesAutoresizingMaskIntoConstraints = false
webView!.backgroundColor = UIColor.clear
webView!.navigationDelegate = self
webView!.scrollView.isScrollEnabled = false
view = webView
// Set UILongPressGestureRecognizer to show sub keys
// let hold = UILongPressGestureRecognizer(target: self, action: #selector(self.holdAction))
// hold.minimumPressDuration = 0.5
// hold.delegate = self
// view.addGestureRecognizer(hold)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow),
name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide),
name: UIResponder.keyboardWillHideNotification, object: nil)
reloadKeyboard()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.userContentController.add(self, name: keymanWebViewName)
}
// Very useful for immediately adjusting the WebView's properties upon loading.
override func viewDidAppear(_ animated: Bool) {
fixLayout()
// Initialize the keyboard's size/scale. In iOS 13 (at least), the system
// keyboard's width will be set at this stage, but not in viewWillAppear.
keyboardSize = view.bounds.size
}
override func viewWillDisappear(_ animated: Bool) {
self.userContentController.removeScriptMessageHandler(forName: keymanWebViewName)
}
}
// MARK: - JavaScript functions
extension KeymanWebViewController {
func languageMenuPosition(_ completion: @escaping (CGRect) -> Void) {
webView!.evaluateJavaScript("langMenuPos();") { result, _ in
guard let result = result as? String, !result.isEmpty else {
return
}
let components = result.components(separatedBy: ",")
let x = CGFloat(Float(components[0])!)
let y = CGFloat(Float(components[1])!)
let w = CGFloat(Float(components[2])!)
let h = CGFloat(Float(components[3])!)
completion(KeymanWebViewController.keyFrame(x: x, y: y, w: w, h: h))
}
}
// FIXME: text is unused in the JS
func executePopupKey(id: String, text: String) {
// Text must be checked for ', ", and \ characters; they must be escaped properly!
do {
let encodingArray = [ text ];
let jsonString = try String(data: JSONSerialization.data(withJSONObject: encodingArray), encoding: .utf8)!
let start = jsonString.index(jsonString.startIndex, offsetBy: 2)
let end = jsonString.index(jsonString.endIndex, offsetBy: -2)
let escapedText = jsonString[start..<end]
let cmd = "executePopupKey(\"\(id)\",\"\(escapedText)\");"
webView!.evaluateJavaScript(cmd, completionHandler: nil)
} catch {
SentryManager.captureAndLog(error)
return
}
}
func setOskWidth(_ width: Int) {
webView?.evaluateJavaScript("setOskWidth(\(width));", completionHandler: nil)
}
func setOskHeight(_ height: Int) {
webView?.evaluateJavaScript("setOskHeight(\(height));", completionHandler: nil)
}
func setPopupVisible(_ visible: Bool) {
webView!.evaluateJavaScript("popupVisible(\(visible));", completionHandler: nil)
}
private func setSpacebarText(_ mode: SpacebarText) {
webView!.evaluateJavaScript("setSpacebarText('\(mode.rawValue)');", completionHandler: nil);
}
func updateSpacebarText() {
let userData = Storage.active.userDefaults
setSpacebarText(userData.optSpacebarText)
}
func setCursorRange(_ range: NSRange) {
if range.location != NSNotFound {
webView!.evaluateJavaScript("setCursorRange(\(range.location),\(range.length));", completionHandler: nil)
self.currentCursorRange = range
}
}
func setText(_ text: String?) {
var text = text ?? ""
// Remove any system-added LTR/RTL marks.
text = text.replacingOccurrences(of: "\u{200e}", with: "") // Unicode's LTR codepoint
text = text.replacingOccurrences(of: "\u{200f}", with: "") // Unicode's RTL codepoint (v1)
text = text.replacingOccurrences(of: "\u{202e}", with: "") // Unicode's RTL codepoint (v2)
do {
let encodingArray = [ text ];
let jsonString = try String(data: JSONSerialization.data(withJSONObject: encodingArray), encoding: .utf8)!
let start = jsonString.index(jsonString.startIndex, offsetBy: 2)
let end = jsonString.index(jsonString.endIndex, offsetBy: -2)
let jsonText = jsonString[start..<end]
self.currentText = String(jsonText)
webView!.evaluateJavaScript("setKeymanVal(\"\(jsonText)\");", completionHandler: nil)
} catch {
SentryManager.captureAndLog(error.localizedDescription)
}
}
func resetContext() {
webView!.evaluateJavaScript("doResetContext();", completionHandler: nil)
}
private func fontObject(from font: Font?, keyboard: InstallableKeyboard, isOsk: Bool) -> [String: Any]? {
guard let font = font else {
return nil
}
// family does not have to match the name in the font file. It only has to be unique.
return [
"family": "\(keyboard.id)__\(isOsk ? "osk" : "display")",
"files": font.source.map { storage.fontURL(forResource: keyboard, filename: $0)!.absoluteString }
]
}
func setKeyboard(_ keyboard: InstallableKeyboard) throws {
let fileURL = storage.keyboardURL(for: keyboard)
var stub: [String: Any] = [
"KI": "Keyboard_\(keyboard.id)",
"KN": keyboard.name,
"KLC": keyboard.languageID,
"KL": keyboard.languageName,
"KF": fileURL.absoluteString
]
if let packageID = keyboard.packageID {
stub["KP"] = packageID
}
if let displayName = keyboard.displayName {
stub["displayName"] = displayName
}
// Warning: without special handling, any `guard` that fails here can trigger an
// infinite keyboard reload, as the keyboard page itself will note that the keyboard
// failed to initialize properly.
guard FileManager.default.fileExists(atPath: fileURL.path) else {
let event = Sentry.Event(level: .error)
event.message = SentryMessage(formatted: "File missing for keyboard")
event.extra = [ "id": keyboard.id, "file": fileURL ]
if let packageID = keyboard.packageID {
event.extra?["package"] = packageID
}
SentryManager.captureAndLog(event)
throw KeyboardError.fileMissing
}
let displayFont = fontObject(from: keyboard.font, keyboard: keyboard, isOsk: false)
let oskFont = fontObject(from: keyboard.oskFont, keyboard: keyboard, isOsk: true) ?? displayFont
if let displayFont = displayFont {
stub["KFont"] = displayFont
}
if let oskFont = oskFont {
stub["KOskFont"] = oskFont
}
let data: Data
do {
data = try JSONSerialization.data(withJSONObject: stub, options: [])
} catch {
let event = Sentry.Event(error: error)
event.message = SentryMessage(formatted: "Failed to serialize keyboard stub: \(error)")
event.extra = [:]
event.extra!["id"] = stub["KI"]
event.extra!["package"] = stub["KP"]
SentryManager.captureAndLog(event)
throw KeyboardError.keyboardLoadingError
}
guard let stubString = String(data: data, encoding: .utf8) else {
let event = Sentry.Event(level: .error)
event.message = SentryMessage(formatted: "Failed to create keyboard stub string for embedded KMW")
event.extra = [:]
event.extra!["id"] = stub["KI"]
event.extra!["package"] = stub["KP"]
SentryManager.captureAndLog(event)
throw KeyboardError.keyboardLoadingError
}
SentryManager.breadcrumbAndLog("Keyboard stub built for \(keyboard.id)", logLevel: XCGLogger.Level.none)
log.info("Keyboard stub: \(stubString)")
webView!.evaluateJavaScript("setKeymanLanguage(\(stubString));", completionHandler: nil)
}
func deregisterLexicalModel(_ lexicalModel: InstallableLexicalModel) {
webView!.evaluateJavaScript("keyman.removeModel(\"\(lexicalModel.id)\")")
}
func registerLexicalModel(_ lexicalModel: InstallableLexicalModel) throws {
let fileURL = storage.lexicalModelURL(for: lexicalModel)
let stub: [String: Any] = [
"id": lexicalModel.id,
"languages": [lexicalModel.languageID], // Change when InstallableLexicalModel is updated to store an array
"path": fileURL.absoluteString
]
guard FileManager.default.fileExists(atPath: fileURL.path) else {
let event = Sentry.Event(level: .error)
event.message = SentryMessage(formatted: "File missing for lexical model")
event.extra = [ "id": lexicalModel.id, "file": fileURL ]
if let packageID = lexicalModel.packageID {
event.extra?["package"] = packageID
}
SentryManager.captureAndLog(event)
throw KeyboardError.fileMissing
}
let data: Data
do {
data = try JSONSerialization.data(withJSONObject: stub, options: [])
} catch {
let event = Sentry.Event(error: error)
event.message = SentryMessage(formatted: "Failed to serialize lexical model stub: \(error)")
event.extra = [:]
event.extra!["id"] = stub["id"]
SentryManager.captureAndLog(event)
throw KeyboardError.lexicalModelLoadingError
}
guard let stubString = String(data: data, encoding: .utf8) else {
let event = Sentry.Event(level: .error)
event.message = SentryMessage(formatted: "Failed to create lexical model stub string for embedded KMW")
event.extra = [:]
event.extra!["id"] = stub["id"]
SentryManager.captureAndLog(event)
throw KeyboardError.lexicalModelLoadingError
}
SentryManager.breadcrumbAndLog("LexicalModel stub built for \(lexicalModel.id)", logLevel: XCGLogger.Level.none)
log.info("LexicalModel stub: \(stubString)")
if lexicalModel.languageID == Manager.shared.currentKeyboardID?.languageID {
// We're registering a lexical model for the now-current keyboard.
// Enact any appropriate language-modeling settings!
let userDefaults = Storage.active.userDefaults
let predict = userDefaults.predictSettingForLanguage(languageID: lexicalModel.languageID)
let correct = userDefaults.correctSettingForLanguage(languageID: lexicalModel.languageID)
// Pass these off to KMW!
// We do these first so that they're automatically set for the to-be-registered model in advance.
webView!.evaluateJavaScript("enableSuggestions(\(stubString), \(predict), \(correct))")
self.activeModel = predict
} else { // We're registering a model in the background - don't change settings.
webView!.evaluateJavaScript("keyman.addModel(\(stubString));", completionHandler: nil)
}
setBannerHeight(to: Int(InputViewController.topBarHeight))
}
func showBanner(_ display: Bool) {
SentryManager.breadcrumbAndLog("Changing banner's alwaysShow property to \(display).", category: "engine", sentryLevel: .debug)
webView?.evaluateJavaScript("showBanner(\(display ? "true" : "false"))", completionHandler: nil)
}
func setBannerImage(to path: String) {
bannerImgPath = path // Save the path in case delayed initializaiton is needed.
var logString: String
if path.contains("base64") || path.count > 256 {
logString = "<base64 image>"
} else {
logString = path
}
log.debug("Banner image path: '\(logString).'")
webView?.evaluateJavaScript("setBannerImage(\"\(path)\");", completionHandler: nil)
}
func setBannerHeight(to height: Int) {
webView?.evaluateJavaScript("setBannerHeight(\(height));", completionHandler: nil)
}
/**
* Ensures that the embedded KMW instance uses the app's current error-report toggle setting.
* This is always called during keyboard page initialization and may also be called any time
* thereafter for settings updates.
*/
func setSentryState(enabled: Bool = SentryManager.enabled) {
// This may be called before the page - and thus the `sentryManager` variable -
// is ready. It's a known limitation, so why log error reports for it?
webView?.evaluateJavaScript("try { sentryManager.enabled = \(enabled ? "true" : "false") } catch(err) { }")
}
}
// MARK: - WKScriptMessageHandler
extension KeymanWebViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController,
didReceive message: WKScriptMessage) {
guard let fragment = message.body as? String, !fragment.isEmpty else {
return
}
if fragment.hasPrefix("#insertText-") {
let dnRange = fragment.range(of: "+dn=")!
let sRange = fragment.range(of: "+s=")!
let drRange = fragment.range(of: "+dr=")!
let dn = Int(fragment[dnRange.upperBound..<sRange.lowerBound])!
let s = fragment[sRange.upperBound..<drRange.lowerBound]
// This computes the number of requested right-deletion characters.
// Use it when we're ready to implement that.
// Our .insertText will need to be adjusted accordingly.
_ = Int(fragment[drRange.upperBound...])!
// KMW uses dn == -1 to perform special processing of deadkeys.
// This is handled outside of Swift so we don't delete any characters.
let numCharsToDelete = max(0, dn)
let newText = String(s).stringFromUTF16CodeUnits() ?? ""
insertText(self, numCharsToDelete: numCharsToDelete, newText: newText)
delegate?.insertText(self, numCharsToDelete: numCharsToDelete, newText: newText)
} else if fragment.hasPrefix("#showKeyPreview-") {
let xKey = fragment.range(of: "+x=")!
let yKey = fragment.range(of: "+y=")!
let wKey = fragment.range(of: "+w=")!
let hKey = fragment.range(of: "+h=")!
let tKey = fragment.range(of: "+t=")!
let x = CGFloat(Float(fragment[xKey.upperBound..<yKey.lowerBound])!)
let y = CGFloat(Float(fragment[yKey.upperBound..<wKey.lowerBound])!)
let w = CGFloat(Float(fragment[wKey.upperBound..<hKey.lowerBound])!)
let h = CGFloat(Float(fragment[hKey.upperBound..<tKey.lowerBound])!)
let t = String(fragment[tKey.upperBound...])
let frame = KeymanWebViewController.keyFrame(x: x, y: y, w: w, h: h)
let preview = t.stringFromUTF16CodeUnits() ?? ""
showKeyPreview(self, keyFrame: frame, preview: preview)
delegate?.showKeyPreview(self, keyFrame: frame, preview: preview)
} else if fragment.hasPrefix("#dismissKeyPreview-") {
dismissKeyPreview(self)
delegate?.dismissKeyPreview(self)
} else if fragment.hasPrefix("#showMore-") {
let baseFrameKey = fragment.range(of: "+baseFrame=")!
let keysKey = fragment.range(of: "+keys=")!
let fontKey = fragment.range(of: "+font=")
let baseFrame = fragment[baseFrameKey.upperBound..<keysKey.lowerBound]
let keys = fragment[keysKey.upperBound..<(fontKey?.lowerBound ?? fragment.endIndex)]
let useSpecialFont = fontKey != nil
let frameComponents = baseFrame.components(separatedBy: ",")
let x = CGFloat(Float(frameComponents[0])!)
let y = CGFloat(Float(frameComponents[1])!)
let w = CGFloat(Float(frameComponents[2])!)
let h = CGFloat(Float(frameComponents[3])!)
let frame = KeymanWebViewController.keyFrame(x: x, y: y, w: w, h: h)
let keyArray = keys.components(separatedBy: ";")
var subkeyIDs: [String] = []
var subkeyTexts: [String] = []
for key in keyArray {
let values = key.components(separatedBy: ":")
switch values.count {
case 1:
let id = values[0]
subkeyIDs.append(id)
// id is in the form layer-keyID. We only process keyIDs with prefix U_.
if let index = id.range(of: "-U_", options: .backwards)?.upperBound,
let codepoint = UInt32(id[index...], radix: 16),
let scalar = Unicode.Scalar(codepoint) {
subkeyTexts.append(String(Character(scalar)))
} else {
subkeyTexts.append("")
}
case 2:
subkeyIDs.append(values[0])
subkeyTexts.append(values[1].stringFromUTF16CodeUnits() ?? "")
default:
log.warning("Unexpected subkey key: \(key)")
}
}
showSubkeys(self,
keyFrame: frame,
subkeyIDs: subkeyIDs,
subkeyTexts: subkeyTexts,
useSpecialFont: useSpecialFont)
delegate?.showSubkeys(self,
keyFrame: frame,
subkeyIDs: subkeyIDs,
subkeyTexts: subkeyTexts,
useSpecialFont: useSpecialFont)
self.touchHoldBegan()
} else if fragment.hasPrefix("#menuKeyDown-") {
perform(#selector(self.menuKeyHeld), with: self, afterDelay: 0.5)
menuKeyDown(self)
delegate?.menuKeyDown(self)
} else if fragment.hasPrefix("#menuKeyUp-") {
// Blocks summoning the globe-key menu for quick taps. (Hence the 0.5 delay.)
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(self.menuKeyHeld), object: self)
menuKeyUp(self)
delegate?.menuKeyUp(self)
} else if fragment.hasPrefix("#hideKeyboard-") {
hideKeyboard(self)
Manager.shared.hideKeyboard()
} else if fragment.hasPrefix("ios-log:#iOS#") {
let message = fragment.dropFirst(13)
// This may need filtering for proper use with Sentry?
// Then again, if KMW is logging it... we already have to worry
// about it showing up in Web-oriented Sentry logs.
SentryManager.breadcrumbAndLog("KMW Log: \(message)", category: "engine")
} else if fragment.hasPrefix("#beep-") {
beep(self)
delegate?.beep(self)
} else if fragment.hasPrefix("#suggestPopup"){
let cmdKey = fragment.range(of: "+cmd=")!
let cmdStr = fragment[cmdKey.upperBound..<fragment.endIndex]
let cmdData = cmdStr.data(using: .utf16)
let decoder = JSONDecoder()
do {
let cmd = try decoder.decode(SuggestionPopup.self, from: cmdData!)
log.verbose("Longpress detected on suggestion: \"\(cmd.suggestion.displayAs)\".")
} catch {
SentryManager.captureAndLog(error, message: "Unexpected JSON parse error: \(error).")
}
// Will need processing upon extraction from the resulting object.
// let frameComponents = baseFrame.components(separatedBy: ",")
// let x = CGFloat(Float(frameComponents[0])!)
// let y = CGFloat(Float(frameComponents[1])!)
// let w = CGFloat(Float(frameComponents[2])!)
// let h = CGFloat(Float(frameComponents[3])!)
// let frame = KeymanWebViewController.keyFrame(x: x, y: y, w: w, h: h)
} else {
SentryManager.captureAndLog("Unexpected KMW event: \(fragment)")
}
}
private static func keyFrame(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat) -> CGRect {
return CGRect(x: x - (w/2), y: y, width: w, height: h)
}
public func beep(_ keymanWeb: KeymanWebViewController) {
let vibrationSupport = Manager.shared.vibrationSupportLevel
let kSystemSoundID_MediumVibrate: SystemSoundID = 1520
if vibrationSupport == .none {
// TODO: Find something we can do visually and/or audibly to provide feedback.
} else if vibrationSupport == .basic {
// The publicly-suggested kSystemSoundID_Vibrate lasts for 0.4 seconds.
// Better than nothing, though it's easily too long for a proper beep.
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
} else if vibrationSupport == .basic_plus {
// Code 1519 is near-undocumented, but should result in a 'weaker'/shorter vibration.
// Corresponds directly to UIImpactFeedbackGenerator below, but on select phones that
// don't support that part of the API.
//
// Ref: https://stackoverflow.com/questions/10570553/how-to-set-iphone-vibrate-length/44495798#44495798
// Not usable by older iPhone models.
AudioServicesPlaySystemSound(kSystemSoundID_MediumVibrate)
} else { // if vibrationSupport == .taptic
// Available with iPhone 7 and beyond, we can now produce nicely customized haptic feedback.
// We use this style b/c it's short, and in essence it is a minor UI element collision -
// a single key with blocked (erroneous) output.
// Oddly, is a closer match to SystemSoundID 1520 than 1521.
let vibrator = UIImpactFeedbackGenerator(style: UIImpactFeedbackGenerator.FeedbackStyle.heavy)
vibrator.impactOccurred()
}
}
}
// MARK: - WKNavigationDelegate
extension KeymanWebViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
guard let url = webView.url else {
return
}
guard url.lastPathComponent == Resources.kmwFilename && (url.fragment?.isEmpty ?? true) else {
return
}
keyboardLoaded(self)
delegate?.keyboardLoaded(self)
}
}
// MARK: - KeymanWebDelegate
extension KeymanWebViewController: KeymanWebDelegate {
func keyboardLoaded(_ keymanWeb: KeymanWebViewController) {
delegate?.keyboardLoaded(keymanWeb)
isLoading = false
SentryManager.breadcrumbAndLog("Loaded keyboard.", sentryLevel: .debug, logLevel: .info)
self.setSentryState()
resizeKeyboard()
// There may have been attempts to set these values before the keyboard loaded!
self.setText(self.currentText)
if let cursorRange = self.currentCursorRange {
self.setCursorRange(cursorRange)
}
var newKb = Manager.shared.currentKeyboard
if !shouldReload { // otherwise, we automatically reload anyway.
let userData = Manager.shared.isSystemKeyboard ? UserDefaults.standard : Storage.active.userDefaults
if newKb == nil {
if let id = userData.currentKeyboardID {
if let kb = Storage.active.userDefaults.userKeyboard(withFullID: id) {
newKb = kb
}
}
// Failsafe in case the previous lookup fails - at least load A keyboard.
if newKb == nil, let userKbs = Storage.active.userDefaults.userKeyboards, !userKbs.isEmpty {
newKb = userKbs[0]
}
}
SentryManager.breadcrumbAndLog("Setting initial keyboard.", category: "engine")
// Compare against resetKeyboard & Manager.setKeyboard;
// setting this to `nil` allows us to force keyboard reloads when needed.
Manager.shared.currentKeyboardID = nil
if !Manager.shared.setKeyboard(newKb!) {
// The keyboard couldn't load for... whatever reason.
// Default-keyboard fallback time. We _know_ that one should work.
do {
var defaultWasMissing = false
// Ensure the default keyboard is installed in this case.
if !(Storage.active.userDefaults.userKeyboards?.contains(where: {$0.fullID == Defaults.keyboardID }) ?? true) ||
!FileManager.default.fileExists(atPath: Storage.active.keyboardURL(for: Defaults.keyboard).path) {
defaultWasMissing = true
try Storage.active.installDefaultKeyboard(from: Resources.bundle)
}
// Ensures we don't infinitely try to reload the keyboard.
if(defaultWasMissing || newKb!.fullID != Defaults.keyboard.fullID) {
// Be sure to force a reset again.
Manager.shared.currentKeyboardID = nil
_ = Manager.shared.setKeyboard(Defaults.keyboard)
}
} catch {
SentryManager.captureAndLog("Could not load default keyboard as a fallback for keyboard loading failure", sentryLevel: .fatal)
}
newKb = Defaults.keyboard
}
}
// in case `shouldReload == true`. Is set otherwise above.
if(newKb == nil) {
newKb = Defaults.keyboard
}
updateSpacebarText()
updateShowBannerSetting()
setBannerImage(to: bannerImgPath)
// Reset the keyboard's size.
keyboardSize = kbSize
fixLayout()
// Will trigger Manager's `keyboardLoaded` method.
NotificationCenter.default.post(name: Notifications.keyboardLoaded, object: self, value: newKb!)
// "Should reload" the keyboard assets. (Not the host page.)
if shouldReload {
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(self.resetKeyboard), object: nil)
perform(#selector(self.resetKeyboard), with: nil, afterDelay: 0.25)
shouldReload = false
}
}
func updateShowBannerSetting() {
let userData = Storage.active.userDefaults
let alwaysShow = userData.bool(forKey: Key.optShouldShowBanner)
if !Manager.shared.isSystemKeyboard {
showBanner(false)
} else {
showBanner(alwaysShow)
}
}
func insertText(_ view: KeymanWebViewController, numCharsToDelete: Int, newText: String) {
dismissHelpBubble()
Manager.shared.isKeymanHelpOn = false
}
func showKeyPreview(_ view: KeymanWebViewController, keyFrame: CGRect, preview: String) {
if UIDevice.current.userInterfaceIdiom == .pad || isSubKeysMenuVisible {
return
}
dismissKeyPreview()
clearSubKeyArrays()
keyPreviewView = KeyPreviewView(frame: keyFrame)
keyPreviewView!.setLabelText(preview)
let oskFontName = Manager.shared.oskFontNameForKeyboard(withFullID: Manager.shared.currentKeyboardID!)
?? Manager.shared.fontNameForKeyboard(withFullID: Manager.shared.currentKeyboardID!)
keyPreviewView!.setLabelFont(oskFontName)
self.view.addSubview(keyPreviewView!)
}
func dismissKeyPreview(_ view: KeymanWebViewController) {
if UIDevice.current.userInterfaceIdiom == .pad || keyPreviewView == nil {
return
}
let dismissKeyPreview = #selector(self.dismissKeyPreview as () -> Void)
NSObject.cancelPreviousPerformRequests(withTarget: self, selector: dismissKeyPreview, object: nil)
perform(dismissKeyPreview, with: nil, afterDelay: 0.1)
clearSubKeyArrays()
}
func showSubkeys(_ view: KeymanWebViewController,
keyFrame: CGRect,
subkeyIDs: [String],
subkeyTexts: [String],
useSpecialFont: Bool) {
dismissHelpBubble()
Manager.shared.isKeymanHelpOn = false
dismissSubKeys()
dismissKeyboardMenu()
subKeyAnchor = keyFrame
subKeyIDs = subkeyIDs
subKeyTexts = subkeyTexts
self.useSpecialFont = useSpecialFont
}
func menuKeyUp(_ view: KeymanWebViewController) {
dismissHelpBubble()
Manager.shared.isKeymanHelpOn = false
if Util.isSystemKeyboard {
let userData = UserDefaults.standard
userData.set(true, forKey: Key.keyboardPickerDisplayed)
userData.synchronize()
}
}
func hideKeyboard(_ view: KeymanWebViewController) {
dismissHelpBubble()
dismissSubKeys()
dismissKeyboardMenu()
}
}
// MARK: - UIGestureRecognizerDelegate
extension KeymanWebViewController: UIGestureRecognizerDelegate {
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
// An adaptation of holdAction, just for direct taps.
@objc func tapAction(_ sender: UITapGestureRecognizer) {
switch sender.state {
case .ended:
// Touch Ended
guard let subKeysView = subKeysView else {
return
}
let touchPoint = sender.location(in: subKeysView.containerView)
var buttonClicked = false
for button in subKeys {
if button.frame.contains(touchPoint) {
button.isEnabled = true
button.isHighlighted = true
button.backgroundColor = subKeyColorHighlighted
button.sendActions(for: .touchUpInside)
buttonClicked = true
} else {
button.isHighlighted = false
button.isEnabled = false
button.backgroundColor = subKeyColor
}
}
if !buttonClicked {
clearSubKeyArrays()
}
default:
return
}
}
@objc func holdAction(_ sender: UILongPressGestureRecognizer) {
switch sender.state {
case .ended:
// Touch Ended
if let subKeysView = subKeysView {
subKeysView.removeFromSuperview()
subKeysView.subviews.forEach { $0.removeFromSuperview() }
self.subKeysView = nil
setPopupVisible(false)
}
var buttonClicked = false
for button in subKeys where button.isHighlighted {
button.isHighlighted = false
button.backgroundColor = subKeyColor
button.isEnabled = false
button.sendActions(for: .touchUpInside)
buttonClicked = true
break
}
if !buttonClicked {
clearSubKeyArrays()
}
case .began:
// // Touch & Hold Began
// let touchPoint = sender.location(in: sender.view)
// // Check if touch was for language menu button
// languageMenuPosition { keyFrame in
// if keyFrame.contains(touchPoint) {
// self.delegate?.menuKeyHeld(self)
// return
// }
// self.touchHoldBegan()
// }
return
default:
// Hold & Move
guard let subKeysView = subKeysView else {
return
}
let touchPoint = sender.location(in: subKeysView.containerView)
for button in subKeys {
if button.frame.contains(touchPoint) {
button.isEnabled = true
button.isHighlighted = true
button.backgroundColor = subKeyColorHighlighted
} else {
button.isHighlighted = false
button.isEnabled = false
button.backgroundColor = subKeyColor
}
}
}
}
private func touchHoldBegan() {
// Is also called for banner longpresses. Will need a way to properly differentiate.
let isPad = UIDevice.current.userInterfaceIdiom == .pad
let fontSize = isPad ? UIFont.buttonFontSize * 2 : UIFont.buttonFontSize
let oskFontName = Manager.shared.oskFontNameForKeyboard(withFullID: Manager.shared.currentKeyboardID!)
?? Manager.shared.fontNameForKeyboard(withFullID: Manager.shared.currentKeyboardID!)
if subKeyIDs.isEmpty {
subKeys = []
return
}
subKeys = subKeyTexts.enumerated().map { i, subKeyText in
let button = UIButton(type: .custom)
button.tag = i
button.backgroundColor = subKeyColor
button.setRoundedBorder(withRadius: 4.0, borderWidth: 1.0, color: .gray)
button.setTitleColor(Colors.keyText, for: .disabled)
button.setTitleColor(Colors.keyText, for: .highlighted)
button.setTitleColor(Colors.keyText, for: .normal)
if let oskFontName = oskFontName {
button.titleLabel?.font = UIFont(name: oskFontName, size: fontSize)
} else {
button.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
}
if useSpecialFont{
if FontManager.shared.registerFont(at: Storage.active.specialOSKFontURL),
let fontName = FontManager.shared.fontName(at: Storage.active.specialOSKFontURL) {
button.titleLabel?.font = UIFont(name: fontName, size: fontSize)
}
button.setTitleColor(.gray, for: .disabled)
}
button.addTarget(self, action: #selector(subKeyButtonClick), for: .touchUpInside)
// Detect the text width for subkeys. The 'as Any' silences an inappropriate warning from Swift.
let textSize = subKeyText.size(withAttributes: [NSAttributedString.Key.font: button.titleLabel?.font! as Any])
var displayText = subKeyText
if textSize.width <= 0 && subKeyText.count > 0 {
// It's probably a diacritic in need of a combining character!
// Also check if the language is RTL!
if Manager.shared.currentKeyboard?.isRTL ?? false {
displayText = "\u{200f}\u{25cc}" + subKeyText
} else {
displayText = "\u{25cc}" + subKeyText
}
}
button.setTitle(displayText, for: .normal)
button.tintColor = Colors.popupKeyTint
button.isEnabled = false
return button
}
dismissKeyPreview()
subKeysView = SubKeysView(keyFrame: subKeyAnchor, subKeys: subKeys)
view.addSubview(subKeysView!)
let tap = UITapGestureRecognizer(target: self, action: #selector(self.tapAction))
subKeysView!.addGestureRecognizer(tap)
let hold = UILongPressGestureRecognizer(target: self, action: #selector(self.holdAction))
hold.minimumPressDuration = 0.01
hold.delegate = self
subKeysView!.addGestureRecognizer(hold)
setPopupVisible(true)
}
@objc func menuKeyHeld(_ keymanWeb: KeymanWebViewController) {
self.delegate?.menuKeyHeld(self)
}
@objc func subKeyButtonClick(_ sender: UIButton) {
let keyIndex = sender.tag
if keyIndex < subKeyIDs.count && keyIndex < subKeyTexts.count {
let subKeyID = subKeyIDs[keyIndex]
let subKeyText = subKeyTexts[keyIndex]
executePopupKey(id: subKeyID, text: subKeyText)
}
subKeys.removeAll()
subKeyIDs.removeAll()
subKeyTexts.removeAll()
}
}
// MARK: - Manage views
extension KeymanWebViewController {
// MARK: - Sizing
public var keyboardHeight: CGFloat {
return keyboardSize.height
}
func constraintTargetHeight(isPortrait: Bool) -> CGFloat {
return KeyboardScaleMap.getDeviceDefaultKeyboardScale(forPortrait: isPortrait)?.keyboardHeight ?? 216 // default for ancient devices
}
var keyboardWidth: CGFloat {
return keyboardSize.width
}
func initKeyboardSize() {
var width: CGFloat
var height: CGFloat
width = UIScreen.main.bounds.width