Skip to content

Commit

Permalink
Introduce boxed types for margins and handling of input
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaraslaut committed Mar 27, 2024
1 parent 521b140 commit 0353420
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 73 deletions.
17 changes: 14 additions & 3 deletions src/contour/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,20 @@ enum class RenderingBackend
OpenGL,
};

struct HorizontalMarginTag
{
};
struct VerticalMarginTag
{
};

using HorizontalMargin = boxed::boxed<unsigned, HorizontalMarginTag>;
using VerticalMargin = boxed::boxed<unsigned, VerticalMarginTag>;

struct WindowMargins
{
unsigned horizontal = 0; // TODO use boxed
unsigned vertical = 0; // TODO use boxed
HorizontalMargin horizontal { 0 };
VerticalMargin vertical { 0 };
};

template <typename T, documentation::StringLiteral doc>
Expand Down Expand Up @@ -302,7 +312,8 @@ struct TerminalProfile
ConfigEntry<bool, documentation::MouseHideWhileTyping> mouseHideWhileTyping { true };
ConfigEntry<vtbackend::LineOffset, documentation::CopyLastMarkRangeOffset> copyLastMarkRangeOffset { 0 };
ConfigEntry<std::string, documentation::WMClass> wmClass { "contour" };
ConfigEntry<WindowMargins, documentation::Margins> margins { { 0u, 0u } };
ConfigEntry<WindowMargins, documentation::Margins> margins { { HorizontalMargin { 0u },
VerticalMargin { 0u } } };
ConfigEntry<vtbackend::PageSize, documentation::TerminalSize> terminalSize { {
vtbackend::LineCount(25),
vtbackend::ColumnCount(80),
Expand Down
4 changes: 2 additions & 2 deletions src/contour/display/TerminalDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,8 @@ constexpr ImageSize computeRequiredSize(config::WindowMargins margins,
PageSize totalPageSize) noexcept
{
// We multiply by 2 because the margins are applied to both sides of the terminal.
auto const marginSize = ImageSize { vtbackend::Width::cast_from(margins.horizontal * 2),
vtbackend::Height::cast_from(margins.vertical * 2) };
auto const marginSize = ImageSize { vtbackend::Width::cast_from(unbox(margins.horizontal) * 2),
vtbackend::Height::cast_from(unbox(margins.vertical) * 2) };

return (cellSize * totalPageSize + marginSize);
}
Expand Down
26 changes: 14 additions & 12 deletions src/contour/helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <array>
#include <mutex>

#include "contour/Config.h"

using std::array;
using std::clamp;
using std::get;
Expand Down Expand Up @@ -58,8 +60,8 @@ namespace
auto const cellSize = session.display()->cellSize();
auto const dpr = session.contentScale();

auto const marginTop = static_cast<int>(session.profile().margins.value().vertical * dpr);
auto const marginLeft = static_cast<int>(session.profile().margins.value().horizontal * dpr);
auto const marginTop = static_cast<int>(unbox(session.profile().margins.value().vertical) * dpr);
auto const marginLeft = static_cast<int>(unbox(session.profile().margins.value().horizontal) * dpr);

auto const sx = int(double(x) * dpr);
auto const sy = int(double(y) * dpr);
Expand All @@ -82,8 +84,8 @@ namespace
#else
auto const position = event->pos();
#endif
auto const marginLeft = static_cast<int>(margins.horizontal * dpr);
auto const marginTop = static_cast<int>(margins.vertical * dpr);
auto const marginLeft = static_cast<int>(unbox(margins.horizontal) * dpr);
auto const marginTop = static_cast<int>(unbox(margins.vertical) * dpr);
return PixelCoordinate { PixelCoordinate::X { int(double(position.x()) * dpr) - marginLeft },
PixelCoordinate::Y { int(double(position.y()) * dpr) - marginTop } };
}
Expand All @@ -97,8 +99,8 @@ namespace
#else
auto const position = QPointF { static_cast<qreal>(event->x()), static_cast<qreal>(event->y()) };
#endif
auto const marginLeft = static_cast<int>(margins.horizontal * dpr);
auto const marginTop = static_cast<int>(margins.vertical * dpr);
auto const marginLeft = static_cast<int>(unbox(margins.horizontal) * dpr);
auto const marginTop = static_cast<int>(unbox(margins.vertical) * dpr);
return PixelCoordinate { PixelCoordinate::X { int(double(position.x()) * dpr) - marginLeft },
PixelCoordinate::Y { int(double(position.y()) * dpr) - marginTop } };
}
Expand All @@ -112,8 +114,8 @@ namespace
#else
auto const position = event->posF();
#endif
auto const marginLeft = static_cast<int>(margins.horizontal * dpr);
auto const marginTop = static_cast<int>(margins.vertical * dpr);
auto const marginLeft = static_cast<int>(unbox(margins.horizontal) * dpr);
auto const marginTop = static_cast<int>(unbox(margins.vertical) * dpr);
return PixelCoordinate { PixelCoordinate::X { int(double(position.x()) * dpr) - marginLeft },
PixelCoordinate::Y { int(double(position.y()) * dpr) - marginTop } };
}
Expand Down Expand Up @@ -573,10 +575,10 @@ vtrasterizer::PageMargin computeMargin(ImageSize cellSize,
{
auto const usedHeight = unbox(charCells.lines) * unbox(cellSize.height);

auto const topMargin = static_cast<int>(minimumMargins.vertical);
auto const bottomMargin = static_cast<int>(
std::min(unbox(displaySize.height) - usedHeight - topMargin, minimumMargins.vertical));
auto const leftMargin = static_cast<int>(minimumMargins.horizontal);
auto const topMargin = unbox<int>(minimumMargins.vertical);
auto const bottomMargin = unbox<int>(std::min(
config::VerticalMargin(unbox(displaySize.height) - usedHeight - topMargin), minimumMargins.vertical));
auto const leftMargin = unbox<int>(minimumMargins.horizontal);

return { .left = leftMargin, .top = topMargin, .bottom = bottomMargin };
}
Expand Down
9 changes: 5 additions & 4 deletions src/contour/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ vtbackend::FontDef getFontDefinition(vtrasterizer::Renderer& renderer);

constexpr config::WindowMargins applyContentScale(config::WindowMargins margins, double contentScale) noexcept
{
return { .horizontal = static_cast<unsigned>(margins.horizontal * contentScale),
.vertical = static_cast<unsigned>(margins.vertical * contentScale) };
return { .horizontal = config::HorizontalMargin(unbox(margins.horizontal) * contentScale),
.vertical = config::VerticalMargin(unbox(margins.vertical) * contentScale) };
}

vtrasterizer::PageMargin computeMargin(vtbackend::ImageSize cellSize,
Expand All @@ -186,8 +186,9 @@ constexpr vtbackend::PageSize pageSizeForPixels(vtbackend::ImageSize totalViewSi
config::WindowMargins margins)
{
// NB: Multiplied by 2, because margins are applied on both sides of the terminal.
auto const marginSize = vtbackend::ImageSize { vtbackend::Width::cast_from(2 * margins.horizontal),
vtbackend::Height::cast_from(2 * margins.vertical) };
auto const marginSize =
vtbackend::ImageSize { vtbackend::Width::cast_from(2 * unbox(margins.horizontal)),
vtbackend::Height::cast_from(2 * unbox(margins.vertical)) };

auto const usableViewSize = totalViewSize - marginSize;

Expand Down
11 changes: 9 additions & 2 deletions src/vtbackend/InputHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@

#include <vtbackend/InputGenerator.h>

#include <boxed-cpp/boxed.hpp>

namespace vtbackend
{

struct HandledTag
{
};
using Handled = boxed::boxed<bool, HandledTag>;

/**
* Generic input handler interface.
*
Expand All @@ -16,8 +23,8 @@ class InputHandler
{
public:
virtual ~InputHandler() = default;
virtual bool sendKeyPressEvent(Key key, Modifiers modifiers) = 0;
virtual bool sendCharPressEvent(char32_t codepoint, Modifiers modifiers) = 0;
virtual Handled sendKeyPressEvent(Key key, Modifiers modifiers) = 0;
virtual Handled sendCharPressEvent(char32_t codepoint, Modifiers modifiers) = 0;
};

} // namespace vtbackend
38 changes: 19 additions & 19 deletions src/vtbackend/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,54 +594,54 @@ void Terminal::updateIndicatorStatusLine()
}
}

bool Terminal::sendKeyEvent(Key key, Modifiers modifiers, KeyboardEventType eventType, Timestamp now)
Handled Terminal::sendKeyEvent(Key key, Modifiers modifiers, KeyboardEventType eventType, Timestamp now)
{
_cursorBlinkState = 1;
_lastCursorBlink = now;

if (allowInput() && eventType != KeyboardEventType::Release
&& _inputHandler.sendKeyPressEvent(key, modifiers))
return true;
return Handled { true };

// Early exit if KAM is enabled.
if (isModeEnabled(AnsiMode::KeyboardAction))
return true;
return Handled { true };

bool const success = _inputGenerator.generate(key, modifiers, eventType);
if (success)
{
flushInput();
_viewport.scrollToBottom();
}
return success;
return Handled { success };
}

bool Terminal::sendCharEvent(
Handled Terminal::sendCharEvent(
char32_t ch, uint32_t physicalKey, Modifiers modifiers, KeyboardEventType eventType, Timestamp now)
{
_cursorBlinkState = 1;
_lastCursorBlink = now;

// Early exit if KAM is enabled.
if (isModeEnabled(AnsiMode::KeyboardAction))
return true;
return Handled { true };

if (eventType != KeyboardEventType::Release && _inputHandler.sendCharPressEvent(ch, modifiers))
return true;
return Handled { true };

auto const success = _inputGenerator.generate(ch, physicalKey, modifiers, eventType);
if (success)
{
flushInput();
_viewport.scrollToBottom();
}
return success;
return Handled { success };
}

bool Terminal::sendMousePressEvent(Modifiers modifiers,
MouseButton button,
PixelCoordinate pixelPosition,
bool uiHandledHint)
Handled Terminal::sendMousePressEvent(Modifiers modifiers,
MouseButton button,
PixelCoordinate pixelPosition,
bool uiHandledHint)
{
if (button == MouseButton::Left)
{
Expand All @@ -661,7 +661,7 @@ bool Terminal::sendMousePressEvent(Modifiers modifiers,
// TODO: Ctrl+(Left)Click's should still be catched by the terminal iff there's a hyperlink
// under the current position
flushInput();
return eventHandledByApp && !isModeEnabled(DECMode::MousePassiveTracking);
return Handled { eventHandledByApp && !isModeEnabled(DECMode::MousePassiveTracking) };
}

bool Terminal::handleMouseSelection(Modifiers modifiers)
Expand Down Expand Up @@ -847,10 +847,10 @@ void Terminal::sendMouseMoveEvent(Modifiers modifiers,
}
}

bool Terminal::sendMouseReleaseEvent(Modifiers modifiers,
MouseButton button,
PixelCoordinate pixelPosition,
bool uiHandledHint)
Handled Terminal::sendMouseReleaseEvent(Modifiers modifiers,
MouseButton button,
PixelCoordinate pixelPosition,
bool uiHandledHint)
{
verifyState();

Expand Down Expand Up @@ -879,10 +879,10 @@ bool Terminal::sendMouseReleaseEvent(Modifiers modifiers,
flushInput();

if (!isModeEnabled(DECMode::MousePassiveTracking))
return true;
return Handled { true };
}

return true;
return Handled { true };
}

bool Terminal::sendFocusInEvent()
Expand Down
20 changes: 10 additions & 10 deletions src/vtbackend/Terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -411,21 +411,21 @@ class Terminal

// {{{ input proxy
using Timestamp = std::chrono::steady_clock::time_point;
bool sendKeyEvent(Key key, Modifiers modifiers, KeyboardEventType eventType, Timestamp now);
bool sendCharEvent(
Handled sendKeyEvent(Key key, Modifiers modifiers, KeyboardEventType eventType, Timestamp now);
Handled sendCharEvent(
char32_t ch, uint32_t physicalKey, Modifiers modifiers, KeyboardEventType eventType, Timestamp now);
bool sendMousePressEvent(Modifiers modifiers,
MouseButton button,
PixelCoordinate pixelPosition,
bool uiHandledHint);
Handled sendMousePressEvent(Modifiers modifiers,
MouseButton button,
PixelCoordinate pixelPosition,
bool uiHandledHint);
void sendMouseMoveEvent(Modifiers modifiers,
CellLocation newPosition,
PixelCoordinate pixelPosition,
bool uiHandledHint);
bool sendMouseReleaseEvent(Modifiers modifiers,
MouseButton button,
PixelCoordinate pixelPosition,
bool uiHandledHint);
Handled sendMouseReleaseEvent(Modifiers modifiers,
MouseButton button,
PixelCoordinate pixelPosition,
bool uiHandledHint);
bool sendFocusInEvent();
bool sendFocusOutEvent();
void sendPaste(std::string_view text); // Sends verbatim text in bracketed mode to application.
Expand Down
2 changes: 1 addition & 1 deletion src/vtbackend/Terminal_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ TEST_CASE("Terminal.TextSelection", "[terminal]")

// We want to ensure that this call is returning false if the app has not explicitly requested
// to listen on mouse events (without passive mode being on).
REQUIRE(appHandledMouse == false);
REQUIRE(appHandledMouse == Handled { false });

CHECK(mock.terminal.selector()->state() == Selection::State::Waiting);

Expand Down
Loading

0 comments on commit 0353420

Please sign in to comment.