diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a2a2a3..0d7ddd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed +- Text selection bugs + +## [1.1.0] - 2024-05-256 + +### Fixed + - Text selection and copy/paste shortcuts ## [1.0.1] - 2024-05-26 diff --git a/mod.json b/mod.json index 1812817..9f2a4c3 100644 --- a/mod.json +++ b/mod.json @@ -3,7 +3,7 @@ "gd": { "win": "2.204" }, - "version": "v1.1.0", + "version": "v1.2.0", "id": "spaghettdev.betterinputs", "name": "BetterInputs", "developer": "SpaghettDev", diff --git a/src/HighlightedString/HighlightedString.hpp b/src/HighlightedString/HighlightedString.hpp index 7be0ba0..fc65331 100644 --- a/src/HighlightedString/HighlightedString.hpp +++ b/src/HighlightedString/HighlightedString.hpp @@ -7,7 +7,7 @@ struct HighlightedString { - HighlightedString(std::string_view str, int from = -2, int to = -2) + constexpr HighlightedString(std::string_view str, int from = -2, int to = -2) : m_from(from == -1 ? str.size() : from), m_to(to == -1 ? str.size() : to), m_og_str(str) @@ -15,11 +15,11 @@ struct HighlightedString update_string_substr(); } - HighlightedString() + constexpr HighlightedString() : m_from(-2), m_to(-2), m_og_str(""), str("") {} - void reset() + constexpr void reset() { m_from = -2; m_to = -2; @@ -27,7 +27,7 @@ struct HighlightedString str = ""; } - void updateStr(std::string_view s) + constexpr void updateStr(std::string_view s) { m_og_str = s; @@ -35,7 +35,7 @@ struct HighlightedString update_string_substr(); } - void update(std::string_view str, std::pair pos) + constexpr void update(std::string_view str, std::pair pos) { m_from = pos.first == -1 ? str.size() : pos.first; m_to = pos.second == -1 ? str.size() : pos.second; @@ -45,11 +45,11 @@ struct HighlightedString update_string_substr(); } - inline bool isHighlighting() const { return !str.empty(); } + inline constexpr bool isHighlighting() const { return !str.empty(); } - inline int getFromPos() const { return m_from == m_og_str.size() ? -1 : m_from; } - inline int getToPos() const { return m_to == m_og_str.size() ? -1 : m_to; } - inline int getLength() const { return str.size(); } + inline constexpr int getFromPos() const { return m_from == m_og_str.size() ? -1 : m_from; } + inline constexpr int getToPos() const { return m_to == m_og_str.size() ? -1 : m_to; } + inline constexpr std::size_t getLength() const { return str.size(); } public: std::string_view str; @@ -59,8 +59,8 @@ struct HighlightedString int m_from = 0; int m_to = -1; - void update_string_substr() + constexpr void update_string_substr() { - str = m_og_str.substr(m_from, m_to); + str = m_og_str.substr(m_from, m_to - m_from); } }; diff --git a/src/main.cpp b/src/main.cpp index 63058e2..8d7ca56 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -314,10 +314,8 @@ struct BetterTextInputNode : Modify const std::string& clipboardText = clipboard::read(); const int pos = m_fields->m_pos == -1 ? m_fields->m_string.size() : m_fields->m_pos; - log::debug("from: {} len: {} text: \"{}\"", m_fields->m_highlighted.getFromPos(), m_fields->m_highlighted.getLength(), clipboardText); - if (m_fields->m_highlighted.isHighlighting()) - insertStringAtPos(m_fields->m_highlighted.getFromPos(), m_fields->m_highlighted.getLength(), clipboardText); + insertStrAtPos(m_fields->m_highlighted.getFromPos(), m_fields->m_highlighted.getLength(), clipboardText); else { setAndUpdateString(BI::utils::insertStrAtIndex(m_fields->m_string, m_fields->m_pos, clipboardText)); @@ -332,7 +330,7 @@ struct BetterTextInputNode : Modify clipboard::write(m_fields->m_highlighted.str.data()); - insertStringAtPos( + insertStrAtPos( m_fields->m_highlighted.getFromPos(), m_fields->m_highlighted.getLength(), "" @@ -413,7 +411,7 @@ struct BetterTextInputNode : Modify ) return; if (m_fields->m_highlighted.isHighlighting()) - insertStringAtPos( + insertStrAtPos( m_fields->m_highlighted.getFromPos(), m_fields->m_highlighted.getLength(), "" @@ -433,19 +431,15 @@ struct BetterTextInputNode : Modify * @param len * @param str */ - void insertStringAtPos(int pos, std::size_t len, const std::string_view str) + void insertStrAtPos(int pos, std::size_t len, const std::string& str) { - log::debug("before inputting str: {}", m_fields->m_string); - m_fields->m_string.replace(pos, len, str); - log::debug("after inputting str: {}", m_fields->m_string); + const std::size_t endPos = m_fields->m_highlighted.getFromPos() + str.length(); + + m_fields->m_string.replace(pos, len, str.data()); setAndUpdateString(m_fields->m_string); - updateBlinkLabelToCharForced( - (m_fields->m_highlighted.getFromPos() + str.length()) == m_fields->m_string.length() - ? -1 - : m_fields->m_highlighted.getFromPos() + str.length() - ); + updateBlinkLabelToCharForced(endPos == m_fields->m_string.length() ? -1 : endPos); } /** @@ -467,7 +461,7 @@ struct BetterTextInputNode : Modify if (m_fields->m_highlighted.isHighlighting()) { - insertStringAtPos( + insertStrAtPos( m_fields->m_highlighted.getFromPos(), m_fields->m_highlighted.getLength(), ""