Skip to content

Commit

Permalink
Improved layout
Browse files Browse the repository at this point in the history
  • Loading branch information
yury committed Aug 16, 2023
1 parent f3bc0d8 commit 9e5e024
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 174 deletions.
4 changes: 2 additions & 2 deletions Blink.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -4868,7 +4868,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 809;
CURRENT_PROJECT_VERSION = 819;
DEAD_CODE_STRIPPING = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEFINES_MODULE = YES;
Expand Down Expand Up @@ -4918,7 +4918,7 @@
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 809;
CURRENT_PROJECT_VERSION = 819;
DEAD_CODE_STRIPPING = NO;
DEFINES_MODULE = YES;
ENABLE_BITCODE = NO;
Expand Down
76 changes: 8 additions & 68 deletions Blink/KBTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ class KBObserver: NSObject, UIInteraction {
let kbEndFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
kbEndFrame != self.kbScreenFrame
else {
// print("!!! will show", notification.userInfo)
return
}

self.kbScreenFrame = kbEndFrame
self.view?.setNeedsLayout()

NotificationCenter.default.post(name: NSNotification.Name(rawValue: LayoutManagerBottomInsetDidUpdate), object: nil)
}

@objc private func _keyboardWillHide(notification: Notification) {
Expand All @@ -105,21 +106,25 @@ class KBObserver: NSObject, UIInteraction {
let kbEndFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
kbEndFrame != self.kbScreenFrame
else {
// print("!!! will hide", notification.userInfo)
return
}

self.kbScreenFrame = kbEndFrame
self.view?.setNeedsLayout()
NotificationCenter.default.post(name: NSNotification.Name(rawValue: LayoutManagerBottomInsetDidUpdate), object: nil)
}

@objc private func _keyboardWillChangeFrame(notification: Notification) {
return
guard
let screen = notification.object as? UIScreen,
let view = self.view,
screen.isEqual(view.window?.screen),
let kbEndFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
kbEndFrame != self.kbScreenFrame
else {
// print("!!! change frame", notification.userInfo)
return
}

Expand Down Expand Up @@ -292,86 +297,21 @@ class KBTracker: NSObject {
}

@objc private func _keyboardDidChangeFrame(_ notification: Notification) {
// debugPrint("_keyboardDidChangeFrame", notification.userInfo);
// debugPrint("_keyboardDidChangeFrame");

guard
let userInfo = notification.userInfo,
let kbFrameEnd = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
let kbFrameBegin = userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? CGRect //,
// let kbFrameCenterBegin = userInfo["UIKeyboardCenterBeginUserInfoKey"] as? CGRect,
// let isLocal = userInfo[UIResponder.keyboardIsLocalUserInfoKey] as? Bool
let _ = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect,
let _ = userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as? CGRect //,
else {
return
}



let screenMaxY = UIScreen.main.bounds.size.height

var bottomInset: CGFloat = 0
let idiom = UIDevice.current.userInterfaceIdiom
if !isHardwareKB, let fr = self.input?.spaceController?.trackingKBFrame {
bottomInset = screenMaxY - CGRectGetMinY(fr)


if idiom == UIUserInterfaceIdiom.pad {


if kbFrameBegin.isEmpty {
// kbTraits.isFloatingKB = true
// kbDevice = .in6_5
// kbTraits.isPortrait = true
// print("floating_frame", fr, kbFrameEnd, kbFrameBegin)
bottomInset = 0
}
}
}



//
// // kb frame in screen coordinates
// if (kbFrameEnd.origin.x == 0 && kbFrameEnd.width == screenWidth && kbFrameEnd.height > 0) {
// if kbMaxY >= screenMaxY {
// bottomInset = screenMaxY - kbMinY
// }
//
// if (bottomInset < 30) {
// bottomInset = 0
// }
// }
//
// if isLocal && idiom == .pad {
// let isFloating = kbFrameEnd.origin.y > 0 && kbFrameEnd.origin.x > 0 || kbFrameEnd == .zero
//
// if !kbTraits.isFloatingKB && isFloating {
// kbDevice = .in6_5
// kbTraits.isPortrait = true
// } else if kbTraits.isFloatingKB && !isFloating && !kbTraits.isHKBAttached {
// kbDevice = .detect()
// }
// kbTraits.isFloatingKB = isFloating
// }

// if bottomInset == 0 && _kbTraits.isFloatingKB,
// let safeInsets = superview?.safeAreaInsets {
// bottomInset = _kbView.intrinsicContentSize.height + safeInsets.bottom
// }

if isHardwareKB {
if kbTraits.isFloatingKB {
kbDevice = .detect()
}
kbTraits.isFloatingKB = false
bottomInset = 0

if !hideSmartKeysWithHKB && idiom == .phone,
let safeInsets = input?.kbView.superview?.safeAreaInsets {
bottomInset = (input?.kbView.intrinsicContentSize.height ?? 0) + safeInsets.bottom;
}
}
LayoutManager.updateMainWindowKBBottomInset(bottomInset);
}

@objc private func _keyboardWillShow(_ notification: Notification) {
Expand Down
4 changes: 2 additions & 2 deletions Blink/LayoutManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ extern NSString *LayoutManagerBottomInsetDidUpdate;
+ (BKLayoutMode) deviceDefaultLayoutMode;
+ (UIEdgeInsets) buildSafeInsetsForController:(UIViewController *)ctrl andMode:(BKLayoutMode) mode;
+ (NSString *) layoutModeToString:(BKLayoutMode)mode;
+ (CGFloat) mainWindowKBBottomInset;
+ (void) updateMainWindowKBBottomInset:(CGFloat) bottomInset;
//+ (CGFloat) mainWindowKBBottomInset;
//+ (void) updateMainWindowKBBottomInset:(CGFloat) bottomInset;

@end

Expand Down
39 changes: 12 additions & 27 deletions Blink/LayoutManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,9 @@
#import <Blink-Swift.h>


CGFloat __mainWindowKBBottomInset = 0;

NSString * LayoutManagerBottomInsetDidUpdate = @"LayoutManagerBottomInsetDidUpdate";
NSTimer *__debounceTimer = nil;

@implementation LayoutManager {

}

+ (CGFloat) mainWindowKBBottomInset {
return __mainWindowKBBottomInset;
}

+ (void) updateMainWindowKBBottomInset:(CGFloat) bottomInset {
if (__mainWindowKBBottomInset == bottomInset) {
return;
}

__mainWindowKBBottomInset = bottomInset;
[__debounceTimer invalidate];

__debounceTimer = [NSTimer scheduledTimerWithTimeInterval:0.6 repeats:NO block:^(NSTimer * _Nonnull timer) {
[NSNotificationCenter.defaultCenter postNotificationName:LayoutManagerBottomInsetDidUpdate object:nil];
}];
}
@implementation LayoutManager

+ (BKLayoutMode) deviceDefaultLayoutMode {
DeviceInfo *device = [DeviceInfo shared];
Expand Down Expand Up @@ -92,10 +70,19 @@ + (UIEdgeInsets) buildSafeInsetsForController:(UIViewController *)ctrl andMode:(
return window.safeAreaInsets;
}

SpaceController *spaceCtrl = nil;
UIViewController *parent = ctrl.parentViewController;
while (parent) {
if ([parent isKindOfClass:[SpaceController class]]) {
spaceCtrl = (SpaceController *)parent;
break;
}
parent = parent.parentViewController;
}

UIEdgeInsets deviceMargins = window.safeAreaInsets;// UIEdgeInsetsZero;// ctrl.viewDeviceSafeMargins;

BOOL fullScreen = CGRectEqualToRect(mainScreen.bounds, window.bounds);
CGRect windowRect = [window.coordinateSpace convertRect:window.bounds toCoordinateSpace:mainScreen.coordinateSpace];

UIEdgeInsets result = UIEdgeInsetsZero;

Expand Down Expand Up @@ -156,9 +143,7 @@ + (UIEdgeInsets) buildSafeInsetsForController:(UIViewController *)ctrl andMode:(
}
}

CGFloat relative = __mainWindowKBBottomInset - (CGRectGetMaxY(mainScreen.bounds) - CGRectGetMaxY(windowRect));

result.bottom = MAX(result.bottom, relative);
result.bottom = MAX(result.bottom, [spaceCtrl bottomInset]);

return result;
}
Expand Down
88 changes: 13 additions & 75 deletions Blink/SpaceController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,11 @@ class SpaceController: UIViewController {
private weak var _termViewToFocus: TermView? = nil
var stuckKeyCode: KeyCode? = nil

private var _kbTrackerView = UIView()
private var _kbObserver = KBObserver()
private var _snippetsVC: SnippetsViewController? = nil
private var _blinkMenu: BlinkMenu? = nil
private var _bottomTapAreaView = UIView()


public var trackingKBFrame: CGRect? {
self.view.window?.screen.coordinateSpace.convert(_kbTrackerView.frame, from: self.view.coordinateSpace)
}

var safeFrame: CGRect {
_overlay.frame
}
Expand All @@ -88,20 +82,10 @@ class SpaceController: UIViewController {
return
}

if let bottomInset = _kbObserver.bottomInset {
var insets = UIEdgeInsets.zero
insets.bottom = bottomInset
_overlay.frame = view.bounds.inset(by: insets)
} else {
if window.screen === UIScreen.main {
var insets = UIEdgeInsets.zero
insets.bottom = LayoutManager.mainWindowKBBottomInset()
_overlay.frame = view.bounds.inset(by: insets)
} else {
_overlay.frame = view.bounds
}
}

let bottomInset = _kbObserver.bottomInset ?? 0
var insets = UIEdgeInsets.zero
insets.bottom = bottomInset
_overlay.frame = view.bounds.inset(by: insets)
_snippetsVC?.view.frame = _overlay.frame

if let menu = _blinkMenu {
Expand All @@ -127,7 +111,6 @@ class SpaceController: UIViewController {
}
}
}
self.view.sendSubviewToBack(_kbTrackerView);
let windowBounds = window.bounds
_bottomTapAreaView.frame = CGRect(x: windowBounds.width * 0.5 - 250, y: windowBounds.height - 18, width: 250 * 2, height: 18)
// _bottomTapAreaView.backgroundColor = UIColor.red
Expand Down Expand Up @@ -179,6 +162,10 @@ class SpaceController: UIViewController {
view.setNeedsLayout()
}

@objc public func bottomInset() -> CGFloat {
_kbObserver.bottomInset ?? 0
}

@objc private func _setupAppearance() {
self.view.tintColor = .cyan
switch BLKDefaults.keyboardStyle() {
Expand Down Expand Up @@ -228,16 +215,6 @@ class SpaceController: UIViewController {
_viewportsController.setViewControllers([term], direction: .forward, animated: false)
}

_kbTrackerView.translatesAutoresizingMaskIntoConstraints = false;

self.view.addSubview(_kbTrackerView)

_kbTrackerView.leadingAnchor.constraint(equalTo: self.view.keyboardLayoutGuide.leadingAnchor).isActive = true;
_kbTrackerView.widthAnchor.constraint(equalTo: self.view.keyboardLayoutGuide.widthAnchor).isActive = true;
_kbTrackerView.heightAnchor.constraint(equalTo: self.view.keyboardLayoutGuide.heightAnchor).isActive = true;
_kbTrackerView.bottomAnchor.constraint(equalTo: self.view.keyboardLayoutGuide.bottomAnchor).isActive = true;
// _kbTrackerView.backgroundColor = UIColor.yellow

self.view.addInteraction(_kbObserver)

self.view.addSubview(_bottomTapAreaView)
Expand All @@ -254,33 +231,11 @@ class SpaceController: UIViewController {
}


// public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
// super.viewWillTransition(to: size, with: coordinator)
// if view.window?.isKeyWindow == true {
// DispatchQueue.main.async {
// KBTracker.shared.attach(input: KBTracker.shared.input)
// input?.sync(traits: kbTraits, device: kbDevice, hideSmartKeysWithHKB: hideSmartKeysWithHKB)
// self.currentTerm()?.termDevice.view?.webView?.kbView.reset()
// SmarterTermInput.shared.contentView()?.reloadInputViews()
// }
// }
// }

func showAlert(msg: String) {
let ctrl = UIAlertController(title: "Error", message: msg, preferredStyle: .alert)
ctrl.addAction(UIAlertAction(title: "Ok", style: .default))
self.present(ctrl, animated: true)
}

// override var editingInteractionConfiguration: UIEditingInteractionConfiguration {
// // IOS ISSUE: editingInteractionConfiguration doesn't called anymore in iOS/PadOS 16....
// // Moved attach to focus.
// DispatchQueue.main.async {
// self._attachHUD()
// }
// return .default
// return .none
// }

deinit {
NotificationCenter.default.removeObserver(self)
Expand Down Expand Up @@ -1082,28 +1037,11 @@ extension SpaceController {
DispatchQueue.main.async {
_ = KBTracker.shared.input?.resignFirstResponder();

// if UIApplication.shared.supportsMultipleScenes {
//
// var opened: UISceneSession? = nil
// for session in UIApplication.shared.openSessions {
// if session.configuration.name == "whatsnew" {
// opened = session
// break
// }
// }
// let options = UIWindowScene.ActivationRequestOptions()
// options.preferredPresentationStyle = .prominent
// options.requestingScene = self.view.window?.windowScene;
//
// let activity = NSUserActivity(activityType: "com.blink.whatsnew")
//
// UIApplication.shared.requestSceneSessionActivation(opened, userActivity: activity, options: options)
// } else {
// Reset version when opening.
WhatsNewInfo.setNewVersion()
let root = UIHostingController(rootView: GridView(rowsProvider: RowsViewModel(baseURL: XCConfig.infoPlistWhatsNewURL())))
self.present(root, animated: true, completion: nil)
// }
// Reset version when opening.
WhatsNewInfo.setNewVersion()
let root = UIHostingController(rootView: GridView(rowsProvider: RowsViewModel(baseURL: XCConfig.infoPlistWhatsNewURL())))
self.present(root, animated: true, completion: nil)

}
}

Expand Down

0 comments on commit 9e5e024

Please sign in to comment.