Skip to content

Commit

Permalink
feat(docs): bump to saucer v4
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed Oct 1, 2024
1 parent 5c39a16 commit 2e0dd7f
Show file tree
Hide file tree
Showing 26 changed files with 267 additions and 196 deletions.
10 changes: 8 additions & 2 deletions docs/advanced/modules.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,14 @@ To use the module, extend your `smartview` declaration as shown in the following
```cpp
int main()
{
auto app = saucer::application::acquire({
.id = "modules",
});
// green-start
saucer::smartview<saucer::default_serializer, awesome_module> webview;
saucer::smartview<saucer::default_serializer, awesome_module> webview{{
.application = app,
}};
// green-end
webview.set_url("https://github.com/saucer/saucer");
Expand All @@ -99,7 +105,7 @@ int main()
// green-end
webview.show();
webview.run();
app->run();
return 0;
}
Expand Down
34 changes: 24 additions & 10 deletions docs/advanced/schemes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ To add a new custom scheme, you must register the scheme-name **before creating

int main()
{
// green
saucer::webview::register_scheme("demo");
saucer::smartview smartview;

// ...
auto app = saucer::application::acquire({
.id = "scheme-demo",
});

smartview.show();
smartview.run();
saucer::smartview smartview{{
.application = app,
}};

smartview.show();
app->run();

return 0;
}
Expand All @@ -26,26 +32,34 @@ int main()
## Handle Scheme

After registering a scheme it can be handled per-instance. To do so, call `webview.handle_scheme("name", <callback>)`.
The callback will receive a `const saucer::request &` and should return either a `saucer::response` or an error.
The callback will receive a `const saucer::scheme::request &` and should return either a `saucer::scheme::response` or an error.

The given request data contains various information on the requested resource, such as the url, request method, body and headers.

```cpp title="Example: Handle Scheme"
#include <print>
#include <saucer/smartview.hpp>
#include <print>

