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