setting of active tab improvements (#166)

This commit is contained in:
Red J Adaya 2024-08-01 03:49:20 +08:00 committed by GitHub
parent 4df8e16a53
commit 3ff03f7b34
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -52,26 +52,37 @@ func (svc *WindowService) CloseTab(ctx context.Context, uiContext wstore.UIConte
if err != nil { if err != nil {
return nil, fmt.Errorf("error getting tab: %w", err) return nil, fmt.Errorf("error getting tab: %w", err)
} }
ws, err := wstore.DBMustGet[*wstore.Workspace](ctx, window.WorkspaceId)
if err != nil {
return nil, fmt.Errorf("error getting workspace: %w", err)
}
tabIndex := -1
for i, id := range ws.TabIds {
if id == tabId {
tabIndex = i
break
}
}
for _, blockId := range tab.BlockIds { for _, blockId := range tab.BlockIds {
blockcontroller.StopBlockController(blockId) blockcontroller.StopBlockController(blockId)
} }
err = wstore.DeleteTab(ctx, window.WorkspaceId, tabId) if err := wstore.DeleteTab(ctx, window.WorkspaceId, tabId); err != nil {
if err != nil {
return nil, fmt.Errorf("error closing tab: %w", err) return nil, fmt.Errorf("error closing tab: %w", err)
} }
if window.ActiveTabId == tabId { if window.ActiveTabId == tabId && tabIndex != -1 {
ws, err := wstore.DBMustGet[*wstore.Workspace](ctx, window.WorkspaceId) if len(ws.TabIds) == 1 {
if err != nil {
return nil, fmt.Errorf("error getting workspace: %w", err)
}
if len(ws.TabIds) > 0 {
newActiveTabId := ws.TabIds[0]
wstore.SetActiveTab(ctx, uiContext.WindowId, newActiveTabId)
} else {
eventbus.SendEventToElectron(eventbus.WSEventType{ eventbus.SendEventToElectron(eventbus.WSEventType{
EventType: eventbus.WSEvent_ElectronCloseWindow, EventType: eventbus.WSEvent_ElectronCloseWindow,
Data: uiContext.WindowId, Data: uiContext.WindowId,
}) })
} else {
if tabIndex < len(ws.TabIds)-1 {
newActiveTabId := ws.TabIds[tabIndex+1]
wstore.SetActiveTab(ctx, uiContext.WindowId, newActiveTabId)
} else {
newActiveTabId := ws.TabIds[tabIndex-1]
wstore.SetActiveTab(ctx, uiContext.WindowId, newActiveTabId)
}
} }
} }
return wstore.ContextGetUpdatesRtn(ctx), nil return wstore.ContextGetUpdatesRtn(ctx), nil