EscapeOneShot: Make it possible to disable the plugin at run-time

We want to make it possible to have the plugin in firmwares shipped by
Chrysalis, but still have the functionality optional. To achieve this, we need
to be able to toggle it on and off at will.

We move both the existing `cancel_oneshot_key_` property, and the new toggle
into a struct, which we will later make use of in the upcoming configuration
plugin.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/1089/head
Gergely Nagy 3 years ago
parent 2962f0f0c9
commit 4d1e59e769
No known key found for this signature in database
GPG Key ID: AC1E90BAC433F68F

@ -1,6 +1,6 @@
/* -*- mode: c++ -*-
* Kaleidoscope-Escape-OneShot -- Turn ESC into a key that cancels OneShots, if active.
* Copyright (C) 2016-2020 Keyboard.io, Inc
* Copyright (C) 2016-2021 Keyboard.io, Inc
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
@ -24,14 +24,21 @@
namespace kaleidoscope {
namespace plugin {
Key EscapeOneShot::cancel_oneshot_key_{Key_Escape};
EscapeOneShot::Settings EscapeOneShot::settings_ = {
.disabled = false,
.cancel_oneshot_key = Key_Escape
};
EventHandlerResult EscapeOneShot::onKeyEvent(KeyEvent &event) {
// If we're disabled, just pass through.
if (settings_.disabled)
return EventHandlerResult::OK;
// We only act on an escape key (or `cancel_oneshot_key_`, if that has been
// set) that has just been pressed, and not generated by some other
// plugin. Also, only if at least one OneShot key is active and/or
// sticky. Last, only if there are no OneShot keys currently being held.
if (event.key == cancel_oneshot_key_ &&
if (event.key == settings_.cancel_oneshot_key &&
keyToggledOn(event.state) &&
!keyIsInjected(event.state) &&
::OneShot.isActive()) {

@ -1,6 +1,6 @@
/* -*- mode: c++ -*-
* Kaleidoscope-Escape-OneShot -- Turn ESC into a key that cancels OneShots, if active.
* Copyright (C) 2016-2020 Keyboard.io, Inc
* Copyright (C) 2016-2021 Keyboard.io, Inc
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
@ -24,19 +24,41 @@ constexpr Key OneShotCancelKey {kaleidoscope::ranges::OS_CANCEL};
namespace kaleidoscope {
namespace plugin {
class EscapeOneShot : public kaleidoscope::Plugin {
public:
EscapeOneShot(void) {}
EventHandlerResult onKeyEvent(KeyEvent &event);
void setCancelKey(Key cancel_key) {
cancel_oneshot_key_ = cancel_key;
static void setCancelKey(Key cancel_key) {
settings_.cancel_oneshot_key = cancel_key;
}
static Key getCancelKey() {
return settings_.cancel_oneshot_key;
}
static void enable() {
settings_.disabled = false;
}
static void disable() {
settings_.disabled = true;
}
static void toggle() {
settings_.disabled = !settings_.disabled;
}
static bool isEnabled() {
return !settings_.disabled;
}
private:
static Key cancel_oneshot_key_;
struct Settings {
bool disabled;
Key cancel_oneshot_key;
};
static Settings settings_;
};
};
}
}

Loading…
Cancel
Save