diff options
| author | joesis | 2017-08-28 15:02:20 +0800 | 
|---|---|---|
| committer | GitHub | 2017-08-28 15:02:20 +0800 | 
| commit | e78cfcfa229acdd9af7489540c20419ee5cb05a8 (patch) | |
| tree | 7bb33595a16c1a57b4454f014b48fc08e1ec2406 /systray.go | |
| parent | ef254071ef3abfdf0a220f76fcdd468d24bff519 (diff) | |
| parent | 1698df20cadf507689bd091174b466e7c6c6876e (diff) | |
| download | systray-e78cfcfa229acdd9af7489540c20419ee5cb05a8.tar.bz2 | |
Merge pull request #28 from getlantern/flashlight-258-ox
Made onExit run reliably and process terminate on quit
Diffstat (limited to 'systray.go')
| -rw-r--r-- | systray.go | 42 | 
1 files changed, 22 insertions, 20 deletions
| @@ -36,8 +36,8 @@ type MenuItem struct {  var (  	log = golog.LoggerFor("systray") -	readyCh       = make(chan struct{}) -	exitCh        = make(chan struct{}) +	systrayReady  func() +	systrayExit   func()  	menuItems     = make(map[int32]*MenuItem)  	menuItemsLock sync.RWMutex @@ -50,17 +50,27 @@ var (  // Should be called at the very beginning of main() to lock at main thread.  func Run(onReady func(), onExit func()) {  	runtime.LockOSThread() -	go func() { -		for { -			select { -			case <-readyCh: -				onReady() -			case <-exitCh: -				onExit() -				return -			} + +	if onReady == nil { +		systrayReady = func() {} +	} else { +		// Run onReady on separate goroutine to avoid blocking event loop +		readyCh := make(chan interface{}) +		go func() { +			<-readyCh +			onReady() +		}() +		systrayReady = func() { +			close(readyCh)  		} -	}() +	} + +	// unlike onReady, onExit runs in the event loop to make sure it has time to +	// finish before the process terminates +	if onExit == nil { +		onExit = func() {} +	} +	systrayExit = onExit  	nativeLoop()  } @@ -136,14 +146,6 @@ func (item *MenuItem) update() {  	addOrUpdateMenuItem(item)  } -func systrayReady() { -	readyCh <- struct{}{} -} - -func systrayExit() { -	exitCh <- struct{}{} -} -  func systrayMenuItemSelected(id int32) {  	menuItemsLock.RLock()  	item := menuItems[id] | 
