Skip to content

Commit 52b81e1

Browse files
committed
MaterialCard - Support mobile orientation feature.
1 parent bd3a8a7 commit 52b81e1

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialCard.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
package gwt.material.design.client.ui;
2121

2222
import com.google.gwt.dom.client.Document;
23+
import com.google.gwt.event.shared.HandlerRegistration;
2324
import gwt.material.design.client.base.HasAxis;
2425
import gwt.material.design.client.base.MaterialWidget;
2526
import gwt.material.design.client.base.mixin.CssNameMixin;
2627
import gwt.material.design.client.constants.Axis;
2728
import gwt.material.design.client.constants.CssName;
29+
import gwt.material.design.client.js.Window;
2830

2931
//@formatter:off
3032

@@ -94,6 +96,8 @@
9496
public class MaterialCard extends MaterialWidget implements HasAxis {
9597

9698
private final CssNameMixin<MaterialCard, Axis> axisMixin = new CssNameMixin<>(this);
99+
private boolean detectOrientation = false;
100+
protected HandlerRegistration orientationHandler;
97101

98102
/**
99103
* Creates and empty card.
@@ -117,4 +121,32 @@ public void setAxis(Axis axis) {
117121
public Axis getAxis() {
118122
return axisMixin.getCssName();
119123
}
120-
}
124+
125+
public void setDetectOrientation(boolean detectOrientation) {
126+
this.detectOrientation = detectOrientation;
127+
128+
if(orientationHandler != null) {
129+
orientationHandler.removeHandler();
130+
orientationHandler = null;
131+
}
132+
133+
if(detectOrientation) {
134+
orientationHandler = com.google.gwt.user.client.Window.addResizeHandler(resizeEvent -> {
135+
detectAndApplyOrientation();
136+
});
137+
detectAndApplyOrientation();
138+
}
139+
}
140+
141+
protected void detectAndApplyOrientation() {
142+
if (Window.matchMedia("(orientation: portrait)")) {
143+
setAxis(Axis.VERTICAL);
144+
} else {
145+
setAxis(Axis.HORIZONTAL);
146+
}
147+
}
148+
149+
public boolean isDetectOrientation() {
150+
return detectOrientation;
151+
}
152+
}

0 commit comments

Comments
 (0)