aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--init.lua11
-rw-r--r--lua/null-browser/api.lua4
-rw-r--r--src/LuaRuntimeApi.hpp13
-rw-r--r--src/WindowActionRouter.cpp51
-rw-r--r--src/WindowActionRouter.hpp23
-rw-r--r--src/widgets/BrowserWindow.hpp16
-rw-r--r--src/widgets/Decorations.cpp9
-rw-r--r--src/widgets/Decorations.hpp30
-rw-r--r--src/widgets/EdgeDecoration.hpp10
9 files changed, 117 insertions, 50 deletions
diff --git a/init.lua b/init.lua
index 7498e70..b16b08a 100644
--- a/init.lua
+++ b/init.lua
@@ -61,12 +61,21 @@ web.event.add_listener('NotificationReceived', {
end,
})
+-- Decorations config
web.event.add_listener('WinCreated', {
callback = function(event)
web.decorations.bottom.enable({ win = event.win_id })
+ local timer = web.uv.new_timer()
+ timer:start(100, 0, function()
+ local view = web.decorations.bottom.view({ win = event.win_id })
+ print('>>> view', view)
+ if view ~= nil then
+ web.view.set_url('https://google.com', view)
+ end
+ timer:close()
+ end)
end,
})
-
web.keymap.set('n', '<space>gg', function()
if web.decorations.bottom.is_enabled({ win = 0 }) then
web.decorations.bottom.disable({ win = 0 })
diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua
index f12d127..a414522 100644
--- a/lua/null-browser/api.lua
+++ b/lua/null-browser/api.lua
@@ -289,6 +289,7 @@ function web.view.scroll_to_bottom(view_id) return __internals.view_scroll_to_bo
--- @field disable fun(opts?: DecorationOpts): nil
--- @field set_enabled fun(enabled: boolean, opts?: DecorationOpts): nil
--- @field is_enabled fun(opts?: DecorationOpts): boolean
+--- @field view fun(opts?: DecorationOpts): number|nil
---
--- @alias DecorationType number
---
@@ -304,6 +305,9 @@ local function create_decoration_api(type)
is_enabled = function(opts)
return __internals.decorations_get_enabled(type, (opts or {}).win)
end,
+ view = function(opts)
+ return __internals.decorations_get_view(type, (opts or {}).win)
+ end,
}
end
diff --git a/src/LuaRuntimeApi.hpp b/src/LuaRuntimeApi.hpp
index b308f56..9f6c631 100644
--- a/src/LuaRuntimeApi.hpp
+++ b/src/LuaRuntimeApi.hpp
@@ -289,6 +289,18 @@ int lua_api_decorations_get_enabled(lua_State *state) {
return 1;
}
+int lua_api_decorations_get_view(lua_State *state) {
+ auto type = (DecorationType)lua_tointeger(state, 1);
+ WindowId win_id = lua_isnoneornil(state, 2) ? lua_tointeger(state, 2) : 0;
+ auto &router = WindowActionRouter::instance();
+ auto view_id = router.fetch_get_decoration_view_id(type, win_id);
+ if (view_id.has_value())
+ lua_pushinteger(state, view_id.value());
+ else
+ lua_pushnil(state);
+ return 1;
+}
+
// NOLINTNEXTLINE
static luaL_Reg internals_api[] = {
luaL_Reg{"event_add_listener", &lua_event_register},
@@ -315,5 +327,6 @@ static luaL_Reg internals_api[] = {
luaL_Reg{"search_next", &lua_api_search_next},
luaL_Reg{"decorations_set_enabled", &lua_api_decorations_set_enabled},
luaL_Reg{"decorations_get_enabled", &lua_api_decorations_get_enabled},
+ luaL_Reg{"decorations_get_view", &lua_api_decorations_get_view},
luaL_Reg{nullptr, nullptr},
};
diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp
index 3a11134..6028c60 100644
--- a/src/WindowActionRouter.cpp
+++ b/src/WindowActionRouter.cpp
@@ -99,7 +99,7 @@ void WindowActionRouter::initialize(Configuration *config) {
// Decoration
connect(&runtime, &LuaRuntime::decoration_set_enabled, this,
[this](DecorationType type, bool enabled, std::optional<WindowId> win_id) {
- for (auto *win : get_windows_for_optional_win_id(win_id))
+ for (auto *win : get_relevant_windows(win_id))
win->set_decoration_enabled(type, enabled);
});
}
@@ -141,26 +141,14 @@ void WindowActionRouter::add_keymap(const QString &mode_string, const QString &k
}
WebViewId WindowActionRouter::fetch_current_view_id(WindowId win_id) {
- const std::lock_guard<std::mutex> lock(window_map_mutex);
- for (auto &pair : window_map) {
- auto *win = pair.second;
- auto is_current_window = win_id == win->get_id() || (win_id == 0 && win->isActiveWindow());
- if (is_current_window) {
- return win->current_webview_id();
- }
- }
+ for (auto *win : get_relevant_windows(std::make_optional(win_id)))
+ return win->current_webview_id();
return 0;
}
QList<WebViewData> WindowActionRouter::fetch_webview_data_list(WindowId win_id) {
- const std::lock_guard<std::mutex> lock(window_map_mutex);
- for (auto &pair : window_map) {
- auto *win = pair.second;
- auto is_current_window = win_id == win->get_id() || (win_id == 0 && win->isActiveWindow());
- if (is_current_window) {
- return win->get_webview_list();
- }
- }
+ for (auto *win : get_relevant_windows(std::make_optional(win_id)))
+ return win->get_webview_list();
return {};
}
@@ -169,8 +157,35 @@ KeyMode WindowActionRouter::fetch_current_mode() const {
}
bool WindowActionRouter::fetch_is_decoration_enabled(DecorationType type, WindowId win_id) {
- auto windows = get_windows_for_optional_win_id(std::make_optional(win_id));
+ auto windows = get_relevant_windows(std::make_optional(win_id));
for (auto *win : windows)
return win->get_decoration_enabled(type);
return false;
}
+
+std::optional<WebViewId> WindowActionRouter::fetch_get_decoration_view_id(DecorationType type,
+ WindowId win_id) {
+ for (auto *win : get_relevant_windows(std::make_optional(win_id)))
+ return win->get_decoration_view_id(type);
+ return std::nullopt;
+}
+
+std::vector<BrowserWindow *>
+WindowActionRouter::get_relevant_windows(std::optional<WindowId> win_id) {
+ const std::lock_guard<std::mutex> lock(window_map_mutex);
+ std::vector<BrowserWindow *> windows;
+
+ if (!win_id.has_value()) {
+ for (auto &win_pair : window_map)
+ windows.push_back(win_pair.second);
+ } else if (win_id.value() == 0) {
+ for (auto &win_pair : window_map) {
+ if (win_pair.second->isActiveWindow())
+ windows.push_back(win_pair.second);
+ }
+ } else if (win_id.value() > 0 && window_map.contains(win_id.value())) {
+ windows.push_back(window_map.at(win_id.value()));
+ }
+
+ return windows;
+}
diff --git a/src/WindowActionRouter.hpp b/src/WindowActionRouter.hpp
index 7cc530c..cec6224 100644
--- a/src/WindowActionRouter.hpp
+++ b/src/WindowActionRouter.hpp
@@ -42,6 +42,7 @@ public:
QVariant fetch_config_value(const QString &key);
KeyMode fetch_current_mode() const;
bool fetch_is_decoration_enabled(DecorationType type, WindowId win_id);
+ std::optional<WebViewId> fetch_get_decoration_view_id(DecorationType type, WindowId win_id);
DEFINE_GETTER(fetch_current_search_text, current_search_text)
DEFINE_SETTER(set_current_search_text, current_search_text)
@@ -49,29 +50,12 @@ public:
DELEGATE(&event_queue, dispatch_event, dispatch_event)
DELEGATE(&event_queue, register_event, register_event)
- std::vector<BrowserWindow *> get_windows_for_optional_win_id(std::optional<WindowId> win_id) {
- std::vector<BrowserWindow *> windows;
-
- if (!win_id.has_value()) {
- for (auto &win_pair : window_map)
- windows.push_back(win_pair.second);
- } else if (win_id.value() == 0) {
- for (auto &win_pair : window_map) {
- if (win_pair.second->isActiveWindow())
- windows.push_back(win_pair.second);
- }
- } else if (win_id.value() > 0 && window_map.contains(win_id.value())) {
- windows.push_back(window_map.at(win_id.value()));
- }
-
- return windows;
- }
-
-protected:
+private:
WindowActionRouter() = default;
void add_keymap(const QString &mode_string, const QString &keyseq, std::function<void()> action);
void find_current_search_text(WebViewId webview_id, bool forward);
+ std::vector<BrowserWindow *> get_relevant_windows(std::optional<WindowId> win_id);
signals:
void new_window_requested(const QUrl &url);
@@ -81,7 +65,6 @@ private:
WindowMap window_map;
uint64_t last_id = 1;
Configuration *configuration;
-
EventQueue event_queue;
QString current_search_text;
};
diff --git a/src/widgets/BrowserWindow.hpp b/src/widgets/BrowserWindow.hpp
index 2927683..207524a 100644
--- a/src/widgets/BrowserWindow.hpp
+++ b/src/widgets/BrowserWindow.hpp
@@ -18,12 +18,12 @@ public:
DEFINE_GETTER(get_id, win_id)
DEFINE_SETTER(set_id, win_id)
- DELEGATE(webview_stack, has_webview, has_webview)
+ // DELEGATE(webview_stack, has_webview, has_webview)
DELEGATE(webview_stack, current_webview_id, current_webview_id)
DELEGATE(webview_stack, get_webview_list, get_webview_list)
DELEGATE(webview_stack, set_search_text, set_search_text)
DELEGATE(webview_stack, open_devtools, open_devtools)
- DELEGATE(webview_stack, open_url, open_url)
+ // DELEGATE(webview_stack, open_url, open_url)
DELEGATE(webview_stack, webview_history_back, history_back)
DELEGATE(webview_stack, webview_history_forward, history_forward)
DELEGATE(webview_stack, close, close_webview)
@@ -33,6 +33,18 @@ public:
DELEGATE(webview_stack, scroll_to_bottom, scroll_to_bottom)
DELEGATE(decorations, set_enabled, set_decoration_enabled)
DELEGATE(decorations, get_enabled, get_decoration_enabled)
+ DELEGATE(decorations, get_view_id, get_decoration_view_id)
+
+ bool has_webview(WebViewId webview_id) {
+ return webview_stack->has_webview(webview_id) || decorations->has_webview(webview_id);
+ }
+
+ void open_url(const QUrl &url, OpenType open_type, WebViewId webview_id) {
+ if (decorations->has_webview(webview_id))
+ decorations->open_url(url, webview_id);
+ else
+ webview_stack->open_url(url, open_type, webview_id);
+ }
bool on_window_key_event(QKeyEvent *event);
diff --git a/src/widgets/Decorations.cpp b/src/widgets/Decorations.cpp
index 82c1a65..9df4c23 100644
--- a/src/widgets/Decorations.cpp
+++ b/src/widgets/Decorations.cpp
@@ -15,15 +15,6 @@ Decorations::Decorations(QWidget *content_widget, QWebEngineProfile *profile, QW
decoration_left = new EdgeDecoration(true, profile, this);
decoration_right = new EdgeDecoration(true, profile, this);
- QString content = R"HTML(
- <div>
- Hello world testing testing
- <button>Btn 1</button><button>Btn 2</button>
- </div>
- )HTML";
- decoration_bottom->set_html(content);
- // decoration_bottom->set_enabled(true);
-
auto *vbox = new QVBoxLayout();
vbox->setContentsMargins(0, 0, 0, 0);
vbox->setSpacing(0);
diff --git a/src/widgets/Decorations.hpp b/src/widgets/Decorations.hpp
index 30df104..57fc526 100644
--- a/src/widgets/Decorations.hpp
+++ b/src/widgets/Decorations.hpp
@@ -1,6 +1,7 @@
#pragma once
#include "widgets/EdgeDecoration.hpp"
+#include "widgets/WebViewStack.hpp"
#include <QWidget>
#include <QtCore>
#include <optional>
@@ -22,11 +23,40 @@ public:
void set_enabled(DecorationType type, bool enabled);
bool get_enabled(DecorationType type);
+ std::optional<WebViewId> get_view_id(DecorationType type) {
+ auto decoration = get_decoration_widget(type);
+ return decoration.has_value() ? decoration.value()->get_view_id() : std::nullopt;
+ }
+
+ bool has_webview(WebViewId view_id) {
+ for (auto *decoration : decorations())
+ if (decoration->get_view_id() == view_id)
+ return true;
+ return false;
+ }
+
+ void open_url(const QUrl &url, WebViewId view_id) {
+ for (auto *decoration : decorations()) {
+ if (decoration->get_view_id() == view_id) {
+ decoration->set_url(url);
+ return;
+ }
+ }
+ }
+
private:
EdgeDecoration *decoration_top;
EdgeDecoration *decoration_bottom;
EdgeDecoration *decoration_left;
EdgeDecoration *decoration_right;
+ std::vector<EdgeDecoration *> decorations() {
+ return {
+ decoration_top,
+ decoration_bottom,
+ decoration_left,
+ decoration_right,
+ };
+ }
std::optional<EdgeDecoration *> get_decoration_widget(DecorationType type);
};
diff --git a/src/widgets/EdgeDecoration.hpp b/src/widgets/EdgeDecoration.hpp
index 3ee0ab5..f627636 100644
--- a/src/widgets/EdgeDecoration.hpp
+++ b/src/widgets/EdgeDecoration.hpp
@@ -9,6 +9,7 @@
#include "utils.hpp"
#include "widgets/WebView.hpp"
+#include "widgets/WebViewStack.hpp"
class EdgeDecoration : public QWidget {
Q_OBJECT
@@ -19,9 +20,18 @@ public:
void set_size(uint16_t size_value);
void set_html(const QString &content);
void set_enabled(bool enabled_value);
+ void set_url(const QUrl &url) {
+ if (!webview.has_value())
+ return;
+ webview.value()->setUrl(url);
+ }
DEFINE_GETTER(is_enabled, enabled)
+ std::optional<WebViewId> get_view_id() {
+ return webview.has_value() ? std::make_optional(webview.value()->get_id()) : std::nullopt;
+ }
+
private:
bool vertical;
std::optional<WebView *> webview = std::nullopt;