2025-10-27 03:14:50 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-23 19:27:52 +00:00
|
|
|
"context"
|
2025-10-27 03:14:50 +00:00
|
|
|
"embed"
|
|
|
|
|
"io"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/wailsapp/wails/v3/pkg/application"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// This file defines the public API contracts (interfaces) for the services
|
|
|
|
|
// in the Core framework. Services depend on these interfaces, not on
|
|
|
|
|
// concrete implementations.
|
|
|
|
|
|
2025-11-14 14:33:58 +00:00
|
|
|
// Contract specifies the operational guarantees that the Core and its services must adhere to.
|
|
|
|
|
// This is used for configuring panic handling and other resilience features.
|
2025-10-27 03:14:50 +00:00
|
|
|
type Contract struct {
|
2025-11-14 14:33:58 +00:00
|
|
|
// DontPanic, if true, instructs the Core to recover from panics and return an error instead.
|
|
|
|
|
DontPanic bool
|
|
|
|
|
// DisableLogging, if true, disables all logging from the Core and its services.
|
2025-10-27 03:14:50 +00:00
|
|
|
DisableLogging bool
|
|
|
|
|
}
|
2025-11-02 03:03:54 +00:00
|
|
|
|
|
|
|
|
// Features provides a way to check if a feature is enabled.
|
2025-11-14 14:33:58 +00:00
|
|
|
// This is used for feature flagging and conditional logic.
|
2025-11-02 03:03:54 +00:00
|
|
|
type Features struct {
|
2025-11-14 14:33:58 +00:00
|
|
|
// Flags is a list of enabled feature flags.
|
2025-11-02 03:03:54 +00:00
|
|
|
Flags []string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsEnabled returns true if the given feature is enabled.
|
|
|
|
|
func (f *Features) IsEnabled(feature string) bool {
|
|
|
|
|
for _, flag := range f.Flags {
|
|
|
|
|
if flag == feature {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2025-11-14 14:33:58 +00:00
|
|
|
|
|
|
|
|
// Option is a function that configures the Core.
|
|
|
|
|
// This is used to apply settings and register services during initialization.
|
2025-10-27 03:14:50 +00:00
|
|
|
type Option func(*Core) error
|
2025-11-14 14:33:58 +00:00
|
|
|
|
|
|
|
|
// Message is the interface for all messages that can be sent through the Core's IPC system.
|
|
|
|
|
// Any struct can be a message, allowing for structured data to be passed between services.
|
2025-10-27 03:14:50 +00:00
|
|
|
type Message interface{}
|
2025-11-14 14:33:58 +00:00
|
|
|
|
2025-11-23 19:27:52 +00:00
|
|
|
// Startable is an interface for services that need to perform initialization.
|
|
|
|
|
type Startable interface {
|
|
|
|
|
OnStartup(ctx context.Context) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stoppable is an interface for services that need to perform cleanup.
|
|
|
|
|
type Stoppable interface {
|
|
|
|
|
OnShutdown(ctx context.Context) error
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-14 14:33:58 +00:00
|
|
|
// Core is the central application object that manages services, assets, and communication.
|
2025-10-27 03:14:50 +00:00
|
|
|
type Core struct {
|
|
|
|
|
once sync.Once
|
|
|
|
|
initErr error
|
|
|
|
|
App *application.App
|
|
|
|
|
assets embed.FS
|
2025-11-02 03:03:54 +00:00
|
|
|
Features *Features
|
2025-10-27 03:14:50 +00:00
|
|
|
serviceLock bool
|
|
|
|
|
ipcMu sync.RWMutex
|
|
|
|
|
ipcHandlers []func(*Core, Message) error
|
|
|
|
|
serviceMu sync.RWMutex
|
|
|
|
|
services map[string]any
|
|
|
|
|
servicesLocked bool
|
2025-11-23 19:27:52 +00:00
|
|
|
startables []Startable
|
|
|
|
|
stoppables []Stoppable
|
2025-10-27 03:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var instance *Core
|
|
|
|
|
|
|
|
|
|
// Config provides access to application configuration.
|
|
|
|
|
type Config interface {
|
2025-11-14 14:33:58 +00:00
|
|
|
// Get retrieves a configuration value by key and stores it in the 'out' variable.
|
2025-10-27 03:14:50 +00:00
|
|
|
Get(key string, out any) error
|
2025-11-14 14:33:58 +00:00
|
|
|
// Set stores a configuration value by key.
|
2025-10-27 03:14:50 +00:00
|
|
|
Set(key string, v any) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WindowConfig represents the configuration for a window.
|
|
|
|
|
type WindowConfig struct {
|
|
|
|
|
Name string
|
|
|
|
|
Title string
|
|
|
|
|
URL string
|
|
|
|
|
Width int
|
|
|
|
|
Height int // Add other common window options here as needed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WindowOption configures window creation.
|
|
|
|
|
type WindowOption interface {
|
2025-11-14 14:33:58 +00:00
|
|
|
// Apply applies the window option to the given configuration.
|
2025-10-28 21:42:29 +00:00
|
|
|
Apply(*WindowConfig)
|
2025-10-27 03:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Display manages windows and UI.
|
|
|
|
|
type Display interface {
|
2025-11-14 14:33:58 +00:00
|
|
|
// OpenWindow creates and displays a new window with the given options.
|
2025-10-27 03:14:50 +00:00
|
|
|
OpenWindow(opts ...WindowOption) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Help manages the in-app documentation and help system.
|
|
|
|
|
type Help interface {
|
2025-11-14 14:33:58 +00:00
|
|
|
// Show displays the main help topic.
|
2025-10-27 03:14:50 +00:00
|
|
|
Show() error
|
2025-11-14 14:33:58 +00:00
|
|
|
// ShowAt displays the help topic for the given anchor.
|
2025-10-27 03:14:50 +00:00
|
|
|
ShowAt(anchor string) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Crypt provides cryptographic functions.
|
|
|
|
|
type Crypt interface {
|
2025-11-14 14:33:58 +00:00
|
|
|
// EncryptPGP encrypts data using PGP and writes the result to the given writer.
|
2025-10-27 03:14:50 +00:00
|
|
|
EncryptPGP(writer io.Writer, recipientPath, data string, signerPath, signerPassphrase *string) (string, error)
|
2025-11-14 14:33:58 +00:00
|
|
|
// DecryptPGP decrypts a PGP message.
|
2025-10-27 03:14:50 +00:00
|
|
|
DecryptPGP(recipientPath, message, passphrase string, signerPath *string) (string, error)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// I18n provides internationalization and localization services.
|
|
|
|
|
type I18n interface {
|
|
|
|
|
// Translate returns the translated string for the given key.
|
|
|
|
|
Translate(key string) string
|
|
|
|
|
// SetLanguage changes the active language.
|
|
|
|
|
SetLanguage(lang string) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Workspace manages user workspaces.
|
|
|
|
|
type Workspace interface {
|
2025-11-14 14:33:58 +00:00
|
|
|
// CreateWorkspace creates a new workspace with the given identifier and password.
|
2025-10-27 03:14:50 +00:00
|
|
|
CreateWorkspace(identifier, password string) (string, error)
|
2025-11-14 14:33:58 +00:00
|
|
|
// SwitchWorkspace changes the active workspace.
|
2025-10-27 03:14:50 +00:00
|
|
|
SwitchWorkspace(name string) error
|
2025-11-14 14:33:58 +00:00
|
|
|
// WorkspaceFileGet retrieves the content of a file from the current workspace.
|
2025-10-27 03:14:50 +00:00
|
|
|
WorkspaceFileGet(filename string) (string, error)
|
2025-11-14 14:33:58 +00:00
|
|
|
// WorkspaceFileSet writes content to a file in the current workspace.
|
2025-10-27 03:14:50 +00:00
|
|
|
WorkspaceFileSet(filename, content string) error
|
|
|
|
|
}
|