Skip to content

Commit cbe3a42

Browse files
CB-13969 cordova-inappbrowser:iOS&Android now includes a extra, optional parameter to swap position of navigationbuttons and close/done button
1 parent 313e0ae commit cbe3a42

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

src/android/InAppBrowser.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public class InAppBrowser extends CordovaPlugin {
9898
private static final String TOOLBAR_COLOR = "toolbarcolor";
9999
private static final String CLOSE_BUTTON_CAPTION = "closebuttoncaption";
100100
private static final String CLOSE_BUTTON_COLOR = "closebuttoncolor";
101+
private static final String LEFT_TO_RIGHT = "lefttoright";
101102
private static final String HIDE_NAVIGATION = "hidenavigationbuttons";
102103
private static final String NAVIGATION_COLOR = "navigationbuttoncolor";
103104
private static final String HIDE_URL = "hideurlbar";
@@ -125,6 +126,7 @@ public class InAppBrowser extends CordovaPlugin {
125126
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
126127
private String closeButtonCaption = "";
127128
private String closeButtonColor = "";
129+
private boolean leftToRight = false;
128130
private int toolbarColor = android.graphics.Color.LTGRAY;
129131
private boolean hideNavigationButtons = false;
130132
private String navigationButtonColor = "";
@@ -609,6 +611,10 @@ public String showWebPage(final String url, HashMap<String, String> features) {
609611
if (closeButtonColorSet != null) {
610612
closeButtonColor = closeButtonColorSet;
611613
}
614+
String leftToRightSet = features.get(LEFT_TO_RIGHT);
615+
if (leftToRightSet != null) {
616+
leftToRight = leftToRightSet.equals("yes") ? true : false;
617+
}
612618
String toolbarColorSet = features.get(TOOLBAR_COLOR);
613619
if (toolbarColorSet != null) {
614620
toolbarColor = android.graphics.Color.parseColor(toolbarColorSet);
@@ -673,7 +679,8 @@ private View createCloseButton(int id){
673679
}
674680

675681
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
676-
closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
682+
if (leftToRight) closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
683+
else closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
677684
_close.setLayoutParams(closeLayoutParams);
678685

679686
if (Build.VERSION.SDK_INT >= 16)
@@ -717,15 +724,22 @@ public void run() {
717724
toolbar.setBackgroundColor(toolbarColor);
718725
toolbar.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44)));
719726
toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2));
720-
toolbar.setHorizontalGravity(Gravity.LEFT);
727+
if (leftToRight) {
728+
toolbar.setHorizontalGravity(Gravity.LEFT);
729+
} else {
730+
toolbar.setHorizontalGravity(Gravity.RIGHT);
731+
}
721732
toolbar.setVerticalGravity(Gravity.TOP);
722733

723734
// Action Button Container layout
724735
RelativeLayout actionButtonContainer = new RelativeLayout(cordova.getActivity());
725-
actionButtonContainer.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
736+
RelativeLayout.LayoutParams actionButtonLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
737+
if (leftToRight) actionButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
738+
else actionButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
739+
actionButtonContainer.setLayoutParams(actionButtonLayoutParams);
726740
actionButtonContainer.setHorizontalGravity(Gravity.LEFT);
727741
actionButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL);
728-
actionButtonContainer.setId(Integer.valueOf(1));
742+
actionButtonContainer.setId(leftToRight ? Integer.valueOf(5) : Integer.valueOf(1));
729743

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

806820

807821
// Header Close/Done button
808-
View close = createCloseButton(5);
822+
int closeButtonId = leftToRight ? 1 : 5;
823+
View close = createCloseButton(closeButtonId);
809824
toolbar.addView(close);
810825

811826
// Footer

src/ios/CDVInAppBrowser.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
@property (nonatomic, assign) BOOL toolbar;
5151
@property (nonatomic, copy) NSString* closebuttoncaption;
5252
@property (nonatomic, copy) NSString* closebuttoncolor;
53+
@property (nonatomic, assign) BOOL lefttoright;
5354
@property (nonatomic, copy) NSString* toolbarposition;
5455
@property (nonatomic, copy) NSString* toolbarcolor;
5556
@property (nonatomic, assign) BOOL toolbartranslucent;
@@ -103,7 +104,7 @@
103104
- (void)navigateTo:(NSURL*)url;
104105
- (void)showLocationBar:(BOOL)show;
105106
- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition;
106-
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString;
107+
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex;
107108

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

src/ios/CDVInAppBrowser.m

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
163163
[self.inAppBrowserViewController showLocationBar:browserOptions.location];
164164
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition];
165165
if (browserOptions.closebuttoncaption != nil || browserOptions.closebuttoncolor != nil) {
166-
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor];
166+
int closeButtonIndex = browserOptions.lefttoright ? (browserOptions.hidenavigationbuttons ? 1 : 4) : 0;
167+
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor :closeButtonIndex];
167168
}
168169
// Set Presentation Style
169170
UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
@@ -650,11 +651,18 @@ - (void)createViews
650651

651652
// Filter out Navigation Buttons if user requests so
652653
if (_browserOptions.hidenavigationbuttons) {
653-
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
654+
if (_browserOptions.lefttoright) {
655+
[self.toolbar setItems:@[flexibleSpaceButton, self.closeButton]];
656+
} else {
657+
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
658+
}
654659
} else {
655-
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
660+
if (_browserOptions.lefttoright) {
661+
[self.toolbar setItems:@[self.backButton, fixedSpaceButton, self.forwardButton, flexibleSpaceButton, self.closeButton]];
662+
} else {
663+
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
664+
}
656665
}
657-
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
658666

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

670-
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString
678+
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex
671679
{
672680
// the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically
673681
// 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)
@@ -679,7 +687,7 @@ - (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString
679687
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];
680688

681689
NSMutableArray* items = [self.toolbar.items mutableCopy];
682-
[items replaceObjectAtIndex:0 withObject:self.closeButton];
690+
[items replaceObjectAtIndex:buttonIndex withObject:self.closeButton];
683691
[self.toolbar setItems:items];
684692
}
685693

@@ -1022,6 +1030,7 @@ - (id)init
10221030
self.disallowoverscroll = NO;
10231031
self.hidenavigationbuttons = NO;
10241032
self.closebuttoncolor = nil;
1033+
self.lefttoright = false;
10251034
self.toolbarcolor = nil;
10261035
self.toolbartranslucent = YES;
10271036
}

0 commit comments

Comments
 (0)