2020package gwt .material .design .client .ui ;
2121
2222import com .google .gwt .dom .client .Document ;
23+ import com .google .gwt .event .shared .HandlerRegistration ;
2324import gwt .material .design .client .base .HasAxis ;
2425import gwt .material .design .client .base .MaterialWidget ;
2526import gwt .material .design .client .base .mixin .CssNameMixin ;
2627import gwt .material .design .client .constants .Axis ;
2728import gwt .material .design .client .constants .CssName ;
29+ import gwt .material .design .client .js .Window ;
2830
2931//@formatter:off
3032
9496public 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