2024-06-17 18:58:28 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package windowservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-20 04:10:53 +02:00
|
|
|
"fmt"
|
2024-06-25 23:56:37 +02:00
|
|
|
"log"
|
2024-06-20 04:10:53 +02:00
|
|
|
"time"
|
2024-06-17 18:58:28 +02:00
|
|
|
|
2024-09-05 23:25:45 +02:00
|
|
|
"github.com/wavetermdev/waveterm/pkg/blockcontroller"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/eventbus"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/tsgen/tsgenmeta"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/waveobj"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wcore"
|
|
|
|
"github.com/wavetermdev/waveterm/pkg/wlayout"
|
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-17 18:58:28 +02:00
|
|
|
)
|
|
|
|
|
2024-06-20 04:10:53 +02:00
|
|
|
const DefaultTimeout = 2 * time.Second
|
|
|
|
|
2024-06-17 18:58:28 +02:00
|
|
|
type WindowService struct{}
|
|
|
|
|
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) {
|
2024-06-17 18:58:28 +02:00
|
|
|
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)
|
2024-06-17 18:58:28 +02:00
|
|
|
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
|
2024-06-17 18:58:28 +02:00
|
|
|
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-17 18:58:28 +02:00
|
|
|
}
|
2024-06-20 04:10:53 +02:00
|
|
|
|
2024-10-17 23:34:02 +02:00
|
|
|
type CloseTabRtnType struct {
|
|
|
|
CloseWindow bool `json:"closewindow,omitempty"`
|
|
|
|
NewActiveTabId string `json:"newactivetabid,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the new active tabid
|
|
|
|
func (svc *WindowService) CloseTab(ctx context.Context, windowId string, tabId string, fromElectron bool) (*CloseTabRtnType, waveobj.UpdatesRtnType, error) {
|
2024-08-20 23:56:48 +02:00
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
2024-10-17 23:34:02 +02:00
|
|
|
window, err := wstore.DBMustGet[*waveobj.Window](ctx, windowId)
|
2024-06-20 04:10:53 +02:00
|
|
|
if err != nil {
|
2024-10-17 23:34:02 +02:00
|
|
|
return nil, nil, fmt.Errorf("error getting window: %w", err)
|
2024-06-20 04:10:53 +02:00
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
tab, err := wstore.DBMustGet[*waveobj.Tab](ctx, tabId)
|
2024-06-20 04:10:53 +02:00
|
|
|
if err != nil {
|
2024-10-17 23:34:02 +02:00
|
|
|
return nil, nil, fmt.Errorf("error getting tab: %w", err)
|
2024-06-20 04:10:53 +02:00
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
ws, err := wstore.DBMustGet[*waveobj.Workspace](ctx, window.WorkspaceId)
|
2024-07-31 21:49:20 +02:00
|
|
|
if err != nil {
|
2024-10-17 23:34:02 +02:00
|
|
|
return nil, nil, fmt.Errorf("error getting workspace: %w", err)
|
2024-07-31 21:49:20 +02:00
|
|
|
}
|
|
|
|
tabIndex := -1
|
|
|
|
for i, id := range ws.TabIds {
|
|
|
|
if id == tabId {
|
|
|
|
tabIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2024-09-06 01:36:03 +02:00
|
|
|
go func() {
|
|
|
|
for _, blockId := range tab.BlockIds {
|
|
|
|
blockcontroller.StopBlockController(blockId)
|
|
|
|
}
|
|
|
|
}()
|
2024-08-20 23:56:48 +02:00
|
|
|
if err := wcore.DeleteTab(ctx, window.WorkspaceId, tabId); err != nil {
|
2024-10-17 23:34:02 +02:00
|
|
|
return nil, nil, fmt.Errorf("error closing tab: %w", err)
|
2024-06-20 04:10:53 +02:00
|
|
|
}
|
2024-10-17 23:34:02 +02:00
|
|
|
rtn := &CloseTabRtnType{}
|
2024-07-31 21:49:20 +02:00
|
|
|
if window.ActiveTabId == tabId && tabIndex != -1 {
|
|
|
|
if len(ws.TabIds) == 1 {
|
2024-10-17 23:34:02 +02:00
|
|
|
rtn.CloseWindow = true
|
|
|
|
svc.CloseWindow(ctx, windowId, fromElectron)
|
|
|
|
if !fromElectron {
|
|
|
|
eventbus.SendEventToElectron(eventbus.WSEventType{
|
|
|
|
EventType: eventbus.WSEvent_ElectronCloseWindow,
|
|
|
|
Data: windowId,
|
|
|
|
})
|
|
|
|
}
|
2024-07-31 21:49:20 +02:00
|
|
|
} else {
|
|
|
|
if tabIndex < len(ws.TabIds)-1 {
|
|
|
|
newActiveTabId := ws.TabIds[tabIndex+1]
|
2024-10-17 23:34:02 +02:00
|
|
|
wstore.SetActiveTab(ctx, windowId, newActiveTabId)
|
|
|
|
rtn.NewActiveTabId = newActiveTabId
|
2024-07-31 21:49:20 +02:00
|
|
|
} else {
|
|
|
|
newActiveTabId := ws.TabIds[tabIndex-1]
|
2024-10-17 23:34:02 +02:00
|
|
|
wstore.SetActiveTab(ctx, windowId, newActiveTabId)
|
|
|
|
rtn.NewActiveTabId = newActiveTabId
|
2024-07-31 21:49:20 +02:00
|
|
|
}
|
2024-06-20 04:10:53 +02:00
|
|
|
}
|
|
|
|
}
|
2024-10-17 23:34:02 +02:00
|
|
|
updates := waveobj.ContextGetUpdatesRtn(ctx)
|
|
|
|
go func() {
|
|
|
|
wps.Broker.SendUpdateEvents(updates)
|
|
|
|
}()
|
|
|
|
return rtn, updates, nil
|
2024-06-20 04:10:53 +02:00
|
|
|
}
|
|
|
|
|
2024-06-25 23:56:37 +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) {
|
2024-06-25 23:56:37 +02:00
|
|
|
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)
|
2024-06-25 23:56:37 +02:00
|
|
|
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-08-27 00:17:37 +02:00
|
|
|
newWindow, err := wcore.CreateWindow(ctx, nil)
|
2024-06-25 23:56:37 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating window: %w", err)
|
|
|
|
}
|
|
|
|
err = wstore.MoveBlockToTab(ctx, currentTabId, newWindow.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")
|
|
|
|
}
|
2024-08-28 03:38:57 +02:00
|
|
|
wlayout.QueueLayoutActionForTab(ctx, currentTabId, waveobj.LayoutActionData{
|
|
|
|
ActionType: wlayout.LayoutActionDataType_Remove,
|
|
|
|
BlockId: blockId,
|
2024-06-25 23:56:37 +02:00
|
|
|
})
|
2024-08-28 03:38:57 +02:00
|
|
|
wlayout.QueueLayoutActionForTab(ctx, newWindow.ActiveTabId, waveobj.LayoutActionData{
|
|
|
|
ActionType: wlayout.LayoutActionDataType_Insert,
|
|
|
|
BlockId: blockId,
|
2024-08-28 08:16:07 +02:00
|
|
|
Focused: true,
|
2024-06-25 23:56:37 +02:00
|
|
|
})
|
2024-08-20 23:56:48 +02:00
|
|
|
return waveobj.ContextGetUpdatesRtn(ctx), nil
|
2024-06-25 23:56:37 +02:00
|
|
|
}
|
|
|
|
|
2024-10-17 23:34:02 +02:00
|
|
|
func (svc *WindowService) CloseWindow(ctx context.Context, windowId string, fromElectron bool) error {
|
2024-08-20 23:56:48 +02:00
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
|
|
|
window, err := wstore.DBMustGet[*waveobj.Window](ctx, windowId)
|
2024-06-20 04:10:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting window: %w", err)
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
workspace, err := wstore.DBMustGet[*waveobj.Workspace](ctx, window.WorkspaceId)
|
2024-06-20 04:10:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting workspace: %w", err)
|
|
|
|
}
|
|
|
|
for _, tabId := range workspace.TabIds {
|
2024-10-17 23:34:02 +02:00
|
|
|
_, _, err := svc.CloseTab(ctx, windowId, tabId, fromElectron)
|
2024-06-20 04:10:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error closing tab: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
err = wstore.DBDelete(ctx, waveobj.OType_Workspace, window.WorkspaceId)
|
2024-06-20 04:10:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting workspace: %w", err)
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
err = wstore.DBDelete(ctx, waveobj.OType_Window, windowId)
|
2024-06-20 04:10:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error deleting window: %w", err)
|
|
|
|
}
|
2024-08-20 23:56:48 +02:00
|
|
|
client, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
2024-06-20 09:00:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting client: %w", err)
|
|
|
|
}
|
2024-08-01 03:02:36 +02:00
|
|
|
client.WindowIds = utilfn.RemoveElemFromSlice(client.WindowIds, windowId)
|
2024-06-20 09:00:00 +02:00
|
|
|
err = wstore.DBUpdate(ctx, client)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error updating client: %w", err)
|
|
|
|
}
|
2024-06-20 04:10:53 +02:00
|
|
|
return nil
|
|
|
|
}
|