progrium/darwinkit

menuitem .SetTitle() open/closes systray every time its called

Closed this issue · 7 comments

Im in the process of moving over some code that's using https://github.com/getlantern/systray to macdriver. I have hit a weird issue where every call to menuitem .SetTitle() open/closes systray. When using https://github.com/getlantern/systray im able to set the item title as many time as i want without hitting this issue.

A simple bit of code that reproduces the issue. Run the code and then click on the systray icon, you will see the systray open and close every second.

go 1.16rc1

`package main

import (
"runtime"
"strconv"
"time"

"github.com/progrium/macdriver/cocoa"
"github.com/progrium/macdriver/objc"

)

func Run() {
runtime.LockOSThread()

app := cocoa.NSApp_WithDidLaunch(func(n objc.Object) {
	obj := cocoa.NSStatusBar_System().StatusItemWithLength(cocoa.NSVariableStatusItemLength)
	obj.Retain()
	obj.Button().SetTitle("counting")

	itemNext := cocoa.NSMenuItem_New()

	go func() {
		timer := 1
		for {
			select {
			case <-time.After(1 * time.Second):
				itemNext.SetTitle(strconv.Itoa(timer))
				timer++
			}
		}
	}()
	menu := cocoa.NSMenu_New()
	menu.AddItem(itemNext)
	obj.SetMenu(menu)

})
app.Run()

}`

Fascinating. Anybody else able to reproduce or know what's going on? I have some vague ideas but I can't explore just yet

Can you point we in the direction you think might be wrong and i can try and have a poke around.

To start I need to reproduce and see it for myself. Maybe you can include a video?

Hey @AStrangwood try wrapping the itemNext.SetTitle(strconv.Itoa(timer)) into a core.Dispatch

Here is your code fixed: https://gist.github.com/flexzuu/f119fdfaca9515f63d1c786b2efe35db

@progrium I will try to fix that in the example too

@flexzuu Nice thinking! I remember having this exact same issue years back, it's a reliable demo of how non-main-thread UI updates won't work.

@flexzuu Yup that did it. Thanks :)

@flexzuu Thanks for saving me lots of time.