Coder Social home page Coder Social logo

soundux / webviewpp Goto Github PK

View Code? Open in Web Editor NEW
23.0 3.0 5.0 475 KB

๐Ÿ›ฐ๏ธ A cross-platform C++ webview library

License: MIT License

CMake 4.00% C++ 96.00%
cpp17 webview webview2 webkit2gtk webview-library webviewclient webview-app cpp-library cpp17-library windows

webviewpp's Introduction

Archived

This project has been superseded by saucer.


webviewpp

A cross-platform C++17 library that allows you to create a simple webview.

Compatibility

Platform Used Browser GUI
Windows Webview2 (Edge Chromium) WinAPI
Linux WebKit2GTK GTK

Supported Windows Versions

Version Remarks
11 Will not require Webview2 Runtime or Canary-Edge build
10 Explicit installation of the Webview2 Runtime may be required
8 Requires WINDOWS_8 to be set to ON from your CMakeLists

Usage

  • Add the library to your project
    • add_subdirectory(/path/to/webviewpp EXCLUDE_FROM_ALL)
      link_libraries(webview)
  • Use the library

Dependencies

  • Windows
    • (Runtime) Webview2 or Edge Chromium Canary Build
  • Linux
    • (Runtime & Build) webkit2gtk

Example

#include <webview.hpp>

int main()
{
  Webview::Window webview("webview", 800, 900);
  webview.expose(Webview::Function("addTen", [](int num) {
      return num + 10;
  }));

  webview.show();
  webview.run();
  return 0;
}

For more examples see examples

Embedding

webviewpp supports embedding of all required files.
To embed your files you have to use the embed-helper.

Usage:

  • Compile the embed-helper
    • mkdir build && cd build && cmake .. && cmake --build . --config Release
  • Run the embed-helper
    • ./embed_helper <path to folder containing all the required files>
  • Add the parent folder of the embedded folder to your include directories
  • Change setUrl calls to
    • embedded:///<filepath> on Linux
    • file:///embedded/<filepath> on Windows

For an example see examples/embedded

Documentation

Window::hide

void hide();

Hides the window


Window::show

void show();

Shows the window


Window::isHidden

bool isHidden();

Returns:

Whether or the window is hidden


Window::setSize

void setSize(std::size_t, std::size_t);

Sets the window size


Window::getSize

std::pair<std::size_t, std::size_t> getSize();

Returns:

The width and height in form of an std::pair


Window::getTitle

std::string getTitle();

Returns:

The title of the window


Window::setTitle

void setTitle(std::string);

Sets the window title


Window::run

void run();

Runs the mainloop

Remarks:

Is blocking


Window::exit

void exit();

Closes the webview


Window::getUrl

std::string getUrl();

Returns:

The current url


Window::setUrl

void setUrl(std::string);

Navigates to the given url


Window::enableContextMenu

void enableContextMenu(bool);

Enables the context menu


Window::enableDevTools

void enableDevTools(bool);

Enables the developer tools


Window::expose

void expose(Webview::Function const&);

Exposes the given function

Remarks:

If the given Function is an AsyncFunction it will be run in a new thread


Window::callFunction

template <typename T>
std::future<T> callFunction(Webview::JavaScriptFunction&& function);

Calls the given javascript function

Returns:

The result of the javascript function call as T

Preconditions

T must be serializable by nlohmann::json

Remarks:

You should never call .get() on the returned future in a non async context as it will freeze the webview


Window::runCode

void runCode(std::string const&);

Runs the given javascript code


Window::injectCode

void injectCode(std::string const&);

Makes the given javascript code run on document load


Window::setCloseCallback

void setCloseCallback(std::function<bool ()>);

Sets the close-callback to the given callback

Remarks:

If the callback returns true the webview will not close


Window::setNavigateCallback

void setNavigateCallback(std::function<void (const std::string &)>);

Sets the navigate-callback to the given callback


Window::setResizeCallback

void setResizeCallback(std::function<void (std::size_t, std::size_t)>);

Sets the resize-callback to the given callback


This work was originally based on the work of MichaelKim

webviewpp's People

Contributors

curve avatar d3sox avatar michaelkim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

webviewpp's Issues

Windows 7 x64 examples/simple Problem

Hi, I create the webview-example( examples/main.cc ) fine and when I run it in Windows 7

Error1(Can't find entry point )
SetProcessDpiAwarenessContext .....

The same happend with the Serge version. If I change to older version of DPI API(webview/webview#527), no more Error1 but it ask for some dll missing

Error2(missing dll)

api-ms-win-core-libraryloader-l1-2-0.dll
api-ms-win-core-libraryloader-l 1-2-0.dll

I download those files( x64 ver ) and the new error

Error3
application cannot start correctly 0xc00005

The same happend with Serge version. The problem is that Webview2( microsoft original ) works perfect with windows 7 x64/x32. I test it. Why this happend? Something added to webviewpp/webview has a conflict with win7.
Any Ideas?

Thanks a Lot!

Multiple webview window

Hi,

I am trying to open multiple webview window but Sound WebView::setup() returns false if more than webview instance will be created. What might be the problem?

Thanks

#include <iostream>
#include <webview.hpp>

int main()
{
    SounduxWebView webview;
    webview.setup(800, 900);
    webview.setTitle("Example");
    webview.enableDevTools(true);

    // Call me using the dev tools!
    webview.addCallback("testCallback", [](const std::string &someString, int someInt) {
        std::cout << "Got " << someString << " and " << someInt;
        return someInt * 10;
    });
    webview.navigate("https://ddg.gg");
    webview.run();

    SounduxWebView webview2;

    webview2.setup(800, 900); // <- this returns false and app crashes
    webview2.setTitle("Example");
    webview2.enableDevTools(true);

    // Call me using the dev tools!
    webview2.addCallback("testCallback", [](const std::string &someString, int someInt) {
        std::cout << "Got " << someString << " and " << someInt;
        return someInt * 10;
    });
    webview2.navigate("https://ddg.gg");
    webview2.run();


    return 0;
}

Build without Visual Studio

Hi, I would like to compile the simple/main.cpp example without Visual Studio. With MSVC cl.exe from command line.
It's possible?

Great job your webviewpp!

Thanks
Diego.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.