github.com/provpn/mobile@v0.0.0-20210315122651-28c475f89f6c/event/size/size.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package size defines an event for the dimensions, physical resolution and
     6  // orientation of the app's window.
     7  //
     8  // See the github.com/provpn/mobile/app package for details on the event model.
     9  package size // import "github.com/provpn/mobile/event/size"
    10  
    11  import (
    12  	"image"
    13  
    14  	"github.com/provpn/mobile/geom"
    15  )
    16  
    17  // Event holds the dimensions, physical resolution and orientation of the app's
    18  // window.
    19  type Event struct {
    20  	// WidthPx and HeightPx are the window's dimensions in pixels.
    21  	WidthPx, HeightPx int
    22  
    23  	// WidthPt and HeightPt are the window's physical dimensions in points
    24  	// (1/72 of an inch).
    25  	//
    26  	// The values are based on PixelsPerPt and are therefore approximate, as
    27  	// per the comment on PixelsPerPt.
    28  	WidthPt, HeightPt geom.Pt
    29  
    30  	// PixelsPerPt is the window's physical resolution. It is the number of
    31  	// pixels in a single geom.Pt, from the github.com/provpn/mobile/geom package.
    32  	//
    33  	// There are a wide variety of pixel densities in existing phones and
    34  	// tablets, so apps should be written to expect various non-integer
    35  	// PixelsPerPt values. In general, work in geom.Pt.
    36  	//
    37  	// The value is approximate, in that the OS, drivers or hardware may report
    38  	// approximate or quantized values. An N x N pixel square should be roughly
    39  	// 1 square inch for N = int(PixelsPerPt * 72), although different square
    40  	// lengths (in pixels) might be closer to 1 inch in practice. Nonetheless,
    41  	// this PixelsPerPt value should be consistent with e.g. the ratio of
    42  	// WidthPx to WidthPt.
    43  	PixelsPerPt float32
    44  
    45  	// Orientation is the orientation of the device screen.
    46  	Orientation Orientation
    47  }
    48  
    49  // Size returns the window's size in pixels, at the time this size event was
    50  // sent.
    51  func (e Event) Size() image.Point {
    52  	return image.Point{e.WidthPx, e.HeightPx}
    53  }
    54  
    55  // Bounds returns the window's bounds in pixels, at the time this size event
    56  // was sent.
    57  //
    58  // The top-left pixel is always (0, 0). The bottom-right pixel is given by the
    59  // width and height.
    60  func (e Event) Bounds() image.Rectangle {
    61  	return image.Rectangle{Max: image.Point{e.WidthPx, e.HeightPx}}
    62  }
    63  
    64  // Orientation is the orientation of the device screen.
    65  type Orientation int
    66  
    67  const (
    68  	// OrientationUnknown means device orientation cannot be determined.
    69  	//
    70  	// Equivalent on Android to Configuration.ORIENTATION_UNKNOWN
    71  	// and on iOS to:
    72  	//	UIDeviceOrientationUnknown
    73  	//	UIDeviceOrientationFaceUp
    74  	//	UIDeviceOrientationFaceDown
    75  	OrientationUnknown Orientation = iota
    76  
    77  	// OrientationPortrait is a device oriented so it is tall and thin.
    78  	//
    79  	// Equivalent on Android to Configuration.ORIENTATION_PORTRAIT
    80  	// and on iOS to:
    81  	//	UIDeviceOrientationPortrait
    82  	//	UIDeviceOrientationPortraitUpsideDown
    83  	OrientationPortrait
    84  
    85  	// OrientationLandscape is a device oriented so it is short and wide.
    86  	//
    87  	// Equivalent on Android to Configuration.ORIENTATION_LANDSCAPE
    88  	// and on iOS to:
    89  	//	UIDeviceOrientationLandscapeLeft
    90  	//	UIDeviceOrientationLandscapeRight
    91  	OrientationLandscape
    92  )