aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-07-25 18:56:49 +0530
committerAkshay Nair <phenax5@gmail.com>2025-07-25 18:56:49 +0530
commit2c7d36b4aa53ffa638a7aa7046767f10e7a152bf (patch)
tree679c34d6566f5a774bcd5f979af755cf3cfeac4f
parent51be15cd7c9f39276f27d416356744041e62c22c (diff)
downloadnull-browser-2c7d36b4aa53ffa638a7aa7046767f10e7a152bf.tar.gz
null-browser-2c7d36b4aa53ffa638a7aa7046767f10e7a152bf.zip
Expose decorations enable/disable lua api
-rw-r--r--TODO.org5
-rw-r--r--init.lua14
-rw-r--r--lua/null-browser/api.lua21
-rw-r--r--src/LuaRuntime.hpp4
-rw-r--r--src/LuaRuntimeApi.hpp12
-rw-r--r--src/WindowActionRouter.cpp6
-rw-r--r--src/widgets/BrowserWindow.hpp1
-rw-r--r--src/widgets/Decorations.cpp25
-rw-r--r--src/widgets/Decorations.hpp12
-rw-r--r--src/widgets/EdgeDecoration.cpp18
-rw-r--r--src/widgets/EdgeDecoration.hpp7
11 files changed, 109 insertions, 16 deletions
diff --git a/TODO.org b/TODO.org
index ce4cb95..f2a714a 100644
--- a/TODO.org
+++ b/TODO.org
@@ -96,8 +96,9 @@ web.search.get_search_text()
web.search.current()
web.search.total()
-web.decorations.top.configure({ visible = true, size = 30 })
-web.decorations.top.configure({ visible = false }) -- hide/show
+web.decorations.top.enable()
+web.decorations.top.disable()
+web.decorations.top.set_size(20)
local view_id = web.decorations.top.view()
web.view.set_html('<div>Hello world</div>', { view = view })
web.decorations.top.destroy() -- Maybe to destroy unwanted webviews
diff --git a/init.lua b/init.lua
index bd73189..ffbff29 100644
--- a/init.lua
+++ b/init.lua
@@ -61,4 +61,18 @@ web.event.add_listener('NotificationReceived', {
end,
})
+-- EVENT for window creation for setting up decorations
+-- web.decorations.top.enable()
+-- web.decorations.bottom.enable()
+
+web.keymap.set('n', '<space>gg', function()
+ web.decorations.top.enable()
+ web.decorations.bottom.enable()
+end)
+
+web.keymap.set('n', '<space>gt', function()
+ web.decorations.top.disable()
+ web.decorations.bottom.disable()
+end)
+
print('ending...')
diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua
index 7189ae2..e1053b8 100644
--- a/lua/null-browser/api.lua
+++ b/lua/null-browser/api.lua
@@ -9,6 +9,7 @@ web.keymap = web.keymap or {}
web.view = web.view or {}
web.history = web.history or {}
web.event = web.event or {}
+web.decorations = web.decorations or {}
require 'null-browser.utils'
@@ -278,4 +279,24 @@ function web.view.scroll_to_top(view_id) return __internals.view_scroll_to_top(v
--- TODO: Document
function web.view.scroll_to_bottom(view_id) return __internals.view_scroll_to_bottom(view_id) end
+--- Decoration api
+---
+--- @alias DecorationType number
+---
+--- @param type DecorationType
+local function create_decoration_api(type)
+ return {
+ enable = function() __internals.decorations_set_enabled(type, true) end,
+ disable = function() __internals.decorations_set_enabled(type, false) end,
+ }
+end
+
+--- @see DecorationType in ./src/widgets/Decorations.hpp
+local DecorationType = { top = 1, bottom = 2, left = 3, right = 4 }
+
+web.decorations.top = create_decoration_api(DecorationType.top)
+web.decorations.bottom = create_decoration_api(DecorationType.bottom)
+web.decorations.left = create_decoration_api(DecorationType.left)
+web.decorations.right = create_decoration_api(DecorationType.right)
+
print("api loaded")
diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp
index 527501e..58362af 100644
--- a/src/LuaRuntime.hpp
+++ b/src/LuaRuntime.hpp
@@ -7,6 +7,7 @@
#include "AsyncEventLoop.hpp"
#include "lua.h"
#include "utils.hpp"
+#include "widgets/Decorations.hpp"
#include "widgets/WebViewStack.hpp"
#ifndef PROJECT_LUA_PATH
@@ -46,7 +47,7 @@ signals:
void history_back_requested(WebViewId webview_id, qsizetype history_index);
void history_forward_requested(WebViewId webview_id, qsizetype history_index);
- void keymap_add_requested(QString mode, QString keyseq, std::function<void()>);
+ void keymap_add_requested(QString mode, QString keyseq, std::function<void()> action);
void keymap_mode_update_requested(const QString &mode);
void url_opened(QString url, OpenType open_type, WebViewId webview_id);
void webview_closed(WebViewId webview_id);
@@ -59,6 +60,7 @@ signals:
void webview_scroll_requested(WebViewId webview_id, int deltax, int deltay);
void webview_scroll_top_requested(WebViewId webview_id);
void webview_scroll_bottom_requested(WebViewId webview_id);
+ void decoration_set_enabled(DecorationType type, bool enabled);
protected:
LuaRuntime();
diff --git a/src/LuaRuntimeApi.hpp b/src/LuaRuntimeApi.hpp
index 5ec72cc..d8e6073 100644
--- a/src/LuaRuntimeApi.hpp
+++ b/src/LuaRuntimeApi.hpp
@@ -5,6 +5,7 @@
#include "LuaRuntime.hpp"
#include "WindowActionRouter.hpp"
#include "events/Event.hpp"
+#include "widgets/Decorations.hpp"
int lua_api_view_set_url(lua_State *state) {
const char *url = lua_tostring(state, 1);
@@ -265,6 +266,16 @@ int lua_api_view_scroll_bottom(lua_State *state) {
return 1;
}
+int lua_api_decorations_set_enabled(lua_State *state) {
+ auto type = (DecorationType)lua_tointeger(state, 1);
+ bool enabled = lua_toboolean(state, 2);
+ qDebug() << type << enabled;
+ auto &runtime = LuaRuntime::instance();
+ emit runtime.decoration_set_enabled(type, enabled);
+ lua_pushnil(state);
+ return 1;
+}
+
// NOLINTNEXTLINE
static luaL_Reg internals_api[] = {
luaL_Reg{"event_add_listener", &lua_event_register},
@@ -289,5 +300,6 @@ static luaL_Reg internals_api[] = {
luaL_Reg{"search_set_text", &lua_api_search_set_text},
luaL_Reg{"search_previous", &lua_api_search_previous},
luaL_Reg{"search_next", &lua_api_search_next},
+ luaL_Reg{"decorations_set_enabled", &lua_api_decorations_set_enabled},
luaL_Reg{nullptr, nullptr},
};
diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp
index 1c68a35..ba479f0 100644
--- a/src/WindowActionRouter.cpp
+++ b/src/WindowActionRouter.cpp
@@ -94,6 +94,12 @@ void WindowActionRouter::initialize(Configuration *config) {
[this](WebViewId webview_id) {
WITH_WEBVIEW_WINDOW(webview_id, window, { window->scroll_to_bottom(webview_id); });
});
+ connect(&runtime, &LuaRuntime::decoration_set_enabled, this,
+ [this](DecorationType type, bool enabled) {
+ qDebug() << "routing" << type << enabled << window_map.size();
+ for (auto &win_match : window_map)
+ win_match.second->set_decoration_enabled(type, enabled);
+ });
}
void WindowActionRouter::find_current_search_text(WebViewId webview_id, bool forward) {
diff --git a/src/widgets/BrowserWindow.hpp b/src/widgets/BrowserWindow.hpp
index 7dc6ee4..98686fd 100644
--- a/src/widgets/BrowserWindow.hpp
+++ b/src/widgets/BrowserWindow.hpp
@@ -31,6 +31,7 @@ public:
DELEGATE(webview_stack, scroll, scroll)
DELEGATE(webview_stack, scroll_to_top, scroll_to_top)
DELEGATE(webview_stack, scroll_to_bottom, scroll_to_bottom)
+ DELEGATE(decorations, set_enabled, set_decoration_enabled)
bool on_window_key_event(QKeyEvent *event);
diff --git a/src/widgets/Decorations.cpp b/src/widgets/Decorations.cpp
index 659f972..d559776 100644
--- a/src/widgets/Decorations.cpp
+++ b/src/widgets/Decorations.cpp
@@ -1,10 +1,12 @@
#include <QWidget>
#include <QtCore>
+#include <optional>
#include <qboxlayout.h>
#include <qlabel.h>
#include <qwidget.h>
#include "Decorations.hpp"
+#include "widgets/EdgeDecoration.hpp"
Decorations::Decorations(QWidget *content_widget, QWebEngineProfile *profile, QWidget *parent)
: QWidget(parent) {
@@ -20,7 +22,7 @@ Decorations::Decorations(QWidget *content_widget, QWebEngineProfile *profile, QW
</div>
)HTML";
decoration_bottom->set_html(content);
- decoration_bottom->set_enabled(true);
+ // decoration_bottom->set_enabled(true);
auto *vbox = new QVBoxLayout();
vbox->setContentsMargins(0, 0, 0, 0);
@@ -41,3 +43,24 @@ Decorations::Decorations(QWidget *content_widget, QWebEngineProfile *profile, QW
hbox->addWidget(content_widget);
hbox->addWidget(decoration_right);
}
+
+void Decorations::set_enabled(DecorationType type, bool enabled) {
+ std::optional<EdgeDecoration *> decoration = get_decoration_widget(type);
+
+ if (decoration.has_value())
+ decoration.value()->set_enabled(enabled);
+}
+
+std::optional<EdgeDecoration *> Decorations::get_decoration_widget(DecorationType type) {
+ switch (type) {
+ case DecorationType::DecorationTop:
+ return decoration_top;
+ case DecorationType::DecorationBottom:
+ return decoration_bottom;
+ case DecorationType::DecorationLeft:
+ return decoration_left;
+ case DecorationType::DecorationRight:
+ return decoration_right;
+ }
+ return nullptr;
+}
diff --git a/src/widgets/Decorations.hpp b/src/widgets/Decorations.hpp
index e35e83c..d58ee7f 100644
--- a/src/widgets/Decorations.hpp
+++ b/src/widgets/Decorations.hpp
@@ -3,17 +3,29 @@
#include "widgets/EdgeDecoration.hpp"
#include <QWidget>
#include <QtCore>
+#include <optional>
#include <qwebengineprofile.h>
+enum DecorationType : uint8_t {
+ DecorationTop = 1,
+ DecorationBottom = 2,
+ DecorationLeft = 3,
+ DecorationRight = 4,
+};
+
class Decorations : public QWidget {
Q_OBJECT
public:
Decorations(QWidget *content_widget, QWebEngineProfile *profile, QWidget *parent = nullptr);
+ void set_enabled(DecorationType type, bool enabled);
+
private:
EdgeDecoration *decoration_top;
EdgeDecoration *decoration_bottom;
EdgeDecoration *decoration_left;
EdgeDecoration *decoration_right;
+
+ std::optional<EdgeDecoration *> get_decoration_widget(DecorationType type);
};
diff --git a/src/widgets/EdgeDecoration.cpp b/src/widgets/EdgeDecoration.cpp
index 58db639..4cbb004 100644
--- a/src/widgets/EdgeDecoration.cpp
+++ b/src/widgets/EdgeDecoration.cpp
@@ -47,21 +47,21 @@ void EdgeDecoration::set_enabled(bool enabled_value) {
void EdgeDecoration::setup_webview() {
if (enabled) {
- if (webview == nullptr) {
+ if (!webview.has_value()) {
webview = new WebView(WebViewStack::next_webview_id++, profile, this);
- layout()->addWidget(webview);
+ layout()->addWidget(webview.value());
}
- webview->setHtml(default_html_layout.replace("{{body}}", html_content));
+ webview.value()->setHtml(default_html_layout.replace("{{body}}", html_content));
if (vertical)
- setFixedWidth(size);
+ webview.value()->setFixedWidth(size);
else
- setFixedHeight(size);
+ webview.value()->setFixedHeight(size);
} else {
- if (webview != nullptr) {
- layout()->removeWidget(webview);
- webview->deleteLater();
- webview = nullptr;
+ if (webview.has_value()) {
+ layout()->removeWidget(webview.value());
+ webview.value()->deleteLater();
+ webview = std::nullopt;
}
}
}
diff --git a/src/widgets/EdgeDecoration.hpp b/src/widgets/EdgeDecoration.hpp
index c83227a..59390dc 100644
--- a/src/widgets/EdgeDecoration.hpp
+++ b/src/widgets/EdgeDecoration.hpp
@@ -3,6 +3,7 @@
#include <QWidget>
#include <QtCore>
#include <cstdint>
+#include <optional>
#include <qwebengineprofile.h>
#include <qwebengineview.h>
@@ -20,10 +21,10 @@ public:
private:
bool vertical;
- WebView *webview = nullptr;
+ std::optional<WebView *> webview = std::nullopt;
QWebEngineProfile *profile;
- bool enabled;
- QString html_content;
+ bool enabled = false;
+ QString html_content = "";
uint16_t size = 24;
void setup_webview();