Skip to content

Commit d01bd25

Browse files
Merge pull request #262 from landsbankinn/CB-13969
Cb 13969 - Allow close button and navigation buttons positions to be swapped
2 parents eb3acc0 + f861655 commit d01bd25

File tree

8 files changed

+51
-17
lines changed

8 files changed

+51
-17
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ instance, or the system browser.
123123
- __hideurlbar__: set to `yes` to hide the url bar on the location toolbar, only has effect if user has location set to `yes`. The default value is `no`.
124124
- __navigationbuttoncolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the color of both navigation buttons from default. Only has effect if user has location set to `yes` and not hidenavigationbuttons set to `yes`.
125125
- __toolbarcolor__: set to a valid hex color string, for example: `#00ff00`, and it will change the color the toolbar from default. Only has effect if user has location set to `yes`.
126+
- __lefttoright__: Set to `yes` to swap positions of the navigation buttons and the close button. Specifically, navigation buttons go to the left and close button to the right.
126127
- __zoom__: set to `yes` to show Android browser's zoom controls, set to `no` to hide them. Default value is `yes`.
127128
- __mediaPlaybackRequiresUserAction__: Set to `yes` to prevent HTML5 audio or video from autoplaying (defaults to `no`).
128129
- __shouldPauseOnSuspend__: Set to `yes` to make InAppBrowser WebView to pause/resume with the app to stop background audio (this may be required to avoid Google Play issues like described in [CB-11013](https://issues.apache.org/jira/browse/CB-11013)).
@@ -144,6 +145,7 @@ instance, or the system browser.
144145
- __toolbar__: set to `yes` or `no` to turn the toolbar on or off for the InAppBrowser (defaults to `yes`)
145146
- __toolbarcolor__: set as a valid hex color string, for example: `#00ff00`, to change from the default color of the toolbar. Only applicable if toolbar is not disabled.
146147
- __toolbartranslucent__: set to `yes` or `no` to make the toolbar translucent(semi-transparent) (defaults to `yes`). Only applicable if toolbar is not disabled.
148+
- __lefttoright__: Set to `yes` to swap positions of the navigation buttons and the close button. Specifically, close button goes to the right and navigation buttons to the left.
147149
- __enableViewportScale__: Set to `yes` or `no` to prevent viewport scaling through a meta tag (defaults to `no`). Only applicable to UIWebView (`usewkwebview=no`) and WKWebView (`usewkwebview=yes`) on iOS 10+.
148150
- __mediaPlaybackRequiresUserAction__: Set to `yes` to prevent HTML5 audio or video from autoplaying (defaults to `no`). Applicable to UIWebView (`usewkwebview=no`) and WKWebView (`usewkwebview=yes`).
149151
- __allowInlineMediaPlayback__: Set to `yes` or `no` to allow in-line HTML5 media playback, displaying within the browser window rather than a device-specific playback interface. The HTML's `video` element must also include the `webkit-playsinline` attribute (defaults to `no`). Applicable to UIWebView (`usewkwebview=no`) and WKWebView (`usewkwebview=yes`).

src/android/InAppBrowser.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public class InAppBrowser extends CordovaPlugin {
110110
private static final String TOOLBAR_COLOR = "toolbarcolor";
111111
private static final String CLOSE_BUTTON_CAPTION = "closebuttoncaption";
112112
private static final String CLOSE_BUTTON_COLOR = "closebuttoncolor";
113+
private static final String LEFT_TO_RIGHT = "lefttoright";
113114
private static final String HIDE_NAVIGATION = "hidenavigationbuttons";
114115
private static final String NAVIGATION_COLOR = "navigationbuttoncolor";
115116
private static final String HIDE_URL = "hideurlbar";
@@ -138,6 +139,7 @@ public class InAppBrowser extends CordovaPlugin {
138139
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
139140
private String closeButtonCaption = "";
140141
private String closeButtonColor = "";
142+
private boolean leftToRight = false;
141143
private int toolbarColor = android.graphics.Color.LTGRAY;
142144
private boolean hideNavigationButtons = false;
143145
private String navigationButtonColor = "";
@@ -679,6 +681,10 @@ public String showWebPage(final String url, HashMap<String, String> features) {
679681
if (closeButtonColorSet != null) {
680682
closeButtonColor = closeButtonColorSet;
681683
}
684+
String leftToRightSet = features.get(LEFT_TO_RIGHT);
685+
if (leftToRightSet != null) {
686+
leftToRight = leftToRightSet.equals("yes") ? true : false;
687+
}
682688
String toolbarColorSet = features.get(TOOLBAR_COLOR);
683689
if (toolbarColorSet != null) {
684690
toolbarColor = android.graphics.Color.parseColor(toolbarColorSet);
@@ -746,7 +752,8 @@ private View createCloseButton(int id){
746752
}
747753

748754
RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
749-
closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
755+
if (leftToRight) closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
756+
else closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
750757
_close.setLayoutParams(closeLayoutParams);
751758

752759
if (Build.VERSION.SDK_INT >= 16)
@@ -777,6 +784,7 @@ public void run() {
777784
dialog = new InAppBrowserDialog(cordova.getActivity(), android.R.style.Theme_NoTitleBar);
778785
dialog.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;
779786
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
787+
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
780788
dialog.setCancelable(true);
781789
dialog.setInAppBroswer(getInAppBrowser());
782790

@@ -790,15 +798,22 @@ public void run() {
790798
toolbar.setBackgroundColor(toolbarColor);
791799
toolbar.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, this.dpToPixels(44)));
792800
toolbar.setPadding(this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2), this.dpToPixels(2));
793-
toolbar.setHorizontalGravity(Gravity.LEFT);
801+
if (leftToRight) {
802+
toolbar.setHorizontalGravity(Gravity.LEFT);
803+
} else {
804+
toolbar.setHorizontalGravity(Gravity.RIGHT);
805+
}
794806
toolbar.setVerticalGravity(Gravity.TOP);
795807

796808
// Action Button Container layout
797809
RelativeLayout actionButtonContainer = new RelativeLayout(cordova.getActivity());
798-
actionButtonContainer.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
810+
RelativeLayout.LayoutParams actionButtonLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
811+
if (leftToRight) actionButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
812+
else actionButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
813+
actionButtonContainer.setLayoutParams(actionButtonLayoutParams);
799814
actionButtonContainer.setHorizontalGravity(Gravity.LEFT);
800815
actionButtonContainer.setVerticalGravity(Gravity.CENTER_VERTICAL);
801-
actionButtonContainer.setId(Integer.valueOf(1));
816+
actionButtonContainer.setId(leftToRight ? Integer.valueOf(5) : Integer.valueOf(1));
802817

803818
// Back button
804819
ImageButton back = new ImageButton(cordova.getActivity());
@@ -878,7 +893,8 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
878893

879894

880895
// Header Close/Done button
881-
View close = createCloseButton(5);
896+
int closeButtonId = leftToRight ? 1 : 5;
897+
View close = createCloseButton(closeButtonId);
882898
toolbar.addView(close);
883899

884900
// Footer

src/ios/CDVInAppBrowserOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
@property (nonatomic, assign) BOOL toolbar;
2626
@property (nonatomic, copy) NSString* closebuttoncaption;
2727
@property (nonatomic, copy) NSString* closebuttoncolor;
28+
@property (nonatomic, assign) BOOL lefttoright;
2829
@property (nonatomic, copy) NSString* toolbarposition;
2930
@property (nonatomic, copy) NSString* toolbarcolor;
3031
@property (nonatomic, assign) BOOL toolbartranslucent;

src/ios/CDVInAppBrowserOptions.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ - (id)init
4444
self.disallowoverscroll = NO;
4545
self.hidenavigationbuttons = NO;
4646
self.closebuttoncolor = nil;
47+
self.lefttoright = false;
4748
self.toolbarcolor = nil;
4849
self.toolbartranslucent = YES;
4950
self.beforeload = @"";

src/ios/CDVUIInAppBrowser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
- (void)navigateTo:(NSURL*)url;
8585
- (void)showLocationBar:(BOOL)show;
8686
- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition;
87-
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString;
87+
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex;
8888

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

src/ios/CDVUIInAppBrowser.m

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
172172
[self.inAppBrowserViewController showLocationBar:browserOptions.location];
173173
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition];
174174
if (browserOptions.closebuttoncaption != nil || browserOptions.closebuttoncolor != nil) {
175-
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor];
175+
int closeButtonIndex = browserOptions.lefttoright ? (browserOptions.hidenavigationbuttons ? 1 : 4) : 0;
176+
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor :closeButtonIndex];
176177
}
177178
// Set Presentation Style
178179
UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
@@ -763,9 +764,15 @@ - (void)createViews
763764

764765
// Filter out Navigation Buttons if user requests so
765766
if (_browserOptions.hidenavigationbuttons) {
766-
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
767+
if (_browserOptions.lefttoright) {
768+
[self.toolbar setItems:@[flexibleSpaceButton, self.closeButton]];
769+
} else {
770+
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
771+
}
772+
} else if (_browserOptions.lefttoright) {
773+
[self.toolbar setItems:@[self.backButton, fixedSpaceButton, self.forwardButton, flexibleSpaceButton, self.closeButton]];
767774
} else {
768-
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
775+
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
769776
}
770777

771778
self.view.backgroundColor = [UIColor grayColor];
@@ -779,7 +786,7 @@ - (void) setWebViewFrame : (CGRect) frame {
779786
[self.webView setFrame:frame];
780787
}
781788

782-
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString
789+
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex
783790
{
784791
// the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically
785792
// 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)
@@ -791,7 +798,7 @@ - (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString
791798
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];
792799

793800
NSMutableArray* items = [self.toolbar.items mutableCopy];
794-
[items replaceObjectAtIndex:0 withObject:self.closeButton];
801+
[items replaceObjectAtIndex:buttonIndex withObject:self.closeButton];
795802
[self.toolbar setItems:items];
796803
}
797804

src/ios/CDVWKInAppBrowser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
- (void)navigateTo:(NSURL*)url;
7474
- (void)showLocationBar:(BOOL)show;
7575
- (void)showToolBar:(BOOL)show : (NSString *) toolbarPosition;
76-
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString;
76+
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex;
7777

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

src/ios/CDVWKInAppBrowser.m

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
228228
[self.inAppBrowserViewController showLocationBar:browserOptions.location];
229229
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar :browserOptions.toolbarposition];
230230
if (browserOptions.closebuttoncaption != nil || browserOptions.closebuttoncolor != nil) {
231-
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor];
231+
int closeButtonIndex = browserOptions.lefttoright ? (browserOptions.hidenavigationbuttons ? 1 : 4) : 0;
232+
[self.inAppBrowserViewController setCloseButtonTitle:browserOptions.closebuttoncaption :browserOptions.closebuttoncolor :closeButtonIndex];
232233
}
233234
// Set Presentation Style
234235
UIModalPresentationStyle presentationStyle = UIModalPresentationFullScreen; // default
@@ -884,9 +885,15 @@ - (void)createViews
884885

