Skip to content

Commit

Permalink
Merge pull request #41 from Brainfuse/master
Browse files Browse the repository at this point in the history
Fixes ISSUE 307 Nav bar  showing on the nexus 7, 7 inch when the keyboard is shown
  • Loading branch information
dankurka committed Jan 22, 2014
2 parents f4a50c8 + 1cbdc91 commit e315f5a
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 159 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/googlecode/mgwt/ui/UI.gwt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,27 @@ under * the License.
<when-type-is class="com.googlecode.mgwt.ui.client.widget.menu.SwipeMenuAppearance" />
</replace-with>

<!-- Orientation handler -->
<replace-with class="com.googlecode.mgwt.ui.client.util.impl.IOSOrientationHandler">
<when-type-is class="com.googlecode.mgwt.ui.client.util.OrientationHandler" />
</replace-with>

<!--
Uncomment this to use exactly the old implementation
<replace-with class="com.googlecode.mgwt.ui.client.util.impl.ResizeOrientationHandler">
<when-type-is class="com.googlecode.mgwt.ui.client.util.OrientationHandler" />
</replace-with>
<replace-with class="com.googlecode.mgwt.ui.client.util.impl.IOSOrientationHandler">
<when-type-is class="com.googlecode.mgwt.ui.client.util.OrientationHandler" />
<any>
<when-property-is name="mgwt.os" value="iphone" />
<when-property-is name="mgwt.os" value="ipad"/>
<when-property-is name="mgwt.os" value="retina" />
<when-property-is name="mgwt.os" value="ipad_retina"/>
</any>
</replace-with> -->


<source path="client" />
</module>
177 changes: 18 additions & 159 deletions src/main/java/com/googlecode/mgwt/ui/client/MGWT.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.RootPanel;

import com.googlecode.mgwt.dom.client.event.orientation.OrientationChangeEvent;
import com.googlecode.mgwt.dom.client.event.orientation.OrientationChangeEvent.ORIENTATION;
import com.googlecode.mgwt.dom.client.event.orientation.OrientationChangeHandler;
import com.googlecode.mgwt.ui.client.MGWTSettings.ViewPort;
import com.googlecode.mgwt.ui.client.theme.base.UtilCss;
import com.googlecode.mgwt.ui.client.util.AddressBarUtil;
import com.googlecode.mgwt.ui.client.util.OrientationHandler;

/**
* The MGWT Object is used to apply settings for an MGWT App. It also provides an instance of
Expand All @@ -60,14 +60,25 @@ public class MGWT {

private static EventBus manager;

private static ORIENTATION currentOrientation;
private static Timer timer;

private static OrientationHandler orientationHandler;

private static boolean scrollingDisabled;
private static JavaScriptObject nativeJsFunction;

private static AddressBarUtil addressBarUtil;

/**
* Return an orientation handler based on the current os.
* @return
*/
public static OrientationHandler getOrientationHandler() {
if ( orientationHandler == null){
orientationHandler = GWT.create(OrientationHandler.class);

}
return orientationHandler;
}
/**
* Add a orientation handler to detect the device orientation
*
Expand All @@ -76,7 +87,7 @@ public class MGWT {
* @return a {@link com.google.gwt.event.shared.HandlerRegistration} object.
*/
public static HandlerRegistration addOrientationChangeHandler(OrientationChangeHandler handler) {
maybeSetupOrientation();
getOrientationHandler().maybeSetupOrientation(getManager());
return getManager().addHandler(OrientationChangeEvent.getType(), handler);
}

