diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-01-27 22:53:40 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-01-27 22:53:40 +0100 |
| commit | 9b9aa83298b82ce2d29d7a89d6a1147a613c7c71 (patch) | |
| tree | f6e3365e167f50a25846f5499fe280fc64aabbf5 /sb.cpp | |
| parent | d7e89fbed51291df145ac326646fa4cefa64a28c (diff) | |
make spawn new window when requested
Diffstat (limited to 'sb.cpp')
| -rw-r--r-- | sb.cpp | 54 |
1 files changed, 36 insertions, 18 deletions
@@ -19,7 +19,6 @@ #include <QShortcut> #include <QVBoxLayout> -#include "webengine.hpp" bool is_valid_url(const std::string & url); void open_url(const std::string & url); @@ -31,6 +30,8 @@ void history_move(bool back); void reload(); uint32_t parent_id = 0; +#include "webengine.hpp" + #include "config.hpp" bool is_valid_url(const std::string & url) { @@ -65,27 +66,30 @@ std::string read_from_dmenu() { return output; } -void open_url(const std::string & url) { +inline void __web_load(const std::string &url) { web->load(QString::fromStdString(url)); } -void search() { - auto url = read_from_dmenu(); - +void open_url(const std::string & url) { if (url.empty()) return; if (is_valid_url(url)) { - open_url(url); + __web_load(url); return; } if (is_valid_url("https://" + url)) { - open_url("https://" + url); + __web_load("https://" + url); return; } - open_url(search_engine + url); + __web_load(search_engine + url); +} + +void search() { + auto url = read_from_dmenu(); + open_url(url); } void page_scroll(int scroll) { @@ -112,8 +116,9 @@ void reload() { } void help(char *arg0) { - std::cout << arg0 << '\n'; - std::cout << "-e [xid] - embed in other X window\n"; + std::cout << arg0; + std::cout << " [url] [-e <xid>]\n"; + exit(-1); } int @@ -125,11 +130,13 @@ main(int argc, char **argv) { QLabel url_label; QWidget *main = new QWidget; QVBoxLayout layout(main); + std::string url = getenv("WWW_HOME"); + bool url_set_by_arg = false; + view = new QMainWindow(); web = new QWebEngineView(); web->setPage(&page); - web->load(QUrl(getenv("WWW_HOME"))); page.setBackgroundColor(QColor(40, 40, 40)); progressbar.setMaximum(100); progressbar.setMinimum(0); @@ -140,15 +147,26 @@ main(int argc, char **argv) { url_label.setMargin(5); url_label.setStyleSheet("QLabel { background: #1d2021; color: #ebdbb2 }"); - 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]); + + for (int i = 1; i < argc; ++i) { + if (argv[i] == std::string("-e")) { + if (++i == argc || (argv[i][0] < '0' || argv[i][0] > '9')) + help(argv[0]); + + dpy = XOpenDisplay(NULL); + parent_id = std::stoi(argv[i]); + XReparentWindow(dpy, view->winId(), parent_id, 0, 0); + XFlush(dpy); + + } else if (!url_set_by_arg) { + url = argv[i]; + url_set_by_arg = true; + } else { + help(argv[0]); + } } + open_url(url); layout.setContentsMargins(0, 0, 0, 0); layout.setSpacing(0); |