size.go 3.0 KB

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