Skip to content

Commit

Permalink
[contour] Work on session resize policy when handling tabs
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Parpart <christian@parpart.family>
  • Loading branch information
christianparpart committed Oct 1, 2024
1 parent 9faca38 commit 68e6cca
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
28 changes: 21 additions & 7 deletions src/contour/TerminalSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void TerminalSession::detachDisplay(display::TerminalDisplay& display)
_display = nullptr;
}

void TerminalSession::attachDisplay(display::TerminalDisplay& newDisplay)
void TerminalSession::attachDisplay(display::TerminalDisplay& newDisplay, SessionResizePolicy resizePolicy)
{
sessionLog()("Attaching display.");
// newDisplay.setSession(*this); // NB: we're being called by newDisplay!
Expand All @@ -242,13 +242,27 @@ void TerminalSession::attachDisplay(display::TerminalDisplay& newDisplay)
setContentScale(newDisplay.contentScale());

{
// NB: Inform connected TTY and local Screen instance about initial cell pixel size.
auto const pixels = _display->cellSize() * _terminal.pageSize();
// auto const pixels =
// ImageSize { _display->cellSize().width * boxed_cast<Width>(_terminal.pageSize().columns),
// _display->cellSize().height * boxed_cast<Height>(_terminal.pageSize().lines) };
auto const l = scoped_lock { _terminal };
_terminal.resizeScreen(_terminal.pageSize(), pixels);
switch (resizePolicy)
{
case SessionResizePolicy::ResizeDisplay: {
auto const pixels = _display->cellSize() * _terminal.pageSize();
_terminal.resizeScreen(_terminal.totalPageSize(), pixels);
break;
}
case SessionResizePolicy::ResizeTerminal: {
#if 0
// FIXME: shouldn't that be sufficient?
adaptToWidgetSize();
#else
auto const pixels = _display->pixelSize();
auto const margins = applyContentScale(_profile.margins.value(), _display->contentScale());
auto const totalPageSize = pageSizeForPixels(pixels, _display->cellSize(), margins);
_terminal.resizeScreen(totalPageSize, pixels);
#endif
break;
}
}
_terminal.setRefreshRate(_display->refreshRate());
}

Expand Down
14 changes: 13 additions & 1 deletion src/contour/TerminalSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ enum class GuardedRole : uint8_t
*/
using PermissionCache = std::map<GuardedRole, bool>;

/**
* Determines what to resize when the terminal session is attached to a display.
*/
enum class SessionResizePolicy
{
/// The display is resized to fit the terminal.
ResizeDisplay,

/// The terminal is resized to fit the display.
ResizeTerminal
};

/**
* Manages a single terminal session (Client, Terminal, Display)
*
Expand Down Expand Up @@ -239,7 +251,7 @@ class TerminalSession: public QAbstractItemModel, public vtbackend::Terminal::Ev
display::TerminalDisplay* display() noexcept { return _display; }
display::TerminalDisplay const* display() const noexcept { return _display; }

void attachDisplay(display::TerminalDisplay& display);
void attachDisplay(display::TerminalDisplay& display, SessionResizePolicy resizePolicy);
void detachDisplay(display::TerminalDisplay& display);

Q_INVOKABLE void applyPendingFontChange(bool allow, bool remember);
Expand Down
16 changes: 3 additions & 13 deletions src/contour/TerminalSessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,12 @@ void TerminalSessionManager::setSession(size_t index)
if (!isAllowedToChangeTabs())
return;

auto imageSize = display->pixelSize();
auto pageSize = display->calculatePageSize();
_activeSession->detachDisplay(*display);
if (index < _sessions.size())
{
_sessions[index]->terminal().resizeScreen(pageSize);
_activeSession = _sessions[index];
}
else
{
auto* session = createSession();
session->terminal().resizeScreen(pageSize);
_activeSession = session;
}
display->setSession(_activeSession);
display->resizeWindow(imageSize.width, imageSize.height);
_activeSession = createSession();
auto const resizePolicy = _sessions.empty() ? SessionResizePolicy::ResizeDisplay : SessionResizePolicy::ResizeTerminal;
display->setSessionWithResizePolicy(_activeSession, resizePolicy);
updateStatusLine();
_lastTabChange = std::chrono::steady_clock::now();
}
Expand Down
14 changes: 13 additions & 1 deletion src/contour/display/TerminalDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,22 @@ TerminalDisplay::~TerminalDisplay()
}

void TerminalDisplay::setSession(TerminalSession* newSession)
{
setSessionWithResizePolicy(newSession, SessionResizePolicy::ResizeDisplay);
}

void TerminalDisplay::setSessionWithResizePolicy(TerminalSession* newSession, SessionResizePolicy resizePolicy)
{
displayLog()("TerminalDisplay::setSession: {} -> {}\n", (void*) _session, (void*) newSession);
if (_session == newSession)
return;

if (_session)
{
_session->detachDisplay(*this);
QObject::disconnect(_session, &TerminalSession::titleChanged, this, &TerminalDisplay::titleChanged);
}

// This will print the same pointer address for `this` but a new one for newSession (model data).
displayLog()("Assigning session to terminal widget({} <- {}): shell={}, terminalSize={}, fontSize={}, "
"contentScale={}",
Expand Down Expand Up @@ -309,7 +320,8 @@ void TerminalDisplay::setSession(TerminalSession* newSession)
updateImplicitSize();
updateMinimumSize();

_session->attachDisplay(*this); // NB: Requires Renderer to be instanciated to retrieve grid metrics.
// NB: Call to attachDisplay() requires Renderer to be instanciated to retrieve grid metrics.
_session->attachDisplay(*this, resizePolicy);

emit sessionChanged(newSession);
}
Expand Down
2 changes: 2 additions & 0 deletions src/contour/display/TerminalDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class TerminalDisplay: public QQuickItem
// NB: Use TerminalSession.attachDisplay, that one is calling this here. TODO(PR) ?
void setSession(TerminalSession* newSession);

void setSessionWithResizePolicy(TerminalSession* newSession, SessionResizePolicy resizePolicy);

[[nodiscard]] TerminalSession& session() noexcept
{
assert(_session != nullptr);
Expand Down

0 comments on commit 68e6cca

Please sign in to comment.