885886
// Filter out Navigation Buttons if user requests so
886887
if (_browserOptions.hidenavigationbuttons) {
887-
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
888+
if (_browserOptions.lefttoright) {
889+
[self.toolbar setItems:@[flexibleSpaceButton, self.closeButton]];
890+
} else {
891+
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton]];
892+
}
893+
} else if (_browserOptions.lefttoright) {
894+
[self.toolbar setItems:@[self.backButton, fixedSpaceButton, self.forwardButton, flexibleSpaceButton, self.closeButton]];
888895
} else {
889-
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
896+
[self.toolbar setItems:@[self.closeButton, flexibleSpaceButton, self.backButton, fixedSpaceButton, self.forwardButton]];
890897
}
891898

892899
self.view.backgroundColor = [UIColor grayColor];
@@ -900,7 +907,7 @@ - (void) setWebViewFrame : (CGRect) frame {
900907
[self.webView setFrame:frame];
901908
}
902909

903-
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString
910+
- (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) buttonIndex
904911
{
905912
// the advantage of using UIBarButtonSystemItemDone is the system will localize it for you automatically
906913
// 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)
@@ -912,7 +919,7 @@ - (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString
912919
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];
913920

914921
NSMutableArray* items = [self.toolbar.items mutableCopy];
915-
[items replaceObjectAtIndex:0 withObject:self.closeButton];
922+
[items replaceObjectAtIndex:buttonIndex withObject:self.closeButton];
916923
[self.toolbar setItems:items];
917924
}
918925

0 commit comments

Comments
 (0)