From 1b7816f7ca585a84dbbeb88d6779e9d5d71686cc Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Wed, 19 Sep 2018 23:38:36 +0300 Subject: [PATCH] macOS: Use the native emoji picker fixes #79 --- CMakeLists.txt | 4 ++-- src/TextInputWidget.cpp | 15 +++++++++++++++ src/emoji/MacHelper.h | 10 ++++++++++ src/emoji/MacHelper.mm | 26 ++++++++++++++++++++++++++ src/main.cpp | 10 ++++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/emoji/MacHelper.h create mode 100644 src/emoji/MacHelper.mm diff --git a/CMakeLists.txt b/CMakeLists.txt index 86a4a656..8f179dcd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -376,8 +376,8 @@ else() endif() if (APPLE) - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Foundation") - set(SRC_FILES ${SRC_FILES} src/notifications/ManagerMac.mm) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Foundation -framework Cocoa") + set(SRC_FILES ${SRC_FILES} src/notifications/ManagerMac.mm src/emoji/MacHelper.mm) elseif (WIN32) file(DOWNLOAD "https://raw.githubusercontent.com/mohabouje/WinToast/41ed1c58d5dce0ee9c01dbdeac05be45358d4f57/src/wintoastlib.cpp" diff --git a/src/TextInputWidget.cpp b/src/TextInputWidget.cpp index 7167f572..4a03762d 100644 --- a/src/TextInputWidget.cpp +++ b/src/TextInputWidget.cpp @@ -37,6 +37,10 @@ #include "ui/FlatButton.h" #include "ui/LoadingIndicator.h" +#if defined(Q_OS_MAC) +#include "emoji/MacHelper.h" +#endif + static constexpr size_t INPUT_HISTORY_SIZE = 127; static constexpr int MAX_TEXTINPUT_HEIGHT = 120; static constexpr int InputHeight = 26; @@ -124,6 +128,12 @@ FilteredTextEdit::keyPressEvent(QKeyEvent *event) { const bool isModifier = (event->modifiers() != Qt::NoModifier); +#if defined(Q_OS_MAC) + if (event->modifiers() == (Qt::ControlModifier | Qt::MetaModifier) && + event->key() == Qt::Key_Space) + MacHelper::showEmojiWindow(); +#endif + if (!isModifier) { if (!typingTimer_->isActive()) emit startedTyping(); @@ -503,6 +513,11 @@ TextInputWidget::TextInputWidget(QWidget *parent) emojiBtn_ = new emoji::PickButton(this); emojiBtn_->setToolTip(tr("Emoji")); +#if defined(Q_OS_MAC) + // macOS has a native emoji picker. + emojiBtn_->hide(); +#endif + QIcon emoji_icon; emoji_icon.addFile(":/icons/icons/ui/smile.png"); emojiBtn_->setIcon(emoji_icon); diff --git a/src/emoji/MacHelper.h b/src/emoji/MacHelper.h new file mode 100644 index 00000000..a2e94158 --- /dev/null +++ b/src/emoji/MacHelper.h @@ -0,0 +1,10 @@ +#pragma once + +#include + +class MacHelper +{ +public: + static void showEmojiWindow(); + static void initializeMenus(); +}; diff --git a/src/emoji/MacHelper.mm b/src/emoji/MacHelper.mm new file mode 100644 index 00000000..11dbd7f4 --- /dev/null +++ b/src/emoji/MacHelper.mm @@ -0,0 +1,26 @@ +#include "MacHelper.h" + +#include +#include +#include +#include + +void +MacHelper::showEmojiWindow() +{ + NSApplication *theNSApplication = [NSApplication sharedApplication]; + [theNSApplication orderFrontCharacterPalette:nil]; +} + +void +MacHelper::initializeMenus() +{ + NSApplication *theNSApplication = [NSApplication sharedApplication]; + + NSArray *menus = [theNSApplication mainMenu].itemArray; + NSUInteger size = menus.count; + for (NSUInteger i = 0; i < size; i++) { + NSMenuItem *item = [menus objectAtIndex:i]; + [item setTitle:@"Edit"]; + } +} diff --git a/src/main.cpp b/src/main.cpp index 77e62381..65fb9b75 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,6 +38,10 @@ #include "Utils.h" #include "version.h" +#if defined(Q_OS_MAC) +#include "emoji/MacHelper.h" +#endif + #if defined(Q_OS_LINUX) #include #include @@ -186,6 +190,12 @@ main(int argc, char *argv[]) } }); +#if defined(Q_OS_MAC) + // Temporary solution for the emoji picker until + // nheko has a proper menu bar with more functionality. + MacHelper::initializeMenus(); +#endif + nhlog::ui()->info("starting nheko {}", nheko::version); return app.exec();