aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/platform/gui/Keyboard.hpp3
-rw-r--r--include/cru/ui/helper/ShortcutHub.hpp12
-rw-r--r--src/osx/gui/Keyboard.mm2
-rw-r--r--src/osx/gui/Window.mm26
-rw-r--r--src/platform/gui/Keyboard.cpp7
-rw-r--r--src/ui/controls/TextHostControlService.cpp2
6 files changed, 36 insertions, 16 deletions
diff --git a/include/cru/platform/gui/Keyboard.hpp b/include/cru/platform/gui/Keyboard.hpp
index 80524133..926b8d91 100644
--- a/include/cru/platform/gui/Keyboard.hpp
+++ b/include/cru/platform/gui/Keyboard.hpp
@@ -110,7 +110,8 @@ enum class KeyCode {
NumPad9,
LeftCommand,
RightCommand,
- Return
+ Return,
+ Space
};
namespace details {
diff --git a/include/cru/ui/helper/ShortcutHub.hpp b/include/cru/ui/helper/ShortcutHub.hpp
index ed90381d..5b9612ba 100644
--- a/include/cru/ui/helper/ShortcutHub.hpp
+++ b/include/cru/ui/helper/ShortcutHub.hpp
@@ -49,7 +49,7 @@ class ShortcutKeyBind {
return !this->operator==(other);
}
- String ToString() {
+ String ToString() const {
String result = u"(";
result += platform::gui::ToString(modifier_);
result += u")";
@@ -61,6 +61,10 @@ class ShortcutKeyBind {
platform::gui::KeyCode key_;
platform::gui::KeyModifier modifier_;
};
+
+inline String ToString(const ShortcutKeyBind& key_bind) {
+ return key_bind.ToString();
+}
} // namespace cru::ui::helper
namespace std {
@@ -78,7 +82,7 @@ struct hash<cru::ui::helper::ShortcutKeyBind> {
namespace cru::ui::helper {
struct Shortcut {
// Just for debug.
- std::u16string name;
+ String name;
ShortcutKeyBind key_bind;
// Return true if it consumes the shortcut. Or return false if it does not
// handle the shortcut.
@@ -87,7 +91,7 @@ struct Shortcut {
struct ShortcutInfo {
int id;
- std::u16string name;
+ String name;
ShortcutKeyBind key_bind;
std::function<bool()> handler;
};
@@ -101,7 +105,7 @@ class ShortcutHub : public Object {
~ShortcutHub() override = default;
- int RegisterShortcut(std::u16string name, ShortcutKeyBind bind,
+ int RegisterShortcut(String name, ShortcutKeyBind bind,
std::function<bool()> handler) {
return RegisterShortcut({std::move(name), bind, std::move(handler)});
}
diff --git a/src/osx/gui/Keyboard.mm b/src/osx/gui/Keyboard.mm
index 8e14ec83..ff2f110b 100644
--- a/src/osx/gui/Keyboard.mm
+++ b/src/osx/gui/Keyboard.mm
@@ -87,6 +87,7 @@ KeyCode KeyCodeFromOsxToCru(unsigned short n) {
CRU_DEFINE_KEYCODE_MAP(kVK_ANSI_Keypad7, KeyCode::NumPad7)
CRU_DEFINE_KEYCODE_MAP(kVK_ANSI_Keypad8, KeyCode::NumPad8)
CRU_DEFINE_KEYCODE_MAP(kVK_ANSI_Keypad9, KeyCode::NumPad9)
+ CRU_DEFINE_KEYCODE_MAP(kVK_Space, KeyCode::Space)
default:
return KeyCode::Unknown;
}
@@ -177,6 +178,7 @@ unsigned short KeyCodeFromCruToOsx(KeyCode k) {
CRU_DEFINE_KEYCODE_MAP(KeyCode::NumPad7, kVK_ANSI_Keypad7)
CRU_DEFINE_KEYCODE_MAP(KeyCode::NumPad8, kVK_ANSI_Keypad8)
CRU_DEFINE_KEYCODE_MAP(KeyCode::NumPad9, kVK_ANSI_Keypad9)
+ CRU_DEFINE_KEYCODE_MAP(KeyCode::Space, kVK_Space)
default:
return 0;
}
diff --git a/src/osx/gui/Window.mm b/src/osx/gui/Window.mm
index 356c6be6..04ec4bce 100644
--- a/src/osx/gui/Window.mm
+++ b/src/osx/gui/Window.mm
@@ -432,9 +432,10 @@ cru::platform::gui::KeyModifier GetKeyModifier(NSEvent* event) {
namespace {
using cru::platform::gui::KeyCode;
-const std::unordered_set<KeyCode> bypass_codes{
- KeyCode::Backspace, KeyCode::Delete, KeyCode::Left, KeyCode::Right, KeyCode::Up,
- KeyCode::Down, KeyCode::Home, KeyCode::End, KeyCode::PageUp, KeyCode::PageDown};
+const std::unordered_set<KeyCode> bypass_codes{KeyCode::Return, KeyCode::Space, KeyCode::Backspace,
+ KeyCode::Delete, KeyCode::Left, KeyCode::Right,
+ KeyCode::Up, KeyCode::Down, KeyCode::Home,
+ KeyCode::End, KeyCode::PageUp, KeyCode::PageDown};
}
- (void)keyDown:(NSEvent*)event {
@@ -448,9 +449,22 @@ const std::unordered_set<KeyCode> bypass_codes{
auto c = cru::platform::gui::osx::KeyCodeFromOsxToCru(event.keyCode);
- if (!(input_context->GetCompositionText().text.empty() && bypass_codes.count(c)) &&
- input_context->IsEnabled()) {
- handled = [[self inputContext] handleEvent:event];
+ if (input_context->IsEnabled()) {
+ if (bypass_codes.count(c)) {
+ if (!(input_context->GetCompositionText().text.empty())) {
+ handled = [[self inputContext] handleEvent:event];
+ } else {
+ if (c == KeyCode::Return) {
+ _input_context_p->RaiseTextEvent(u"\n");
+ handled = true;
+ } else if (c == KeyCode::Space) {
+ _input_context_p->RaiseTextEvent(u" ");
+ handled = true;
+ }
+ }
+ } else {
+ handled = [[self inputContext] handleEvent:event];
+ }
}
if (!handled) {
diff --git a/src/platform/gui/Keyboard.cpp b/src/platform/gui/Keyboard.cpp
index 7d4903e5..08be37c9 100644
--- a/src/platform/gui/Keyboard.cpp
+++ b/src/platform/gui/Keyboard.cpp
@@ -114,9 +114,8 @@ String ToString(KeyCode key_code) {
return key_code_string_list[static_cast<int>(key_code)].ToString();
}
-std::u16string ToString(KeyModifier key_modifier,
- std::u16string_view separator) {
- std::vector<std::u16string> list;
+String ToString(KeyModifier key_modifier, StringView separator) {
+ std::vector<String> list;
if (key_modifier & KeyModifiers::shift) {
list.push_back(u"Shift");
}
@@ -130,7 +129,7 @@ std::u16string ToString(KeyModifier key_modifier,
}
if (list.empty()) return u"";
- std::u16string result = list.front();
+ String result = list.front();
for (auto iter = list.cbegin() + 1; iter != list.cend(); ++iter) {
result += separator;
result += *iter;
diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp
index cbbfde58..e3d25f14 100644
--- a/src/ui/controls/TextHostControlService.cpp
+++ b/src/ui/controls/TextHostControlService.cpp
@@ -479,7 +479,7 @@ void TextHostControlService::SetUpShortcuts() {
auto text = this->GetTextView();
auto caret = this->GetCaretPosition();
auto new_position = pattern.Move(this, text, caret);
- this->SetSelection(caret);
+ this->SetSelection(new_position);
return true;
});