Skip to content

Commit

Permalink
Make some actions non repeatable
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaraslaut committed Jan 20, 2025
1 parent ca16c0e commit b697425
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/contour/Actions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include <crispy/assert.h>
#include <crispy/utils.h>

#include <format>
#include <optional>
Expand Down Expand Up @@ -151,6 +152,11 @@ using Action = std::variant<CancelSelection,
SwitchToTabLeft,
SwitchToTabRight>;

template <typename T>
concept NonRepeatableActionConcept = crispy::one_of<T,
CreateNewTab,
CloseTab>;

std::optional<Action> fromString(std::string const& name);

namespace documentation
Expand Down
28 changes: 24 additions & 4 deletions src/contour/TerminalSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,28 @@ void TerminalSession::onScrollOffsetChanged(vtbackend::ScrollOffset value)
}
// }}}
// {{{ Input Events

void handleAction(auto const& actions, auto eventType, auto callback)
{
if (eventType == KeyboardEventType::Press)
callback(*actions);
else if (eventType == KeyboardEventType::Repeat)
{
// filter out actions that are not repeatable
std::vector<actions::Action> tmpActions;
auto set = crispy::overloaded {
[&]([[maybe_unused]] actions::NonRepeatableActionConcept auto const& action) {},
[&](auto const& action) { tmpActions.emplace_back(action); },
};

for (auto const& action: *actions)
{
std::visit(set, action);
}
callback(tmpActions);
}
}

void TerminalSession::sendKeyEvent(Key key, Modifiers modifiers, KeyboardEventType eventType, Timestamp now)
{
inputLog()("Key {} event received: {} {}", eventType, modifiers, key);
Expand All @@ -833,8 +855,7 @@ void TerminalSession::sendKeyEvent(Key key, Modifiers modifiers, KeyboardEventTy
if (auto const* actions =
config::apply(_config.inputMappings.value().keyMappings, key, modifiers, matchModeFlags()))
{
if (eventType == KeyboardEventType::Press)
executeAllActions(*actions);
handleAction(actions, eventType, [&](auto const& actions) { executeAllActions(actions); });
return;
}
}
Expand Down Expand Up @@ -869,8 +890,7 @@ void TerminalSession::sendCharEvent(
config::apply(_config.inputMappings.value().charMappings, value, modifiers, matchModeFlags());
actions && !_terminal.inputHandler().isEditingSearch())
{
if (eventType == KeyboardEventType::Press)
executeAllActions(*actions);
handleAction(actions, eventType, [&](auto const& actions) { executeAllActions(actions); });
return;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/crispy/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,4 +560,7 @@ struct overloaded: Ts...
template <class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;

template <typename T, typename... Ts>
concept one_of = (std::same_as<T, Ts> || ...);

} // namespace crispy

0 comments on commit b697425

Please sign in to comment.