Skip to content

Commit

Permalink
feat: focus on second opening
Browse files Browse the repository at this point in the history
  • Loading branch information
BigJk committed Sep 21, 2024
1 parent 9e487fc commit ff8b4ea
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
33 changes: 25 additions & 8 deletions cmd/app/electron.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"io/ioutil"
stdlog "log"
"net/http"
"os"
"os/exec"
"os/signal"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/BigJk/snd/printing/preview"
"github.com/BigJk/snd/rendering"
"github.com/BigJk/snd/server"
"github.com/labstack/echo/v4"

"github.com/asticode/go-astikit"
"github.com/asticode/go-astilectron"
Expand All @@ -33,11 +35,28 @@ var astilectronVersion = "0.0.1"

var prev preview.Preview

var mainWindow *astilectron.Window

// This will change the starting routine so that an additional Electron window
// will open with the frontend in it.
func init() {
startFunc = startElectron
serverOptions = append(serverOptions, server.WithPrinter(&prev))
onAlreadyRunning = func() {
fmt.Println("ERROR: Sales & Dungeons is already running!")

// Send GET request to the server to focus the Electron window
if _, err := http.Get("http://127.0.0.1:7123/api/focusElectron"); err != nil {
fmt.Println("ERROR: could not focus Electron window:", err)
}
}
serverOptions = append(serverOptions, server.WithPrinter(&prev), server.WithAdditionalRoutes(func(api *echo.Group) {
api.GET("/focusElectron", func(c echo.Context) error {
if mainWindow != nil {
go mainWindow.Show()
}
return c.NoContent(http.StatusOK)
})
}))
}

func startElectron(db database.Database, debug bool) {
Expand Down Expand Up @@ -83,14 +102,10 @@ contextBridge.exposeInMainWorld("api", {
}

if isMacAppBundle() {
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
opts.BaseDirectoryPath = filepath.Join(home, "/Documents/Sales & Dungeons/electron")
opts.DataDirectoryPath = filepath.Join(home, "/Documents/Sales & Dungeons/electron")
opts.BaseDirectoryPath = filepath.Join(sndDataDir, "/electron")
opts.DataDirectoryPath = filepath.Join(sndDataDir, "/electron")

err = os.MkdirAll(opts.BaseDirectoryPath, 0777)
err := os.MkdirAll(opts.BaseDirectoryPath, 0777)
log.Info("Changed electron provision folder because of app bundle", log.WithValue("path", opts.BaseDirectoryPath), log.WithValue("mkdir error", err))

// copy icon files to the electron folder
Expand Down Expand Up @@ -133,6 +148,8 @@ contextBridge.exposeInMainWorld("api", {
_ = w.OpenDevTools()
}

mainWindow = w

// Wait for interrupt signal to trigger shutdown of application.
go func() {
quit := make(chan os.Signal, 1)
Expand Down
16 changes: 15 additions & 1 deletion cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (

var serverOptions []server.Option
var startFunc = startServer
var onAlreadyRunning = func() {
fmt.Println("ERROR: Sales & Dungeons is already running!")
}

func syncBaseUrl() string {
override := os.Getenv("SND_SYNC_BASE_URL")
Expand Down Expand Up @@ -70,6 +73,10 @@ func openDatabase() database.Database {

db, err := badger.New(userdata)
if err != nil {
if strings.Contains(err.Error(), "Another process is using this Badger database") {
onAlreadyRunning()
os.Exit(0)
}
panic(err)
}

Expand All @@ -92,7 +99,14 @@ func openDatabase() database.Database {
func startServer(db database.Database, debug bool) {
rand.Seed(time.Now().UnixNano())

s, err := server.New(db, append(serverOptions, server.WithDataDir(sndDataDir), server.WithDebug(debug), server.WithPrinter(&cups.CUPS{}), server.WithPrinter(&remote.Remote{}), server.WithPrinter(&serial.Serial{}), server.WithPrinter(&dump.Dump{}))...)
s, err := server.New(db, append(serverOptions,
server.WithDataDir(sndDataDir),
server.WithDebug(debug),
server.WithPrinter(&cups.CUPS{}),
server.WithPrinter(&remote.Remote{}),
server.WithPrinter(&serial.Serial{}),
server.WithPrinter(&dump.Dump{}))...,
)
if err != nil {
panic(err)
}
Expand Down
27 changes: 20 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ type Option func(s *Server) error
// Server represents an instance of the S&D server.
type Server struct {
sync.RWMutex
debug bool
db database.Database
dataDir string
e *echo.Echo
m *melody.Melody
cache *cache.Cache
printers printing.PossiblePrinter
debug bool
db database.Database
dataDir string
e *echo.Echo
m *melody.Melody
cache *cache.Cache
printers printing.PossiblePrinter
additionalRoutes []func(e *echo.Group)
}

// New creates a new instance of the S&D server.
Expand Down Expand Up @@ -101,6 +102,14 @@ func WithDataDir(dir string) Option {
}
}

// WithAdditionalRoutes adds additional routes to the server.
func WithAdditionalRoutes(routes ...func(e *echo.Group)) Option {
return func(s *Server) error {
s.additionalRoutes = routes
return nil
}
}

// Close closes the server and all its connections.
func (s *Server) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
Expand Down Expand Up @@ -140,6 +149,10 @@ func (s *Server) Start(bindAddr string) error {
api := s.e.Group("/api")
extern := api.Group("/extern")

for i := range s.additionalRoutes {
s.additionalRoutes[i](api)
}

api.GET("/dataDir", func(c echo.Context) error {
absDir, err := filepath.Abs(s.dataDir)
if err != nil {
Expand Down

0 comments on commit ff8b4ea

Please sign in to comment.