Skip to content

Commit

Permalink
app, app/internal: [wasm,android] new Option to lock orientation
Browse files Browse the repository at this point in the history
Signed-off-by: Inkeliz <inkeliz@inkeliz.com>
  • Loading branch information
inkeliz committed Apr 22, 2021
1 parent 9dae298 commit 724ddff
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/internal/wm/GioView.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ private void setNavigationColor(int color, int luminance) {
this.setBarColor(Bar.NAVIGATION, color, luminance);
}

private void setOrientation(int id) {
((Activity) this.getContext()).setRequestedOrientation(id);
}

private void dispatchMotionEvent(MotionEvent event) {
for (int j = 0; j < event.getHistorySize(); j++) {
long time = event.getHistoricalEventTime(j);
Expand Down
25 changes: 25 additions & 0 deletions app/internal/wm/os_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type window struct {
// re-creates our Activity.
type windowState struct {
cursor *pointer.CursorName
orientation *OrientationMode
navigationColor *color.NRGBA
statusColor *color.NRGBA
}
Expand All @@ -98,6 +99,7 @@ var gioView struct {
hideTextInput C.jmethodID
postFrameCallback C.jmethodID
setCursor C.jmethodID
setOrientation C.jmethodID
setNavigationColor C.jmethodID
setStatusColor C.jmethodID
}
Expand Down Expand Up @@ -226,6 +228,7 @@ func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.j
m.hideTextInput = getMethodID(env, class, "hideTextInput", "()V")
m.postFrameCallback = getMethodID(env, class, "postFrameCallback", "()V")
m.setCursor = getMethodID(env, class, "setCursor", "(I)V")
m.setOrientation = getMethodID(env, class, "setOrientation", "(I)V")
m.setNavigationColor = getMethodID(env, class, "setNavigationColor", "(II)V")
m.setStatusColor = getMethodID(env, class, "setStatusColor", "(II)V")
})
Expand Down Expand Up @@ -686,6 +689,11 @@ func (w *window) ReadClipboard() {
}

func (w *window) Option(opts *Options) {
if o := opts.Orientation; o != nil {
w.setState(func(state *windowState) {
state.orientation = o
})
}
if o := opts.NavigationColor; o != nil {
w.setState(func(state *windowState) {
state.navigationColor = o
Expand Down Expand Up @@ -723,6 +731,9 @@ func applyStateDiff(env *C.JNIEnv, view C.jobject, old, state windowState) {
if state.cursor != nil && old.cursor != state.cursor {
setCursor(env, view, *state.cursor)
}
if state.orientation != nil && old.orientation != state.orientation {
setOrientation(env, view, *state.orientation)
}
if state.navigationColor != nil && old.navigationColor != state.navigationColor {
setNavigationColor(env, view, *state.navigationColor)
}
Expand Down Expand Up @@ -754,6 +765,20 @@ func setCursor(env *C.JNIEnv, view C.jobject, name pointer.CursorName) {
callVoidMethod(env, view, gioView.setCursor, jvalue(curID))
}

func setOrientation(env *C.JNIEnv, view C.jobject, mode OrientationMode) {
var id int
// Constants defined at https://developer.android.com/reference/android/content/pm/ActivityInfo.
switch mode {
case UnlockedOrientation:
id = 2 // SCREEN_ORIENTATION_USER;
case LockedLandscape:
id = 0 // SCREEN_ORIENTATION_LANDSCAPE;
case LockedPortrait:
id = 1 // SCREEN_ORIENTATION_PORTRAIT;
}
callVoidMethod(env, view, gioView.setOrientation, jvalue(id))
}

func setStatusColor(env *C.JNIEnv, view C.jobject, color color.NRGBA) {
callVoidMethod(env, view, gioView.setStatusColor,
jvalue(uint32(color.A)<<24|uint32(color.R)<<16|uint32(color.G)<<8|uint32(color.B)),
Expand Down
22 changes: 22 additions & 0 deletions app/internal/wm/os_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type window struct {
requestAnimationFrame js.Value
browserHistory js.Value
visualViewport js.Value
screenOrientation js.Value
cleanfuncs []func()
touches []js.Value
composing bool
Expand Down Expand Up @@ -74,6 +75,9 @@ func NewWindow(win Callbacks, opts *Options) error {
if w.visualViewport.IsUndefined() {
w.visualViewport = w.window
}
if screen := w.window.Get("screen"); screen.Truthy() {
w.screenOrientation = screen.Get("orientation")
}
w.chanAnimation = make(chan struct{}, 1)
w.chanRedraw = make(chan struct{}, 1)
w.redraw = w.funcOf(func(this js.Value, args []js.Value) interface{} {
Expand Down Expand Up @@ -494,6 +498,9 @@ func (w *window) Option(opts *Options) {
if o := opts.NavigationColor; o != nil {
w.navigationColor(*o)
}
if o := opts.Orientation; o != nil {
w.orientation(*o)
}
}

func (w *window) SetCursor(name pointer.CursorName) {
Expand Down Expand Up @@ -591,6 +598,21 @@ func (w *window) windowMode(mode WindowMode) {
}
}

func (w *window) orientation(mode OrientationMode) {
if j := w.screenOrientation; !j.Truthy() || !j.Get("unlock").Truthy() || !j.Get("lock").Truthy() {
return // Browser don't support Screen Orientation API.
}

switch mode {
case UnlockedOrientation:
w.screenOrientation.Call("unlock")
case LockedLandscape:
w.screenOrientation.Call("lock", "landscape-primary").Call("then", w.redraw)
case LockedPortrait:
w.screenOrientation.Call("lock", "portrait-primary").Call("then", w.redraw)
}
}

func (w *window) navigationColor(c color.NRGBA) {
theme := w.head.Call("querySelector", `meta[name="theme-color"]`)
if !theme.Truthy() {
Expand Down
9 changes: 9 additions & 0 deletions app/internal/wm/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Options struct {
WindowMode *WindowMode
StatusColor *color.NRGBA
NavigationColor *color.NRGBA
Orientation *OrientationMode
}

type WindowMode uint8
Expand All @@ -37,6 +38,14 @@ const (
Fullscreen
)

type OrientationMode uint8

const (
UnlockedOrientation OrientationMode = iota
LockedLandscape
LockedPortrait
)

type FrameEvent struct {
system.FrameEvent

Expand Down
20 changes: 19 additions & 1 deletion app/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,31 @@ const (

// WindowMode sets the window mode.
//
// Supported platforms are macOS, X11 and Windows.
// Supported platforms are macOS, X11, Windows and JS.
func WindowMode(mode wm.WindowMode) Option {
return func(opts *wm.Options) {
opts.WindowMode = &mode
}
}

const (
// UnlockedOrientation is the default behavior, allowing the rotate of the screen.
UnlockedOrientation = wm.UnlockedOrientation
// LockedPortrait locks the screen to portrait.
LockedPortrait = wm.LockedPortrait
// LockedLandscape locks the screen to landscape.
LockedLandscape = wm.LockedLandscape
)

// Orientation sets the orientation of the app.
//
// Supported platforms are Android and JS (JS may requires to run on Fullscreen).
func Orientation(mode wm.OrientationMode) Option {
return func(opts *wm.Options) {
opts.Orientation = &mode
}
}

// Title sets the title of the wm.
func Title(t string) Option {
return func(opts *wm.Options) {
Expand Down

0 comments on commit 724ddff

Please sign in to comment.