Skip to content

Commit

Permalink
chore: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
keshavbhatt committed Jan 27, 2023
1 parent 011db44 commit 085205e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 52 deletions.
44 changes: 22 additions & 22 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void MainWindow::restoreMainWindow() {
if (settings.value("geometry").isValid()) {
restoreGeometry(settings.value("geometry").toByteArray());
QPoint pos = QCursor::pos();
for (QScreen *screen : QGuiApplication::screens()) {
auto localScreens = QGuiApplication::screens();
for (auto screen : qAsConst(localScreens)) {
QRect screenRect = screen->geometry();
if (screenRect.contains(pos)) {
this->move(screenRect.center() - this->rect().center());
Expand Down Expand Up @@ -356,9 +357,8 @@ void MainWindow::initSettingWidget() {
});

connect(settingsWidget, &SettingsWidget::notificationPopupTimeOutChanged,
settingsWidget, [=]() {
setNotificationPresenter(webEngine->page()->profile());
});
settingsWidget,
[=]() { setNotificationPresenter(webEngine->page()->profile()); });

connect(settingsWidget, &SettingsWidget::notify, settingsWidget,
[=](QString message) { notify("", message); });
Expand All @@ -381,7 +381,7 @@ void MainWindow::initSettingWidget() {
settings.value("lockscreen", false).toBool());

// spell checker
settingsWidget->loadDictionaries(m_dictionaries);
settingsWidget->loadDictionaries(dictionaries);
}
}

Expand All @@ -392,7 +392,8 @@ void MainWindow::changeEvent(QEvent *e) {
QMainWindow::changeEvent(e);
}

void MainWindow::handleZoomOnWindowStateChange(QWindowStateChangeEvent *ev) {
void MainWindow::handleZoomOnWindowStateChange(
const QWindowStateChangeEvent *ev) {
if (settingsWidget != nullptr) {
if (ev->oldState().testFlag(Qt::WindowMaximized) &&
windowState().testFlag(Qt::WindowNoState)) {
Expand Down Expand Up @@ -743,17 +744,18 @@ void MainWindow::appAutoLockChanged() {
void MainWindow::checkWindowState() {
QObject *tray_icon_menu = this->findChild<QObject *>("trayIconMenu");
if (tray_icon_menu != nullptr) {
QMenu *menu = qobject_cast<QMenu *>(tray_icon_menu);
if (this->isVisible()) {
((QMenu *)(tray_icon_menu))->actions().at(0)->setDisabled(false);
((QMenu *)(tray_icon_menu))->actions().at(1)->setDisabled(true);
menu->actions().at(0)->setDisabled(false);
menu->actions().at(1)->setDisabled(true);
} else {
((QMenu *)(tray_icon_menu))->actions().at(0)->setDisabled(true);
((QMenu *)(tray_icon_menu))->actions().at(1)->setDisabled(false);
menu->actions().at(0)->setDisabled(true);
menu->actions().at(1)->setDisabled(false);
}
if (lockWidget && lockWidget->getIsLocked()) {
((QMenu *)(tray_icon_menu))->actions().at(4)->setDisabled(true);
menu->actions().at(4)->setDisabled(true);
} else {
((QMenu *)(tray_icon_menu))->actions().at(4)->setDisabled(false);
menu->actions().at(4)->setDisabled(false);
}
}
}
Expand Down Expand Up @@ -804,9 +806,9 @@ void MainWindow::createWebEngine() {
widgetSize.setHorizontalStretch(1);
widgetSize.setVerticalStretch(1);

m_dictionaries = Dictionaries::GetDictionaries();
dictionaries = Dictionaries::GetDictionaries();

WebView *webEngineView = new WebView(this, m_dictionaries);
WebView *webEngineView = new WebView(this, dictionaries);
setCentralWidget(webEngineView);
webEngineView->setSizePolicy(widgetSize);
webEngineView->show();
Expand All @@ -832,11 +834,11 @@ const QIcon MainWindow::getTrayIcon(const int &notificationCount) const {
}

void MainWindow::createWebPage(bool offTheRecord) {
if (offTheRecord && !m_otrProfile) {
m_otrProfile.reset(new QWebEngineProfile);
if (offTheRecord && !otrProfile) {
otrProfile.reset(new QWebEngineProfile);
}
auto profile =
offTheRecord ? m_otrProfile.get() : QWebEngineProfile::defaultProfile();
offTheRecord ? otrProfile.get() : QWebEngineProfile::defaultProfile();

QStringList dict_names;
dict_names.append(settings.value("sc_dict", "en-US").toString());
Expand Down Expand Up @@ -864,7 +866,7 @@ void MainWindow::createWebPage(bool offTheRecord) {
QUrl("https://web.whatsapp.com?v=" + QString::number(randomValue)));

connect(profile, &QWebEngineProfile::downloadRequested,
&m_downloadManagerWidget, &DownloadManagerWidget::downloadRequested);
&downloadManagerWidget, &DownloadManagerWidget::downloadRequested);

connect(page, SIGNAL(fullScreenRequested(QWebEngineFullScreenRequest)), this,
SLOT(fullScreenRequested(QWebEngineFullScreenRequest)));
Expand Down Expand Up @@ -971,8 +973,6 @@ void MainWindow::handleLoadFinished(bool loaded) {
}
}



void MainWindow::checkLoadedCorrectly() {
if (webEngine && webEngine->page()) {
// test 1 based on the class name of body tag of the page
Expand Down Expand Up @@ -1108,7 +1108,7 @@ void MainWindow::doReload(bool byPassCache, bool isAskedByCLI,
bool byLoadingQuirk) {
if (byLoadingQuirk) {
webEngine->triggerPageAction(QWebEnginePage::ReloadAndBypassCache,
byPassCache);
byPassCache);
} else {
if (lockWidget && !lockWidget->getIsLocked()) {
this->notify(QApplication::applicationName(),
Expand All @@ -1125,7 +1125,7 @@ void MainWindow::doReload(bool byPassCache, bool isAskedByCLI,
return;
}
webEngine->triggerPageAction(QWebEnginePage::ReloadAndBypassCache,
byPassCache);
byPassCache);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ protected slots:
QSystemTrayIcon *trayIcon;
QWebEngineView *webEngine;
SettingsWidget *settingsWidget = nullptr;
DownloadManagerWidget m_downloadManagerWidget;
QScopedPointer<QWebEngineProfile> m_otrProfile;
DownloadManagerWidget downloadManagerWidget;
QScopedPointer<QWebEngineProfile> otrProfile;
Lock *lockWidget = nullptr;
int correctlyLoaderRetries = 4;
QStringList m_dictionaries;
QStringList dictionaries;
AutoLockEventFilter *autoLockEventFilter = nullptr;

private slots:
Expand All @@ -121,7 +121,7 @@ private slots:
void quitApp();
void initRateWidget();
void initThemes();
void handleZoomOnWindowStateChange(QWindowStateChangeEvent *ev);
void handleZoomOnWindowStateChange(const QWindowStateChangeEvent *ev);
void handleZoom();
void changeLockPassword();
void forceLogOut();
Expand Down
12 changes: 6 additions & 6 deletions src/webenginepage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ void WebEnginePage::handleLoadFinished(bool ok) {
QWebEnginePage::PermissionPolicy::PermissionGrantedByUser);
}

if(ok){
injectMutationObserver();
injectPreventScrollWheelZoomHelper();
injectFullWidthJavaScript();
injectClassChangeObserver();
injectNewChatJavaScript();
if (ok) {
injectMutationObserver();
injectPreventScrollWheelZoomHelper();
injectFullWidthJavaScript();
injectClassChangeObserver();
injectNewChatJavaScript();
}
}

Expand Down
41 changes: 22 additions & 19 deletions src/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

WebView::WebView(QWidget *parent, QStringList dictionaries)
: QWebEngineView(parent) {
m_dictionaries = dictionaries;
dictionaries = dictionaries;

QObject *parentMainWindow = this->parent();
while (!parentMainWindow->objectName().contains("MainWindow")) {
Expand Down Expand Up @@ -49,18 +49,23 @@ WebView::WebView(QWidget *parent, QStringList dictionaries)
}

void WebView::wheelEvent(QWheelEvent *event) {
if (event->modifiers().testFlag(Qt::ControlModifier)) {
bool controlKeyIsHeld =
QGuiApplication::keyboardModifiers().testFlag(Qt::ControlModifier);
// this doesn't work, (even after checking the global QApplication keyboard
// modifiers) as expected, the Ctrl+wheel is managed by Chromium
// WebenginePage directly. So, we manage it by injecting js to page using
// WebEnginePage::injectPreventScrollWheelZoomHelper
if ((event->modifiers() & Qt::ControlModifier) != 0 || controlKeyIsHeld) {
qDebug() << "skipped ctrl + m_wheel event on webengineview";
// do nothing
event->ignore();
} else {
qDebug() << "wheel event on webengine view";
QWebEngineView::wheelEvent(event);
}
}

void WebView::contextMenuEvent(QContextMenuEvent *event) {

QMenu *menu = page()->createStandardContextMenu();
auto menu = page()->createStandardContextMenu();
menu->setAttribute(Qt::WA_DeleteOnClose, true);
// hide reload, back, forward, savepage, copyimagelink menus
foreach (auto *action, menu->actions()) {
Expand All @@ -87,29 +92,27 @@ void WebView::contextMenuEvent(QContextMenuEvent *event) {
return;
}

QWebEngineProfile *profile = page()->profile();
const QStringList &languages = profile->spellCheckLanguages();

auto pageWebengineProfile = page()->profile();
const QStringList &languages = pageWebengineProfile->spellCheckLanguages();
menu->addSeparator();

QAction *spellcheckAction = new QAction(tr("Check Spelling"), menu);
auto *spellcheckAction = new QAction(tr("Check Spelling"), menu);
spellcheckAction->setCheckable(true);
spellcheckAction->setChecked(profile->isSpellCheckEnabled());
spellcheckAction->setChecked(pageWebengineProfile->isSpellCheckEnabled());
connect(spellcheckAction, &QAction::toggled, this,
[profile, this](bool toogled) {
profile->setSpellCheckEnabled(toogled);
[pageWebengineProfile, this](bool toogled) {
pageWebengineProfile->setSpellCheckEnabled(toogled);
settings.setValue("sc_enabled", toogled);
});
menu->addAction(spellcheckAction);

if (profile->isSpellCheckEnabled()) {
QMenu *subMenu = menu->addMenu(tr("Select Language"));
for (const QString &dict : qAsConst(m_dictionaries)) {
QAction *action = subMenu->addAction(dict);
if (pageWebengineProfile->isSpellCheckEnabled()) {
auto subMenu = menu->addMenu(tr("Select Language"));
for (const QString &dict : qAsConst(dictionaries)) {
auto action = subMenu->addAction(dict);
action->setCheckable(true);
action->setChecked(languages.contains(dict));
connect(action, &QAction::triggered, this, [profile, dict, this]() {
profile->setSpellCheckLanguages(QStringList() << dict);
connect(action, &QAction::triggered, this, [pageWebengineProfile, dict, this]() {
pageWebengineProfile->setSpellCheckLanguages(QStringList() << dict);
settings.setValue("sc_dict", dict);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected slots:
void wheelEvent(QWheelEvent *event);

private:
QStringList m_dictionaries;
QStringList dictionaries;
QSettings settings;
};

Expand Down

0 comments on commit 085205e

Please sign in to comment.