waveterm/pkg/service/windowservice/windowservice.go

170 lines
5.1 KiB
Go
Raw Normal View History

// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package windowservice
import (
"context"
2024-06-20 04:10:53 +02:00
"fmt"
"log"
2024-06-20 04:10:53 +02:00
"time"
2024-09-05 23:25:45 +02:00
"github.com/wavetermdev/waveterm/pkg/eventbus"
2024-11-21 03:05:13 +01:00
"github.com/wavetermdev/waveterm/pkg/panichandler"
2024-09-05 23:25:45 +02:00
"github.com/wavetermdev/waveterm/pkg/tsgen/tsgenmeta"
"github.com/wavetermdev/waveterm/pkg/waveobj"
"github.com/wavetermdev/waveterm/pkg/wcore"
2024-10-17 23:34:02 +02:00
"github.com/wavetermdev/waveterm/pkg/wps"
2024-09-05 23:25:45 +02:00
"github.com/wavetermdev/waveterm/pkg/wstore"
)
2024-06-20 04:10:53 +02:00
const DefaultTimeout = 2 * time.Second
type WindowService struct{}
2024-12-02 19:56:56 +01:00
func (svc *WindowService) GetWindow_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"windowId"},
}
}
func (svc *WindowService) GetWindow(windowId string) (*waveobj.Window, error) {
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
defer cancelFn()
window, err := wstore.DBGet[*waveobj.Window](ctx, windowId)
if err != nil {
return nil, fmt.Errorf("error getting window: %w", err)
}
return window, nil
}
func (svc *WindowService) CreateWindow_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"ctx", "winSize", "workspaceId"},
}
}
func (svc *WindowService) CreateWindow(ctx context.Context, winSize *waveobj.WinSize, workspaceId string) (*waveobj.Window, error) {
window, err := wcore.CreateWindow(ctx, winSize, workspaceId)
if err != nil {
return nil, fmt.Errorf("error creating window: %w", err)
}
return window, nil
}
func (svc *WindowService) SetWindowPosAndSize_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
Desc: "set window position and size",
ArgNames: []string{"ctx", "windowId", "pos", "size"},
}
}
2024-08-20 23:56:48 +02:00
func (ws *WindowService) SetWindowPosAndSize(ctx context.Context, windowId string, pos *waveobj.Point, size *waveobj.WinSize) (waveobj.UpdatesRtnType, error) {
if pos == nil && size == nil {
return nil, nil
}
2024-08-20 23:56:48 +02:00
ctx = waveobj.ContextWithUpdates(ctx)
win, err := wstore.DBMustGet[*waveobj.Window](ctx, windowId)
if err != nil {
return nil, err
}
if pos != nil {
win.Pos = *pos
}
if size != nil {
win.WinSize = *size
}
2024-10-11 01:12:56 +02:00
win.IsNew = false
err = wstore.DBUpdate(ctx, win)
if err != nil {
return nil, err
}
2024-08-20 23:56:48 +02:00
return waveobj.ContextGetUpdatesRtn(ctx), nil
}
2024-06-20 04:10:53 +02:00
func (svc *WindowService) MoveBlockToNewWindow_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
Desc: "move block to new window",
ArgNames: []string{"ctx", "currentTabId", "blockId"},
}
}
2024-08-20 23:56:48 +02:00
func (svc *WindowService) MoveBlockToNewWindow(ctx context.Context, currentTabId string, blockId string) (waveobj.UpdatesRtnType, error) {
log.Printf("MoveBlockToNewWindow(%s, %s)", currentTabId, blockId)
2024-08-20 23:56:48 +02:00
ctx = waveobj.ContextWithUpdates(ctx)
tab, err := wstore.DBMustGet[*waveobj.Tab](ctx, currentTabId)
if err != nil {
return nil, fmt.Errorf("error getting tab: %w", err)
}
log.Printf("tab.BlockIds[%s]: %v", tab.OID, tab.BlockIds)
var foundBlock bool
for _, tabBlockId := range tab.BlockIds {
if tabBlockId == blockId {
foundBlock = true
break
}
}
if !foundBlock {
return nil, fmt.Errorf("block not found in current tab")
}
2024-12-02 19:56:56 +01:00
newWindow, err := wcore.CreateWindow(ctx, nil, "")
if err != nil {
return nil, fmt.Errorf("error creating window: %w", err)
}
2024-12-02 19:56:56 +01:00
ws, err := wcore.GetWorkspace(ctx, newWindow.WorkspaceId)
if err != nil {
return nil, fmt.Errorf("error getting workspace: %w", err)
}
err = wstore.MoveBlockToTab(ctx, currentTabId, ws.ActiveTabId, blockId)
if err != nil {
return nil, fmt.Errorf("error moving block to tab: %w", err)
}
eventbus.SendEventToElectron(eventbus.WSEventType{
EventType: eventbus.WSEvent_ElectronNewWindow,
Data: newWindow.OID,
})
windowCreated := eventbus.BusyWaitForWindowId(newWindow.OID, 2*time.Second)
if !windowCreated {
return nil, fmt.Errorf("new window not created")
}
wcore.QueueLayoutActionForTab(ctx, currentTabId, waveobj.LayoutActionData{
ActionType: wcore.LayoutActionDataType_Remove,
BlockId: blockId,
})
wcore.QueueLayoutActionForTab(ctx, ws.ActiveTabId, waveobj.LayoutActionData{
ActionType: wcore.LayoutActionDataType_Insert,
BlockId: blockId,
Focused: true,
})
2024-08-20 23:56:48 +02:00
return waveobj.ContextGetUpdatesRtn(ctx), nil
}
2024-12-02 19:56:56 +01:00
func (svc *WindowService) SwitchWorkspace_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"ctx", "windowId", "workspaceId"},
}
2024-12-02 19:56:56 +01:00
}
func (svc *WindowService) SwitchWorkspace(ctx context.Context, windowId string, workspaceId string) (*waveobj.Workspace, error) {
ctx = waveobj.ContextWithUpdates(ctx)
ws, err := wcore.SwitchWorkspace(ctx, windowId, workspaceId)
updates := waveobj.ContextGetUpdatesRtn(ctx)
go func() {
2024-12-02 23:52:34 +01:00
defer panichandler.PanicHandler("WindowService:SwitchWorkspace:SendUpdateEvents")
2024-12-02 19:56:56 +01:00
wps.Broker.SendUpdateEvents(updates)
}()
return ws, err
}
func (svc *WindowService) CloseWindow_Meta() tsgenmeta.MethodMeta {
return tsgenmeta.MethodMeta{
ArgNames: []string{"ctx", "windowId", "fromElectron"},
}
2024-12-02 19:56:56 +01:00
}
func (svc *WindowService) CloseWindow(ctx context.Context, windowId string, fromElectron bool) error {
ctx = waveobj.ContextWithUpdates(ctx)
return wcore.CloseWindow(ctx, windowId, fromElectron)
2024-06-20 04:10:53 +02:00
}