aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow/menubar.cpp
blob: f8979de9f0275b29ec74b98c03195a1a6f799b64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * This file is part of smolbote. It's copyrighted by the contributors recorded
 * in the version control history of the file, available from its original
 * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
 *
 * SPDX-License-Identifier: GPL-3.0
 */

#include "menubar.h"
#include "bookmarks/bookmarkswidget.h"
#include "browser.h"
#include "configuration.h"
#include "downloadswidget.h"
#include "mainwindow.h"
#include "subwindow/subwindow.h"
#include "webengine/webprofilemanager.h"
#include "webengine/webview.h"
#include "widgets/menusearch.h"
#include <QDir>
#include <QFileDialog>
#include <QPrintDialog>
#include <QPrinter>
#include <QPrinterInfo>
#include <QToolBar>
#include <QVBoxLayout>
#include <functional>

inline void run_if(SubWindow *_subwindow, const std::function<void(SubWindow *, int)> &f)
{
    if(_subwindow != nullptr) {
        f(_subwindow, _subwindow->currentTabIndex());
    }
}

inline void trigger_if(WebView *_view, QWebEnginePage::WebAction action)
{
    if(_view != nullptr) {
        _view->triggerPageAction(action);
    }
}

inline QDialog *createDevToolsDialog(QWebEnginePage *page)
{
    Q_CHECK_PTR(page);

    auto *popup = new QDialog(nullptr);
    popup->setWindowTitle(QObject::tr("Developer Tools"));
    popup->setAttribute(Qt::WA_DeleteOnClose, true);
    popup->resize(800, 600);

    auto *view = new QWebEngineView(popup);
    auto *devPage = new QWebEnginePage(view);
    view->setPage(devPage);

    auto *l = new QVBoxLayout(popup);
    l->setContentsMargins(0, 0, 0, 0);
    l->addWidget(view);

    page->setDevToolsPage(devPage);
    QObject::connect(popup, &QDialog::destroyed, [page]() {
        page->setDevToolsPage(nullptr);
    });

    return popup;
}

