Skip to content

Commit

Permalink
CB-13969 cordova-inappbrowser:iOS&Android now includes a extra, optio…
Browse files Browse the repository at this point in the history
…nal parameter to swap position of navigationbuttons and close/done button
  • Loading branch information
steinaragustli committed Mar 13, 2018
1 parent 313e0ae commit cbe3a42
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
25 changes: 20 additions & 5 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public class InAppBrowser extends CordovaPlugin {
private static final String TOOLBAR_COLOR = "toolbarcolor";
private static final String CLOSE_BUTTON_CAPTION = "closebuttoncaption";
private static final String CLOSE_BUTTON_COLOR = "closebuttoncolor";
private static final String LEFT_TO_RIGHT = "lefttoright";
private static final String HIDE_NAVIGATION = "hidenavigationbuttons";
private static final String NAVIGATION_COLOR = "navigationbuttoncolor";
private static final String HIDE_URL = "hideurlbar";
Expand Down Expand Up @@ -125,6 +126,7 @@ public class InAppBrowser extends CordovaPlugin {
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
private String closeButtonCaption = "";
private String closeButtonColor = "";
private boolean leftToRight = false;
private int toolbarColor = android.graphics.Color.LTGRAY;
private boolean hideNavigationButtons = false;
private String navigationButtonColor = "";
Expand Down Expand Up @@ -609,6 +611,10 @@ public String showWebPage(final String url, HashMap<String, String> features) {
if (closeButtonColorSet != null) {
closeButtonColor = closeButtonColorSet;
}
String leftToRightSet = features.get(LEFT_TO_RIGHT);
if (leftToRightSet != null) {
leftToRight = leftToRightSet.equals("yes") ? true : false;
}
String toolbarColorSet = features.get(TOOLBAR_COLOR);
if (toolbarColorSet != null) {
toolbarColor = android.graphics.Color.parseColor(toolbarColorSet);
Expand Down Expand Up @@ -673,7 +679,8 @@ private View createCloseButton(int id){
}

RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
if (leftToRight) closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
else closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
_close.setLayoutParams(closeLayoutParams);

if (Build.VERSION.SDK_INT >= 16)
Expand Down Expand Up @@ -717,15 +724,22 @@ public void run() {
toolbar.setBackgroundColor(toolbarColor);
toolbar.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44)));
toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2));
toolbar.setHorizontalGravity(Gravity.LEFT);
if (leftToRight) {
toolbar.setHorizontalGravity(Gravity.LEFT);
} else {
toolbar.setHorizontalGravity(Gravity.RIGHT);
}
toolbar.setVerticalGravity(Gravity.TOP);

// Action Button Container layout
RelativeLayout actionButtonContainer = new RelativeLayout(cordova.getActivity());
actionButtonContainer.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams actionButtonLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (leftToRight) actionButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
else actionButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
actionButtonContainer.setLayoutParams(actionButtonLayoutParams);
actionButtonContainer.setHorizontalGravity(Gravity.LEFT);
actionButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL);
actionButtonContainer.setId(Integer.valueOf(1));
actionButtonContainer.setId(leftToRight ? Integer.valueOf(5) : Integer.valueOf(1));

// Back button
ImageButton back = new ImageButton(cordova.getActivity());
Expand Down Expand Up @@ -805,7 +819,8 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {


// Header Close/Done button
View close = createCloseButton(5);
int closeButtonId = leftToRight ? 1 : 5;
View close = createCloseButton(closeButtonId);
toolbar.addView(close);

// Footer
Expand Down
3 changes: 2 additions & 1 deletion src/ios/CDVInAppBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
@property (nonatomic, assign) BOOL toolbar;
@property (nonatomic, copy) NSString* closebuttoncaption;
@property (nonatomic, copy) NSString* closebuttoncolor;
@property (nonatomic, assign) BOOL lefttoright;
@property (nonatomic, copy) NSString* toolbarposition;
@property (nonatomic, copy) NSString* toolbarcolor;
@property (nonatomic, assign) BOOL toolbartranslucent;
Expand Down Expand Up @@ -103,7 +104,7 @@
- (void)navigateTo:(NSURL*)url;
- (void)showLocationBar:(BOOL)show;
- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition;
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString;
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex;

- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent browserOptions: (CDVInAppBrowserOptions*) browserOptions;

Expand Down
21 changes: 15 additions & 6 deletions src/ios/CDVInAppBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
[self.inAppBrowserViewController showLocationBar:browserOptions.location];
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition];
if (browserOptions.closebuttoncaption != nil || browserOptions.closebuttoncolor != nil) {
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor];
int closeButtonIndex = browserOptions.lefttoright ? (browserOptions.hidenavigationbuttons ? 1 : 4) : 0;
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor :closeButtonIndex];
}
// Set Presentation Style
UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
Expand Down Expand Up @@ -650,11 +651,18 @@ - (void)createViews

// Filter out Navigation Buttons if user requests so
if (_browserOptions.hidenavigationbuttons) {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
if (_browserOptions.lefttoright) {
[self.toolbar setItems:@[flexibleSpaceButton, self.closeButton]];
} else {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
}
} else {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
if (_browserOptions.lefttoright) {
[self.toolbar setItems:@[self.backButton, fixedSpaceButton, self.forwardButton, flexibleSpaceButton, self.closeButton]];
} else {
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
}
}
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];

self.view.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.toolbar];
Expand All @@ -667,7 +675,7 @@ - (void) setWebViewFrame : (CGRect) frame {
[self.webView setFrame:frame];
}

- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex
{
// the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically
// but, if you want to set this yourself, knock yourself out (we can't set the title for a system Done button, so we have to create a new one)
Expand All @@ -679,7 +687,7 @@ - (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString
self.closeButton.tintColor = colorString != nil ? [self colorFromHexString:colorString] : [UIColor colorWithRed:60.0 / 255.0 green:136.0 / 255.0 blue:230.0 / 255.0 alpha:1];

NSMutableArray* items = [self.toolbar.items mutableCopy];
[items replaceObjectAtIndex:0 withObject:self.closeButton];
[items replaceObjectAtIndex:buttonIndex withObject:self.closeButton];
[self.toolbar setItems:items];
}

Expand Down Expand Up @@ -1022,6 +1030,7 @@ - (id)init
self.disallowoverscroll = NO;
self.hidenavigationbuttons = NO;
self.closebuttoncolor = nil;
self.lefttoright = false;
self.toolbarcolor = nil;
self.toolbartranslucent = YES;
}
Expand Down

0 comments on commit cbe3a42

Please sign in to comment.