Skip to content

Commit

Permalink
Use internal/build for mobile lookup as well
Browse files Browse the repository at this point in the history
Does not seem to help deadcode elimination anything but it seems nice to clean up some code and have all build type information in one common place?
  • Loading branch information
Jacalz committed Nov 27, 2023
1 parent 4a18a7f commit 1e86292
Show file tree
Hide file tree
Showing 27 changed files with 105 additions and 71 deletions.
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal"
"fyne.io/fyne/v2/internal/app"
"fyne.io/fyne/v2/internal/build"
intRepo "fyne.io/fyne/v2/internal/repository"
"fyne.io/fyne/v2/storage/repository"
)
Expand Down Expand Up @@ -145,7 +146,7 @@ func newAppWithDriver(d fyne.Driver, id string) fyne.App {
store.Docs = makeStoreDocs(id, store)
newApp.storage = store

if !d.Device().IsMobile() {
if !build.IsMobile() {
newApp.settings.watchSettings()
}

Expand Down
3 changes: 2 additions & 1 deletion container/apptabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package container
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/build"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
Expand Down Expand Up @@ -348,7 +349,7 @@ func (r *appTabsRenderer) buildTabButtons(count int) *fyne.Container {
buttons := &fyne.Container{}

var iconPos buttonIconPosition
if fyne.CurrentDevice().IsMobile() {
if build.IsMobile() {
cells := count
if cells == 0 {
cells = 1
Expand Down
31 changes: 17 additions & 14 deletions container/tabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal"
"fyne.io/fyne/v2/internal/build"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
Expand Down Expand Up @@ -89,20 +90,22 @@ type baseTabs interface {
}

func tabsAdjustedLocation(l TabLocation) TabLocation {
if !build.IsMobile() {
return l
}

// Mobile has limited screen space, so don't put app tab bar on long edges
if d := fyne.CurrentDevice(); d.IsMobile() {
if o := d.Orientation(); fyne.IsVertical(o) {
if l == TabLocationLeading {
return TabLocationTop
} else if l == TabLocationTrailing {
return TabLocationBottom
}
} else {
if l == TabLocationTop {
return TabLocationLeading
} else if l == TabLocationBottom {
return TabLocationTrailing
}
if o := fyne.CurrentDevice().Orientation(); fyne.IsVertical(o) {
if l == TabLocationLeading {
return TabLocationTop
} else if l == TabLocationTrailing {
return TabLocationBottom
}
} else {
if l == TabLocationTop {
return TabLocationLeading
} else if l == TabLocationBottom {
return TabLocationTrailing
}
}

Expand Down Expand Up @@ -696,7 +699,7 @@ func (r *tabButtonRenderer) Refresh() {
r.icon.Hide()
}

if d := fyne.CurrentDevice(); r.button.onClosed != nil && (d.IsMobile() || r.button.hovered || r.close.hovered) {
if r.button.onClosed != nil && (build.IsMobile() || r.button.hovered || r.close.hovered) {
r.close.Show()
} else {
r.close.Hide()
Expand Down
4 changes: 2 additions & 2 deletions driver/software/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"image/color"
"testing"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/internal/build"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
Expand Down Expand Up @@ -45,7 +45,7 @@ func TestRenderCanvas(t *testing.T) {
c := NewCanvas()
c.SetContent(obj)

if fyne.CurrentDevice().IsMobile() {
if build.IsMobile() {
test.AssertImageMatches(t, "canvas_mobile.png", RenderCanvas(c, theme.LightTheme()))
} else {
test.AssertImageMatches(t, "canvas.png", RenderCanvas(c, theme.LightTheme()))
Expand Down
9 changes: 9 additions & 0 deletions internal/build/device_desktop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !mobile && !js && !wasm
// +build !mobile,!js,!wasm

package build

// IsMobile returns true if running on a mobile device.
func IsMobile() bool {
return false
}
19 changes: 19 additions & 0 deletions internal/build/device_goxjs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build js || wasm
// +build js wasm

package build

import (
"regexp"
"syscall/js"
)

var isMobile = regexp.MustCompile("Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile")

Check failure on line 11 in internal/build/device_goxjs.go

View workflow job for this annotation

GitHub Actions / web_tests

previous declaration

var userAgent = js.Global().Get("navigator").Get("userAgent").String()
var isMobile = isMobile.MatchString(userAgent)

// IsMobile returns true if running on a mobile device.
func IsMobile() bool {
return isMobile
}
9 changes: 9 additions & 0 deletions internal/build/device_mobile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build mobile
// +build mobile

package build

// IsMobile returns true if running on a mobile device.
func IsMobile() bool {
return true
}
5 changes: 5 additions & 0 deletions internal/driver/glfw/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"runtime"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal/build"
)

type glDevice struct {
Expand All @@ -23,3 +24,7 @@ func (*glDevice) HasKeyboard() bool {
func (*glDevice) IsBrowser() bool {
return runtime.GOARCH == "js" || runtime.GOOS == "js"
}

func (*glDevice) IsMobile() bool {
return build.IsMobile()
}
4 changes: 0 additions & 4 deletions internal/driver/glfw/device_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"fyne.io/fyne/v2"
)

func (*glDevice) IsMobile() bool {
return false
}

func (*glDevice) SystemScaleForWindow(w fyne.Window) float32 {
if runtime.GOOS == "darwin" {
return 1.0 // macOS scaling is done at the texture level
Expand Down
11 changes: 0 additions & 11 deletions internal/driver/glfw/device_goxjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@
package glfw

import (
"regexp"
"syscall/js"

"fyne.io/fyne/v2"
)

var isMobile = regexp.MustCompile("Android|BlackBerry|iPhone|iPad|iPod|Opera Mini|IEMobile")

var navigator = js.Global().Get("navigator")
var userAgent = navigator.Get("userAgent").String()
var mobileCheck = isMobile.MatchString(userAgent)

func (*glDevice) IsMobile() bool {
return mobileCheck
}

func (*glDevice) SystemScaleForWindow(w fyne.Window) float32 {
// Get the scale information from the web browser directly
return float32(js.Global().Get("devicePixelRatio").Float())
Expand Down
3 changes: 2 additions & 1 deletion internal/widget/scroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal/build"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/theme"
)
Expand Down Expand Up @@ -407,7 +408,7 @@ func (s *Scroll) DragEnd() {

// Dragged will scroll on any drag - bar or otherwise - for mobile
func (s *Scroll) Dragged(e *fyne.DragEvent) {
if !fyne.CurrentDevice().IsMobile() {
if !build.IsMobile() {
return
}

Expand Down
5 changes: 5 additions & 0 deletions test/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"runtime"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal/build"
)

type device struct {
Expand Down Expand Up @@ -31,3 +32,7 @@ func (d *device) SystemScaleForWindow(fyne.Window) float32 {
func (*device) IsBrowser() bool {
return runtime.GOARCH == "js" || runtime.GOOS == "js"
}

func (*device) IsMobile() bool {
return build.IsMobile()
}
8 changes: 0 additions & 8 deletions test/device_mobile.go

This file was deleted.

8 changes: 0 additions & 8 deletions test/device_other.go

This file was deleted.

3 changes: 2 additions & 1 deletion test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal/build"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/driver"
"fyne.io/fyne/v2/internal/painter/software"
Expand Down Expand Up @@ -179,7 +180,7 @@ func LaidOutObjects(o fyne.CanvasObject) (objects []fyne.CanvasObject) {

// MoveMouse simulates a mouse movement to the given position.
func MoveMouse(c fyne.Canvas, pos fyne.Position) {
if fyne.CurrentDevice().IsMobile() {
if build.IsMobile() {
return
}

Expand Down
5 changes: 3 additions & 2 deletions widget/button_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal/build"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
Expand Down Expand Up @@ -144,14 +145,14 @@ func TestButton_Hover(t *testing.T) {
w := test.NewWindow(b)
defer w.Close()

if !fyne.CurrentDevice().IsMobile() {
if !build.IsMobile() {
test.MoveMouse(w.Canvas(), fyne.NewPos(5, 5))
test.AssertImageMatches(t, "button/hovered.png", w.Canvas().Capture())
}

b.Importance = widget.HighImportance
b.Refresh()
if !fyne.CurrentDevice().IsMobile() {
if !build.IsMobile() {
test.AssertImageMatches(t, "button/high_importance_hovered.png", w.Canvas().Capture())

test.MoveMouse(w.Canvas(), fyne.NewPos(0, 0))
Expand Down
3 changes: 2 additions & 1 deletion widget/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal/build"
"fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/theme"
)
Expand Down Expand Up @@ -187,7 +188,7 @@ func (c *Check) MouseMoved(*desktop.MouseEvent) {

// Tapped is called when a pointer tapped event is captured and triggers any change handler
func (c *Check) Tapped(*fyne.PointEvent) {
if !c.focused && !fyne.CurrentDevice().IsMobile() {
if !c.focused && !build.IsMobile() {
impl := c.super()

if c := fyne.CurrentApp().Driver().CanvasForObject(impl); c != nil {
Expand Down
14 changes: 7 additions & 7 deletions widget/check_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/stretchr/testify/assert"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal/build"
"fyne.io/fyne/v2/test"
"fyne.io/fyne/v2/theme"
)
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestCheck_Focused(t *testing.T) {
assert.Equal(t, color.Transparent, render.focusIndicator.FillColor)

test.Tap(check)
if fyne.CurrentDevice().IsMobile() {
if build.IsMobile() {
assert.False(t, check.focused)
} else {
assert.True(t, check.focused)
Expand All @@ -151,7 +151,7 @@ func TestCheck_Focused(t *testing.T) {
assert.Equal(t, color.Transparent, render.focusIndicator.FillColor)

check.Enable()
if fyne.CurrentDevice().IsMobile() {
if build.IsMobile() {
assert.False(t, check.focused)
} else {
assert.True(t, check.focused)
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestCheck_Hovered(t *testing.T) {

test.Tap(check)
assert.True(t, check.hovered)
if !fyne.CurrentDevice().IsMobile() {
if !build.IsMobile() {
assert.Equal(t, theme.FocusColor(), render.focusIndicator.FillColor)
}

Expand All @@ -191,15 +191,15 @@ func TestCheck_Hovered(t *testing.T) {

check.Enable()
assert.True(t, check.hovered)
if fyne.CurrentDevice().IsMobile() {
if build.IsMobile() {
assert.Equal(t, theme.HoverColor(), render.focusIndicator.FillColor)
} else {
assert.Equal(t, theme.FocusColor(), render.focusIndicator.FillColor)
}

check.MouseOut()
assert.False(t, check.hovered)
if fyne.CurrentDevice().IsMobile() {
if build.IsMobile() {
assert.Equal(t, color.Transparent, render.focusIndicator.FillColor)
} else {
assert.Equal(t, theme.FocusColor(), render.focusIndicator.FillColor)
Expand All @@ -217,7 +217,7 @@ func TestCheck_TypedRune(t *testing.T) {
assert.False(t, check.Checked)

test.Tap(check)
if !fyne.CurrentDevice().IsMobile() {
if !build.IsMobile() {
assert.True(t, check.focused)
}
assert.True(t, check.Checked)
Expand Down
3 changes: 2 additions & 1 deletion widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/driver/mobile"
"fyne.io/fyne/v2/internal/build"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/widget"
"fyne.io/fyne/v2/theme"
Expand Down Expand Up @@ -537,7 +538,7 @@ func (e *Entry) Append(text string) {
//
// Implements: fyne.Tappable
func (e *Entry) Tapped(ev *fyne.PointEvent) {
if fyne.CurrentDevice().IsMobile() && e.selecting {
if build.IsMobile() && e.selecting {
e.selecting = false
}
}
Expand Down
Loading

0 comments on commit 1e86292

Please sign in to comment.