
准备好为您的 Mac 构建一个很酷的市政厅时钟应用程序了吗?伟大的!我们将创建一个位于菜单栏中的应用程序,每 15 分钟发出一次提示音,甚至可以计算时间。让我们一步步分解,我将解释代码的每一部分,以便您能够理解发生了什么。
我们的市政厅时钟应用程序将:
首先,让我们设置我们的项目:
mkdir CityHallClock cd CityHallClock
go mod init cityhallclock
go get github.com/getlantern/systray go get github.com/faiface/beep
现在,让我们创建 main.go 文件并浏览每个函数:
package main
import (
"bytes"
"log"
"os"
"path/filepath"
"time"
"github.com/faiface/beep"
"github.com/faiface/beep/mp3"
"github.com/faiface/beep/speaker"
"github.com/getlantern/systray"
)
var (
audioBuffer *beep.Buffer
)
func main() {
initAudio()
systray.Run(onReady, onExit)
}
// ... (other functions will go here)
让我们分解一下每个功能:
func main() {
initAudio()
systray.Run(onReady, onExit)
}
这是我们的应用程序启动的地方。它做了两件重要的事情:
func initAudio() {
execPath, err := os.Executable()
if err != nil {
log.Fatal(err)
}
resourcesPath := filepath.Join(filepath.Dir(execPath), "..", "Resources")
chimeFile := filepath.Join(resourcesPath, "chime.mp3")
f, err := os.Open(chimeFile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
streamer, format, err := mp3.Decode(f)
if err != nil {
log.Fatal(err)
}
defer streamer.Close()
audioBuffer = beep.NewBuffer(format)
audioBuffer.Append(streamer)
err = speaker.Init(format.SampleRate, format.SampleRate.N(time.Second/10))
if err != nil {
log.Fatal(err)
}
}
此函数设置我们的音频:
如果出现任何问题(例如找不到声音文件),它会记录错误并退出。
func onReady() {
systray.SetIcon(getIcon())
systray.SetTitle("City Hall Clock")
systray.SetTooltip("City Hall Clock")
mQuit := systray.AddMenuItem("Quit", "Quit the app")
go func() {
<-mQuit.ClickedCh
systray.Quit()
}()
go runClock()
}
此函数设置我们的菜单栏图标:
func onExit() {
// Cleanup tasks go here
}
当应用程序退出时调用此函数。我们在这里不做任何事情,但您可以根据需要添加清理任务。
func runClock() {
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case t := <-ticker.C:
if t.Minute() == 0 || t.Minute() == 15 || t.Minute() == 30 || t.Minute() == 45 {
go chime(t)
}
}
}
}
这是我们时钟的“心脏”:
func chime(t time.Time) {
hour := t.Hour()
minute := t.Minute()
var chimeTimes int
if minute == 0 {
chimeTimes = hour % 12
if chimeTimes == 0 {
chimeTimes = 12
}
} else {
chimeTimes = 1
}
for i := 0; i < chimeTimes; i++ {
streamer := audioBuffer.Streamer(0, audioBuffer.Len())
speaker.Play(streamer)
time.Sleep(time.Duration(audioBuffer.Len()) * time.Second / time.Duration(audioBuffer.Format().SampleRate))
if i < chimeTimes-1 {
time.Sleep(500 * time.Millisecond) // Wait between chimes
}
}
}
此功能播放我们的铃声:
func getIcon() []byte {
execPath, err := os.Executable()
if err != nil {
log.Fatal(err)
}
iconPath := filepath.Join(filepath.Dir(execPath), "..", "Resources", "icon.png")
// Read the icon file
icon, err := os.ReadFile(iconPath)
if err != nil {
log.Fatal(err)
}
return icon
}
此函数获取我们的菜单栏图标:
为了使我们的应用程序成为真正的 macOS 公民,我们需要创建一个应用程序包。这涉及创建一个 Info.plist 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>CityHallClock</string>
<key>CFBundleIconFile</key>
<string>AppIcon</string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.cityhallclock</string>
<key>CFBundleName</key>
<string>City Hall Clock</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>10.12</string>
<key>LSUIElement</key>
<true/>
<key>NSHighResolutionCapable</key>
<true/>
</dict>
</plist>
将其保存为项目目录中的 Info.plist。
我们需要两个图标:
让我们创建一个构建脚本(build.sh):
#!/bin/bash # Build the Go application go build -o CityHallClock # Create the app bundle structure mkdir -p CityHallClock.app/Contents/MacOS mkdir -p CityHallClock.app/Contents/Resources # Move the executable to the app bundle mv CityHallClock CityHallClock.app/Contents/MacOS/ # Copy the Info.plist cp Info.plist CityHallClock.app/Contents/ # Copy the chime sound to Resources cp chime.mp3 CityHallClock.app/Contents/Resources/ # Copy the menu bar icon cp icon.png CityHallClock.app/Contents/Resources/ # Copy the application icon cp AppIcon.icns CityHallClock.app/Contents/Resources/ echo "Application bundle created: CityHallClock.app"
使用 chmod +x build.sh 使其可执行,然后使用 ./build.sh 运行它。
就是这样!您已经为 macOS 构建了功能齐全的市政厅时钟应用程序。您已了解:
请随意扩展这一点。也许添加自定义铃声或不同铃声间隔的首选项。天空才是极限!
您可以在这里找到完整的源代码 https://github.com/rezmoss/citychime
快乐编码,享受你的新时钟!
以上是为 macOS 构建市政厅时钟应用程序:综合指南的详细内容。更多信息请关注PHP中文网其他相关文章!