mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
Add TextBox::onReturnPressed.
This commit is contained in:
parent
7a0a7abc52
commit
d305c316e1
Notes:
sideshowbarker
2024-07-19 18:48:43 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/d305c316e11
5 changed files with 14 additions and 1 deletions
|
@ -105,6 +105,7 @@ enum KeyboardKey {
|
|||
UpArrow,
|
||||
DownArrow,
|
||||
Backspace,
|
||||
Return,
|
||||
};
|
||||
|
||||
class KeyEvent final : public Event {
|
||||
|
|
|
@ -40,6 +40,7 @@ void EventLoopSDL::handleKeyEvent(Event::Type type, const SDL_KeyboardEvent& sdl
|
|||
case SDLK_UP: key = KeyboardKey::UpArrow; break;
|
||||
case SDLK_DOWN: key = KeyboardKey::DownArrow; break;
|
||||
case SDLK_BACKSPACE: key = KeyboardKey::Backspace; break;
|
||||
case SDLK_RETURN: key = KeyboardKey::Return; break;
|
||||
}
|
||||
keyEvent->m_key = key;
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ void TextBox::paintEvent(PaintEvent&)
|
|||
painter.drawBitmap({x, y}, *bitmap, Color::Black);
|
||||
}
|
||||
|
||||
if (m_cursorBlinkState) {
|
||||
if (isFocused() && m_cursorBlinkState) {
|
||||
unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar;
|
||||
Rect cursorRect(innerRect.x() + visibleCursorPosition * font.glyphWidth(), innerRect.y(), 1, innerRect.height());
|
||||
painter.fillRect(cursorRect, foregroundColor());
|
||||
|
@ -107,6 +107,10 @@ void TextBox::keyDownEvent(KeyEvent& event)
|
|||
return;
|
||||
case KeyboardKey::Backspace:
|
||||
return handleBackspace();
|
||||
case KeyboardKey::Return:
|
||||
if (onReturnPressed)
|
||||
onReturnPressed(*this);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!event.text().isEmpty()) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "Widget.h"
|
||||
#include <functional>
|
||||
|
||||
class TextBox final : public Widget {
|
||||
public:
|
||||
|
@ -10,6 +11,8 @@ public:
|
|||
String text() const { return m_text; }
|
||||
void setText(String&&);
|
||||
|
||||
std::function<void(TextBox&)> onReturnPressed;
|
||||
|
||||
private:
|
||||
virtual void paintEvent(PaintEvent&) override;
|
||||
virtual void mouseDownEvent(MouseEvent&) override;
|
||||
|
|
|
@ -83,6 +83,10 @@ int main(int argc, char** argv)
|
|||
tb->setText("Hello!");
|
||||
tb->setFocus(true);
|
||||
|
||||
tb->onReturnPressed = [] (TextBox& textBox) {
|
||||
printf("TextBox %p return pressed: '%s'\n", &textBox, textBox.text().characters());
|
||||
};
|
||||
|
||||
WindowManager::the().setActiveWindow(widgetTestWindow);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue