-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
fbf5b01
commit ed1f8c1
Showing
7 changed files
with
206 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
PlantUMLKeyboard/Sources/PlantUMLKeyboard/CustomKeyboardObject.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import Combine | ||
import SwiftUI | ||
|
||
|
||
public class CustomKeyboardObject : ObservableObject { | ||
|
||
@Published public var showKeyboard = false | ||
|
||
private var controller:UIHostingController<PlantUMLKeyboardView>? | ||
|
||
private var keyboardRect:CGRect = .zero | ||
|
||
private var cancellable:AnyCancellable? | ||
|
||
public init() { | ||
|
||
NotificationCenter.default.addObserver( | ||
|
||
forName: UIResponder.keyboardDidShowNotification, object: nil, queue: .main) { [weak self] notification in | ||
|
||
print( "keyboardDidShowNotification: \(notification)" ) | ||
|
||
if let keyboardFrameEndUser = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue) { | ||
|
||
print( "keyboardFrameEndUser pos=\(keyboardFrameEndUser)") | ||
|
||
self?.keyboardRect = keyboardFrameEndUser.cgRectValue | ||
} | ||
|
||
} | ||
|
||
NotificationCenter.default.addObserver( | ||
|
||
forName: UIResponder.keyboardDidHideNotification, object: nil, queue: .main) {_ in | ||
|
||
print( "keyboardDidHideNotification" ) | ||
|
||
} | ||
|
||
cancellable = _showKeyboard.projectedValue.sink { value in | ||
print( "showKeyboard: \(value)" ) | ||
|
||
if( value ) { | ||
self.show() | ||
} | ||
else { | ||
self.hide() | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
// | ||
//[Cannot convert value of type 'Published<Bool>.Publisher' to expected argument type 'Binding<Bool>'](https://stackoverflow.com/a/63282875/521197) | ||
// | ||
private var showKeyboardBinding:Binding<Bool> { | ||
Binding( | ||
get: { [weak self] in | ||
(self?.showKeyboard ?? false) | ||
}, | ||
set: { [weak self] in | ||
self?.showKeyboard = $0 | ||
} | ||
) | ||
} | ||
|
||
private func show() { | ||
|
||
guard !self.showKeyboard && self.keyboardRect != .zero, let keyboardWindow = getKeyboardWindow() else { | ||
return | ||
} | ||
|
||
print( "keyboardRect: \(keyboardRect)") | ||
|
||
let controller = UIHostingController( rootView: PlantUMLKeyboardView( show: showKeyboardBinding) ) | ||
self.controller = controller | ||
controller.view.frame = CGRect( origin: keyboardRect.origin, size: CGSize( width: keyboardRect.width, height: 500.0) ) | ||
keyboardWindow.addSubview( controller.view ) | ||
|
||
|
||
|
||
} | ||
|
||
private func hide() { | ||
keyboardRect = .zero | ||
controller?.view.removeFromSuperview() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
PlantUMLKeyboard/Sources/PlantUMLKeyboard/UIImage+Tiles.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// UIImage+Tiles.swift | ||
// | ||
// | ||
// Created by Bartolomeo Sorrentino on 13/09/22. | ||
// | ||
|
||
import UIKit | ||
|
||
// [StackOverflow](https://stackoverflow.com/a/73628496/521197) | ||
extension UIImage { | ||
|
||
func extractTiles(with tileSize: CGSize) -> [UIImage?] { | ||
|
||
let hCount = Int(self.size.height / tileSize.height ) | ||
let wCount = Int(self.size.width / tileSize.width ) | ||
|
||
var tiles:[UIImage] = [] | ||
|
||
for i in 0...hCount-1 { | ||
for p in 0...wCount-1 { | ||
let rect = CGRect( | ||
x: CGFloat(p) * tileSize.width, | ||
y: CGFloat(i) * tileSize.height, | ||
width: tileSize.width, | ||
height: tileSize.height) | ||
let temp:CGImage = self.cgImage!.cropping(to: rect)! | ||
tiles.append(UIImage(cgImage: temp)) | ||
} | ||
} | ||
return tiles | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
PlantUMLKeyboard/Sources/PlantUMLKeyboard/UIView+Responder.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Bartolomeo Sorrentino on 13/09/22. | ||
// | ||
|
||
import UIKit | ||
|
||
// [Get the current first responder without using a private API](https://stackoverflow.com/a/1823360/521197) | ||
extension UIView { | ||
|
||
var firstResponder: UIView? { | ||
guard !isFirstResponder else { return self } | ||
|
||
for subview in subviews { | ||
if let firstResponder = subview.firstResponder { | ||
return firstResponder | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
46 changes: 46 additions & 0 deletions
46
PlantUMLKeyboard/Sources/PlantUMLKeyboard/Window+Utils.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Bartolomeo Sorrentino on 13/09/22. | ||
// | ||
|
||
import UIKit | ||
|
||
|
||
public func getKeyboardWindow() -> UIWindow? { | ||
return UIApplication.shared.windows.last | ||
/* | ||
return UIApplication.shared.connectedScenes | ||
// Keep only active scenes, onscreen and visible to the user | ||
//.filter { $0.activationState == .foregroundActive } | ||
// Keep only the first `UIWindowScene` | ||
//.first(where: { $0 is UIWindowScene }) | ||
// Get its associated windows | ||
.compactMap { $0 as? UIWindowScene } | ||
.compactMap { $0.windows } | ||
.flatMap { $0 } | ||
.last | ||
*/ | ||
} | ||
|
||
public func getWindows() -> [UIWindow]? { | ||
|
||
|
||
let scenes = UIApplication.shared.connectedScenes | ||
guard let windowScene = scenes.first as? UIWindowScene else { | ||
return nil | ||
} | ||
return windowScene.windows | ||
|
||
} | ||
|
||
func getFirstTextFieldResponder() -> UITextField? { | ||
|
||
guard let firstWindow = getWindows()?.first, let firstResponder = firstWindow.firstResponder else { | ||
return nil | ||
} | ||
|
||
return firstResponder as? UITextField | ||
} | ||
|