mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 17:44:56 +09: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: d305c316e1
5 changed files with 14 additions and 1 deletions
|
@ -105,6 +105,7 @@ enum KeyboardKey {
|
||||||
UpArrow,
|
UpArrow,
|
||||||
DownArrow,
|
DownArrow,
|
||||||
Backspace,
|
Backspace,
|
||||||
|
Return,
|
||||||
};
|
};
|
||||||
|
|
||||||
class KeyEvent final : public Event {
|
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_UP: key = KeyboardKey::UpArrow; break;
|
||||||
case SDLK_DOWN: key = KeyboardKey::DownArrow; break;
|
case SDLK_DOWN: key = KeyboardKey::DownArrow; break;
|
||||||
case SDLK_BACKSPACE: key = KeyboardKey::Backspace; break;
|
case SDLK_BACKSPACE: key = KeyboardKey::Backspace; break;
|
||||||
|
case SDLK_RETURN: key = KeyboardKey::Return; break;
|
||||||
}
|
}
|
||||||
keyEvent->m_key = key;
|
keyEvent->m_key = key;
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,7 @@ void TextBox::paintEvent(PaintEvent&)
|
||||||
painter.drawBitmap({x, y}, *bitmap, Color::Black);
|
painter.drawBitmap({x, y}, *bitmap, Color::Black);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_cursorBlinkState) {
|
if (isFocused() && m_cursorBlinkState) {
|
||||||
unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar;
|
unsigned visibleCursorPosition = m_cursorPosition - firstVisibleChar;
|
||||||
Rect cursorRect(innerRect.x() + visibleCursorPosition * font.glyphWidth(), innerRect.y(), 1, innerRect.height());
|
Rect cursorRect(innerRect.x() + visibleCursorPosition * font.glyphWidth(), innerRect.y(), 1, innerRect.height());
|
||||||
painter.fillRect(cursorRect, foregroundColor());
|
painter.fillRect(cursorRect, foregroundColor());
|
||||||
|
@ -107,6 +107,10 @@ void TextBox::keyDownEvent(KeyEvent& event)
|
||||||
return;
|
return;
|
||||||
case KeyboardKey::Backspace:
|
case KeyboardKey::Backspace:
|
||||||
return handleBackspace();
|
return handleBackspace();
|
||||||
|
case KeyboardKey::Return:
|
||||||
|
if (onReturnPressed)
|
||||||
|
onReturnPressed(*this);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!event.text().isEmpty()) {
|
if (!event.text().isEmpty()) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "Widget.h"
|
#include "Widget.h"
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
class TextBox final : public Widget {
|
class TextBox final : public Widget {
|
||||||
public:
|
public:
|
||||||
|
@ -10,6 +11,8 @@ public:
|
||||||
String text() const { return m_text; }
|
String text() const { return m_text; }
|
||||||
void setText(String&&);
|
void setText(String&&);
|
||||||
|
|
||||||
|
std::function<void(TextBox&)> onReturnPressed;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void paintEvent(PaintEvent&) override;
|
virtual void paintEvent(PaintEvent&) override;
|
||||||
virtual void mouseDownEvent(MouseEvent&) override;
|
virtual void mouseDownEvent(MouseEvent&) override;
|
||||||
|
|
|
@ -83,6 +83,10 @@ int main(int argc, char** argv)
|
||||||
tb->setText("Hello!");
|
tb->setText("Hello!");
|
||||||
tb->setFocus(true);
|
tb->setFocus(true);
|
||||||
|
|
||||||
|
tb->onReturnPressed = [] (TextBox& textBox) {
|
||||||
|
printf("TextBox %p return pressed: '%s'\n", &textBox, textBox.text().characters());
|
||||||
|
};
|
||||||
|
|
||||||
WindowManager::the().setActiveWindow(widgetTestWindow);
|
WindowManager::the().setActiveWindow(widgetTestWindow);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue