Skip to content

Commit

Permalink
vaev-browser: Move the inspector into its own component.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Nov 14, 2024
1 parent e74d5cd commit 450f636
Show file tree
Hide file tree
Showing 20 changed files with 436 additions and 321 deletions.
2 changes: 1 addition & 1 deletion project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"extern": {
"skift-org/karm": {
"git": "https://github.com/skift-org/karm.git",
"tag": "v0.1.9"
"tag": "v0.2.0"
},
"cute-engineering/cat": {
"git": "https://github.com/cute-engineering/cat.git",
Expand Down
4 changes: 3 additions & 1 deletion src/vaev-base/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ struct FontWeight {
};

struct FontSize {
enum _Named {
enum struct _Named {
// absolute
XX_SMALL,
X_SMALL,
Expand All @@ -194,6 +194,8 @@ struct FontSize {
_LEN,
};

using enum _Named;

_Named _named;
PercentOr<Length> _value;

Expand Down
143 changes: 93 additions & 50 deletions src/vaev-tools/inspector.cpp → src/vaev-browser/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,50 @@
#include <karm-kira/error-page.h>
#include <karm-kira/scaffold.h>
#include <karm-kira/side-panel.h>
#include <karm-kira/titlebar.h>
#include <karm-kira/toolbar.h>
#include <karm-mime/mime.h>
#include <karm-sys/file.h>
#include <karm-sys/launch.h>
#include <karm-ui/app.h>
#include <karm-ui/dialog.h>
#include <karm-ui/drag.h>
#include <karm-ui/input.h>
#include <karm-ui/layout.h>
#include <karm-ui/popover.h>
#include <karm-ui/reducer.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>

#include "inspector.h"
#include "inspect.h"

namespace Vaev::Tools {
namespace Vaev::Browser {

enum struct SidePanel {
CLOSE,
BOOKMARKS,
DEVELOPER_TOOLS,
};

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

bool canGoBack() const {
return false;
Expand All @@ -56,12 +63,12 @@ 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{
[&](Reload) {
s.dom = Driver::fetchDocument(s.url);
s.dom = Vaev::Driver::fetchDocument(s.url);
},
[&](GoBack) {
},
Expand All @@ -70,15 +77,24 @@ void reduce(State &s, Action a) {
[&](SidePanel p) {
s.sidePanel = p;
},
[&](InspectorAction a) {
s.inspect.apply(a);
}
});
}

using Model = Ui::Model<State, Action, reduce>;

Ui::Child mainMenu([[maybe_unused]] State const &s) {
return Kr::contextMenuContent({
Kr::contextMenuItem(
Ui::NOP,
Mdi::BOOKMARK_OUTLINE, "Add bookmark..."
),
Kr::contextMenuItem(Model::bind(SidePanel::BOOKMARKS), Mdi::BOOKMARK, "Bookmarks"),
Ui::separator(),
Kr::contextMenuItem(Ui::NOP, Mdi::PRINTER, "Print..."),
#ifdef __ck_host__
Kr::contextMenuItem(
[&](auto &n) {
auto res = Sys::launch(Mime::Uti::PUBLIC_OPEN, s.url);
Expand All @@ -87,30 +103,45 @@ Ui::Child mainMenu([[maybe_unused]] State const &s) {
n,
Kr::alert(
"Error"s,
Io::format("Failed to open in browser\n\n{}", res).unwrap()
Io::format("Failed to open in default browser\n\n{}", res).unwrap()
)
);
},
Mdi::WEB, "Open in browser..."
Mdi::WEB, "Open in default browser..."
),
#endif
Ui::separator(),
Kr::contextMenuItem(Model::bind(SidePanel::DEVELOPER_TOOLS), Mdi::CODE_TAGS, "Inspector"),
Kr::contextMenuItem(Model::bind(SidePanel::DEVELOPER_TOOLS), Mdi::CODE_TAGS, "Developer Tools"),
Ui::separator(),
Kr::contextMenuItem(Ui::NOP, Mdi::COG, "Settings"),
});
}

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,

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(),

Kr::contextMenuIcon(Model::bind<Reload>(), Mdi::REFRESH)
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 All @@ -120,7 +151,8 @@ Ui::Child addressBar(Mime::Url const &url) {
Ui::Child contextMenu(State const &s) {
return Kr::contextMenuContent({
Kr::contextMenuDock({
Kr::contextMenuIcon(Model::bind<Reload>(), Mdi::REFRESH),
Kr::contextMenuIcon(Ui::NOP, Mdi::ARROW_LEFT),
Kr::contextMenuIcon(Ui::NOP, Mdi::REFRESH),
}),
Ui::separator(),
Kr::contextMenuItem(
Expand All @@ -139,14 +171,29 @@ Ui::Child inspectorContent(State const &s) {
Ui::center();
}

return 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) {
switch (s.sidePanel) {
case SidePanel::BOOKMARKS:
return Kr::sidePanelContent({
Kr::sidePanelTitle(Model::bind(SidePanel::CLOSE), "Bookmarks"),
Ui::separator(),
Ui::labelMedium(Ui::GRAY500, "No bookmarks") |
Ui::center() |
Ui::grow(),
});

case SidePanel::DEVELOPER_TOOLS:
return Kr::sidePanelContent({
Kr::sidePanelTitle(Model::bind(SidePanel::CLOSE), "Inspector"),
Kr::sidePanelTitle(Model::bind(SidePanel::CLOSE), "Developer Tools"),
Ui::separator(),
inspectorContent(s) | Ui::grow(),
});
Expand All @@ -158,7 +205,7 @@ Ui::Child sidePanel(State const &s) {

Ui::Child alert(State const &s, String title, String body) {
return Kr::errorPageContent({
Kr::errorPageTitle(Mdi::ALERT_DECAGRAM, title),
Kr::errorPageTitle(Mdi::GOOGLE_DOWNASAUR, title),
Kr::errorPageBody(body),
Kr::errorPageFooter({
Ui::button(Model::bindIf<GoBack>(s.canGoBack()), "Go Back"),
Expand All @@ -171,7 +218,7 @@ Ui::Child webview(State const &s) {
if (not s.dom)
return alert(s, "The page could not be loaded"s, Io::toStr(s.dom).unwrap());

return View::view(s.dom.unwrap()) |
return Vaev::View::view(s.dom.unwrap()) |
Ui::vscroll() |
Ui::box({
.backgroundFill = Gfx::WHITE,
Expand All @@ -184,43 +231,39 @@ 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)
);
}

Ui::Child inspector(Mime::Url url, Res<Strong<Markup::Document>> dom) {
Ui::Child app(Mime::Url url, Res<Strong<Vaev::Markup::Document>> dom) {
return Ui::reducer<Model>(
{
url,
dom,
},
[](State const &s) {
return Ui::vflow(
Kr::toolbar({
Ui::button(
[&](Ui::Node &n) {
Ui::showDialog(n, Kr::alert("Paper-Muncher"s, "Copyright © 2024, Odoo S.A."s));
},
Ui::ButtonStyle::subtle(),
Mdi::SURFING
),
addressBar(s.url) | Ui::grow(),
Ui::button(
[&](Ui::Node &n) {
Ui::showPopover(n, n.bound().bottomEnd(), mainMenu(s));
},
Ui::ButtonStyle::subtle(), Mdi::DOTS_HORIZONTAL
),
Kr::titlebarControls(),
}) | Ui::dragRegion(),
appContent(s) | Ui::grow()
) |
Ui::pinSize({800, 600}) |
Ui::dialogLayer() |
Ui::popoverLayer();
return Kr::scaffold({
.icon = Mdi::SURFING,
.title = "Vaev"s,
.startTools = slots$(
Ui::button(Model::bindIf<GoBack>(s.canGoBack()), Ui::ButtonStyle::subtle(), Mdi::ARROW_LEFT),
),
.midleTools = slots$(addressBar(s.url) | Ui::grow()),
.endTools = slots$(
Ui::button(
[&](Ui::Node &n) {
Ui::showPopover(n, n.bound().bottomEnd(), mainMenu(s));
},
Ui::ButtonStyle::subtle(),
Mdi::DOTS_HORIZONTAL
)
),
.body = slot$(appContent(s)),
.size = {1024, 720},
.compact = true,
});
}
);
}

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

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

namespace Vaev::Browser {

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

} // namespace Hideo::Browser
Loading

0 comments on commit 450f636

Please sign in to comment.