@@ -74,7 +74,8 @@ TerminalSession* TerminalSessionManager::createSessionInBackground()
74
74
#endif
75
75
76
76
auto * session = new TerminalSession (this , createPty (ptyPath), _app);
77
- managerLog ()(" Create new session with ID {} at index {}" , session->id (), _sessions.size ());
77
+ managerLog ()(
78
+ " Create new session with ID {}({}) at index {}" , session->id (), (void *) session, _sessions.size ());
78
79
79
80
_sessions.insert (_sessions.end (), session);
80
81
@@ -105,91 +106,101 @@ TerminalSession* TerminalSessionManager::activateSession(TerminalSession* sessio
105
106
if (!session)
106
107
return nullptr ;
107
108
108
- managerLog ()(
109
- " Activating session ID {} at index {}" , session->id (), getSessionIndexOf (session).value_or (-1 ));
109
+ // debug for displayStates
110
+ for (auto & [display, state]: _displayStates)
111
+ {
112
+ managerLog ()(" display: {}, session: {}\n " , (void *) display, (void *) state);
113
+ }
114
+
115
+ managerLog ()(" Activating session ID {} {} at index {}" ,
116
+ session->id (),
117
+ (void *) session,
118
+ getSessionIndexOf (session).value_or (-1 ));
110
119
111
120
// iterate over _displayStates to see if this session is already active
112
121
for (auto & [display, state]: _displayStates)
113
122
{
114
- if (state. activeSession == session)
123
+ if (state == session && ( nullptr != display) )
115
124
{
116
- managerLog ()(" Session is already active : (display {}, ID {})" , (void *) display, session->id ());
117
- // return session;
125
+ managerLog ()(" Session is already active : (display {}, ID {} {})" ,
126
+ (void *) display,
127
+ session->id (),
128
+ (void *) session);
129
+ return session;
118
130
}
119
131
}
120
132
121
133
if (!activeDisplay)
122
134
{
123
135
managerLog ()(" No active display fond. something went wrong." );
124
136
}
125
- else
126
- {
127
- managerLog ()(" Active display found: {}" , (void *) activeDisplay);
128
- _displayStates[activeDisplay].activeSession = session;
129
- _displayStates.erase (nullptr );
130
- }
131
137
132
138
auto & displayState = _displayStates[activeDisplay];
133
- displayState.previousActiveSession = displayState.activeSession ;
134
- displayState.activeSession = session;
139
+ displayState = session;
135
140
updateStatusLine ();
136
141
137
- if (session->display ())
138
- {
139
- managerLog ()(" Session already has another display attached." );
140
- }
141
142
if (activeDisplay)
142
143
{
143
- // check if we are trying to attach to another display, we need to deatach the session from the old
144
- // display first
145
- // if (session->display() != activeDisplay)
146
- // {
147
- // managerLog()(
148
- // "Deataching display {} from session: {}.", (void*) session->display(), session->id());
149
- // if (session->display() != nullptr)
150
- // //session->detachDisplay(*session->display());
151
- // }
152
144
153
145
auto const pixels = activeDisplay->pixelSize ();
154
- auto const totalPageSize = activeDisplay->calculatePageSize () + [&] {
155
- if (displayState.previousActiveSession )
156
- return displayState.previousActiveSession ->terminal ().statusLineHeight ();
157
- return vtpty::LineCount (0 );
158
- }();
146
+ auto const totalPageSize =
147
+ activeDisplay->calculatePageSize () + displayState->terminal ().statusLineHeight ();
159
148
160
149
// Ensure that the existing session is resized to the display's size.
161
150
if (!isNewSession)
162
151
{
163
152
managerLog ()(" Resize existing session to display size: {}x{}." ,
164
153
activeDisplay->width (),
165
154
activeDisplay->height ());
155
+ displayState->terminal ().resizeScreen (totalPageSize, pixels);
166
156
}
167
- // displayState.activeSession->terminal().resizeScreen(totalPageSize, pixels);
168
157
169
- managerLog ()(" Set display {} to session: {}." , (void *) activeDisplay, session->id ());
170
- activeDisplay->setSession (displayState.activeSession );
171
- managerLog ()(" Done." );
158
+ managerLog ()(
159
+ " Set display {} to session: {}({})." , (void *) activeDisplay, session->id (), (void *) session);
160
+ // resize terminal session before display is attached to it
161
+ activeDisplay->setSession (displayState);
172
162
173
163
// Resize active session after display is attached to it
174
164
// to return a lost line
175
- displayState. activeSession ->terminal ().resizeScreen (totalPageSize, pixels);
165
+ displayState->terminal ().resizeScreen (totalPageSize, pixels);
176
166
}
177
167
178
168
return session;
179
169
}
180
170
171
+ void TerminalSessionManager::FocusOnDisplay (display::TerminalDisplay* display)
172
+ {
173
+ managerLog ()(" Setting active display to {}" , (void *) display);
174
+ activeDisplay = display;
175
+
176
+ // if we have a session in nullptr display, set it to this one
177
+ if (_displayStates[nullptr ] != nullptr )
178
+ {
179
+ _displayStates[activeDisplay] = _displayStates[nullptr ];
180
+ _displayStates[nullptr ] = nullptr ;
181
+ activateSession (_displayStates[activeDisplay]);
182
+ }
183
+
184
+ // if this is new display, find a session to attach to
185
+ if (_displayStates[activeDisplay] == nullptr )
186
+ {
187
+ tryFindSessionForDisplayOrClose ();
188
+ }
189
+ }
190
+
181
191
TerminalSession* TerminalSessionManager::createSession ()
182
192
{
183
193
return activateSession (createSessionInBackground (), true /* force resize on before display-attach*/ );
184
194
}
185
195
186
196
void TerminalSessionManager::switchToPreviousTab ()
187
197
{
188
- managerLog ()(" switch to previous tab (current: {}, previous: {})" ,
189
- getSessionIndexOf (_displayStates[activeDisplay].activeSession ).value_or (-1 ),
190
- getSessionIndexOf (_displayStates[activeDisplay].previousActiveSession ).value_or (-1 ));
198
+ return ;
199
+ // managerLog()("switch to previous tab (current: {}, previous: {})",
200
+ // getSessionIndexOf(_displayStates[activeDisplay].activeSession).value_or(-1),
201
+ // getSessionIndexOf(_displayStates[activeDisplay].previousActiveSession).value_or(-1));
191
202
192
- activateSession (_displayStates[activeDisplay].previousActiveSession );
203
+ // activateSession(_displayStates[activeDisplay].previousActiveSession);
193
204
}
194
205
195
206
void TerminalSessionManager::switchToTabLeft ()
@@ -228,7 +239,7 @@ void TerminalSessionManager::switchToTabRight()
228
239
void TerminalSessionManager::switchToTab (int position)
229
240
{
230
241
managerLog ()(" switchToTab from index {} to {} (out of {})" ,
231
- getSessionIndexOf (_displayStates[activeDisplay]. activeSession ).value_or (-1 ),
242
+ getSessionIndexOf (_displayStates[activeDisplay]).value_or (-1 ),
232
243
position - 1 ,
233
244
_sessions.size ());
234
245
@@ -239,15 +250,15 @@ void TerminalSessionManager::switchToTab(int position)
239
250
void TerminalSessionManager::closeTab ()
240
251
{
241
252
managerLog ()(" Close tab: current session ID {}, index {}" ,
242
- getSessionIndexOf (_displayStates[activeDisplay]. activeSession ).value_or (-1 ),
243
- _displayStates[activeDisplay]. activeSession ->id ());
253
+ getSessionIndexOf (_displayStates[activeDisplay]).value_or (-1 ),
254
+ _displayStates[activeDisplay]->id ());
244
255
245
- removeSession (*_displayStates[activeDisplay]. activeSession );
256
+ removeSession (*_displayStates[activeDisplay]);
246
257
}
247
258
248
259
void TerminalSessionManager::moveTabTo (int position)
249
260
{
250
- auto const currentIndexOpt = getSessionIndexOf (_displayStates[activeDisplay]. activeSession );
261
+ auto const currentIndexOpt = getSessionIndexOf (_displayStates[activeDisplay]);
251
262
if (!currentIndexOpt)
252
263
return ;
253
264
@@ -290,13 +301,16 @@ void TerminalSessionManager::moveTabToRight(TerminalSession* session)
290
301
}
291
302
}
292
303
293
- void TerminalSessionManager::removeSession (TerminalSession& thatSession )
304
+ void TerminalSessionManager::currentSessionIsTerminated ( )
294
305
{
295
- managerLog ()(" REMOVE SESSION: session: {}, _sessions.size(): {}" , (void *) &thatSession, _sessions.size ());
306
+ managerLog ()(" got notified that session is terminated, number of existing sessions: _sessions.size(): {}" ,
307
+ _sessions.size ());
308
+ return ;
309
+ }
296
310
297
- if (&thatSession == _displayStates[activeDisplay]. activeSession
298
- && _displayStates[activeDisplay]. previousActiveSession )
299
- activateSession (_displayStates[activeDisplay]. previousActiveSession );
311
+ void TerminalSessionManager::removeSession (TerminalSession& thatSession)
312
+ {
313
+ managerLog ()( " remove session: session: {}, _sessions.size(): {} " , ( void *) &thatSession, _sessions. size () );
300
314
301
315
auto i = std::ranges::find (_sessions, &thatSession);
302
316
if (i == _sessions.end ())
@@ -305,21 +319,36 @@ void TerminalSessionManager::removeSession(TerminalSession& thatSession)
305
319
return ;
306
320
}
307
321
_sessions.erase (i);
308
- _app.onExit (thatSession); // TODO: the logic behind that impl could probably be moved here.
309
-
310
- _displayStates[activeDisplay].previousActiveSession = [&]() -> TerminalSession* {
311
- auto const currentIndex = getSessionIndexOf (_displayStates[activeDisplay].activeSession ).value_or (0 );
312
- if (currentIndex + 1 < _sessions.size ())
313
- return _sessions[currentIndex + 1 ];
314
- else if (currentIndex > 0 )
315
- return _sessions[currentIndex - 1 ];
316
- else
317
- return nullptr ;
318
- }();
319
- managerLog ()(" Calculated next \" previous\" session index {}" ,
320
- getSessionIndexOf (_displayStates[activeDisplay].previousActiveSession ).value_or (-1 ));
322
+ tryFindSessionForDisplayOrClose ();
323
+ // _app.onExit(thatSession); // TODO: the logic behind that impl could probably be moved here.
324
+ }
325
+
326
+ void TerminalSessionManager::tryFindSessionForDisplayOrClose ()
327
+ {
328
+ for (auto & session: _sessions)
329
+ {
330
+ bool saveToSwitch { true };
331
+ // check if session is not used by any display and then switch
332
+ for (auto & [display, state]: _displayStates)
333
+ {
334
+ if ((state == session) && (display != nullptr ))
335
+ {
336
+ saveToSwitch = false ;
337
+ break ;
338
+ }
339
+ }
340
+
341
+ if (saveToSwitch)
342
+ {
343
+ managerLog ()(" Switching to session: {}" , (void *) session);
344
+ activateSession (session);
345
+ return ;
346
+ }
347
+ }
321
348
322
349
updateStatusLine ();
350
+ _displayStates.erase (activeDisplay);
351
+ activeDisplay->closeDisplay ();
323
352
}
324
353
325
354
void TerminalSessionManager::updateColorPreference (vtbackend::ColorPreference const & preference)
0 commit comments