app: plumb through android orientation

While I'm here, make pixelsPerPt local on android and darwin/amd64.
The one place where it's global is darwin/arm, which has another
similar global (screenScale).

Change-Id: I5897e7e5341afca1976fdf0215fb4f6fe2f411be
Reviewed-on: https://go-review.googlesource.com/13446
Reviewed-by: Nigel Tao <nigeltao@golang.org>
This commit is contained in:
David Crawshaw 2015-08-10 11:18:50 -04:00
Родитель 354679495a
Коммит 0f9ce16152
7 изменённых файлов: 48 добавлений и 18 удалений

Просмотреть файл

@ -166,13 +166,7 @@ func onContentRectChanged(activity *C.ANativeActivity, rect *C.ARect) {
}
type windowConfig struct {
// TODO(crawshaw): report orientation
// ACONFIGURATION_ORIENTATION_ANY
// ACONFIGURATION_ORIENTATION_PORT
// ACONFIGURATION_ORIENTATION_LAND
// ACONFIGURATION_ORIENTATION_SQUARE
// Needs to be merged with iOS's notion of orientation first.
orientation int
orientation config.Orientation
pixelsPerPt float32
}
@ -208,8 +202,16 @@ func windowConfigRead(activity *C.ANativeActivity) windowConfig {
}
}
o := config.OrientationUnknown
switch orient {
case C.ACONFIGURATION_ORIENTATION_PORT:
o = config.OrientationPortrait
case C.ACONFIGURATION_ORIENTATION_LAND:
o = config.OrientationLandscape
}
return windowConfig{
orientation: int(orient),
orientation: o,
pixelsPerPt: float32(dpi) / 72,
}
}
@ -250,6 +252,8 @@ func main(f func(App)) {
}()
var q *C.AInputQueue
var pixelsPerPt float32
var orientation config.Orientation
// Android can send a windowRedrawNeeded event any time, including
// in the middle of a paint cycle. The redraw event may have changed
@ -274,8 +278,8 @@ func main(f func(App)) {
case <-donec:
return
case cfg := <-windowConfigChange:
// TODO save orientation
pixelsPerPt = cfg.pixelsPerPt
orientation = cfg.orientation
case w := <-windowRedrawNeeded:
if C.surface == nil {
if errStr := C.createEGLSurface(w); errStr != nil {
@ -292,6 +296,7 @@ func main(f func(App)) {
WidthPt: geom.Pt(float32(widthPx) / pixelsPerPt),
HeightPt: geom.Pt(float32(heightPx) / pixelsPerPt),
PixelsPerPt: pixelsPerPt,
Orientation: orientation,
}
redrawGen++
eventsIn <- paint.Event{redrawGen}

Просмотреть файл

@ -45,7 +45,6 @@ type App interface {
var (
lifecycleStage = lifecycle.StageDead
pixelsPerPt = float32(1)
eventsOut = make(chan interface{})
eventsIn = pump(eventsOut)

Просмотреть файл

@ -15,6 +15,7 @@ import (
"time"
"golang.org/x/mobile/app/internal/apptest"
"golang.org/x/mobile/event/config"
)
// TestAndroidApp tests the lifecycle, event, and window semantics of a
@ -67,6 +68,19 @@ func TestAndroidApp(t *testing.T) {
run(t, "adb", "shell", "input", "keyevent", KeycodePower)
run(t, "adb", "shell", "input", "keyevent", KeycodeUnlock)
const (
rotationPortrait = "0"
rotationLandscape = "1"
)
rotate := func(rotation string) {
run(t, "adb", "shell", "content", "insert", "--uri", "content://settings/system", "--bind", "name:s:user_rotation", "--bind", "value:i:"+rotation)
}
// turn off automatic rotation and start in portrait
run(t, "adb", "shell", "content", "insert", "--uri", "content://settings/system", "--bind", "name:s:accelerometer_rotation", "--bind", "value:i:0")
rotate(rotationPortrait)
// start testapp
run(t,
"adb", "shell", "am", "start", "-n",
@ -95,14 +109,15 @@ func TestAndroidApp(t *testing.T) {
Printf: t.Logf,
}
var PixelsPerPt float32
var pixelsPerPt float32
var orientation config.Orientation
comm.Recv("hello_from_testapp")
comm.Send("hello_from_host")
comm.Recv("lifecycle_visible")
comm.Recv("config", &PixelsPerPt)
if PixelsPerPt < 0.1 {
t.Fatalf("bad PixelsPerPt: %f", PixelsPerPt)
comm.Recv("config", &pixelsPerPt, &orientation)
if pixelsPerPt < 0.1 {
t.Fatalf("bad pixelsPerPt: %f", pixelsPerPt)
}
comm.Recv("paint")
@ -119,6 +134,17 @@ func TestAndroidApp(t *testing.T) {
t.Errorf("want touch end(50, 60), got %s(%d,%d)", ty, x, y)
}
rotate(rotationLandscape)
comm.Recv("config", &pixelsPerPt, &orientation)
if want := config.OrientationLandscape; orientation != want {
t.Errorf("want orientation %d, got %d", want, orientation)
}
rotate(rotationPortrait)
comm.Recv("config", &pixelsPerPt, &orientation)
if want := config.OrientationPortrait; orientation != want {
t.Errorf("want orientation %d, got %d", want, orientation)
}
// TODO: screenshot of gl.Clear to test painting
// TODO: lifecycle testing (NOTE: adb shell input keyevent 4 is the back button)
// TODO: orientation testing

Просмотреть файл

@ -114,8 +114,7 @@ func startloop(ctx C.GLintptr) {
var windowHeightPx float32
//export setGeom
func setGeom(ppp float32, widthPx, heightPx int) {
pixelsPerPt = ppp
func setGeom(pixelsPerPt float32, widthPx, heightPx int) {
windowHeightPx = float32(heightPx)
eventsIn <- config.Event{
WidthPx: widthPx,

Просмотреть файл

@ -64,6 +64,7 @@ func main(f func(App)) {
panic("unexpected return from app.runApp")
}
var pixelsPerPt float32
var screenScale int // [UIScreen mainScreen].scale, either 1, 2, or 3.
//export setScreen

Просмотреть файл

@ -53,7 +53,7 @@ func main() {
}
case config.Event:
c = e
comm.Send("config", c.PixelsPerPt)
comm.Send("config", c.PixelsPerPt, c.Orientation)
case paint.Event:
if sendPainting {
comm.Send("paint")

Просмотреть файл

@ -78,7 +78,7 @@ func main(f func(App)) {
func onResize(w, h int) {
// TODO(nigeltao): don't assume 72 DPI. DisplayWidth and DisplayWidthMM
// is probably the best place to start looking.
pixelsPerPt = 1
pixelsPerPt := 1
eventsIn <- config.Event{
WidthPx: w,
HeightPx: h,