Added ability to apply formatting from Mac menu.

This commit is contained in:
23rd 2019-08-11 19:27:59 +03:00 committed by John Preston
parent 7316d24ca4
commit 1a06714f3a
4 changed files with 81 additions and 21 deletions

View File

@ -114,6 +114,13 @@ private:
QAction *psNewChannel = nullptr;
QAction *psShowTelegram = nullptr;
QAction *psBold = nullptr;
QAction *psItalic = nullptr;
QAction *psUnderline = nullptr;
QAction *psStrikeOut = nullptr;
QAction *psMonospace = nullptr;
QAction *psClearFormat = nullptr;
int _customTitleHeight = 0;
};

View File

@ -638,6 +638,23 @@ void MainWindow::psFirstShow() {
createGlobalMenu();
}
namespace {
void _sendKeySequence(Qt::Key key, Qt::KeyboardModifiers modifiers = Qt::NoModifier) {
QWidget *focused = QApplication::focusWidget();
if (qobject_cast<QLineEdit*>(focused) || qobject_cast<QTextEdit*>(focused) || qobject_cast<HistoryInner*>(focused)) {
QApplication::postEvent(focused, new QKeyEvent(QEvent::KeyPress, key, modifiers));
QApplication::postEvent(focused, new QKeyEvent(QEvent::KeyRelease, key, modifiers));
}
}
void _forceDisabled(QAction *action, bool disabled) {
if (action->isEnabled()) {
if (disabled) action->setDisabled(true);
} else if (!disabled) {
action->setDisabled(false);
}
}
}
void MainWindow::createGlobalMenu() {
auto main = psMainMenu.addMenu(qsl("Telegram"));
auto about = main->addAction(tr::lng_mac_menu_about_telegram(tr::now, lt_telegram, qsl("Telegram")));
@ -662,6 +679,51 @@ void MainWindow::createGlobalMenu() {
psCopy = edit->addAction(tr::lng_mac_menu_copy(tr::now), this, SLOT(psMacCopy()), QKeySequence::Copy);
psPaste = edit->addAction(tr::lng_mac_menu_paste(tr::now), this, SLOT(psMacPaste()), QKeySequence::Paste);
psDelete = edit->addAction(tr::lng_mac_menu_delete(tr::now), this, SLOT(psMacDelete()), QKeySequence(Qt::ControlModifier | Qt::Key_Backspace));
edit->addSeparator();
psBold = edit->addAction(
tr::lng_menu_formatting_bold(tr::now),
this,
[=] {
_sendKeySequence(Qt::Key_B, Qt::ControlModifier);
},
QKeySequence::Bold);
psItalic = edit->addAction(
tr::lng_menu_formatting_italic(tr::now),
this,
[=] {
_sendKeySequence(Qt::Key_I, Qt::ControlModifier);
},
QKeySequence::Italic);
psUnderline = edit->addAction(
tr::lng_menu_formatting_underline(tr::now),
this,
[=] {
_sendKeySequence(Qt::Key_U, Qt::ControlModifier);
},
QKeySequence::Underline);
psStrikeOut = edit->addAction(
tr::lng_menu_formatting_strike_out(tr::now),
this,
[=] {
_sendKeySequence(Qt::Key_X, Qt::ControlModifier | Qt::ShiftModifier);
},
Ui::kStrikeOutSequence);
psMonospace = edit->addAction(
tr::lng_menu_formatting_monospace(tr::now),
this,
[=] {
_sendKeySequence(Qt::Key_M, Qt::ControlModifier | Qt::ShiftModifier);
},
Ui::kMonospaceSequence);
psClearFormat = edit->addAction(
tr::lng_menu_formatting_clear(tr::now),
this,
[=] {
_sendKeySequence(Qt::Key_N, Qt::ControlModifier | Qt::ShiftModifier);
},
Ui::kClearFormatSequence);
edit->addSeparator();
psSelectAll = edit->addAction(tr::lng_mac_menu_select_all(tr::now), this, SLOT(psMacSelectAll()), QKeySequence::SelectAll);
@ -689,23 +751,6 @@ void MainWindow::createGlobalMenu() {
updateGlobalMenu();
}
namespace {
void _sendKeySequence(Qt::Key key, Qt::KeyboardModifiers modifiers = Qt::NoModifier) {
QWidget *focused = QApplication::focusWidget();
if (qobject_cast<QLineEdit*>(focused) || qobject_cast<QTextEdit*>(focused) || qobject_cast<HistoryInner*>(focused)) {
QApplication::postEvent(focused, new QKeyEvent(QEvent::KeyPress, key, modifiers));
QApplication::postEvent(focused, new QKeyEvent(QEvent::KeyRelease, key, modifiers));
}
}
void _forceDisabled(QAction *action, bool disabled) {
if (action->isEnabled()) {
if (disabled) action->setDisabled(true);
} else if (!disabled) {
action->setDisabled(false);
}
}
}
void MainWindow::psMacUndo() {
_sendKeySequence(Qt::Key_Z, Qt::ControlModifier);
}
@ -789,6 +834,13 @@ void MainWindow::updateGlobalMenuHook() {
_forceDisabled(psNewGroup, inactive || support);
_forceDisabled(psNewChannel, inactive || support);
_forceDisabled(psShowTelegram, App::wnd()->isActive());
_forceDisabled(psBold, !showTouchBarItem);
_forceDisabled(psItalic, !showTouchBarItem);
_forceDisabled(psUnderline, !showTouchBarItem);
_forceDisabled(psStrikeOut, !showTouchBarItem);
_forceDisabled(psMonospace, !showTouchBarItem);
_forceDisabled(psClearFormat, !showTouchBarItem);
}
bool MainWindow::psFilterNativeEvent(void *event) {

View File

@ -47,10 +47,6 @@ const auto kNewlineChars = QString("\r\n")
+ QChar(0xfdd1) // QTextEndOfFrame
+ QChar(QChar::ParagraphSeparator)
+ QChar(QChar::LineSeparator);
const auto kClearFormatSequence = QKeySequence("ctrl+shift+n");
const auto kStrikeOutSequence = QKeySequence("ctrl+shift+x");
const auto kMonospaceSequence = QKeySequence("ctrl+shift+m");
const auto kEditLinkSequence = QKeySequence("ctrl+k");
class InputDocument : public QTextDocument {
public:

View File

@ -15,6 +15,11 @@ class UserData;
namespace Ui {
const auto kClearFormatSequence = QKeySequence("ctrl+shift+n");
const auto kStrikeOutSequence = QKeySequence("ctrl+shift+x");
const auto kMonospaceSequence = QKeySequence("ctrl+shift+m");
const auto kEditLinkSequence = QKeySequence("ctrl+k");
class PopupMenu;
void InsertEmojiAtCursor(QTextCursor cursor, EmojiPtr emoji);