mirror of
https://github.com/Airr/nim-cocoa.git
synced 2024-11-24 09:45:30 +00:00
Uploaded NSStatusBar example
This commit is contained in:
parent
b00a02b22e
commit
aa29344f72
BIN
examples/systray/img/icon.png
Normal file
BIN
examples/systray/img/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 457 B |
50
examples/systray/systray.nim
Normal file
50
examples/systray/systray.nim
Normal file
@ -0,0 +1,50 @@
|
||||
# NSStatusBar example
|
||||
|
||||
import Cocoa, os, dnsclient, net
|
||||
from nativesockets import getHostName
|
||||
|
||||
proc externalIP(): string
|
||||
proc getActiveIP(): string
|
||||
proc getComputerName(): string
|
||||
|
||||
proc menuCallback(sender: ID) {.cdecl.} =
|
||||
case sender.tag:
|
||||
of 1001: echo getComputerName()
|
||||
of 1002: echo getActiveIP()
|
||||
of 1003: echo externalIP()
|
||||
of -1:
|
||||
echo "bye."
|
||||
Cocoa_Quit(sender)
|
||||
else: discard
|
||||
|
||||
proc externalIP(): string =
|
||||
let client = newDNSClient("resolver1.opendns.com",53)
|
||||
let resp = client.sendQuery("myip.opendns.com", A)
|
||||
result = "External IP: " & resp.answers[0].toString
|
||||
|
||||
proc getActiveIP(): string =
|
||||
var ip:string
|
||||
var s = newSocket(Domain.AF_INET, SockType.SOCK_DGRAM, Protocol.IPPROTO_UDP)
|
||||
s.connect("1.0.0.0",Port(1))
|
||||
ip = s.getLocalAddr[0]
|
||||
s.close()
|
||||
return "IP Address: " & ip
|
||||
|
||||
proc getComputerName(): string =
|
||||
result = "Computer Name: " & getHostName()
|
||||
|
||||
Status_Init()
|
||||
|
||||
var menu = newStatusMenu()
|
||||
var statusBar = newStatusBar(getAppDir() & "/img/icon.png")
|
||||
|
||||
newStatusMenuItem(menu, 1001, getComputerName(), menuCallback)
|
||||
newStatusMenuItem(menu, 1002, getActiveIP(), menuCallback)
|
||||
newStatusMenuItem(menu, 1003, externalIP(), menuCallback)
|
||||
menu.separator()
|
||||
newStatusMenuItem(menu, -1, "Quit", menuCallback)
|
||||
|
||||
|
||||
statusBar.menu = menu
|
||||
|
||||
Cocoa_Run(menu)
|
Loading…
Reference in New Issue
Block a user