bug-fix: fix Command+-/= to zoom out/in in read mode on macOS

This commit is contained in:
Le Tan 2018-06-01 17:24:52 +08:00
parent af8cd0d73b
commit 7f8075f0ca
6 changed files with 42 additions and 19 deletions

View File

@ -88,6 +88,10 @@ if (typeof VAddTOC == 'undefined') {
VAddTOC = false; VAddTOC = false;
} }
if (typeof VOS == 'undefined') {
VOS = 'win';
}
// Whether highlight special blocks like puml, flowchart. // Whether highlight special blocks like puml, flowchart.
var highlightSpecialBlocks = false; var highlightSpecialBlocks = false;
@ -287,6 +291,7 @@ document.onkeydown = function(e) {
var key; var key;
var shift; var shift;
var ctrl; var ctrl;
var meta;
if (e.which) { if (e.which) {
key = e.which; key = e.which;
} else { } else {
@ -295,12 +300,14 @@ document.onkeydown = function(e) {
shift = !!e.shiftKey; shift = !!e.shiftKey;
ctrl = !!e.ctrlKey; ctrl = !!e.ctrlKey;
meta = !!e.metaKey;
switch (key) { switch (key) {
// Skip Ctrl, Shift, Alt, Supper. // Skip Ctrl, Shift, Alt, Supper.
case 16: case 16:
case 17: case 17:
case 18: case 18:
case 91: case 91:
case 92:
clear = false; clear = false;
break; break;
@ -326,7 +333,7 @@ document.onkeydown = function(e) {
case 104: case 104:
case 105: case 105:
{ {
if (pendingKeys.length != 0 || ctrl || shift) { if (pendingKeys.length != 0 || ctrl || shift || meta) {
accept = false; accept = false;
break; break;
} }
@ -338,7 +345,7 @@ document.onkeydown = function(e) {
} }
case 74: // J case 74: // J
if (!ctrl && !shift) { if (!ctrl && !shift && !meta) {
window.scrollBy(0, 100); window.scrollBy(0, 100);
break; break;
} }
@ -347,7 +354,7 @@ document.onkeydown = function(e) {
break; break;
case 75: // K case 75: // K
if (!ctrl && !shift) { if (!ctrl && !shift && !meta) {
window.scrollBy(0, -100); window.scrollBy(0, -100);
break; break;
} }
@ -356,7 +363,7 @@ document.onkeydown = function(e) {
break; break;
case 72: // H case 72: // H
if (!ctrl && !shift) { if (!ctrl && !shift && !meta) {
window.scrollBy(-100, 0); window.scrollBy(-100, 0);
break; break;
} }
@ -365,7 +372,7 @@ document.onkeydown = function(e) {
break; break;
case 76: // L case 76: // L
if (!ctrl && !shift) { if (!ctrl && !shift && !meta) {
window.scrollBy(100, 0); window.scrollBy(100, 0);
break; break;
} }
@ -381,7 +388,7 @@ document.onkeydown = function(e) {
window.scrollTo(scrollLeft, scrollHeight); window.scrollTo(scrollLeft, scrollHeight);
break; break;
} }
} else if (!ctrl) { } else if (!ctrl && !meta) {
if (pendingKeys.length == 0) { if (pendingKeys.length == 0) {
// First g, pend it. // First g, pend it.
pendingKeys.push({ pendingKeys.push({
@ -440,7 +447,7 @@ document.onkeydown = function(e) {
break; break;
} }
} }
} else if (!ctrl) { } else if (!ctrl && !meta) {
// [ // [
if (pendingKeys.length == 0) { if (pendingKeys.length == 0) {
// First [, pend it. // First [, pend it.
@ -483,7 +490,7 @@ document.onkeydown = function(e) {
break; break;
} }
} }
} else if (!ctrl) { } else if (!ctrl && !meta) {
// ] // ]
if (pendingKeys.length == 0) { if (pendingKeys.length == 0) {
// First ], pend it. // First ], pend it.
@ -526,7 +533,7 @@ document.onkeydown = function(e) {
if (accept) { if (accept) {
e.preventDefault(); e.preventDefault();
} else { } else {
content.keyPressEvent(key, ctrl, shift); content.keyPressEvent(key, ctrl, shift, meta);
} }
}; };
@ -994,9 +1001,10 @@ var vds_scrolled = false;
window.onmousedown = function(e) { window.onmousedown = function(e) {
e = e || window.event; e = e || window.event;
var isCtrl = VOS == 'mac' ? e.metaKey : e.ctrlKey;
// Left button and Ctrl key. // Left button and Ctrl key.
if (e.buttons == 1 if (e.buttons == 1
&& e.ctrlKey && isCtrl
&& window.getSelection().type != 'Range') { && window.getSelection().type != 'Range') {
vds_oriMouseClientX = e.clientX; vds_oriMouseClientX = e.clientX;
vds_oriMouseClientY = e.clientY; vds_oriMouseClientY = e.clientY;

View File

@ -794,6 +794,14 @@ QString VUtils::generateHtmlTemplate(const QString &p_template,
extraFile += "<script>var VStylesToInline = '" + g_config->getStylesToInlineWhenCopied() + "';</script>\n"; extraFile += "<script>var VStylesToInline = '" + g_config->getStylesToInlineWhenCopied() + "';</script>\n";
#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
extraFile += "<script>var VOS = 'mac';</script>\n";
#elif defined(Q_OS_WIN)
extraFile += "<script>var VOS = 'win';</script>\n";
#else
extraFile += "<script>var VOS = 'linux';</script>\n";
#endif
QString htmlTemplate(p_template); QString htmlTemplate(p_template);
htmlTemplate.replace(HtmlHolder::c_JSHolder, jsFile); htmlTemplate.replace(HtmlHolder::c_JSHolder, jsFile);
if (!extraFile.isEmpty()) { if (!extraFile.isEmpty()) {

View File

@ -69,9 +69,9 @@ void VDocument::setLog(const QString &p_log)
emit logChanged(p_log); emit logChanged(p_log);
} }
void VDocument::keyPressEvent(int p_key, bool p_ctrl, bool p_shift) void VDocument::keyPressEvent(int p_key, bool p_ctrl, bool p_shift, bool p_meta)
{ {
emit keyPressed(p_key, p_ctrl, p_shift); emit keyPressed(p_key, p_ctrl, p_shift, p_meta);
} }
void VDocument::highlightTextAsync(const QString &p_text, int p_id, int p_timeStamp) void VDocument::highlightTextAsync(const QString &p_text, int p_id, int p_timeStamp)

View File

@ -81,7 +81,7 @@ public slots:
void setHeader(const QString &anchor); void setHeader(const QString &anchor);
void setLog(const QString &p_log); void setLog(const QString &p_log);
void keyPressEvent(int p_key, bool p_ctrl, bool p_shift); void keyPressEvent(int p_key, bool p_ctrl, bool p_shift, bool p_meta);
void updateText(); void updateText();
void highlightTextCB(const QString &p_html, int p_id, int p_timeStamp); void highlightTextCB(const QString &p_html, int p_id, int p_timeStamp);
@ -126,7 +126,7 @@ signals:
void logChanged(const QString &p_log); void logChanged(const QString &p_log);
void keyPressed(int p_key, bool p_ctrl, bool p_shift); void keyPressed(int p_key, bool p_ctrl, bool p_shift, bool p_meta);
void requestHighlightText(const QString &p_text, int p_id, int p_timeStamp); void requestHighlightText(const QString &p_text, int p_id, int p_timeStamp);

View File

@ -709,10 +709,17 @@ void VMdTab::clearSearchedWordHighlight()
} }
} }
void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift) void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift, bool p_meta)
{ {
V_ASSERT(m_webViewer); V_ASSERT(m_webViewer);
#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
bool macCtrl = p_meta;
#else
Q_UNUSED(p_meta);
bool macCtrl = false;
#endif
switch (p_key) { switch (p_key) {
// Esc // Esc
case 27: case 27:
@ -721,7 +728,7 @@ void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift)
// Dash // Dash
case 189: case 189:
if (p_ctrl) { if (p_ctrl || macCtrl) {
// Zoom out. // Zoom out.
zoomWebPage(false); zoomWebPage(false);
} }
@ -730,7 +737,7 @@ void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift)
// Equal // Equal
case 187: case 187:
if (p_ctrl) { if (p_ctrl || macCtrl) {
// Zoom in. // Zoom in.
zoomWebPage(true); zoomWebPage(true);
} }
@ -739,7 +746,7 @@ void VMdTab::handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift)
// 0 // 0
case 48: case 48:
if (p_ctrl) { if (p_ctrl || macCtrl) {
// Recover zoom. // Recover zoom.
m_webViewer->setZoomFactor(1); m_webViewer->setZoomFactor(1);
} }

View File

@ -131,7 +131,7 @@ private slots:
void updateCurrentHeader(int p_blockNumber); void updateCurrentHeader(int p_blockNumber);
// Handle key press event in Web view. // Handle key press event in Web view.
void handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift); void handleWebKeyPressed(int p_key, bool p_ctrl, bool p_shift, bool p_meta);
// m_editor requests to save changes and enter read mode. // m_editor requests to save changes and enter read mode.
void saveAndRead(); void saveAndRead();