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 {
return nil, fmt.Errorf("error getting tab: %w", err)
}
for _, blockId := range tab.BlockIds {
blockcontroller.StopBlockController(blockId)
}
err = wstore.DeleteTab(ctx, window.WorkspaceId, tabId)
if err != nil {
return nil, fmt.Errorf("error closing tab: %w", err)
}
if window.ActiveTabId == tabId {
ws, err := wstore.DBMustGet[*wstore.Workspace](ctx, window.WorkspaceId)
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 {
tabIndex := -1
for i, id := range ws.TabIds {
if id == tabId {
tabIndex = i
break
}
}
for _, blockId := range tab.BlockIds {
blockcontroller.StopBlockController(blockId)
}
if err := wstore.DeleteTab(ctx, window.WorkspaceId, tabId); err != nil {
return nil, fmt.Errorf("error closing tab: %w", err)
}
if window.ActiveTabId == tabId && tabIndex != -1 {
if len(ws.TabIds) == 1 {
eventbus.SendEventToElectron(eventbus.WSEventType{
EventType: eventbus.WSEvent_ElectronCloseWindow,
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