Expand Down Expand Up @@ -257,47 +268,10 @@ public static OsDetection getOsDetection() {
*
* @return the current orientation of the device
*/
public static ORIENTATION getOrientation() {

if (!orientationSupport()) {
int height = Window.getClientHeight();
int width = Window.getClientWidth();

if (width > height) {
return ORIENTATION.LANDSCAPE;
} else {
return ORIENTATION.PORTRAIT;
}

} else {
int orientation = getOrientation0();

ORIENTATION o;
switch (orientation) {
case 0:
case 180:
o = ORIENTATION.PORTRAIT;
break;

case 90:
case -90:
o = ORIENTATION.LANDSCAPE;
break;

default:
throw new IllegalStateException("this should not happen!?");
}

return o;
}

}

private static void fireOrientationChangedEvent(ORIENTATION orientation) {
setClasses(orientation);
getManager().fireEvent(new OrientationChangeEvent(orientation));
public static ORIENTATION getOrientation(){
return getOrientationHandler().getOrientation();
}

private static Element getHead() {
NodeList<Element> elementsByTagName = Document.get().getElementsByTagName("head");

Expand All @@ -308,121 +282,6 @@ private static Element getHead() {
return elementsByTagName.getItem(0);
}

private static native int getOrientation0()/*-{
if (typeof ($wnd.orientation) == 'undefined') {
return 0;
}
return $wnd.orientation;
}-*/;

private static void onorientationChange(int orientation) {

ORIENTATION o;
switch (orientation) {
case 0:
case 180:
o = ORIENTATION.PORTRAIT;
break;

case 90:
case -90:
o = ORIENTATION.LANDSCAPE;
break;

default:
o = ORIENTATION.PORTRAIT;
break;
}
currentOrientation = o;
fireOrientationChangedEvent(o);

}

// update styles on body
private static void setClasses(ORIENTATION o) {
UtilCss utilCss = MGWTStyle.getTheme().getMGWTClientBundle().getUtilCss();
switch (o) {

case PORTRAIT:
Document.get().getBody().addClassName(utilCss.portrait());
Document.get().getBody().removeClassName(utilCss.landscape());
break;
case LANDSCAPE:
Document.get().getBody().addClassName(utilCss.landscape());
Document.get().getBody().removeClassName(utilCss.portrait());
break;

default:
break;
}
}

private native static boolean orientationSupport()/*-{
var ua = window.navigator.userAgent.toLowerCase();
if (ua.indexOf('android') != -1) {
return false;
}
if (ua.indexOf('iphone') != -1) {
return true;
}
if (ua.indexOf('ipad') != -1) {
return true;
}
return false;
}-*/;

private static boolean orientationInitialized;

private static void maybeSetupOrientation() {
if (orientationInitialized)
return;
if (!GWT.isClient()) {
return;
}

if (!orientationSupport()) {
Window.addResizeHandler(new ResizeHandler() {

@Override
public void onResize(ResizeEvent event) {
ORIENTATION orientation = getOrientation();
if (orientation != currentOrientation) {
currentOrientation = orientation;
fireOrientationChangedEvent(orientation);
}
}
});
} else {
nativeJsFunction = setupOrientation0();
Window.addCloseHandler(new CloseHandler<Window>() {

@Override
public void onClose(CloseEvent<Window> event) {
destroyOrientation(nativeJsFunction);

}
});
}

}

private static native JavaScriptObject setupOrientation0()/*-{
var func = $entry(function() {
@com.googlecode.mgwt.ui.client.MGWT::onorientationChange(I)($wnd.orientation);
});
$doc.body.onorientationchange = func;
$doc.addEventListener("orientationChanged", func);
return func;
}-*/;

private static native void destroyOrientation(JavaScriptObject o)/*-{
$doc.body.onorientationchange = null;
$doc.removeEventListener("orientationChanged", o);
}-*/;

private static native void setupPreventScrolling(Element el)/*-{
var func = function(event) {
event.preventDefault();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.googlecode.mgwt.ui.client.util;

import com.google.web.bindery.event.shared.EventBus;
import com.googlecode.mgwt.dom.client.event.orientation.OrientationChangeEvent.ORIENTATION;

public interface OrientationHandler {

public abstract ORIENTATION getOrientation();

void maybeSetupOrientation(EventBus manager);

}
Loading

0 comments on commit e315f5a

Please sign in to comment.