RidgeChat/ui/user_interface.go

70 lines
1.8 KiB
Go
Raw Permalink Normal View History

2023-09-06 13:12:35 -06:00
package ui
import (
"encoding/json"
"log"
"net/http"
"github.com/webview/webview_go"
"RidgedChat/common"
2023-09-06 13:12:35 -06:00
)
type UIConfig struct {
OfflineMode bool
HeaderVisible bool
SidebarVisible bool
MainChatVisible bool
UserTextEntryVisible bool
SendButtonVisible bool
}
// StartGUI initializes and starts the WebView-based GUI for the chat client
func StartGUI(config UIConfig) {
// Start local HTTP server
go func() {
// Serve static files
fs := http.FileServer(http.Dir("./ui/gui_files"))
http.Handle("/", http.StripPrefix("/", fs))
// API endpoints
http.HandleFunc("/api/sendMessage", sendMessageHandler)
http.HandleFunc("/api/receiveMessage", receiveMessageHandler)
if err := http.ListenAndServe(":9876", nil); err != nil {
log.Fatal("Failed to start server:", err)
}
}()
// Create and configure WebView window
debug := true
w := webview.New(debug)
defer w.Destroy()
w.SetTitle("RidgeChat - The World's most Ridged Self-Hosted Chat Program")
w.SetSize(800, 600, webview.HintNone)
w.Navigate("http://localhost:9876/html/index.html")
// Run the WebView window
w.Run()
}
// sendMessageHandler handles sending messages
func sendMessageHandler(w http.ResponseWriter, r *http.Request) {
var msg common.Message
if err := json.NewDecoder(r.Body).Decode(&msg); err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
// Your logic for sending messages goes here
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"status": "Message sent"})
}
// receiveMessageHandler handles receiving messages
func receiveMessageHandler(w http.ResponseWriter, r *http.Request) {
// Your logic for receiving messages goes here
receivedMsg := common.Message{Username: "Server", Content: "Hello, client!"} // Fixed here
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(receivedMsg)
2023-09-06 13:12:35 -06:00
}