[windows-x] Refactor runtime

This commit is contained in:
Lea Anthony
2021-07-25 15:04:01 +10:00
parent aee395020f
commit 9786053324
5 changed files with 67 additions and 39 deletions

View File

@@ -1,3 +1,5 @@
// +build !experimental
package dialog
import (
@@ -5,7 +7,6 @@ import (
"fmt"
"github.com/wailsapp/wails/v2/internal/crypto"
"github.com/wailsapp/wails/v2/internal/servicebus"
"log"
)
// FileFilter defines a filter for dialog boxes
@@ -76,12 +77,10 @@ func processTitleAndFilter(params ...string) (string, string) {
return title, filter
}
func fatal(caller string) {
log.Fatalf("cannot call '%s': Application not initialised", caller)
}
type Dialog struct{}
// OpenDirectory prompts the user to select a directory
func OpenDirectory(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) {
func (d *Dialog) OpenDirectory(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) {
bus := servicebus.ExtractBus(ctx)
@@ -108,7 +107,7 @@ func OpenDirectory(ctx context.Context, dialogOptions OpenDialogOptions) (string
}
// OpenFile prompts the user to select a file
func OpenFile(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) {
func (d *Dialog) OpenFile(ctx context.Context, dialogOptions OpenDialogOptions) (string, error) {
bus := servicebus.ExtractBus(ctx)
@@ -135,7 +134,7 @@ func OpenFile(ctx context.Context, dialogOptions OpenDialogOptions) (string, err
}
// OpenMultipleFiles prompts the user to select a file
func OpenMultipleFiles(ctx context.Context, dialogOptions OpenDialogOptions) ([]string, error) {
func (d *Dialog) OpenMultipleFiles(ctx context.Context, dialogOptions OpenDialogOptions) ([]string, error) {
bus := servicebus.ExtractBus(ctx)
uniqueCallback := crypto.RandomID()
@@ -160,7 +159,7 @@ func OpenMultipleFiles(ctx context.Context, dialogOptions OpenDialogOptions) ([]
}
// SaveFile prompts the user to select a file
func SaveFile(ctx context.Context, dialogOptions SaveDialogOptions) (string, error) {
func (d *Dialog) SaveFile(ctx context.Context, dialogOptions SaveDialogOptions) (string, error) {
bus := servicebus.ExtractBus(ctx)
uniqueCallback := crypto.RandomID()
@@ -185,7 +184,7 @@ func SaveFile(ctx context.Context, dialogOptions SaveDialogOptions) (string, err
}
// Message show a message to the user
func Message(ctx context.Context, dialogOptions MessageDialogOptions) (string, error) {
func (d *Dialog) Message(ctx context.Context, dialogOptions MessageDialogOptions) (string, error) {
bus := servicebus.ExtractBus(ctx)

View File

@@ -1,3 +1,5 @@
// +build !experimental
package log
import (
@@ -6,50 +8,52 @@ import (
"github.com/wailsapp/wails/v2/pkg/logger"
)
type Log struct{}
// Print prints a Print level message
func Print(ctx context.Context, message string) {
func (l *Log) Print(ctx context.Context, message string) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("log:print", message)
}
// Trace prints a Trace level message
func Trace(ctx context.Context, message string) {
func (l *Log) Trace(ctx context.Context, message string) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("log:trace", message)
}
// Debug prints a Debug level message
func Debug(ctx context.Context, message string) {
func (l *Log) Debug(ctx context.Context, message string) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("log:debug", message)
}
// Info prints a Info level message
func Info(ctx context.Context, message string) {
func (l *Log) Info(ctx context.Context, message string) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("log:info", message)
}
// Warning prints a Warning level message
func Warning(ctx context.Context, message string) {
func (l *Log) Warning(ctx context.Context, message string) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("log:warning", message)
}
// Error prints a Error level message
func Error(ctx context.Context, message string) {
func (l *Log) Error(ctx context.Context, message string) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("log:error", message)
}
// Fatal prints a Fatal level message
func Fatal(ctx context.Context, message string) {
func (l *Log) Fatal(ctx context.Context, message string) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("log:fatal", message)
}
// SetLogLevel sets the log level
func SetLogLevel(ctx context.Context, level logger.LogLevel) {
func (l *Log) SetLogLevel(ctx context.Context, level logger.LogLevel) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("log:setlevel", level)
}

View File

@@ -1,13 +1,17 @@
package runtime
import (
"context"
"github.com/wailsapp/wails/v2/internal/servicebus"
"github.com/wailsapp/wails/v2/pkg/runtime/dialog"
"github.com/wailsapp/wails/v2/pkg/runtime/events"
"github.com/wailsapp/wails/v2/pkg/runtime/log"
"github.com/wailsapp/wails/v2/pkg/runtime/menu"
"github.com/wailsapp/wails/v2/pkg/runtime/window"
)
// Quit the application
func Quit(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
// Start shutdown of Wails
bus.Publish("quit", "runtime.Quit()")
}
var (
Window = window.Window{}
Menu = menu.Menu{}
Log = log.Log{}
Events = events.Events{}
Dialog = dialog.Dialog{}
)

View File

@@ -0,0 +1,17 @@
// +build !experimental
package system
import (
"context"
"github.com/wailsapp/wails/v2/internal/servicebus"
)
type System struct{}
// Quit the application
func (s *System) Quit(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
// Start shutdown of Wails
bus.Publish("quit", "runtime.Quit()")
}

View File

@@ -1,3 +1,5 @@
// +build !experimental
package window
import (
@@ -7,90 +9,92 @@ import (
"github.com/wailsapp/wails/v2/internal/servicebus"
)
type Window struct{}
// SetTitle sets the title of the window
func SetTitle(ctx context.Context, title string) {
func (w *Window) SetTitle(ctx context.Context, title string) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("window:settitle", title)
}
// Fullscreen makes the window fullscreen
func Fullscreen(ctx context.Context) {
func (w *Window) Fullscreen(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("window:fullscreen", "")
}
// UnFullscreen makes the window UnFullscreen
func UnFullscreen(ctx context.Context) {
func (w *Window) UnFullscreen(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("window:unfullscreen", "")
}
// Center the window on the current screen
func Center(ctx context.Context) {
func (w *Window) Center(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("window:center", "")
}
// Show shows the window if hidden
func Show(ctx context.Context) {
func (w *Window) Show(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("window:show", "")
}
// Hide the window
func Hide(ctx context.Context) {
func (w *Window) Hide(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("window:hide", "")
}
// SetSize sets the size of the window
func SetSize(ctx context.Context, width int, height int) {
func (w *Window) SetSize(ctx context.Context, width int, height int) {
bus := servicebus.ExtractBus(ctx)
message := fmt.Sprintf("window:size:%d:%d", width, height)
bus.Publish(message, "")
}
// SetSize sets the size of the window
func SetMinSize(ctx context.Context, width int, height int) {
func (w *Window) SetMinSize(ctx context.Context, width int, height int) {
bus := servicebus.ExtractBus(ctx)
message := fmt.Sprintf("window:minsize:%d:%d", width, height)
bus.Publish(message, "")
}
// SetSize sets the size of the window
func SetMaxSize(ctx context.Context, width int, height int) {
func (w *Window) SetMaxSize(ctx context.Context, width int, height int) {
bus := servicebus.ExtractBus(ctx)
message := fmt.Sprintf("window:maxsize:%d:%d", width, height)
bus.Publish(message, "")
}
// SetPosition sets the position of the window
func SetPosition(ctx context.Context, x int, y int) {
func (w *Window) SetPosition(ctx context.Context, x int, y int) {
bus := servicebus.ExtractBus(ctx)
message := fmt.Sprintf("window:position:%d:%d", x, y)
bus.Publish(message, "")
}
// Maximise the window
func Maximise(ctx context.Context) {
func (w *Window) Maximise(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("window:maximise", "")
}
// Unmaximise the window
func Unmaximise(ctx context.Context) {
func (w *Window) Unmaximise(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("window:unmaximise", "")
}
// Minimise the window
func Minimise(ctx context.Context) {
func (w *Window) Minimise(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("window:minimise", "")
}
// Unminimise the window
func Unminimise(ctx context.Context) {
func (w *Window) Unminimise(ctx context.Context) {
bus := servicebus.ExtractBus(ctx)
bus.Publish("window:unminimise", "")
}