Skip to content

Commit

Permalink
refactor(app): further improve keyboard interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
heapwolf committed May 15, 2024
1 parent 7e1bbe6 commit 8d4bef2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
34 changes: 19 additions & 15 deletions src/app/app.cc
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,13 @@ didFailToContinueUserActivityWithType: (NSString*) userActivityType
CGFloat keyboardHeight = keyboardFrame.size.height;

const auto window = self.app->windowManager.getWindow(0);
window->webview.scrollView.scrollEnabled = YES;

CGRect startFrame = window->webview.frame;
CGRect endFrame = CGRectMake(startFrame.origin.x, startFrame.origin.y, startFrame.size.width, startFrame.size.height + keyboardHeight); // Expanding back

__block CGFloat animationProgress = 0;
NSTimeInterval duration = ([userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]) * 1.25;
NSTimeInterval duration = ([userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]) * 0.9;
NSTimeInterval stepDuration = duration / 120.0;

NSNumber *curve = userInfo[UIKeyboardAnimationCurveUserInfoKey];
Expand Down Expand Up @@ -435,33 +437,35 @@ didFailToContinueUserActivityWithType: (NSString*) userActivityType
CGFloat keyboardHeight = keyboardFrame.size.height;

const auto window = self.app->windowManager.getWindow(0);
window->webview.scrollView.scrollEnabled = NO;

CGRect startFrame = window->webview.frame;
CGRect endFrame = CGRectMake(startFrame.origin.x, startFrame.origin.y, startFrame.size.width, startFrame.size.height - keyboardHeight);

__block CGFloat animationProgress = 0;
NSTimeInterval duration = (([userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]) * 1.6);
NSTimeInterval duration = (([userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]) * 1.9);
NSTimeInterval stepDuration = duration / 120.0;

NSNumber *curve = userInfo[UIKeyboardAnimationCurveUserInfoKey];
UIViewAnimationOptions animationCurve = [curve unsignedIntegerValue] << 16;

[NSTimer scheduledTimerWithTimeInterval:stepDuration repeats:YES block:^(NSTimer * _Nonnull timer) {
animationProgress += stepDuration / duration;
animationProgress += stepDuration / duration;

if (animationProgress >= 1.0) {
animationProgress = 1.0;
window->webview.frame = endFrame;
[timer invalidate];
} else {
CGFloat interpolatedProgress = 1 - pow(1 - animationProgress, 3);
if (animationProgress >= 1.0) {
animationProgress = 1.0;
window->webview.frame = endFrame;
[timer invalidate];
} else {
CGFloat interpolatedProgress = 1 - pow(1 - animationProgress, 3);

CGRect newFrame = CGRectMake(startFrame.origin.x + (endFrame.origin.x - startFrame.origin.x) * interpolatedProgress,
startFrame.origin.y + (endFrame.origin.y - startFrame.origin.y) * interpolatedProgress,
startFrame.size.width + (endFrame.size.width - startFrame.size.width) * interpolatedProgress,
startFrame.size.height + (endFrame.size.height - startFrame.size.height) * interpolatedProgress);
CGRect newFrame = CGRectMake(startFrame.origin.x + (endFrame.origin.x - startFrame.origin.x) * interpolatedProgress,
startFrame.origin.y + (endFrame.origin.y - startFrame.origin.y) * interpolatedProgress,
startFrame.size.width + (endFrame.size.width - startFrame.size.width) * interpolatedProgress,
startFrame.size.height + (endFrame.size.height - startFrame.size.height) * interpolatedProgress);

window->webview.frame = newFrame;
}
window->webview.frame = newFrame;
}
}];

for (const auto window : self.app->windowManager.windows) {
Expand Down
17 changes: 16 additions & 1 deletion src/window/apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1299,9 +1299,24 @@ - (void) webView: (WKWebView*) webview
this->window = [SSCWindow.alloc initWithFrame: frame];
this->viewController = [SSCWebViewController new];
this->viewController.webview = this->webview;
[this->viewController.view addSubview: this->webview];

UIUserInterfaceStyle interfaceStyle = this->window.traitCollection.userInterfaceStyle;

if (interfaceStyle == UIUserInterfaceStyleDark && opts.backgroundColorDark.size() > 0) {
this->setBackgroundColor(opts.backgroundColorDark);
} else if (opts.backgroundColorLight.size() > 0) {
this->setBackgroundColor(opts.backgroundColorLight);
} else {
this->viewController.webview.backgroundColor = [UIColor systemBackgroundColor];
this->window.backgroundColor = [UIColor systemBackgroundColor];
this->viewController.webview.opaque = NO;
}

[this->viewController.view addSubview:this->webview];

this->window.rootViewController = this->viewController;
this->window.rootViewController.view.frame = frame;

#endif

if (opts.title.size() > 0) {
Expand Down
1 change: 1 addition & 0 deletions src/window/webview.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace SSC {
@interface SSCBridgedWebView :
#if SSC_PLATFORM_IOS
WKWebView<WKUIDelegate>
@property (strong, nonatomic) NSLayoutConstraint *keyboardHeightConstraint;
#else
WKWebView<
WKUIDelegate,
Expand Down

0 comments on commit 8d4bef2

Please sign in to comment.