(ctx context.Context, windowId string, workspaceId string)
| 19 | ) |
| 20 | |
| 21 | func SwitchWorkspace(ctx context.Context, windowId string, workspaceId string) (*waveobj.Workspace, error) { |
| 22 | log.Printf("SwitchWorkspace %s %s\n", windowId, workspaceId) |
| 23 | ws, err := GetWorkspace(ctx, workspaceId) |
| 24 | if err != nil { |
| 25 | return nil, fmt.Errorf("error getting new workspace: %w", err) |
| 26 | } |
| 27 | window, err := GetWindow(ctx, windowId) |
| 28 | if err != nil { |
| 29 | return nil, fmt.Errorf("error getting window: %w", err) |
| 30 | } |
| 31 | curWsId := window.WorkspaceId |
| 32 | if curWsId == workspaceId { |
| 33 | return nil, nil |
| 34 | } |
| 35 | |
| 36 | allWindows, err := wstore.DBGetAllObjsByType[*waveobj.Window](ctx, waveobj.OType_Window) |
| 37 | if err != nil { |
| 38 | return nil, fmt.Errorf("error getting all windows: %w", err) |
| 39 | } |
| 40 | |
| 41 | for _, w := range allWindows { |
| 42 | if w.WorkspaceId == workspaceId { |
| 43 | log.Printf("workspace %s already has a window %s, focusing that window\n", workspaceId, w.OID) |
| 44 | client := wshclient.GetBareRpcClient() |
| 45 | err = wshclient.FocusWindowCommand(client, w.OID, &wshrpc.RpcOpts{Route: wshutil.ElectronRoute}) |
| 46 | return nil, err |
| 47 | } |
| 48 | } |
| 49 | window.WorkspaceId = workspaceId |
| 50 | err = wstore.DBUpdate(ctx, window) |
| 51 | if err != nil { |
| 52 | return nil, fmt.Errorf("error updating window: %w", err) |
| 53 | } |
| 54 | |
| 55 | deleted, _, err := DeleteWorkspace(ctx, curWsId, false) |
| 56 | if err != nil && deleted { |
| 57 | print(err.Error()) // @jalileh isolated the error for now, curwId/workspace was deleted when this occurs. |
| 58 | } else if err != nil { |
| 59 | return nil, fmt.Errorf("error deleting workspace: %w", err) |
| 60 | } |
| 61 | |
| 62 | if !deleted { |
| 63 | log.Printf("current workspace %s was not deleted\n", curWsId) |
| 64 | } else { |
| 65 | log.Printf("deleted current workspace %s\n", curWsId) |
| 66 | } |
| 67 | |
| 68 | log.Printf("switching window %s to workspace %s\n", windowId, workspaceId) |
| 69 | return ws, nil |
| 70 | } |
| 71 | |
| 72 | func GetWindow(ctx context.Context, windowId string) (*waveobj.Window, error) { |
| 73 | window, err := wstore.DBMustGet[*waveobj.Window](ctx, windowId) |
no test coverage detected