Skip to content

Commit

Permalink
TerminalView processes JS events to delegate
Browse files Browse the repository at this point in the history
Made some adjustments as the JS events are responsibility of the
TerminalView instead of the Controller.
  • Loading branch information
Carlos Cabanero committed Sep 9, 2016
1 parent e0478b3 commit 40047a0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
28 changes: 8 additions & 20 deletions Blink/TermController.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

static NSDictionary *bkModifierMaps = nil;

@interface TermController () <WKScriptMessageHandler, TerminalDelegate, SessionDelegate>
@interface TermController () <TerminalDelegate, SessionDelegate>
@end

@implementation TermController {
Expand Down Expand Up @@ -72,9 +72,7 @@ - (void)write:(NSString *)input
- (void)loadView
{
[super loadView];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
[theConfiguration.userContentController addScriptMessageHandler:self name:@"interOp"];
_terminal = [[TerminalView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
_terminal = [[TerminalView alloc] initWithFrame:self.view.frame];
_terminal.delegate = self;

self.view = _terminal;
Expand Down Expand Up @@ -223,22 +221,6 @@ - (void)startSession
[_session executeWithArgs:@""];
}

// Since ViewController is a WKScriptMessageHandler, as declared in the ViewController interface, it must implement the userContentController:didReceiveScriptMessage method. This is the method that is triggered each time 'interOp' is sent a message from the JavaScript code.
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
NSDictionary *sentData = (NSDictionary *)message.body;
NSString *operation = sentData[@"op"];
NSDictionary *data = sentData[@"data"];

if ([operation isEqualToString:@"sigwinch"]) {
[self updateTermRows:data[@"rows"] Cols:data[@"columns"]];
} else if ([operation isEqualToString:@"terminalready"]) {
[self setAppearanceFromSettings];
[self startSession];
}
}

- (void)setRawMode:(BOOL)raw
{
[_terminal setRawMode:raw];
Expand All @@ -256,6 +238,12 @@ - (void)updateTermRows:(NSNumber *)rows Cols:(NSNumber *)cols
[_session sigwinch];
}

- (void)terminalIsReady
{
[self setAppearanceFromSettings];
[self startSession];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
Expand Down
8 changes: 6 additions & 2 deletions Blink/TermView.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ extern NSString * const TermViewCursorFuncSeq;
extern NSString * const TermViewFFuncSeq;


@protocol TerminalDelegate
@protocol TerminalDelegate <NSObject>

- (void)write:(NSString *)input;

@optional
- (void)terminalIsReady;
- (void)updateTermRows:(NSNumber *)rows Cols:(NSNumber *)cols;

@end

@interface TerminalView : UIView
Expand All @@ -50,7 +54,7 @@ extern NSString * const TermViewFFuncSeq;
@property (weak) id<TerminalDelegate> delegate;
@property (nonatomic, readonly, weak) NSString *title;

- (id)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration;
- (id)initWithFrame:(CGRect)frame;
- (void)setScrollEnabled:(BOOL)scroll;
- (void)setRawMode:(BOOL)raw;
- (BOOL)rawMode;
Expand Down
27 changes: 24 additions & 3 deletions Blink/TermView.m
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ + (NSString *)FKEY:(NSInteger)number
}
@end

@interface TerminalView () <UIKeyInput, UIGestureRecognizerDelegate>
@interface TerminalView () <UIKeyInput, UIGestureRecognizerDelegate, WKScriptMessageHandler>
@end

@implementation TerminalView {
Expand All @@ -212,15 +212,17 @@ @implementation TerminalView {
NSString *_specialFKeysRow;
}

- (id)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];

if (self) {
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
[configuration.userContentController addScriptMessageHandler:self name:@"interOp"];

_webView = [[WKWebView alloc] initWithFrame:self.frame configuration:configuration];
[self resetDefaultControlKeys];

_webView.opaque = NO;
[self addSubview:_webView];

UITapGestureRecognizer *tapBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(activeControl:)];
Expand Down Expand Up @@ -304,6 +306,25 @@ - (NSString *)title
return _webView.title;
}

// Since TermView is a WKScriptMessageHandler, it must implement the userContentController:didReceiveScriptMessage method. This is the method that is triggered each time 'interOp' is sent a message from the JavaScript code.
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message
{
NSDictionary *sentData = (NSDictionary *)message.body;
NSString *operation = sentData[@"op"];
NSDictionary *data = sentData[@"data"];

if ([operation isEqualToString:@"sigwinch"]) {
if ([self.delegate respondsToSelector:@selector(updateTermRows:Cols:)]) {
[self.delegate updateTermRows:data[@"rows"] Cols:data[@"columns"]];
}
} else if ([operation isEqualToString:@"terminalready"]) {
if ([self.delegate respondsToSelector:@selector(terminalIsReady)]) {
[self.delegate terminalIsReady];
}
}
}

#pragma mark On-Screen keyboard - UIKeyInput
- (UIKeyboardAppearance)keyboardAppearance
{
Expand Down

0 comments on commit 40047a0

Please sign in to comment.