准备工作

  1. 打开 https://github.com/getlantern/systray 并git clone,我们工作就是在它给出的example中进行修改。
  2. 网上下载一张以太坊的Logo(PNG格式的)

将前面下载的systray中example\icon\make_icon.sh也copy到icon目录下。在vscode里打开终端,切换到该目录并运行sh ./make_icon.sh logo.png,它会在当前目录下生成一个iconunix.go的文件,这就是我们小工具所需要的logo。下面是make_icon.sh里的代码:

#/bin/sh

if [ -z "$GOPATH" ]; then
    echo GOPATH environment variable not set
    exit
fi

if [ ! -e "$GOPATH/bin/2goarray" ]; then
    echo "Installing 2goarray..."
    go get github.com/cratonica/2goarray
    if [ $? -ne 0 ]; then
        echo Failure executing go get github.com/cratonica/2goarray
        exit
    fi
fi

if [ -z "$1" ]; then
    echo Please specify a PNG file
    exit
fi

if [ ! -f "$1" ]; then
    echo $1 is not a valid file
    exit
fi    

OUTPUT=iconunix.go
echo Generating $OUTPUT
echo "//+build linux darwin" > $OUTPUT
echo >> $OUTPUT
cat "$1" | $GOPATH/bin/2goarray Data icon >> $OUTPUT
if [ $? -ne 0 ]; then
    echo Failure generating $OUTPUT
    exit
fi
echo Finished

可以看到,它安装了一个2goarray库并且将转换后的数据输出到iconunix.go中,变量名为Data,包名为icon。这里也可以自己修改为不同的名字。

编写主程序

参照systray中的示例,我们在main.go中写入如下代码:

package main

import (
	"encoding/json"
	"ethprice/icon"
	"fmt"
	"net/http"
	"time"

	"github.com/getlantern/systray"
)

const URL = "https://api.pro.coinbase.com/products/ETH-USD/ticker"

type EthPrice struct {
	Price string `json:price`
}
type Callback func(s string)

var priceInfo = new(EthPrice)
var t *time.Ticker

var step = 15

func backgroundTask(cb Callback) {
	t = time.NewTicker(time.Duration(step) * time.Second)
	for range t.C {
		cb(fetchPrice())
	}
}

func fetchPrice() string {
	resp, err := http.Get(URL)
	if err != nil {
		return "-"
	}
	defer resp.Body.Close()
	json.NewDecoder(resp.Body).Decode(priceInfo)
	return fmt.Sprintf("ETH $%s(每%d秒)", priceInfo.Price, step)
}

func main() {
	systray.Run(onReady, onExit)
}

func onReady() {
	go backgroundTask(systray.SetTitle)
	systray.SetIcon(icon.Data)
	systray.SetTitle(fmt.Sprintf("ETH $%s(每%d秒)", priceInfo.Price, step))
	systray.SetTooltip("定时刷新ETH价格")
	oneMinite := systray.AddMenuItem("每分钟", "每分钟刷新一次")
	halfMinite := systray.AddMenuItem("每半分钟", "每30秒刷新一次")
	quartor := systray.AddMenuItem("每15秒", "每15秒刷新一次")
	mQuit := systray.AddMenuItem("退出", "退出程序")
	systray.SetTitle(fetchPrice())

	go func() {
		for {
			select {
			case <-mQuit.ClickedCh:
				systray.Quit()
				return
			case <-oneMinite.ClickedCh:
				if step != 60 {
					step = 60
					systray.SetTitle(fmt.Sprintf("ETH $%s(每%d秒)", priceInfo.Price, step))
					t = time.NewTicker(time.Duration(step) * time.Second)
				}

			case <-halfMinite.ClickedCh:
				if step != 30 {
					step = 30
					systray.SetTitle(fmt.Sprintf("ETH $%s(每%d秒)", priceInfo.Price, step))
					t = time.NewTicker(time.Duration(step) * time.Second)
				}

			case <-quartor.ClickedCh:
				if step != 15 {
					step = 15
					systray.SetTitle(fmt.Sprintf("ETH $%s(每%d秒)", priceInfo.Price, step))
					t = time.NewTicker(time.Duration(step) * time.Second)
				}
			}
		}
	}()

}

// clean up here
func onExit() {
	t.Stop()
}

这里icon包位于本地包(目录)ethprice之下,本地包就是go mod init时定义的包。由于是初学者,代码写的不完善,还有一些需要优化的地方,见谅。

编译运行

切换到项目根目录并运行go build,会在根目录下生成ethprice可执行文件