Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add icons for status bar items #28

Merged
merged 6 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions cocoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,30 @@ func (app *App) SetMenu(menus []Menu) {
C.NSApplication_SetMainMenu(root)
}

func (app *App) AddStatusItem(width int, title string, highlight bool, entries ...MenuEntry) {
type StatusItemOptions struct {
Image *Image // image to show in the status bar, must be non-nil
Width float64 // width of item in pixels (zero means automatic size)
Highlight bool // whether to highlight the item when clicked
Menu []MenuEntry // the menu to display when the item is clicked
}

func (app *App) AddStatusItem(opts StatusItemOptions) {
if menuMgr == nil {
menuMgr = newMenuManager()
}
if opts.Image == nil {
panic("status item image must not be nil")
}

root := C.NSMenu_New(C.CString("<statusbar>"))
for _, m := range entries {
menuMgr.add(m, root)
menu := C.NSMenu_New(C.CString("<statusbar>"))
for _, m := range opts.Menu {
menuMgr.add(m, menu)
}
C.NSStatusBar_AddItem(C.int(width), C.CString(title), C.bool(highlight), root)
C.NSStatusBar_AddItem(
opts.Image.c,
C.float(opts.Width),
C.bool(opts.Highlight),
menu)
}

// Image holds a handle to a platform-specific image structure. On OSX it is NSImage.
Expand All @@ -168,11 +182,7 @@ func ImageFromPNG(buf []byte) (*Image, error) {
return &Image{cimg}, nil
}

// ImageToPNG writes an image to the given file
func ImageToPNG(image *Image, path string) {
C.NSImage_WriteToFile(image.c, C.CString(path))
}

// Notification represents a desktop notification
type Notification struct {
Title string
Subtitle string
Expand Down
2 changes: 1 addition & 1 deletion dist/Gallium.framework/Versions/A/Gallium
Git LFS file not shown
Binary file modified dist/Gallium.framework/Versions/A/Resources/MainMenu.nib
Binary file not shown.
4 changes: 2 additions & 2 deletions dist/include/gallium/cocoa.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ GALLIUM_EXPORT void NSMenuItem_SetSubmenu(
gallium_nsmenu_t* submenu);

GALLIUM_EXPORT void NSStatusBar_AddItem(
int width,
const char* title,
gallium_nsimage_t* image,
float width,
bool highlightMode,
gallium_nsmenu_t* menu);

Expand Down
25 changes: 0 additions & 25 deletions examples/image/image.go

This file was deleted.

13 changes: 0 additions & 13 deletions examples/simple/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,4 @@ func OnReady(app *gallium.App) {
},
},
})
app.AddStatusItem(
20,
"demo",
true,
gallium.MenuItem{
Title: "Do something",
OnClick: handleDoSomething,
},
gallium.MenuItem{
Title: "Do something else",
OnClick: handleDoSomethingElse,
},
)
}
235 changes: 235 additions & 0 deletions examples/statusbar/bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added examples/statusbar/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions examples/statusbar/statusbar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//go:generate go-bindata -o bindata.go icon.png

package main

import (
"fmt"
"log"
"os"
"runtime"

"github.com/alexflint/gallium"
)

func main() {
runtime.LockOSThread()
gallium.RedirectStdoutStderr(os.ExpandEnv("$HOME/Library/Logs/Gallium.log"))
gallium.Loop(os.Args, onReady)
}

func handleMenuQuit() {
log.Println("quit clicked")
os.Exit(0)
}

func handleDoSomething() {
log.Println("do something")
}

func handleDoSomethingElse() {
log.Println("do something else")
}

func onReady(app *gallium.App) {
img, err := gallium.ImageFromPNG(MustAsset("icon.png"))
if err != nil {
fmt.Println("unable to decode icon-gray.png:", err)
os.Exit(1)
}

app.AddStatusItem(gallium.StatusItemOptions{
Image: img,
Width: 27.5,
Highlight: true,
Menu: []gallium.MenuEntry{
gallium.MenuItem{Title: "Do something", OnClick: handleDoSomething},
gallium.MenuItem{Title: "Do something else", OnClick: handleDoSomethingElse},
},
})
}