defensive nil checks around CanvasForObject

This commit is contained in:
Drew Weymouth
2026-03-01 08:24:16 -08:00
parent 12b5d33bbe
commit afb1c6c397
8 changed files with 30 additions and 13 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ func (b *BrowsingPane) doSetPage(p Page) bool {
}
// TODO: reset focus only if something inside the previous page had focus
if c := fyne.CurrentApp().Driver().CanvasForObject(b); c != nil {
c.Focus(nil)
c.Unfocus()
}
b.curPage = p
if np, ok := p.(CanShowNowPlaying); ok {
+3 -1
View File
@@ -392,7 +392,9 @@ func NewPlaylistPageHeader(page *PlaylistPage) *PlaylistPageHeader {
}
buttonRow.Objects[3] = searchEntry
searchEntry.SetMinWidth(minW)
fyne.CurrentApp().Driver().CanvasForObject(a).Focus(searchEntry)
if c := fyne.CurrentApp().Driver().CanvasForObject(a); c != nil {
c.Focus(searchEntry)
}
fyne.NewAnimation(myTheme.AnimationDurationShort, func(f float32) {
w := (200-minW)*f + minW
searchEntry.SetMinWidth(w)
+3 -1
View File
@@ -124,7 +124,9 @@ func NewVolumeSlider(width float32) *volumeSlider {
func (v *volumeSlider) Tapped(e *fyne.PointEvent) {
v.Slider.Tapped(e)
fyne.CurrentApp().Driver().CanvasForObject(v).Unfocus()
if c := fyne.CurrentApp().Driver().CanvasForObject(v); c != nil {
c.Unfocus()
}
}
func (v *volumeSlider) MinSize() fyne.Size {
+4 -3
View File
@@ -67,7 +67,9 @@ func (g *FocusList) FocusNeighbor(curItem widget.ListItemID, up bool) {
other := g.ItemForID(focusIdx)
g.mutex.Unlock()
if other != nil {
fyne.CurrentApp().Driver().CanvasForObject(g).Focus(other.(fyne.Focusable))
if c := fyne.CurrentApp().Driver().CanvasForObject(g); c != nil {
c.Focus(other.(fyne.Focusable))
}
}
}
@@ -120,8 +122,7 @@ func (l *FocusListRowBase) SetItemID(id widget.ListItemID) {
func (l *FocusListRowBase) EnsureUnfocused() {
if l.Focused {
c := fyne.CurrentApp().Driver().CanvasForObject(l)
if c != nil {
if c := fyne.CurrentApp().Driver().CanvasForObject(l); c != nil {
c.Unfocus()
}
}
+6 -2
View File
@@ -286,7 +286,9 @@ func (g *GridView) Resize(size fyne.Size) {
var _ fyne.Tappable = (*GridView)(nil)
func (g *GridView) Tapped(*fyne.PointEvent) {
fyne.CurrentApp().Driver().CanvasForObject(g).Unfocus()
if c := fyne.CurrentApp().Driver().CanvasForObject(g); c != nil {
c.Unfocus()
}
}
func (g *GridView) createGridWrap() {
@@ -343,7 +345,9 @@ func (g *GridView) createNewItemCard() fyne.CanvasObject {
g.grid.ScrollTo(focusIndex)
g.stateMutex.RLock()
if item, ok := g.itemForIndex[focusIndex]; ok {
fyne.CurrentApp().Driver().CanvasForObject(g).Focus(item)
if c := fyne.CurrentApp().Driver().CanvasForObject(g); c != nil {
c.Focus(item)
}
}
g.stateMutex.RUnlock()
}
+7 -3
View File
@@ -513,8 +513,10 @@ func (g *GridViewItem) Update(model *GridViewItemModel) {
g.secondaryText.Refresh()
g.Cover.ResetPlayButton()
if g.focused {
fyne.CurrentApp().Driver().CanvasForObject(g).Focus(nil)
g.FocusLost()
if c := fyne.CurrentApp().Driver().CanvasForObject(g); c != nil {
c.Unfocus()
g.FocusLost()
}
}
}
@@ -589,7 +591,9 @@ func (g *GridViewItem) TypedRune(rune) {
var _ fyne.Tappable = (*GridViewItem)(nil)
func (g *GridViewItem) Tapped(*fyne.PointEvent) {
fyne.CurrentApp().Driver().CanvasForObject(g).Unfocus()
if c := fyne.CurrentApp().Driver().CanvasForObject(g); c != nil {
c.Unfocus()
}
}
func (g *GridViewItem) CreateRenderer() fyne.WidgetRenderer {
+3 -1
View File
@@ -50,7 +50,9 @@ func (t *TrackPosSlider) Tapped(e *fyne.PointEvent) {
t.Slider.Tapped(e)
// don't keep focus after being tapped
fyne.CurrentApp().Driver().CanvasForObject(t).Focus(nil)
if c := fyne.CurrentApp().Driver().CanvasForObject(t); c != nil {
c.Unfocus()
}
}
// override to increase the distance moved by keyboard control
+3 -1
View File
@@ -50,7 +50,9 @@ func (sf *SearchEntry) Init() {
func (s *SearchEntry) TypedKey(e *fyne.KeyEvent) {
if e.Name == fyne.KeyEscape {
s.SetText("")
fyne.CurrentApp().Driver().CanvasForObject(s).Unfocus()
if c := fyne.CurrentApp().Driver().CanvasForObject(s); c != nil {
c.Unfocus()
}
return
}
s.Entry.TypedKey(e)