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

#include "Configuration.hpp"
#include "keymap/KeymapEvaluator.hpp"
#include "widgets/Decorations.hpp"
#include "widgets/WebViewStack.hpp"

#include "widgets/BrowserWindow.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);

  // Stack of web views + decorations
  webview_stack = new WebViewStack(&configuration, profile, this);
  decorations = new Decorations(webview_stack, profile, this);
  layout->addWidget(decorations);

  // 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});

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

  connect(webview_stack, &WebViewStack::new_window_requested, this,
          &BrowserWindow::new_window_requested);
  connect(webview_stack, &WebViewStack::close_window_requested, this,
          &BrowserWindow::close_window_requested);
}

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;
}

void BrowserWindow::update_user_agent(const QString &user_agent) {
  auto *profile = webview_stack->get_profile();
  profile->setHttpUserAgent(user_agent);
}

void BrowserWindow::update_downloads_dir(const QString &downloads_dir) {
  auto *profile = webview_stack->get_profile();
  profile->setDownloadPath(downloads_dir);
}

void BrowserWindow::update_permissions_persistance(const QString &persistance) {
  auto *profile = webview_stack->get_profile();
  auto persistance_policy = Configuration::to_permission_persistance_policy(persistance);
  profile->setPersistentPermissionsPolicy(persistance_policy);
}

IWebViewMediator *BrowserWindow::get_webview_mediator(WebViewId webview_id) {
  if (decorations->has_webview(webview_id))
    return decorations;
  return webview_stack;
}

bool BrowserWindow::has_webview(WebViewId webview_id) {
  return webview_stack->has_webview(webview_id) || decorations->has_webview(webview_id);
}

void BrowserWindow::open_url(const QUrl &url, OpenType open_type, WebViewId webview_id) {
  get_webview_mediator(webview_id)->open_url(url, open_type, webview_id);
}

void BrowserWindow::set_html(const QString &html, WebViewId webview_id) {
  get_webview_mediator(webview_id)->set_html(html, webview_id);
}

void BrowserWindow::expose_rpc_function(const QString &name, const RpcFunc &action,
                                        WebViewId webview_id) {
  get_webview_mediator(webview_id)->expose_rpc_function(name, action, webview_id);
}