int main()
{
// green
saucer::webview::register_scheme("demo");
saucer::smartview smartview;

auto app = saucer::application::acquire({
.id = "scheme-demo",
});

saucer::smartview smartview{{
.application = app,
}};

// green-start
smartview.handle_scheme("demo",
[](const saucer::request &req) -> saucer::scheme_handler::result_type
[](const saucer::scheme::request &req) -> saucer::scheme::handler::result_type
{
std::println("Requested: \"{}\" ({})", req.url(), req.method());

return saucer::response{
return saucer::scheme::response{
.data = saucer::make_stash("<html><body>Hello from scheme handler!</body></html>"),
.mime = "text/html",
};
Expand All @@ -55,7 +69,7 @@ int main()
// green-end

smartview.show();
smartview.run();
app->run();

return 0;
}
Expand Down
94 changes: 40 additions & 54 deletions docs/common-pitfalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

int main()
{
saucer::smartview webview;
auto app = saucer::application::acquire({
.id = "pitfalls",
});

saucer::smartview webview{{
.application = app,
}};

webview.set_size(500, 600);
webview.set_title("Hello World!");
Expand All @@ -13,21 +19,28 @@ int main()
webview.expose(
"add_random",
[&](float number) {
auto random = webview.eval<float>("Math.random()").get();
auto random = webview.evaluate<float>("Math.random()").get();
return number + random;
},
true);
saucer::launch::async);

webview.show();
webview.run();
app->run();

return 0;
}
// end: execution-order-ill-formed
// begin: execution-order-fixed
int main()
{
saucer::smartview webview;
auto app = saucer::application::acquire({
.id = "pitfalls",
});

saucer::smartview webview{{
.application = app,
}};

webview.set_size(500, 600);
webview.set_title("Hello World!");

Expand All @@ -37,29 +50,36 @@ int main()
webview.expose(
"add_random",
[&](float number) {
auto random = webview.eval<float>("Math.random()").get();
auto random = webview.evaluate<float>("Math.random()").get();
return number + random;
},
true);

saucer::launch::async);

// green
webview.set_url("https://github.com");
webview.show();
webview.run();

app->run();

return 0;
}
// end: execution-order-fixed
// begin: run-blocking
int main()
{
saucer::smartview webview;
auto app = saucer::application::acquire({
.id = "pitfalls",
});

saucer::smartview webview{{
.application = app,
}};

webview.set_size(500, 600);
webview.set_title("Hello World!");

webview.show();
webview.run();
app->run();

webview.set_url("https://github.com");

Expand All @@ -69,60 +89,26 @@ int main()
// begin: run-blocking-fixed
int main()
{
saucer::smartview webview;
auto app = saucer::application::acquire({
.id = "pitfalls",
});

saucer::smartview webview{{
.application = app,
}};

webview.set_size(500, 600);
webview.set_title("Hello World!");

// green
webview.set_url("https://github.com");

webview.show();
webview.run();
app->run();

// red
webview.set_url("https://github.com");

return 0;
}
// end: run-blocking-fixed
// begin: static-init
saucer::smartview webview;

int main()
{
webview.set_size(500, 600);
webview.set_title("Hello World!");

webview.set_url("https://github.com");

webview.show();
webview.run();

return 0;
}
// end: static-init
// begin: static-init-fixed
// red
saucer::smartview webview;
// green
std::unique_ptr<saucer::smartview<>> webview;

int main()
{
// green
webview = std::make_unique<saucer::smartview<>>();

webview->set_size(500, 600);
webview->set_title("Hello World!");

webview->set_url("https://github.com");

webview->show();
webview->run();

// green
webview.reset();

return 0;
}
// end: static-init-fixed
16 changes: 1 addition & 15 deletions docs/common-pitfalls.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ They may contain useful information, like in this case, that run is potentially

```cpp
template <bool Blocking = true>
[[sc::may_block]] static void run();
[[sc::may_block]] void run() const;
```
:::

Expand All @@ -56,17 +56,3 @@ The fix for this should be obvious:
<CodeBlock language="cpp" title="Example: Well formed program">
{getCode('run-blocking-fixed')}
</CodeBlock>

## Static Initialization

Certain backends are not compatible with static initialization, thus you should make sure that all saucer instances live within the scope of `main`.

<CodeBlock language="cpp" title="Example: Ill formed program">
{getCode('static-init')}
</CodeBlock>

In case you need a global instance consider using a smart-pointer instead:

<CodeBlock language="cpp" title="Example: Well formed program">
{getCode('static-init-fixed')}
</CodeBlock>
12 changes: 10 additions & 2 deletions docs/custom-types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ struct glz::meta<custom_data>

int main()
{
saucer::smartview smartview;
auto app = saucer::application::acquire({
.id = "hello-world",
});

saucer::smartview smartview{{
.application = app,
}};

smartview.set_title("Hello World!");

// highlight-next-line
Expand All @@ -28,7 +35,8 @@ int main()

smartview.set_url("https://google.com");
smartview.show();
smartview.run();

app->run();

return 0;
}
21 changes: 17 additions & 4 deletions docs/embedding.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ To serve the files simply call `serve` and specify the file name.

int main()
{
saucer::smartview smartview;
auto app = saucer::application::acquire({
.id = "embedding",
});

saucer::smartview smartview{{
.application = app,
}};

smartview.set_title("Hello World!");

smartview.expose("add_ten", [](int i)
Expand All @@ -112,7 +119,7 @@ int main()
smartview.serve("index.html");

smartview.show();
smartview.run();
app->run();

return 0;
}
Expand All @@ -131,7 +138,13 @@ The example below demonstrates how to embed a file that will load the given reso

int main()
{
saucer::smartview smartview;
auto app = saucer::application::acquire({
.id = "embedding",
});

saucer::smartview smartview{{
.application = app,
}};

smartview.embed({{"index.html", saucer::embedded_file{
.content = saucer::stash<>::lazy(
Expand All @@ -146,7 +159,7 @@ int main()
smartview.serve("index.html");

smartview.show();
smartview.run();
app->run();

return 0;
}
Expand Down
Loading

0 comments on commit 2e0dd7f

Please sign in to comment.