aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/BrowserWindow.cpp
blob: d1b9c440cbb9695d66ee36c92dba19a36adf9781 (plain) (blame)
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
#include <QKeyEvent>
#include <QLabel>
#include <QMainWindow>
#include <QStackedLayout>
#include <QVBoxLayout>
#include <QtCore>

#include "Configuration.hpp"
#include "WindowMediator.hpp"
#include "keymap/KeymapEvaluator.hpp"
#include "widgets/BrowserWindow.hpp"
#include "widgets/WebViewStack.hpp"

BrowserWindow::BrowserWindow(const Configuration &configuration, QWebEngineProfile *profile,
                             const QStringList &urls)
    : QMainWindow(nullptr), configuration(configuration) {
  setCentralWidget(new QWidget());

  // Root stacked layout
  auto *layout = new QVBoxLayout();
  layout->setContentsMargins(0, 0, 0, 0);
  layout->setSpacing(0);
  centralWidget()->setLayout(layout);

  // Webengine profile
  profile->setDownloadPath(configuration.downloads_dir());
  profile->setHttpUserAgent(configuration.user_agent());

  // Stack of web views
  auto *webview_stack = new WebViewStack(&configuration, profile);
  layout->addWidget(webview_stack);

  // Open webviews for given urls
  if (urls.isEmpty()) {
    webview_stack->open_url(configuration.new_view_url());
  } else {
    for (const auto &url : urls) {
      webview_stack->open_url(url, OpenType::OpenUrlInView);
    }
  }

  // Default keymaps
  auto &keymap = KeymapEvaluator::instance();
  keymap.define_mode("n", {.passthrough = false});
  keymap.define_mode("i", {.passthrough = true});

  win_mediator = new WindowMediator(webview_stack);

  // Update window title when webview changes
  connect(webview_stack, &WebViewStack::current_webview_title_changed, this, [this](int index) {
    auto webviews = win_mediator->get_webview_list();
    if (index >= 0 && index < webviews.count()) {
      const auto &webview = webviews.at(index);
      setWindowTitle(webview.title);
    }
  });
}

bool BrowserWindow::on_window_key_event(QKeyEvent *event) {
  auto &keymap_evaluator = KeymapEvaluator::instance();
  const bool should_skip = keymap_evaluator.evaluate(event->modifiers(), (Qt::Key)event->key());

  return should_skip;
}