001/*
002 * #%L
003 * GwtMaterial
004 * %%
005 * Copyright (C) 2015 - 2017 GwtMaterialDesign
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 * 
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 * 
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package gwt.material.design.client.base.mixin;
021
022import com.google.gwt.event.shared.HandlerRegistration;
023import gwt.material.design.client.base.HasOrientation;
024import gwt.material.design.client.base.MaterialWidget;
025import gwt.material.design.client.constants.Orientation;
026import gwt.material.design.client.events.OrientationChangeEvent;
027import gwt.material.design.client.js.Window;
028import gwt.material.design.client.ui.MaterialToast;
029
030/**
031 *  @author kevzlou7979
032 */
033public class OrientationMixin<T extends MaterialWidget & HasOrientation> extends AbstractMixin<T>  implements HasOrientation {
034
035    private boolean detectOrientation = false;
036    private HandlerRegistration orientationHandler;
037    private Orientation orientation;
038    private CssNameMixin<MaterialWidget, Orientation> nameMixin;
039
040    public OrientationMixin(T widget) {
041        super(widget);
042    }
043
044    @Override
045    public void setOrientation(Orientation orientation) {
046        this.orientation = orientation;
047        if (orientation != null) {
048            OrientationChangeEvent.fire(uiObject, orientation);
049            getNameMixin().setCssName(orientation);
050        }
051    }
052
053    @Override
054    public Orientation getOrientation() {
055        return orientation;
056    }
057
058    @Override
059    public void setDetectOrientation(boolean detectOrientation) {
060        this.detectOrientation = detectOrientation;
061
062        if(orientationHandler != null) {
063            orientationHandler.removeHandler();
064            orientationHandler = null;
065        }
066
067        if(detectOrientation) {
068            orientationHandler = Window.addResizeHandler(resizeEvent -> detectAndApplyOrientation());
069            detectAndApplyOrientation();
070        }
071    }
072
073    protected void detectAndApplyOrientation() {
074        if (Window.matchMedia("(orientation: portrait)")) {
075            setOrientation(Orientation.PORTRAIT);
076        } else {
077            setOrientation(Orientation.LANDSCAPE);
078        }
079    }
080
081    @Override
082    public boolean isDetectOrientation() {
083        return detectOrientation;
084    }
085
086    @Override
087    public HandlerRegistration addOrientationChangeHandler(OrientationChangeEvent.OrientationChangeHandler handler) {
088        return uiObject.addHandler(handler, OrientationChangeEvent.TYPE);
089    }
090
091    public CssNameMixin<MaterialWidget, Orientation> getNameMixin() {
092        if (nameMixin == null) {
093            nameMixin = new CssNameMixin<>(uiObject);
094        }
095        return nameMixin;
096    }
097}