aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-01-24 21:48:33 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2023-01-24 21:48:33 +0100
commitbbd6d152e7f22013d10572985784ea4f5fb7d013 (patch)
treebc1cf5a7a577d66a5243de13f507e15f6d683bcf
parent19b7700c4907ba24401741dc98f83c81d4b213d0 (diff)
make it work
-rw-r--r--Makefile3
-rw-r--r--config.hpp4
-rw-r--r--sb.cpp56
3 files changed, 46 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index 07988bc..bc8854e 100644
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,9 @@ include config.mk
sb: sb.cpp
g++ sb.cpp -o sb $(LDFLAGS)
+clean:
+ rm sb
+
install:
install sb /usr/local/bin/
diff --git a/config.hpp b/config.hpp
new file mode 100644
index 0000000..e9c9fa1
--- /dev/null
+++ b/config.hpp
@@ -0,0 +1,4 @@
+const bool enable_smooth_scrollig = true;
+const char search_engine[] = "https://searx.org/?q=";
+const char dmenu_args[] = "-s 0";
+const unsigned scroll_speed = 100;
diff --git a/sb.cpp b/sb.cpp
index 661ba4b..617c3b7 100644
--- a/sb.cpp
+++ b/sb.cpp
@@ -5,6 +5,8 @@
#include <QtGui/QWindow>
#include <QtWidgets/QMainWindow>
#include <QtWebEngineWidgets/QWebEngineView>
+#include <QWebEngineView>
+#include <QtWebEngineWidgets/QWebEngineProfile>
#include <QtWidgets/QWidget>
#include <QtWidgets/QLayout>
#include <QtWidgets/QLabel>
@@ -13,13 +15,15 @@
#include <QtX11Extras/QX11Info>
#include <X11/Xlib.h>
+#include "config.hpp"
bool is_valid_url(const std::string & url);
void open_url(const std::string & url);
void search();
void page_scroll(int scroll);
-class Main;
+void goto_end(bool end);
void get_window_id();
+class Main;
uint32_t parent_id = 0;
bool is_valid_url(const std::string & url) {
@@ -39,22 +43,26 @@ class Main : public QMainWindow {
if (event->text() == "o")
search();
else if (event->text() == "j")
- page_scroll(20);
+ page_scroll(scroll_speed);
else if (event->text() == "k")
- page_scroll(-20);
+ page_scroll(-scroll_speed);
else if (event->text() == "H")
web->back();
else if (event->text() == "L")
web->forward();
else if (event->text() == "r")
web->reload();
+ else if (event->text() == "g")
+ goto_end(false);
+ else if (event->text() == "G")
+ goto_end(true);
}
};
std::string read_from_dmenu() {
std::string output;
- std::string cmd = "printf '' | dmenu -p 'open: ' -w ";
+ std::string cmd = std::string("printf '' | dmenu -p 'open: ' ") + dmenu_args + " -w ";
cmd += std::to_string(view->winId());
FILE * f = popen(cmd.c_str(), "r");
@@ -91,11 +99,18 @@ void search() {
return;
}
- open_url("https://duckduckgo.com/?q=" + url);
+ open_url(search_engine + url);
}
void page_scroll(int scroll) {
- std::string js = "window.scrollBy(0, " + std::to_string(scroll) + ");";
+ std::string js = "window.scrollBy({ top: " + std::to_string(scroll) + ", behavior: 'smooth' });";
+ web->page()->runJavaScript(QString::fromStdString(js));
+}
+
+void goto_end(bool end) {
+ std::string js;
+ if (end) js = "window.scroll({ top: document.body.scrollHeight, behavior: 'smooth' })";
+ else js = "window.scroll({ top: 0, behavior: 'smooth' })";
web->page()->runJavaScript(QString::fromStdString(js));
}
@@ -106,25 +121,32 @@ void help(char *arg0) {
int
main(int argc, char **argv) {
- QApplication app(argc, argv);
+ std::vector<char*> arg(argv, argv + argc);
+
+ if (enable_smooth_scrollig)
+ arg.emplace_back((char*)"--enable-smooth-scrolling");
+
+ int size = arg.size();
+
+ QApplication app(size, arg.data());
+ QWebEngineProfile profile;
+ QWebEnginePage page(&profile);
+
view = new Main();
web = new QWebEngineView();
- std::cout << getenv("WWW_HOME") << '\n';
+ web->setPage(&page);
web->load(QUrl(getenv("WWW_HOME")));
- if (argc == 3) {
- if (argv[1] == std::string("-e")) {
- dpy = XOpenDisplay(NULL);
- parent_id = std::stoi(argv[2]);
- XReparentWindow(dpy, view->winId(), parent_id, 0, 0);
- XFlush(dpy);
- } else {
- help(argv[0]);
- }
+ if (argc == 3 && argv[1] == std::string("-e")) {
+ dpy = XOpenDisplay(NULL);
+ parent_id = std::stoi(argv[2]);
+ XReparentWindow(dpy, view->winId(), parent_id, 0, 0);
+ XFlush(dpy);
} else if (argc != 1) {
help(argv[0]);
}
+
view->show();
view->setCentralWidget(web);