Browse Source

Merge pull request #950 from arkpar/mix_osx

Fixing clipboard shortcusts on Mac
cl-refactor
Gav Wood 10 years ago
parent
commit
1b0ced4f6d
  1. 7
      mix/AppContext.cpp
  2. 4
      mix/AppContext.h
  3. 15
      mix/qml/WebCodeEditor.qml
  4. 2
      mix/qml/html/cm/codemirror.js
  5. 20
      mix/qml/html/codeeditor.js

7
mix/AppContext.cpp

@ -50,6 +50,7 @@ AppContext::AppContext(QQmlApplicationEngine* _engine)
m_codeModel.reset(new CodeModel(this)); m_codeModel.reset(new CodeModel(this));
m_clientModel.reset(new ClientModel(this)); m_clientModel.reset(new ClientModel(this));
m_fileIo.reset(new FileIo()); m_fileIo.reset(new FileIo());
connect(QApplication::clipboard(), &QClipboard::dataChanged, [this] { emit clipboardChanged();});
} }
AppContext::~AppContext() AppContext::~AppContext()
@ -106,6 +107,12 @@ void AppContext::displayMessageDialog(QString _title, QString _message)
QMetaObject::invokeMethod(dialogWin, "open"); QMetaObject::invokeMethod(dialogWin, "open");
} }
QString AppContext::clipboard() const
{
QClipboard *clipboard = QApplication::clipboard();
return clipboard->text();
}
void AppContext::toClipboard(QString _text) void AppContext::toClipboard(QString _text)
{ {
QClipboard *clipboard = QApplication::clipboard(); QClipboard *clipboard = QApplication::clipboard();

4
mix/AppContext.h

@ -48,6 +48,7 @@ class FileIo;
class AppContext: public QObject class AppContext: public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString clipboard READ clipboard WRITE toClipboard NOTIFY clipboardChanged)
public: public:
AppContext(QQmlApplicationEngine* _engine); AppContext(QQmlApplicationEngine* _engine);
@ -64,10 +65,13 @@ public:
void displayMessageDialog(QString _title, QString _message); void displayMessageDialog(QString _title, QString _message);
/// Copy text to clipboard /// Copy text to clipboard
Q_INVOKABLE void toClipboard(QString _text); Q_INVOKABLE void toClipboard(QString _text);
/// Get text from clipboard
QString clipboard() const;
signals: signals:
/// Triggered once components have been loaded /// Triggered once components have been loaded
void appLoaded(); void appLoaded();
void clipboardChanged();
private: private:
QQmlApplicationEngine* m_applicationEngine; //owned by app QQmlApplicationEngine* m_applicationEngine; //owned by app

15
mix/qml/WebCodeEditor.qml

@ -4,6 +4,7 @@ import QtQuick.Layouts 1.0
import QtQuick.Controls.Styles 1.1 import QtQuick.Controls.Styles 1.1
import CodeEditorExtensionManager 1.0 import CodeEditorExtensionManager 1.0
import QtWebEngine 1.0 import QtWebEngine 1.0
import QtWebEngine.experimental 1.0
Item { Item {
signal editorTextChanged signal editorTextChanged
@ -29,6 +30,18 @@ Item {
return currentText; return currentText;
} }
function syncClipboard() {
if (Qt.platform.os == "osx") {
var text = appContext.clipboard;
editorBrowser.runJavaScript("setClipboardBase64(\"" + Qt.btoa(text) + "\")");
}
}
Connections {
target: appContext
onClipboardChanged: syncClipboard()
}
anchors.top: parent.top anchors.top: parent.top
id: codeEditorView id: codeEditorView
anchors.fill: parent anchors.fill: parent
@ -36,6 +49,7 @@ Item {
id: editorBrowser id: editorBrowser
url: "qrc:///qml/html/codeeditor.html" url: "qrc:///qml/html/codeeditor.html"
anchors.fill: parent anchors.fill: parent
experimental.settings.javascriptCanAccessClipboard: true
onJavaScriptConsoleMessage: { onJavaScriptConsoleMessage: {
console.log("editor: " + sourceID + ":" + lineNumber + ":" + message); console.log("editor: " + sourceID + ":" + lineNumber + ":" + message);
} }
@ -47,6 +61,7 @@ Item {
setText(currentText, currentMode); setText(currentText, currentMode);
runJavaScript("getTextChanged()", function(result) { }); runJavaScript("getTextChanged()", function(result) { });
pollTimer.running = true; pollTimer.running = true;
syncClipboard();
} }
} }

2
mix/qml/html/cm/codemirror.js

@ -29,7 +29,7 @@
var ie = ie_upto10 || ie_11up; var ie = ie_upto10 || ie_11up;
var ie_version = ie && (ie_upto10 ? document.documentMode || 6 : ie_11up[1]); var ie_version = ie && (ie_upto10 ? document.documentMode || 6 : ie_11up[1]);
var webkit = /WebKit\//.test(navigator.userAgent); var webkit = /WebKit\//.test(navigator.userAgent);
var qtwebkit = webkit && /Qt\/\d+\.\d+/.test(navigator.userAgent); var qtwebkit = true;
var chrome = /Chrome\//.test(navigator.userAgent); var chrome = /Chrome\//.test(navigator.userAgent);
var presto = /Opera\//.test(navigator.userAgent); var presto = /Opera\//.test(navigator.userAgent);
var safari = /Apple Computer/.test(navigator.vendor); var safari = /Apple Computer/.test(navigator.vendor);

20
mix/qml/html/codeeditor.js

@ -6,6 +6,7 @@ var editor = CodeMirror(document.body, {
autofocus: true, autofocus: true,
}); });
editor.setOption("theme", "solarized dark"); editor.setOption("theme", "solarized dark");
editor.setOption("indentUnit", 4); editor.setOption("indentUnit", 4);
editor.setOption("indentWithTabs", true); editor.setOption("indentWithTabs", true);
@ -18,11 +19,24 @@ editor.on("change", function(eMirror, object) {
}); });
var mac = /Mac/.test(navigator.platform);
if (mac === true) {
editor.setOption("extraKeys", {
"Cmd-V": function(cm) {
cm.replaceSelection(clipboard);
},
"Cmd-X": function(cm) {
window.document.execCommand("cut");
},
"Cmd-C": function(cm) {
window.document.execCommand("copy");
}});
}
getTextChanged = function() { getTextChanged = function() {
return editor.changeRegistered; return editor.changeRegistered;
}; };
getText = function() { getText = function() {
editor.changeRegistered = false; editor.changeRegistered = false;
return editor.getValue(); return editor.getValue();
@ -42,3 +56,7 @@ setText = function(text) {
setMode = function(mode) { setMode = function(mode) {
this.editor.setOption("mode", mode); this.editor.setOption("mode", mode);
}; };
setClipboardBase64 = function(text) {
clipboard = window.atob(text);
};

Loading…
Cancel
Save