Skip to content

Commit

Permalink
feat: Implemented basic dart bindings
Browse files Browse the repository at this point in the history
- added webview repo
- modified the wv repo to just its barebones
- wrapped most of the api
- Maybe not fully functional
- works with example
  • Loading branch information
shreyassanthu77 committed Oct 19, 2021
1 parent a5af077 commit 58bc47a
Show file tree
Hide file tree
Showing 50 changed files with 20,672 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Files and directories created by pub.
.dart_tool/
.packages
out/

# Conventional directory for build outputs.
build/
Expand Down
20 changes: 20 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${default}",
"/usr/include/webkitgtk-4.0",
"/usr/include/gtk-3.0",
"/usr/include/glib-2.0",
"/usr/include/libsoup-2.4",
"/usr/include/cairo"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
9 changes: 7 additions & 2 deletions example/webview_dart_example.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:webview_dart/webview_dart.dart';

void main() {
var awesome = Awesome();
print('awesome: ${awesome.isAwesome}');
final url = "https://www.google.com";
Webview(true)
.setTitle("title")
.setSize(1280, 800, SIzeHint.none)
.navigate(url)
.eval("setTimeout(() => console.log('hello'), 1000)")
.run();
}
87 changes: 87 additions & 0 deletions lib/src/bindings.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart';

export 'dart:ffi' show nullptr;
export 'package:ffi/ffi.dart' show StringUtf8Pointer;

typedef WebviewLib = DynamicLibrary;
typedef WindowHandle = Pointer<Void>;

// Native Functions
typedef NativeCreateFunction = WindowHandle Function(Int32, WindowHandle);
typedef NativeDestroyFunction = Void Function(WindowHandle);
typedef NativeRunFunction = Void Function(WindowHandle);
typedef NativeTerminateFunction = Void Function(WindowHandle);
typedef NativeSetTitleFunction = Void Function(WindowHandle, Pointer<Utf8>);
typedef NativeSetSizeFunction = Void Function(
WindowHandle, Int32, Int32, Int32);
typedef NativeNavigateFunction = Void Function(WindowHandle, Pointer<Utf8>);
typedef NativeInitFunction = Void Function(WindowHandle, Pointer<Utf8>);
typedef NativeEvalFunction = Void Function(WindowHandle, Pointer<Utf8>);

// darty functions
typedef CreateFunction = WindowHandle Function(int, WindowHandle);
typedef DestroyFunction = void Function(WindowHandle);
typedef RunFunction = void Function(WindowHandle);
typedef TerminateFunction = void Function(WindowHandle);
typedef SetTitleFunction = void Function(WindowHandle, Pointer<Utf8>);
typedef SetSizeFunction = void Function(WindowHandle, int, int, int);
typedef NavigateFunction = void Function(WindowHandle, Pointer<Utf8>);
typedef InitFunction = void Function(WindowHandle, Pointer<Utf8>);
typedef EvalFunction = void Function(WindowHandle, Pointer<Utf8>);

DynamicLibrary? _library;

// lookups
WebviewLib loadLibrary(String path) {
try {
_library ??= DynamicLibrary.open(path);
} catch (e) {
rethrow;
}
return _library!;
}

CreateFunction webviewCreate(DynamicLibrary webview) {
return webview
.lookupFunction<NativeCreateFunction, CreateFunction>("webview_create");
}

DestroyFunction webviewDestroy(DynamicLibrary webview) {
return webview.lookupFunction<NativeDestroyFunction, DestroyFunction>(
"webview_destroy");
}

RunFunction webviewRun(DynamicLibrary webview) {
return webview.lookupFunction<NativeRunFunction, RunFunction>("webview_run");
}

TerminateFunction webviewTerminate(DynamicLibrary webview) {
return webview.lookupFunction<NativeTerminateFunction, TerminateFunction>(
"webview_terminate");
}

SetTitleFunction webviewSetTitle(DynamicLibrary webview) {
return webview.lookupFunction<NativeSetTitleFunction, SetTitleFunction>(
"webview_set_title");
}

SetSizeFunction webviewSetSize(DynamicLibrary webview) {
return webview.lookupFunction<NativeSetSizeFunction, SetSizeFunction>(
"webview_set_size");
}

