Skip to content

Commit

Permalink
Custom scroll bar in WebKit / Chromium.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Apr 12, 2022
1 parent 2de5972 commit cc510d3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
49 changes: 49 additions & 0 deletions webview/webview_embed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,55 @@ bool Window::finishWebviewEmbedding() {
return false;
}

void Window::updateTheme(
QColor scrollBg,
QColor scrollBgOver,
QColor scrollBarBg,
QColor scrollBarBgOver) {
if (!_webview) {
return;
}
const auto wrap = [](QColor color) {
return u"rgba(%1, %2, %3, %4)"_q
.arg(color.red())
.arg(color.green())
.arg(color.blue())
.arg(color.alphaF()).toStdString();
};
const auto function = R"(
function() {
const style = document.createElement('style');
style.textContent = ' \
::-webkit-scrollbar { \
border-radius: 5px !important; \
border: 3px solid transparent !important; \
background-color: )" + wrap(scrollBg) + R"( !important; \
background-clip: content-box !important; \
width: 10px !important; \
} \
::-webkit-scrollbar:hover { \
background-color: )" + wrap(scrollBgOver) + R"( !important; \
} \
::-webkit-scrollbar-thumb { \
border-radius: 5px !important; \
border: 3px solid transparent !important; \
background-color: )" + wrap(scrollBarBg) + R"( !important; \
background-clip: content-box !important; \
} \
::-webkit-scrollbar-thumb:hover { \
background-color: )" + wrap(scrollBarBgOver) + R"( !important; \
} \
';
document.head.append(style);
}
)";
_webview->init(
"document.addEventListener('DOMContentLoaded', "
+ function
+ ", false);");
_webview->eval("(" + function + "());");
}

void Window::navigate(const QString &url) {
Expects(_webview != nullptr);

Expand Down
5 changes: 5 additions & 0 deletions webview/webview_embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Window final {
return _widget.get();
}

void updateTheme(
QColor scrollBg,
QColor scrollBgOver,
QColor scrollBarBg,
QColor scrollBarBgOver);
void navigate(const QString &url);
void setMessageHandler(Fn<void(std::string)> handler);
void setMessageHandler(Fn<void(const QJsonDocument&)> handler);
Expand Down
10 changes: 10 additions & 0 deletions webview/webview_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
#include <string>
#include <functional>

#include <QtGui/QColor>

// Inspired by https://github.com/webview/webview.

namespace Webview {

struct ThemeParams {
QColor scrollBg;
QColor scrollBgOver;
QColor scrollBarBg;
QColor scrollBarBgOver;
QByteArray json;
};

class Interface {
public:
virtual ~Interface() = default;
Expand Down

0 comments on commit cc510d3

Please sign in to comment.