
準備好為您的 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中文網其他相關文章!