MenuBar::MenuBar(QMenu *appMenu, MainWindow *parent)
    : QMenuBar(parent)
{
    m_parent = parent;
    auto *browser = qobject_cast<Browser *>(qApp);
    Q_CHECK_PTR(browser);
    Configuration conf;

    addMenu(appMenu);

    window = this->addMenu(tr("&Window"));
    {
    auto *actionBookmarks = window->addAction(tr("Bookmarks"), browser, [browser, parent]() {
        browser->showWidget(browser->bookmarks(), parent);
    });
    conf.shortcut<QAction>(*actionBookmarks, "shortcuts.window.bookmarks.show");

    auto *actionDownloads = window->addAction(tr("Downloads"), browser, [browser, parent]() {
        browser->showWidget(browser->downloads(), parent);
    });
    conf.shortcut<QAction>(*actionDownloads, "shortcuts.window.downloads.show");
    window->addSeparator();

        auto *actionNewWindow = window->addAction(tr("New Window"), browser, [browser]() {
            const Session::MainWindow window;
            browser->open({ window }, false);
        });
        conf.shortcut<QAction>(*actionNewWindow, "shortcuts.window.newwindow");

        auto *actionNewSubwindow = window->addAction(tr("New Subwindow"), parent, [parent]() {
            const Session::SubWindow session;
            parent->createSubWindow(session);
        });
        conf.shortcut<QAction>(*actionNewSubwindow, "shortcuts.window.newgroup");

        auto *actionCloseSubwindow = window->addAction(tr("Close Subwindow"), parent, [parent]() {
            parent->currentSubWindow()->close();
        });
        conf.shortcut<QAction>(*actionCloseSubwindow, "shortcuts.subwindow.close");

        window->addSeparator()->setText(tr("Toolbars"));

        bool hasMovableToolbar = false;

        for(auto *toolbar : parent->findChildren<QToolBar *>()) {
            auto *action = window->addAction(toolbar->windowTitle());
            action->setCheckable(true);
            action->setChecked(toolbar->isVisible());
            connect(toolbar, &QToolBar::visibilityChanged, action, &QAction::setChecked);
            connect(action, &QAction::triggered, toolbar, &QToolBar::setVisible);
            conf.shortcut<QAction>(*action, qUtf8Printable(toolbar->objectName() + ".show"));

            if(toolbar->isMovable())
                hasMovableToolbar = true;
        }

        auto *lockAction = window->addAction(tr("Unlock toolbars"));
        lockAction->setCheckable(true);
        lockAction->setChecked(hasMovableToolbar);
        connect(lockAction, &QAction::triggered, parent, [parent](bool checked) {
            for(auto *toolbar : parent->findChildren<QToolBar *>()) {
                toolbar->setMovable(checked);
            }
        });

        window->addSeparator()->setText(tr("Subwindows"));
    }

    auto *subwindow = this->addMenu(tr("&Subwindow"));
    {
        auto *actionNewTab = subwindow->addAction(tr("New Tab"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int) {
                const int index = _subwindow->addTab();
                _subwindow->setCurrentTab(index);
            });
        });
        conf.shortcut<QAction>(*actionNewTab, "shortcuts.subwindow.newtab");

        subwindow->addSeparator();

        auto *actionRestoreTab = subwindow->addAction(tr("Restore last tab"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int) {
                const int index = _subwindow->restoreLastTab();
                _subwindow->setCurrentTab(index);
            });
        });
        conf.shortcut<QAction>(*actionRestoreTab, "shortcuts.subwindow.restoretab");

        auto *restoreTabsMenu = subwindow->addMenu(tr("Restore previous tab"));
        connect(restoreTabsMenu, &QMenu::aboutToShow, parent, [parent, restoreTabsMenu]() {
            restoreTabsMenu->clear();
            auto *_subwindow = parent->currentSubWindow();
            if(_subwindow != nullptr) {
                _subwindow->restoreTabMenu(restoreTabsMenu);
            }
        });

        subwindow->addSeparator()->setText(tr("Tab Settings"));

        auto *actionPinTab = subwindow->addAction(tr("Pin tab"));
        actionPinTab->setCheckable(true);
        actionPinTab->setEnabled(false);

        auto *actionLockClose = subwindow->addAction(tr("Prevent tab from closing"), parent, [parent](bool checked) {
            run_if(parent->currentSubWindow(), [checked](SubWindow *_subwindow, int currentIdx) {
                auto data = _subwindow->tabData(currentIdx);
                data.closeLocked = checked;
                _subwindow->setTabData(data, currentIdx);
            });
        });
        actionLockClose->setCheckable(true);

        auto *actionLockRefresh = subwindow->addAction(tr("Prevent tab from refreshing"), parent, [parent](bool checked) {
            run_if(parent->currentSubWindow(), [checked](SubWindow *_subwindow, int currentIdx) {
                auto data = _subwindow->tabData(currentIdx);
                data.refreshLocked = checked;
                _subwindow->setTabData(data, currentIdx);
            });
        });
        actionLockRefresh->setCheckable(true);

        connect(subwindow, &QMenu::aboutToShow, subwindow, [=]() {
            run_if(parent->currentSubWindow(), [=](SubWindow *_subwindow, int currentIdx) {
                auto data = _subwindow->tabData(currentIdx);
                actionLockClose->setChecked(data.closeLocked);
                actionLockRefresh->setChecked(data.refreshLocked);
            });
        });

        subwindow->addSeparator()->setText(tr("Tab Actions"));

        auto *leftTab = subwindow->addAction(tr("Switch to tab on the left"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                _subwindow->setCurrentTab(qMax(0, currentIdx - 1));
            });
        });
        conf.shortcut<QAction>(*leftTab, "shortcuts.subwindow.tableft");

        auto *moveTabLeft = subwindow->addAction(tr("Move tab left"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                _subwindow->moveTab(currentIdx, currentIdx - 1);
            });
        });
        conf.shortcut<QAction>(*moveTabLeft, "shortcuts.subwindow.movetableft");

        auto *rightTab = subwindow->addAction(tr("Switch to tab on the right"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                _subwindow->setCurrentTab(qMin(currentIdx + 1, _subwindow->tabCount() - 1));
            });
        });
        conf.shortcut<QAction>(*rightTab, "shortcuts.subwindow.tabright");

        auto *moveTabRight = subwindow->addAction(tr("Move tab right"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                _subwindow->moveTab(currentIdx, currentIdx + 1);
            });
        });
        conf.shortcut<QAction>(*moveTabRight, "shortcuts.subwindow.movetabright");

        subwindow->addSeparator();

        auto *closeTab = subwindow->addAction(tr("Close tab"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                _subwindow->closeTab(currentIdx);
            });
        });
        conf.shortcut<QAction>(*closeTab, "shortcuts.subwindow.closetab");

        subwindow->addAction(tr("Close tabs to the left"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                for(int i = currentIdx - 1; i >= 0; i--) {
                    const auto data = _subwindow->tabData(i);
                    if(!data.closeLocked)
                        _subwindow->closeTab(i);
                }
            });
        });
        subwindow->addAction(tr("Close tabs to the right"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                for(int i = _subwindow->tabCount() - 1; i > currentIdx; i--) {
                    const auto data = _subwindow->tabData(i);
                    if(!data.closeLocked)
                        _subwindow->closeTab(i);
                }
            });
        });
        subwindow->addAction(tr("Close all other tabs"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                for(int i = _subwindow->tabCount() - 1; i >= 0; i--) {
                    if(i != currentIdx) {
                        const auto data = _subwindow->tabData(i);
                        if(!data.closeLocked)
                            _subwindow->closeTab(i);
                    }
                }
            });
        });

        subwindow->addSeparator();

        subwindow->addAction(tr("Refresh tabs to the left"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                for(int i = 0; i < currentIdx; i++) {
                    const auto data = _subwindow->tabData(i);
                    if(!data.refreshLocked)
                        _subwindow->view(i)->triggerPageAction(QWebEnginePage::Reload);
                }
            });
        });
        subwindow->addAction(tr("Refresh tabs to the right"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                for(int i = currentIdx + 1; i < _subwindow->tabCount(); i++) {
                    const auto data = _subwindow->tabData(i);
                    if(!data.refreshLocked)
                        _subwindow->view(i)->triggerPageAction(QWebEnginePage::Reload);
                }
            });
        });
        subwindow->addAction(tr("Refresh all other tabs"), parent, [parent]() {
            run_if(parent->currentSubWindow(), [](SubWindow *_subwindow, int currentIdx) {
                for(int i = 0; i < _subwindow->tabCount(); i++) {
                    if(i != currentIdx) {
                        const auto data = _subwindow->tabData(i);
                        if(!data.refreshLocked)
                            _subwindow->view(i)->triggerPageAction(QWebEnginePage::Reload);
                    }
                }
            });
        });

        subwindow->addSeparator();

        auto *subwindowProfile = subwindow->addMenu(tr("Subwindow Profile"));
        connect(subwindowProfile, &QMenu::aboutToShow, subwindowProfile, [=]() {
            subwindowProfile->clear();

            auto *_subwindow = parent->currentSubWindow();
            if(_subwindow != nullptr) {
                profileMenu(
                    subwindowProfile, [_subwindow](WebProfile *profile) {
                        _subwindow->setProfile(profile);
                    },
                    _subwindow->profile(), true);
            }
        });
    }

    auto *page = this->addMenu(tr("&Page"));
    {
        page->addAction(tr("Create Bookmark"), parent, [browser, parent]() {
            if(parent->currentView() != nullptr)
                browser->bookmarks()->addBookmark(parent->currentView()->title(), parent->currentView()->url().toString());
        });

        page->addSeparator();

        page->addAction(tr("Save"), parent, [parent]() {
            if(parent->currentView() != nullptr)
                parent->currentView()->triggerPageAction(QWebEnginePage::SavePage);
        });
        page->addAction(tr("Print"), parent, [parent]() {
            if(parent->currentView() != nullptr) {
                auto *printer = new QPrinter(QPrinterInfo::defaultPrinter());
                QPrintDialog dlg(printer, parent);
                if(dlg.exec() == QDialog::Accepted) {
                    parent->currentView()->page()->print(printer, [printer](bool success) {
                        Q_UNUSED(success);
                        delete printer;
                    });
                }
            }
        });

        page->addAction(tr("Print to PDF"), parent, [parent]() {
            if(parent->currentView() != nullptr) {
                const QString path = QFileDialog::getSaveFileName(parent, tr("Print to PDF"), QDir::homePath(), tr("PDF files (*.pdf)"));
                parent->currentView()->page()->printToPdf(path);
            }
        });

        page->addSeparator();

        auto pageProfile = page->addMenu(tr("Page Profile"));
        connect(pageProfile, &QMenu::aboutToShow, pageProfile, [=]() {
            pageProfile->clear();

            if(parent->currentView() != nullptr) {
                profileMenu(
                    pageProfile, [parent](WebProfile *profile) {
                        parent->currentView()->setProfile(profile);
                    },
                    parent->currentView()->profile(), true);
            }
        });

        page->addSeparator();
        page->addAction(tr("Developer Tools"), parent, [parent]() {
            if(parent->currentView() != nullptr) {
                auto *dlg = createDevToolsDialog(parent->currentView()->page());
                dlg->show();
            }
        });
        page->addAction(tr("View Source"), parent, [parent]() {
            if(parent->currentView() != nullptr) {
                parent->currentView()->triggerPageAction(QWebEnginePage::ViewSource);
            }
        });
#ifdef QT_DEBUG
        // doesn't seem to do anything?
        page->addAction(tr("Inspect Element"), parent, [parent]() {
            if(parent->currentView() != nullptr) {
                parent->currentView()->triggerPageAction(QWebEnginePage::InspectElement);
            }
        });
#endif
    }

    this->addSeparator();

    auto *edit = this->addMenu(tr("Edit"));
    {
        edit->addAction(tr("Undo"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::Undo);
        });
        edit->addAction(tr("Redo"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::Redo);
        });

        edit->addSeparator();

        edit->addAction(tr("Cut"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::Cut);
        });
        edit->addAction(tr("Copy"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::Copy);
        });
        edit->addAction(tr("Paste"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::Paste);
        });
        edit->addAction(tr("Paste and match Style"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::PasteAndMatchStyle);
        });

        edit->addSeparator();

        edit->addAction(tr("Select all"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::SelectAll);
        });
        edit->addAction(tr("Clear selection"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::Unselect);
        });

        edit->addSeparator()->setText(tr("Editing"));

        edit->addAction(tr("Make editable"), parent, [parent]() {
            if(parent->currentView() != nullptr)
                parent->currentView()->page()->runJavaScript("document.documentElement.contentEditable = true");
        });
        edit->addAction(tr("Bold"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::ToggleBold);
        });
        edit->addAction(tr("Italic"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::ToggleItalic);
        });
        edit->addAction(tr("Underline"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::ToggleUnderline);
        });
        edit->addAction(tr("Strikethrough"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::ToggleStrikethrough);
        });
        edit->addAction(tr("Align left"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::AlignLeft);
        });
        edit->addAction(tr("Align right"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::AlignRight);
        });
        edit->addAction(tr("Align center"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::AlignCenter);
        });
        edit->addAction(tr("Align justified"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::AlignJustified);
        });
        edit->addAction(tr("Indent"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::Indent);
        });
        edit->addAction(tr("Outdent"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::Outdent);
        });
        edit->addAction(tr("Insert ordered list"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::InsertOrderedList);
        });
        edit->addAction(tr("Insert unordered list"), parent, [parent]() {
            trigger_if(parent->currentView(), QWebEnginePage::InsertUnorderedList);
        });
    }
}

void MenuBar::insertSubWindow(SubWindow *subwindow)
{
    auto *action = window->addAction(subwindow->windowTitle());

    action->setCheckable(true);
    action->setChecked(subwindow->isVisible());
    connect(subwindow, &SubWindow::visibilityChanged, action, &QAction::setChecked);

    connect(subwindow, &SubWindow::windowTitleChanged, action, &QAction::setText);
    connect(subwindow, &SubWindow::destroyed, action, &QAction::deleteLater);

    auto *optionsMenu = new QMenu;
    optionsMenu->addAction(tr("Switch to"), subwindow, [this, subwindow]() {
        m_parent->setCurrentSubWindow(subwindow);
    });
    optionsMenu->addAction(tr("Show"), subwindow, &SubWindow::show);
    optionsMenu->addAction(tr("Hide"), subwindow, &SubWindow::hide);
    optionsMenu->addAction(tr("Close"), subwindow, &SubWindow::close);
    action->setMenu(optionsMenu);
}