summaryrefslogtreecommitdiff
path: root/android/experimental/LOAndroid3/src/java/org/mozilla/gecko/gfx/ImmutableViewportMetrics.java
blob: b9eb34d94827edf68ae4eb0165a5385ff544ceb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.gfx;

import android.graphics.PointF;
import android.graphics.RectF;

/**
 * ImmutableViewportMetrics are used to store the viewport metrics
 * in way that we can access a version of them from multiple threads
 * without having to take a lock
 */
public class ImmutableViewportMetrics {

    // We need to flatten the RectF and FloatSize structures
    // because Java doesn't have the concept of const classes
    public final float pageSizeWidth;
    public final float pageSizeHeight;
    public final float viewportRectBottom;
    public final float viewportRectLeft;
    public final float viewportRectRight;
    public final float viewportRectTop;
    public final float zoomFactor;

    public ImmutableViewportMetrics(ViewportMetrics m) {
        RectF viewportRect = m.getViewport();
        viewportRectBottom = viewportRect.bottom;
        viewportRectLeft = viewportRect.left;
        viewportRectRight = viewportRect.right;
        viewportRectTop = viewportRect.top;

        FloatSize pageSize = m.getPageSize();
        pageSizeWidth = pageSize.width;
        pageSizeHeight = pageSize.height;

        zoomFactor = m.getZoomFactor();
    }

    public float getWidth() {
        return viewportRectRight - viewportRectLeft;
    }

    public float getHeight() {
        return viewportRectBottom - viewportRectTop;
    }

    // some helpers to make ImmutableViewportMetrics act more like ViewportMetrics

    public PointF getOrigin() {
        return new PointF(viewportRectLeft, viewportRectTop);
    }

    public FloatSize getSize() {
        return new FloatSize(viewportRectRight - viewportRectLeft, viewportRectBottom - viewportRectTop);
    }

    public RectF getViewport() {
        return new RectF(viewportRectLeft,
                viewportRectTop,
                viewportRectRight,
                viewportRectBottom);
    }

    public FloatSize getPageSize() {
        return new FloatSize(pageSizeWidth, pageSizeHeight);
    }
}