NavigateFunction webviewNavigate(DynamicLibrary webview) {
return webview.lookupFunction<NativeNavigateFunction, NavigateFunction>(
"webview_navigate");
}

InitFunction webviewInit(DynamicLibrary webview) {
return webview
.lookupFunction<NativeInitFunction, InitFunction>("webview_init");
}

EvalFunction webviewEval(DynamicLibrary webview) {
return webview
.lookupFunction<NativeEvalFunction, EvalFunction>("webview_eval");
}
68 changes: 68 additions & 0 deletions lib/src/webview_dart_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import 'dart:io';

import 'bindings.dart';

enum SIzeHint { none, min, max, fixed }

class Webview {
static final WebviewLib _lib = loadLibrary(
Directory(
Directory.current.path +
"/webview.${Platform.isWindows ? 'dll' : Platform.isLinux ? 'so' : 'dylib'}",
).absolute.path.replaceAll("\\", "/"),
);
static final CreateFunction _create = webviewCreate(_lib);
static final DestroyFunction _destroy = webviewDestroy(_lib);
static final RunFunction _run = webviewRun(_lib);
static final TerminateFunction _terminate = webviewTerminate(_lib);
static final SetTitleFunction _setTitle = webviewSetTitle(_lib);
static final SetSizeFunction _setSize = webviewSetSize(_lib);
static final NavigateFunction _navigate = webviewNavigate(_lib);
static final InitFunction _init = webviewInit(_lib);
static final EvalFunction _eval = webviewEval(_lib);

late final WindowHandle _handle;

Webview([bool debug = false]) {
_handle = _create(debug ? 1 : 0, nullptr);
}

void terminate() {
_terminate(_handle);
}

Webview setTitle(String title) {
_setTitle(_handle, title.toNativeUtf8());
return this;
}

Webview setSize(final int width, final int height,
[SIzeHint sIzeHint = SIzeHint.none]) {
_setSize(_handle, width, height, sIzeHint.index);
return this;
}

Webview navigate(String url) {
_navigate(_handle, url.toNativeUtf8());
return this;
}

Webview init(String js) {
_init(_handle, js.toNativeUtf8());
return this;
}

Webview eval(String js) {
_eval(_handle, js.toNativeUtf8());
return this;
}

void run([bool autoDestroy = true]) {
_run(_handle);
if (autoDestroy) destroy();
}

void destroy() {
_destroy(_handle);
}
}
6 changes: 0 additions & 6 deletions lib/src/webview_dart_base.dart

This file was deleted.

2 changes: 1 addition & 1 deletion lib/webview_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
/// More dartdocs go here.
library webview_dart;

export 'src/webview_dart_base.dart';
export 'src/webview_dart_api.dart';

// TODO: Export any libraries intended for clients of this package.
1 change: 1 addition & 0 deletions library/webview/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.h linguist-language=c
2 changes: 2 additions & 0 deletions library/webview/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Build atrifacts
/build
17 changes: 17 additions & 0 deletions library/webview/.vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${default}",
"${workspaceFolder}/script"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30133\\bin\\Hostx64\\x64\\cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
21 changes: 21 additions & 0 deletions library/webview/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Serge Zaitsev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions library/webview/main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//bin/echo; [ $(uname) = "Darwin" ] && FLAGS="-framework Webkit" || FLAGS="$(pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0)" ; c++ "$0" $FLAGS -std=c++11 -g -o webview && ./webview ; exit
// +build ignore

#include "webview.h"
#ifdef WIN32
int WINAPI WinMain(HINSTANCE hInt, HINSTANCE hPrevInst, LPSTR lpCmdLine,
int nCmdShow) {
#else
int main() {
#endif
webview_t w = webview_create(0, NULL);
webview_set_title(w, "Webview Example");
webview_set_size(w, 480, 320, WEBVIEW_HINT_NONE);
webview_navigate(w, "https://en.m.wikipedia.org/wiki/Main_Page");
webview_run(w);
webview_destroy(w);
return 0;
}
Loading

0 comments on commit 58bc47a

Please sign in to comment.