GithubHelp home page GithubHelp logo

misitebao / go-systray Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rb-go/go-systray

0.0 2.0 0.0 1.34 MB

[WIP!] a cross platfrom Go library to work with the notification area / system tray

License: Apache License 2.0

Go 84.60% C 6.51% Objective-C 8.69% Makefile 0.20%

go-systray's Introduction

license GoDoc Coverage Status Build Status Go Report Card Release

WIP! WORK IN PROGRESS! Can be unstable!

I tries to fix multilevel menu on windows. Currently work only 2 levels.

About

Package systray is a cross platfrom Go library to place an icon and menu in the notification area. Tested on Windows 8, 10, Mac OSX, Ubuntu 14.10 and Debian 7.6.

Difference with getlantern/systray

  • [All OS] multi-level systray menu (tested only on windows)
  • [Windows] system tray code optimizations (constants)
  • [Windows] left click custom handling
  • [Windows] right click custom handling
  • [Windows] left double click custom handling

Usage

package main

import (
	"fmt"
	"io/ioutil"
	"strconv"
	"time"

	"github.com/riftbit/go-systray"
)

var (
	timezone string
)

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

func onReady() {
	timezone = "Local"
	systray.SetIcon(getIcon("assets/icon.ico"))

	submenu := systray.AddSubMenu("SubMenu")
	_ = submenu.AddSubMenuItem("Start", "", 0)
	_ = submenu.AddSubMenuItem("Stop", "", 0)

	localTime := systray.AddMenuItem("Local time", "Local time", 0)
	hcmcTime := systray.AddMenuItem("Ho Chi Minh time", "Asia/Ho_Chi_Minh", 0)
	sydTime := systray.AddMenuItem("Sydney time", "Australia/Sydney", 0)
	gdlTime := systray.AddMenuItem("Guadalajara time", "America/Mexico_City", 0)
	sfTime := systray.AddMenuItem("San Fransisco time", "America/Los_Angeles", 0)

	systray.AddSeparator()
	mQuit := systray.AddMenuItem("Quit", "Quits this app", 0)

	go func() {
		for {
			systray.SetTitle(getClockTime(timezone))
			systray.SetTooltip(getClockTime(timezone) + " - " + timezone + " timezone")
			time.Sleep(1 * time.Second)
		}
	}()

	go func() {
		for {
			select {
			case <-localTime.OnClickCh():
				timezone = "Local"
			case <-hcmcTime.OnClickCh():
				timezone = "Asia/Ho_Chi_Minh"
			case <-sydTime.OnClickCh():
				timezone = "Australia/Sydney"
			case <-gdlTime.OnClickCh():
				timezone = "America/Mexico_City"
			case <-sfTime.OnClickCh():
				timezone = "America/Los_Angeles"
			case <-mQuit.OnClickCh():
				systray.Quit()
				return
			}
		}
	}()
}

func onExit() {
	// Cleaning stuff here.
}

func getClockTime(tz string) string {
	t := time.Now()
	utc, _ := time.LoadLocation(tz)

	hour, min, sec := t.In(utc).Clock()
	return ItoaTwoDigits(hour) + ":" + ItoaTwoDigits(min) + ":" + ItoaTwoDigits(sec)
}

// ItoaTwoDigits time.Clock returns one digit on values, so we make sure to convert to two digits
func ItoaTwoDigits(i int) string {
	b := "0" + strconv.Itoa(i)
	return b[len(b)-2:]
}

func getIcon(s string) []byte {
	b, err := ioutil.ReadFile(s)
	if err != nil {
		fmt.Print(err)
	}
	return b
}

Menu item can be checked and / or disabled. Methods except Run() can be invoked from any goroutine. See demo code under example folder.

Platform specific concerns

Linux

sudo apt-get install libgtk-3-dev libappindicator3-dev

Checked menu item not implemented on Linux yet.

Try

Under example folder. Place tray icon under icon, and use make_icon.bat or make_icon.sh, whichever suit for your os, to convert the icon to byte array. Your icon should be .ico file under Windows, whereas .ico, .jpg and .png is supported on other platform.

go get
go run main.go

Building and the Console Window

By default, the binary created by go build will cause a console window to be opened on both Windows and macOS when run.

Windows

To prevent launching a console window when running on Windows, add these command-line build flags:

go build -ldflags -H=windowsgui

macOS

On macOS, you will need to create an application bundle to wrap the binary; simply folders with the following minimal structure and assets:

SystrayApp.app/
  Contents/
    Info.plist
    MacOS/
      go-executable
    Resources/
      SystrayApp.icns

Consult the Official Apple Documentation here.

Credits

Additional interesting packages

go-systray's People

Contributors

ergoz avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.