Skip to content

Commit

Permalink
Add support to paste emojis correctly in text entries
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Apr 18, 2024
1 parent f27bace commit 9a3f911
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
11 changes: 6 additions & 5 deletions src/ui/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,9 @@ int Entry::getCaretFromMouse(MouseMessage* mousemsg)
return std::clamp(i, 0, lastPos);
}

void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
void Entry::executeCmd(const EntryCmd cmd,
const base::codepoint_t unicodeChar,
const bool shift_pressed)
{
std::string text = this->text();
const Range range = selectedRange();
Expand Down Expand Up @@ -615,11 +617,10 @@ void Entry::executeCmd(EntryCmd cmd, int unicodeChar, bool shift_pressed)
if (lastCaretPos() < m_maxsize) {
ASSERT(m_caret <= lastCaretPos());

std::wstring unicodeStr;
unicodeStr.push_back(unicodeChar);
const std::string unicodeStr =
base::codepoint_to_utf8(unicodeChar);

text.insert(m_boxes[m_caret].from,
base::to_utf8(unicodeStr));
text.insert(m_boxes[m_caret].from, unicodeStr);
recalcCharBoxes(text);
++m_caret;
}
Expand Down
5 changes: 3 additions & 2 deletions src/ui/entry.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2018-2022 Igara Studio S.A.
// Copyright (C) 2018-2024 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
Expand All @@ -9,6 +9,7 @@
#define UI_ENTRY_H_INCLUDED
#pragma once

#include "base/codepoint.h"
#include "obs/signal.h"
#include "ui/widget.h"

Expand Down Expand Up @@ -99,7 +100,7 @@ namespace ui {
};

int getCaretFromMouse(MouseMessage* mousemsg);
void executeCmd(EntryCmd cmd, int ascii, bool shift_pressed);
void executeCmd(EntryCmd cmd, base::codepoint_t unicodeChar, bool shift_pressed);
void forwardWord();
void backwardWord();
Range wordRange(int pos);
Expand Down
9 changes: 5 additions & 4 deletions src/ui/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ void Message::removeRecipient(Widget* widget)
m_recipient = nullptr;
}

KeyMessage::KeyMessage(MessageType type,
KeyScancode scancode,
KeyModifiers modifiers,
int unicodeChar, int repeat)
KeyMessage::KeyMessage(const MessageType type,
const KeyScancode scancode,
const KeyModifiers modifiers,
const base::codepoint_t unicodeChar,
const int repeat)
: Message(type, modifiers)
, m_scancode(scancode)
, m_unicodeChar(unicodeChar)
Expand Down
7 changes: 4 additions & 3 deletions src/ui/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define UI_MESSAGE_H_INCLUDED
#pragma once

#include "base/codepoint.h"
#include "base/paths.h"
#include "gfx/point.h"
#include "gfx/rect.h"
Expand Down Expand Up @@ -115,18 +116,18 @@ namespace ui {
KeyMessage(MessageType type,
KeyScancode scancode,
KeyModifiers modifiers,
int unicodeChar,
base::codepoint_t unicodeChar,
int repeat);

KeyScancode scancode() const { return m_scancode; }
int unicodeChar() const { return m_unicodeChar; }
base::codepoint_t unicodeChar() const { return m_unicodeChar; }
int repeat() const { return m_repeat; }
bool isDeadKey() const { return m_isDead; }
void setDeadKey(bool state) { m_isDead = state; }

private:
KeyScancode m_scancode;
int m_unicodeChar;
base::codepoint_t m_unicodeChar;
int m_repeat; // repeat=0 means the first time the key is pressed
bool m_isDead;
};
Expand Down

0 comments on commit 9a3f911

Please sign in to comment.