ui: Add SetInitFocused

Updates #769
This commit is contained in:
Hajime Hoshi 2020-02-09 23:03:03 +09:00
parent 90dba581e2
commit bb04fc8a29
7 changed files with 66 additions and 1 deletions

View File

@ -58,6 +58,7 @@ var (
flagWindowPosition = flag.String("windowposition", "", "window position (e.g., 100,200)") flagWindowPosition = flag.String("windowposition", "", "window position (e.g., 100,200)")
flagScreenTransparent = flag.Bool("screentransparent", false, "screen transparent") flagScreenTransparent = flag.Bool("screentransparent", false, "screen transparent")
flagAutoAdjusting = flag.Bool("autoadjusting", false, "make the game screen auto-adjusting") flagAutoAdjusting = flag.Bool("autoadjusting", false, "make the game screen auto-adjusting")
flagInitFocused = flag.Bool("initfocused", true, "whether the window is focused on start")
) )
func init() { func init() {
@ -370,6 +371,11 @@ func main() {
ebiten.SetWindowResizable(true) ebiten.SetWindowResizable(true)
} }
ebiten.SetInitFocused(*flagInitFocused)
if !*flagInitFocused {
ebiten.SetRunnableInBackground(true)
}
const title = "Window Size (Ebiten Demo)" const title = "Window Size (Ebiten Demo)"
if *flagLegacy { if *flagLegacy {
if err := ebiten.Run(g.Update, g.width, g.height, initScreenScale, title); err != nil { if err := ebiten.Run(g.Update, g.width, g.height, initScreenScale, title); err != nil {

View File

@ -49,6 +49,7 @@ type UI interface {
SetRunnableInBackground(runnableInBackground bool) SetRunnableInBackground(runnableInBackground bool)
SetVsyncEnabled(enabled bool) SetVsyncEnabled(enabled bool)
SetScreenTransparent(transparent bool) SetScreenTransparent(transparent bool)
SetInitFocused(focused bool)
Input() Input Input() Input
Window() Window Window() Window

View File

@ -78,6 +78,7 @@ const (
ContextVersionMinor = Hint(0x00022003) ContextVersionMinor = Hint(0x00022003)
Decorated = Hint(0x00020005) Decorated = Hint(0x00020005)
Focused = Hint(0x00020001) Focused = Hint(0x00020001)
FocusOnShow = Hint(0x0002000C)
Resizable = Hint(0x00020003) Resizable = Hint(0x00020003)
TransparentFramebuffer = Hint(0x0002000A) TransparentFramebuffer = Hint(0x0002000A)
Visible = Hint(0x00020004) Visible = Hint(0x00020004)

View File

@ -68,6 +68,7 @@ type UserInterface struct {
initWindowHeightInDP int initWindowHeightInDP int
initScreenTransparent bool initScreenTransparent bool
initIconImages []image.Image initIconImages []image.Image
initFocused bool
reqWidth int reqWidth int
reqHeight int reqHeight int
@ -95,6 +96,7 @@ var (
initWindowPositionYInDP: invalidPos, initWindowPositionYInDP: invalidPos,
initWindowWidthInDP: 640, initWindowWidthInDP: 640,
initWindowHeightInDP: 480, initWindowHeightInDP: 480,
initFocused: true,
vsync: true, vsync: true,
} }
) )
@ -334,6 +336,19 @@ func (u *UserInterface) setInitWindowSize(width, height int) {
u.m.Unlock() u.m.Unlock()
} }
func (u *UserInterface) isInitFocused() bool {
u.m.Lock()
v := u.initFocused
u.m.Unlock()
return v
}
func (u *UserInterface) setInitFocused(focused bool) {
u.m.Lock()
u.initFocused = focused
u.m.Unlock()
}
// toDeviceIndependentPixel must be called from the main thread. // toDeviceIndependentPixel must be called from the main thread.
func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 { func (u *UserInterface) toDeviceIndependentPixel(x float64) float64 {
return x / u.glfwScale() return x / u.glfwScale()
@ -628,6 +643,12 @@ func (u *UserInterface) run(context driver.UIContext) error {
} }
glfw.WindowHint(glfw.Resizable, resizable) glfw.WindowHint(glfw.Resizable, resizable)
focused := glfw.False
if u.isInitFocused() {
focused = glfw.True
}
glfw.WindowHint(glfw.FocusOnShow, focused)
// Set the window visible explicitly or the application freezes on Wayland (#974). // Set the window visible explicitly or the application freezes on Wayland (#974).
if os.Getenv("WAYLAND_DISPLAY") != "" { if os.Getenv("WAYLAND_DISPLAY") != "" {
glfw.WindowHint(glfw.Visible, glfw.True) glfw.WindowHint(glfw.Visible, glfw.True)
@ -1015,6 +1036,13 @@ func (u *UserInterface) MonitorPosition() (int, int) {
return mx, my return mx, my
} }
func (u *UserInterface) SetInitFocused(focused bool) {
if u.isRunning() {
panic("ui: SetInitFocused must be called before the main loop")
}
u.setInitFocused(focused)
}
func (u *UserInterface) monitorPosition() (int, int) { func (u *UserInterface) monitorPosition() (int, int) {
// TODO: toDeviceIndependentPixel might be required. // TODO: toDeviceIndependentPixel might be required.
return u.currentMonitor().GetPos() return u.currentMonitor().GetPos()

View File

@ -33,6 +33,7 @@ type UserInterface struct {
runnableInBackground bool runnableInBackground bool
vsync bool vsync bool
running bool running bool
initFocused bool
sizeChanged bool sizeChanged bool
contextLost bool contextLost bool
@ -46,6 +47,7 @@ type UserInterface struct {
var theUI = &UserInterface{ var theUI = &UserInterface{
sizeChanged: true, sizeChanged: true,
vsync: true, vsync: true,
initFocused: true,
} }
func init() { func init() {
@ -405,7 +407,9 @@ func init() {
} }
func (u *UserInterface) Run(context driver.UIContext) error { func (u *UserInterface) Run(context driver.UIContext) error {
canvas.Call("focus") if u.initFocused {
canvas.Call("focus")
}
u.running = true u.running = true
ch := u.loop(context) ch := u.loop(context)
if runtime.GOARCH == "wasm" { if runtime.GOARCH == "wasm" {
@ -460,6 +464,13 @@ func (u *UserInterface) MonitorPosition() (int, int) {
return 0, 0 return 0, 0
} }
func (u *UserInterface) SetInitFocused(focused bool) {
if u.running {
panic("ui: SetInitFocused must be called before the main loop")
}
u.initFocused = focused
}
func (u *UserInterface) Input() driver.Input { func (u *UserInterface) Input() driver.Input {
return &u.input return &u.input
} }

View File

@ -420,6 +420,10 @@ func (u *UserInterface) MonitorPosition() (int, int) {
return 0, 0 return 0, 0
} }
func (u *UserInterface) SetInitFocused(focused bool) {
// Do nothing
}
func (u *UserInterface) Input() driver.Input { func (u *UserInterface) Input() driver.Input {
return &u.input return &u.input
} }

14
run.go
View File

@ -486,3 +486,17 @@ func IsScreenTransparent() bool {
func SetScreenTransparent(transparent bool) { func SetScreenTransparent(transparent bool) {
uiDriver().SetScreenTransparent(transparent) uiDriver().SetScreenTransparent(transparent)
} }
// SetInitFocused sets whether the application is focused on show.
// The default value is true, i.e., the application is focused.
// Note that the application does not proceed if this is not focused by default.
// This behavior can be changed by SetRunnableInBackground.
//
// SetInitFocused does nothing on mobile.
//
// SetInitFocused panics if this is called after the main loop.
//
// SetInitFocused is cuncurrent-safe.
func SetInitFocused(focused bool) {
uiDriver().SetInitFocused(focused)
}