-
Notifications
You must be signed in to change notification settings - Fork 984
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add react-native-desktop-menu native module
Signed-off-by: Vitaliy Vlasov <siphiuel@gmail.com>
- Loading branch information
Vitaliy Vlasov
committed
Dec 1, 2018
1 parent
c91e514
commit dc4841f
Showing
15 changed files
with
202 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
set(CMAKE_INCLUDE_CURRENT_DIR ON) | ||
|
||
set(REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_TYPE_NAMES ${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_TYPE_NAMES} | ||
\"DesktopMenu\" PARENT_SCOPE) | ||
|
||
set(REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_SRC ${REACT_NATIVE_DESKTOP_EXTERNAL_MODULES_SRC} | ||
${CMAKE_CURRENT_SOURCE_DIR}/desktopmenu.cpp PARENT_SCOPE) | ||
|
||
include(${CMAKE_ROOT}/Modules/ExternalProject.cmake) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "desktopmenu.h" | ||
#include "bridge.h" | ||
|
||
#include <QCoreApplication> | ||
#include <QDebug> | ||
#include <QMenu> | ||
#include <QCursor> | ||
|
||
Q_LOGGING_CATEGORY(DESKTOPMENU, "DesktopMenu") | ||
|
||
namespace { | ||
struct RegisterQMLMetaType { | ||
RegisterQMLMetaType() { qRegisterMetaType<DesktopMenu *>(); } | ||
} registerMetaType; | ||
} // namespace | ||
|
||
class DesktopMenuPrivate { | ||
public: | ||
Bridge *bridge = nullptr; | ||
void createMenu(const QStringList& items, double callback); | ||
private: | ||
void onTriggered(QAction* action); | ||
}; | ||
|
||
void DesktopMenuPrivate::createMenu(const QStringList& items, double callback) { | ||
QMenu* menu = new QMenu(); | ||
for (const QString& name : items) { | ||
menu->addAction(name); | ||
} | ||
QObject::connect(menu, &QMenu::triggered, [=](QAction* action) { | ||
bridge->invokePromiseCallback(callback, QVariantList{action->text()}); | ||
}); | ||
QObject::connect(menu, &QMenu::triggered, menu, &QMenu::deleteLater); | ||
menu->popup(QCursor::pos()); | ||
} | ||
|
||
DesktopMenu::DesktopMenu(QObject *parent) | ||
: QObject(parent), d_ptr(new DesktopMenuPrivate) { | ||
} | ||
|
||
DesktopMenu::~DesktopMenu() { | ||
} | ||
|
||
void DesktopMenu::setBridge(Bridge *bridge) { | ||
Q_D(DesktopMenu); | ||
|
||
d->bridge = bridge; | ||
} | ||
|
||
QString DesktopMenu::moduleName() { return "DesktopMenuManager"; } | ||
|
||
QList<ModuleMethod *> DesktopMenu::methodsToExport() { | ||
return QList<ModuleMethod *>{}; | ||
} | ||
|
||
QVariantMap DesktopMenu::constantsToExport() { return QVariantMap(); } | ||
|
||
void DesktopMenu::show(const QStringList& items, double callback) { | ||
Q_D(DesktopMenu); | ||
d_ptr->createMenu(items, callback); | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef DESKTOPMENU_H | ||
#define DESKTOPMENU_H | ||
|
||
#include "moduleinterface.h" | ||
|
||
#include <QLoggingCategory> | ||
#include <QVariantMap> | ||
|
||
Q_DECLARE_LOGGING_CATEGORY(MENU) | ||
|
||
class DesktopMenuPrivate; | ||
class DesktopMenu : public QObject, public ModuleInterface { | ||
Q_OBJECT | ||
Q_INTERFACES(ModuleInterface) | ||
|
||
Q_DECLARE_PRIVATE(DesktopMenu) | ||
|
||
public: | ||
Q_INVOKABLE DesktopMenu(QObject* parent = 0); | ||
virtual ~DesktopMenu(); | ||
|
||
void setBridge(Bridge* bridge) override; | ||
|
||
QString moduleName() override; | ||
QList<ModuleMethod*> methodsToExport() override; | ||
QVariantMap constantsToExport() override; | ||
|
||
Q_INVOKABLE void show(const QStringList& items, double callback); | ||
|
||
private: | ||
QScopedPointer<DesktopMenuPrivate> d_ptr; | ||
}; | ||
|
||
#endif // DESKTOPMENU_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
type MenuItems = Array<{ | ||
text?: string, | ||
onPress?: ?Function, | ||
}>; | ||
|
||
const NativeModules = require('react-native').NativeModules; | ||
|
||
class DesktopMenu { | ||
|
||
static show( | ||
menuItems?: MenuItems | ||
): void { | ||
var itemNames = menuItems.map(i => i.text); | ||
var itemMap = new Map(); | ||
for (let i = 0; i < menuItems.length; ++i) { | ||
itemMap.set(menuItems[i].text, menuItems[i].onPress); | ||
} | ||
NativeModules.DesktopMenuManager.show( | ||
itemNames, | ||
(name) => { | ||
(itemMap.get(name))(); | ||
} | ||
); | ||
} | ||
} | ||
|
||
module.exports = DesktopMenu; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"private": true, | ||
"nativePackage": true, | ||
"name": "react-native-desktop-menu", | ||
"version": "1.0.0", | ||
"description": "Native popup and context menus for Desktop", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(ns status-im.ui.components.popup-menu.views | ||
(:require [status-im.ui.components.react :as react] | ||
[status-im.react-native.js-dependencies :as rn-dependencies] | ||
[status-im.utils.platform :as platform] | ||
[status-im.i18n :as i18n] | ||
[re-frame.core :as re-frame] | ||
[taoensso.timbre :as log])) | ||
|
||
(defn show-desktop-menu [items] | ||
(.show rn-dependencies/desktop-menu | ||
(clj->js (mapv #(hash-map :text (:text %1) :onPress (:on-select %1)) items)))) | ||
|
||
(defn get-chat-menu-items [group-chat public? chat-id] | ||
(->> [(when (and (not group-chat) (not public?)) | ||
{:text (i18n/label :t/view-profile) | ||
:on-select #(re-frame/dispatch [:show-profile-desktop chat-id])}) | ||
(when (and group-chat (not public?)) | ||
{:text (i18n/label :t/group-info) | ||
:on-select #(re-frame/dispatch [:show-group-chat-profile])}) | ||
{:text (i18n/label :t/clear-history) | ||
:on-select #(re-frame/dispatch [:chat.ui/clear-history-pressed])} | ||
{:text (i18n/label :t/delete-chat) | ||
:on-select #(re-frame/dispatch [(if (and group-chat (not public?)) | ||
:group-chats.ui/remove-chat-pressed | ||
:chat.ui/remove-chat-pressed) | ||
chat-id])}] | ||
(remove nil?))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters