Skip to content

Commit

Permalink
vaev-browser: Unified the inspector and hideo-browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Nov 14, 2024
1 parent d67ce31 commit ca5669c
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 384 deletions.
57 changes: 44 additions & 13 deletions src/apps/hideo-browser/app.cpp → src/web/vaev-browser/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,31 @@
#include <karm-ui/input.h>
#include <karm-ui/layout.h>
#include <karm-ui/popover.h>
#include <karm-ui/resizable.h>
#include <karm-ui/scroll.h>
#include <mdi/alert-decagram.h>
#include <mdi/arrow-left.h>
#include <mdi/bookmark-outline.h>
#include <mdi/bookmark.h>
#include <mdi/button-cursor.h>
#include <mdi/close.h>
#include <mdi/code-tags.h>
#include <mdi/cog.h>
#include <mdi/dots-horizontal.h>
#include <mdi/google-downasaur.h>
#include <mdi/home.h>
#include <mdi/lock.h>
#include <mdi/printer.h>
#include <mdi/refresh.h>
#include <mdi/surfing.h>
#include <mdi/tune-variant.h>
#include <mdi/web.h>
#include <vaev-driver/fetcher.h>
#include <vaev-view/inspect.h>
#include <vaev-view/view.h>

namespace Hideo::Browser {
#include "inspect.h"

namespace Vaev::Browser {

enum struct SidePanel {
CLOSE,
Expand All @@ -38,8 +44,9 @@ enum struct SidePanel {

struct State {
Mime::Url url;
Res<Strong<Vaev::Markup::Document>> dom;
Res<Strong<Markup::Document>> dom;
SidePanel sidePanel = SidePanel::CLOSE;
InspectState inspect = {};

bool canGoBack() const {
return false;
Expand All @@ -56,7 +63,7 @@ struct GoBack {};

struct GoForward {};

using Action = Union<Reload, GoBack, GoForward, SidePanel>;
using Action = Union<Reload, GoBack, GoForward, SidePanel, InspectorAction>;

void reduce(State &s, Action a) {
a.visit(Visitor{
Expand All @@ -70,6 +77,9 @@ void reduce(State &s, Action a) {
[&](SidePanel p) {
s.sidePanel = p;
},
[&](InspectorAction a) {
s.inspect.apply(a);
}
});
}

Expand Down Expand Up @@ -107,16 +117,31 @@ Ui::Child mainMenu([[maybe_unused]] State const &s) {
});
}

Ui::Child addressMenu() {
return Kr::contextMenuContent({
Kr::contextMenuDock({
Ui::labelMedium("Your are viewing a secure page") | Ui::insets(8),
Kr::contextMenuIcon(Ui::NOP, Mdi::CLOSE),
}),
});
}

Ui::Child addressBar(Mime::Url const &url) {
return Ui::hflow(
0,
Math::Align::CENTER,
Ui::text("{}", url),
Ui::grow(NONE),
Ui::button(
[&](auto &n) {
Ui::showPopover(n, n.bound().bottomStart(), addressMenu());
},
Ui::ButtonStyle::subtle(), Mdi::TUNE_VARIANT
),
Ui::text("{}", url) |
Ui::vcenter() |
Ui::hscroll() |
Ui::grow(),
Ui::button(Model::bind<Reload>(), Ui::ButtonStyle::subtle(), Mdi::REFRESH)
) |
Ui::box({
.padding = {0, 0, 0, 12},
.padding = {0, 0, 0, 0},
.borderRadii = 4,
.borderWidth = 1,
.backgroundFill = Ui::GRAY800,
Expand Down Expand Up @@ -146,7 +171,13 @@ Ui::Child inspectorContent(State const &s) {
Ui::center();
}

return Vaev::View::inspect(s.dom.unwrap()) | Ui::vhscroll();
return Vaev::Browser::inspect(
s.dom.unwrap(),
s.inspect,
[&](auto &n, auto a) {
Model::bubble(n, a);
}
);
}

Ui::Child sidePanel(State const &s) {
Expand Down Expand Up @@ -200,8 +231,7 @@ Ui::Child appContent(State const &s) {
return webview(s);
return Ui::hflow(
webview(s) | Ui::grow(),
Ui::separator(),
sidePanel(s)
sidePanel(s) | Ui::resizable(Ui::ResizeHandle::START, {320}, NONE)
);
}

Expand Down Expand Up @@ -229,10 +259,11 @@ Ui::Child app(Mime::Url url, Res<Strong<Vaev::Markup::Document>> dom) {
)
),
.body = slot$(appContent(s)),
.size = {1024, 720},
.compact = true,
});
}
);
}

} // namespace Hideo::Browser
} // namespace Vaev::Browser
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <karm-ui/node.h>
#include <vaev-markup/dom.h>

