Some checks failed
Linting and tests / Linting (push) Failing after 6s
118 lines
2.6 KiB
Go
118 lines
2.6 KiB
Go
package mainwindow
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"bunker/client/internal/application"
|
|
"bunker/client/internal/helpers"
|
|
"bunker/client/internal/services/core"
|
|
"bunker/client/internal/services/core/mainwindow/models"
|
|
"bunker/commons"
|
|
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/lang"
|
|
"fyne.io/fyne/v2/widget"
|
|
)
|
|
|
|
var _ = core.MainWindow(&mainWindow{})
|
|
|
|
type mainWindow struct {
|
|
app *application.Application
|
|
logger *slog.Logger
|
|
window fyne.Window
|
|
options core.Options
|
|
tabsWidget *fyne.Container
|
|
statusBarProgress *widget.ProgressBar
|
|
statusBarStatus *widget.Label
|
|
sysInfoHandlers map[string]*models.SysInfoHandler
|
|
tabs []*models.Tab
|
|
}
|
|
|
|
// Initialize initializes service.
|
|
func Initialize(app *application.Application) error {
|
|
mainW := &mainWindow{
|
|
app: app,
|
|
}
|
|
|
|
if err := app.RegisterService(mainW); err != nil {
|
|
return fmt.Errorf("%w: %w", core.ErrMainWindow, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *mainWindow) Configure() error {
|
|
return nil
|
|
}
|
|
|
|
func (m *mainWindow) ConnectDependencies() error {
|
|
optionsRaw := m.app.Service(core.ServiceNameOptions)
|
|
if optionsRaw == nil {
|
|
return fmt.Errorf("connect dependencies: get options service: %w", application.ErrServiceNotFound)
|
|
}
|
|
|
|
options, valid := optionsRaw.(core.Options)
|
|
if !valid {
|
|
return fmt.Errorf("connect dependencies: type assert options service: %w", core.ErrOptionsIsInvalid)
|
|
}
|
|
|
|
m.options = options
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *mainWindow) Initialize() error {
|
|
m.logger = m.app.NewLogger("service", core.ServiceNameMainWindow)
|
|
|
|
m.logger.Info("Initializing...")
|
|
|
|
m.sysInfoHandlers = make(map[string]*models.SysInfoHandler)
|
|
|
|
m.window = m.app.Fyne().NewWindow(lang.L("window.title"))
|
|
// ToDo: сохранение и восстановление размеров окна.
|
|
//nolint:mnd
|
|
m.window.Resize(fyne.NewSize(1100, 800))
|
|
|
|
var mainWindowCanvas fyne.CanvasObject
|
|
|
|
if helpers.IsMobile() {
|
|
mainWindowCanvas = m.initializeMainWindowMobile()
|
|
} else {
|
|
mainWindowCanvas = m.initializeMainWindowDesktop()
|
|
}
|
|
|
|
m.window.SetContent(mainWindowCanvas)
|
|
|
|
m.window.SetCloseIntercept(m.stopApp)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *mainWindow) MainWindow() fyne.Window {
|
|
return m.window
|
|
}
|
|
|
|
func (m *mainWindow) Name() string {
|
|
return core.ServiceNameMainWindow
|
|
}
|
|
|
|
func (m *mainWindow) LaunchStartupTasks() error {
|
|
m.window.Show()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (m *mainWindow) Shutdown() error {
|
|
return nil
|
|
}
|
|
|
|
func (m *mainWindow) stopApp() {
|
|
if err := m.app.Shutdown(); err != nil {
|
|
slog.Error("Failed to stop Bunker!", "error", err.Error())
|
|
|
|
os.Exit(commons.ExitCodeAppStopFailed)
|
|
}
|
|
}
|