- Added a "InputRaw" mode to IInput interface

- Updated App to send keys directly to focused IInputs in raw mode
- Addded a new hotkey tester (TextInput in IndexerLayout) for hotkey testing
This commit is contained in:
Casey Langen 2016-07-12 22:01:30 -07:00
parent 6f93f602db
commit abb305ea92
7 changed files with 74 additions and 21 deletions

View File

@ -51,11 +51,13 @@ using namespace cursespp;
using namespace std::placeholders;
#define LABEL_HEIGHT 1
#define INPUT_HEIGHT 3
#define HOTKEY_INPUT_WIDTH 20
#define TOP(w) w->GetY()
#define BOTTOM(w) (w->GetY() + w->GetHeight())
#define LEFT(w) w->GetX()
#define RIGHT (x) (x->GetX() + w->GetWidth())
#define RIGHT(w) (w->GetX() + w->GetWidth())
typedef IScrollAdapter::EntryPtr EntryPtr;
static bool showDotfiles = false;
@ -109,6 +111,18 @@ void IndexerLayout::Layout() {
this->dotfileCheckbox->MoveAndResize(1, BOTTOM(this->browseList), cx - 1, LABEL_HEIGHT);
this->removeCheckbox->MoveAndResize(1, BOTTOM(this->dotfileCheckbox), cx - 1, LABEL_HEIGHT);
this->hotkeyLabel->MoveAndResize(
1,
BOTTOM(this->removeCheckbox) + 2,
this->hotkeyLabel->Length(),
LABEL_HEIGHT);
this->hotkeyInput->MoveAndResize(
RIGHT(this->hotkeyLabel),
BOTTOM(this->removeCheckbox) + 1,
HOTKEY_INPUT_WIDTH,
INPUT_HEIGHT);
}
void IndexerLayout::RefreshAddedPaths() {
@ -173,16 +187,21 @@ void IndexerLayout::InitializeWindows() {
this->removeCheckbox->SetText("remove missing files from library");
this->removeCheckbox->CheckChanged.connect(this, &IndexerLayout::OnRemoveMissingCheckChanged);
this->browseList->SetFocusOrder(0);
this->addedPathsList->SetFocusOrder(1);
this->dotfileCheckbox->SetFocusOrder(2);
this->removeCheckbox->SetFocusOrder(3);
this->shortcuts.reset(new ShortcutsWindow());
this->shortcuts->AddShortcut("M-a", "library");
this->shortcuts->AddShortcut("M-`", "console");
this->shortcuts->AddShortcut("^D", "quit");
this->hotkeyLabel.reset(new TextLabel());
this->hotkeyLabel->SetText("hotkey tester: ");
this->hotkeyInput.reset(new TextInput(IInput::InputRaw));
this->browseList->SetFocusOrder(0);
this->addedPathsList->SetFocusOrder(1);
this->dotfileCheckbox->SetFocusOrder(2);
this->removeCheckbox->SetFocusOrder(3);
this->hotkeyInput->SetFocusOrder(4);
this->AddWindow(this->browseLabel);
this->AddWindow(this->addedPathsLabel);
this->AddWindow(this->browseList);
@ -190,6 +209,8 @@ void IndexerLayout::InitializeWindows() {
this->AddWindow(this->dotfileCheckbox);
this->AddWindow(this->removeCheckbox);
this->AddWindow(this->shortcuts);
this->AddWindow(this->hotkeyLabel);
this->AddWindow(this->hotkeyInput);
this->Layout();
}

View File

@ -38,6 +38,7 @@
#include <cursespp/LayoutBase.h>
#include <cursespp/ListWindow.h>
#include <cursespp/TextLabel.h>
#include <cursespp/TextInput.h>
#include <cursespp/SimpleScrollAdapter.h>
#include <app/window/TrackListView.h>
@ -101,6 +102,9 @@ namespace musik {
std::shared_ptr<cursespp::ListWindow> browseList;
std::shared_ptr<cursespp::ListWindow> addedPathsList;
std::shared_ptr<cursespp::TextLabel> hotkeyLabel;
std::shared_ptr<cursespp::TextInput> hotkeyInput;
std::shared_ptr<ShortcutsWindow> shortcuts;
cursespp::SimpleScrollAdapter addedPathsAdapter;

View File

@ -167,6 +167,11 @@ void App::Run(ILayoutPtr layout) {
else if (kn == "KEY_RESIZE") {
resizeAt = App::Now() + REDRAW_DEBOUNCE_MS;
}
else if (this->state.input &&
this->state.input->GetInputMode() == IInput::InputRaw)
{
this->state.input->Write(kn);
}
else if (!keyHandler || !keyHandler(kn)) {
bool processed = false;
if (this->state.input) {

View File

@ -39,9 +39,15 @@
namespace cursespp {
class IInput {
public:
enum InputMode {
InputRaw,
InputNormal
};
virtual ~IInput() { }
virtual bool Write(const std::string& key) = 0;
virtual size_t Length() = 0;
virtual size_t Position() = 0;
virtual InputMode GetInputMode() = 0;
};
}

View File

@ -75,10 +75,11 @@ inline static bool removeUtf8Char(std::string& value, size_t position) {
return false;
}
TextInput::TextInput()
TextInput::TextInput(IInput::InputMode inputMode)
: Window()
, bufferLength(0)
, position(0) {
, position(0)
, inputMode(inputMode) {
}
TextInput::~TextInput() {
@ -101,21 +102,29 @@ size_t TextInput::Position() {
bool TextInput::Write(const std::string& key) {
/* one character at a time. if it's more than one character, we're
dealing with an escape sequence and should not print it. */
if (u8len(key) == 1) {
size_t offset = u8offset(this->buffer, this->position);
offset = (offset == std::string::npos) ? 0 : offset;
this->buffer.insert(offset, key);
dealing with an escape sequence and should not print it unless
the input mode allows for modifiers */
int len = u8len(key);
if (len == 1 || (len > 1 && this->inputMode == InputRaw)) {
if (this->inputMode == InputRaw) {
this->buffer = key;
this->bufferLength = len;
this->position = len;
}
else {
size_t offset = u8offset(this->buffer, this->position);
offset = (offset == std::string::npos) ? 0 : offset;
this->buffer.insert(offset, key);
this->bufferLength = u8len(buffer);
this->position += len;
}
this->TextChanged(this, this->buffer);
++this->position;
}
else {
return false;
redrawContents(*this, buffer);
return true;
}
this->bufferLength = u8len(buffer);
redrawContents(*this, buffer);
return true;
return false;
}
bool TextInput::KeyPress(const std::string& key) {

View File

@ -52,7 +52,7 @@ namespace cursespp {
sigslot::signal1<TextInput*> EnterPressed;
sigslot::signal2<TextInput*, std::string> TextChanged;
TextInput();
TextInput(InputMode inputMode = IInput::InputNormal);
virtual ~TextInput();
virtual void Show();
@ -61,6 +61,12 @@ namespace cursespp {
virtual size_t Length();
virtual size_t Position();
virtual void SetInputMode(InputMode inputMode) {
this->inputMode = inputMode;
};
virtual InputMode GetInputMode() { return this->inputMode; }
virtual bool KeyPress(const std::string& key);
virtual void SetText(const std::string& value);
@ -72,5 +78,6 @@ namespace cursespp {
std::string buffer;
int position;
size_t bufferLength;
InputMode inputMode;
};
}

View File

@ -57,6 +57,7 @@ namespace cursespp {
const text::TextAlign alignment = text::AlignLeft);
virtual std::string GetText() { return this->buffer; }
virtual size_t Length() { return u8cols(this->buffer); }
virtual void Show();