namespace Hideo::Browser {
namespace Vaev::Browser {

Ui::Child app(Mime::Url url, Res<Strong<Vaev::Markup::Document>> dom);

Expand Down
153 changes: 153 additions & 0 deletions src/web/vaev-browser/inspect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#include <karm-kira/side-panel.h>
#include <karm-ui/input.h>
#include <karm-ui/layout.h>
#include <karm-ui/reducer.h>
#include <karm-ui/resizable.h>
#include <karm-ui/scroll.h>
#include <karm-ui/view.h>
#include <mdi/chevron-down.h>
#include <mdi/chevron-right.h>
#include <vaev-markup/dom.h>
#include <vaev-style/styles.h>

#include "inspect.h"

namespace Vaev::Browser {

auto guide() {
return Ui::hflow(
Ui::empty(8),
Ui::separator(),
Ui::empty(9)
);
}

auto idented(isize ident) {
return [ident](Ui::Child c) -> Ui::Child {
Ui::Children res;
res.pushBack(Ui::empty(4));
for (isize i = 0; i < ident; i++) {
res.pushBack(guide());
}
res.pushBack(c);
return Ui::hflow(res);
};
}

Ui::Child elementStartTag(Markup::Element const &el, bool expanded) {
return Ui::text(
Ui::TextStyles::codeSmall().withColor(Ui::ACCENT500),
expanded ? "<{}>" : "<{}> … </{}>", el.tagName, el.tagName
);
}

Ui::Child elementEndTag(Markup::Element const &el) {
return Ui::text(
Ui::TextStyles::codeSmall().withColor(Ui::ACCENT500),
"</{}>", el.tagName
);
}

Ui::Child itemHeader(Strong<Markup::Node> n, Ui::Action<InspectorAction> a, bool expanded) {
if (n.is<Markup::Document>()) {
return Ui::codeMedium("#document");
} else if (n.is<Markup::DocumentType>()) {
return Ui::codeMedium("#document-type");
} else if (auto tx = n.is<Markup::Text>()) {
return Ui::codeMedium("{#}", tx->data);
} else if (auto el = n.is<Markup::Element>()) {
if (el->children().len()) {
return Ui::hflow(
Ui::icon(
expanded ? Mdi::CHEVRON_DOWN : Mdi::CHEVRON_RIGHT
) |
Ui::button(
[n, a](auto &btn) {
a(btn, ExpandNode{n});
},
Ui::ButtonStyle::subtle()
),
elementStartTag(*el, expanded)
);
} else {
return elementStartTag(*el, false);
}
} else if (auto c = n.is<Markup::Comment>()) {
return Ui::codeMedium(Gfx::GREEN, "<!-- {} -->", c->data);
} else {
unreachable();
}
}

Ui::Child itemFooter(Strong<Markup::Node> n, isize ident) {
if (auto el = n.is<Markup::Element>())
return Ui::hflow(n->children().len() ? guide() : Ui::empty(), elementEndTag(*el)) | idented(ident);
return Ui::empty();
}

Ui::ButtonStyle selected() {
return {
.idleStyle = {
.backgroundFill = Ui::GRAY800,
.foregroundFill = Ui::GRAY300,
},
.hoverStyle = {
.borderWidth = 1,
.backgroundFill = Ui::GRAY600,
},
.pressStyle = {
.borderWidth = 1,
.backgroundFill = Ui::GRAY700,
},
};
}

Ui::Child item(Strong<Markup::Node> n, InspectState const &s, Ui::Action<InspectorAction> a, bool expanded, isize ident) {
auto style = s.selectedNode == n ? selected() : Ui::ButtonStyle::subtle().withRadii(0);
return Ui::button(
[n, a](auto &btn) {
a(btn, SelectNode{n});
},
style,
itemHeader(n, a, expanded) | idented(ident)
);
}

Ui::Child node(Strong<Markup::Node> n, InspectState const &s, Ui::Action<InspectorAction> a, isize ident = 0) {
bool expanded = n.is<Markup::Document>() or s.expandedNodes.has(n);
Ui::Children children{item(n, s, a, expanded, ident)};

if (expanded) {
for (auto &c : n->children()) {
children.pushBack(node(c, s, a, n.is<Markup::Document>() ? 0 : ident + 1));
}
children.pushBack(itemFooter(n, ident));
}
return Ui::vflow(children);
}

Ui::Child computedStyles() {
Ui::Children children;

Style::StyleProp::any([&]<typename T>(Meta::Type<T>) {
if constexpr (requires { T::initial(); }) {
children.pushBack(Ui::text(Ui::TextStyles::codeSmall(), "{}: {}", T::name(), T::initial()) | Ui::insets({4, 8}));
}
});

return Ui::vflow(
Kr::sidePanelTitle("Computed Styles") | Ui::dragRegion({0, -1}),
Ui::separator(),
Ui::vflow(children) | Ui::vscroll() | Ui::grow()
) |
Ui::pinSize(128);
}

Ui::Child inspect(Strong<Vaev::Markup::Document> n, InspectState const &s, Ui::Action<InspectorAction> a) {
return Ui::vflow(
node(n, s, a) | Ui::vscroll() | Ui::grow(),
computedStyles() | Ui::resizable(Ui::ResizeHandle::TOP, {128}, NONE)
);
}

} // namespace Vaev::Browser
42 changes: 42 additions & 0 deletions src/web/vaev-browser/inspect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <karm-ui/node.h>
#include <karm-ui/reducer.h>
#include <vaev-markup/dom.h>

namespace Vaev::Browser {

struct ExpandNode {
Strong<Markup::Node> node;
};

struct SelectNode {
Strong<Markup::Node> node;
};

using InspectorAction = Union<ExpandNode, SelectNode>;

struct InspectState {
Map<Strong<Markup::Node>, bool> expandedNodes = {};
Opt<Strong<Markup::Node>> selectedNode = NONE;

void apply(InspectorAction &a) {
a.visit(Visitor{
[&](ExpandNode e) {
if (expandedNodes.has(e.node))
expandedNodes.del(e.node);
else
expandedNodes.put(e.node, true);
},
[&](SelectNode e) {
if (e.node->children())
expandedNodes.put(e.node, true);
selectedNode = e.node;
},
});
}
};

Ui::Child inspect(Strong<Vaev::Markup::Document> dom, InspectState const &s, Ui::Action<InspectorAction> action);

} // namespace Vaev::Browser
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ Async::Task<> entryPointAsync(Sys::Context &ctx) {

co_return Ui::runApp(
ctx,
Hideo::Browser::app(url, dom)
Vaev::Browser::app(url, dom)
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"id": "hideo-browser.main",
"id": "vaev-browser.main",
"props": {
"cpp-excluded": true
},
"type": "exe",
"requires": [
"hideo-browser"
"vaev-browser"
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"id": "hideo-browser",
"id": "vaev-browser",
"type": "lib",
"description": "Browse the web",
"requires": [
Expand Down
Loading

0 comments on commit ca5669c

Please sign in to comment.