Giter Club home page Giter Club logo

webview's Introduction

webview - V Binding

build-status last-commit

This repository provides a V binding for webview - a tiny cross-platform library to build modern cross-platform GUI applications. It allows to combine V as a fast, compiled general purpose programming language with modern web technologies to design a graphical user interface.

Installation

Build Tools and WebKit

  • Linux - Example for debian based destributions

    # Build tools, such as a C compiler
    sudo apt install build-essential
    # WebKit
    sudo apt install libgtk-3-dev libwebkit2gtk-4.0-dev
  • macOS

    xcode-select --install
  • Windows

    E.g., https://www.msys2.org/ provides instructions to install MinGW

V

Webview Module

  • Install the module

    v install ttytm.webview
  • After the installation, build the webview C library to which the webview V module will bind.
    You can re-run the script at any point to rebuild the parent library with the latest upstream changes.

    # Linux/macOS
    v ~/.vmodules/ttytm/webview/build.vsh
    # PowerShell
    v $HOME/.vmodules/ttytm/webview/build.vsh

Usage Example

Tip

When running and building on Windows, it is recommended to use gcc for compilation. E.g.:

v -cc gcc run .

import ttytm.webview

const html = '<!DOCTYPE html>
<html lang="en">
  <head>
    <style>
      body {
        background: linear-gradient(to right, #274060, #1B2845);
        color: GhostWhite;
        font-family: sans-serif;
        text-align: center;
      }
    </style>
  </head>
  <body>
    <h1>Your App Content!</h1>
    <button onclick="callV()">Call V!</button>
  </body>
  <script>
    async function callV() {
      // Call a V function that takes an argument and returns a value.
      const res = await window.my_v_func(\'Hello from JS!\');
      console.log(res);
    }
  </script>
</html>'

fn my_v_func(e &webview.Event) string {
	println('Hello from V from V!')
	e.eval("console.log('Hello from V from JS!');")
	str_arg := e.get_arg[string](0) or { '' } // Get string arg at index `0`
	return str_arg + ' Hello back from V!'
}

w := webview.create(debug: true)
w.bind('my_v_func', my_v_func)
w.set_size(600, 400, .@none)
w.set_html(html)
w.run()

Output when pressing Call V!

Hello from V from V!
CONSOLE LOG Hello from V from JS!
CONSOLE LOG Hello from JS! Hello back from V!

Additional Examples

Examples that can be found in the examples/ directory of the repository.

  1. v-js-interop-simple - simple example with a similar complexity as the readme example above.
  2. v-js-interop-app - shows the basic code architecture of an application.
  3. project-structure - organizes 2. v-js-interop-app into a directory structure that can be used as orientation for more complex projects.
  4. astro-project - uses a modern web framework for the UI.

External Examples

  1. LVbag - minimal example that automates embedding of the UI into the executable.
  2. emoji-mart-desktop - application that combines above concepts. It uses SvelteKit for the UI and embeds it inside a single executable.

Documentation

An overview of exported functions is accessible in the repositories src/lib.v file and on its vdoc site.

Debugging

Note

The debug feature currently works on Linux and Windows.

Use the webview_debug flag to enable developer tools - this enables the Web Inspector (allowing for e.g., right click Inspect Element) and console.log prints to the terminal. E.g.:

v -d webview_debug run .

Alternatively, control the debug mode explicitly for a window by using the optional debug argument.

webview.create() // enabled when the application was build with `-d webview_debug`
webview.create(debug: true) // explicitly enabled for the window
webview.create(debug: false) // explicitly disabled for the window, even when built with `-d webview_debug`

Disclaimer

Until a stable version 1.0 is available, new features will be introduced, existing ones may change, or breaking changes may occur in minor(0.<minor>.*) versions.

Complementary Projects

  • Dialog - Cross-platform utility library to open system dialogs - files, message boxes etc.
  • LVbag - Generate embedded file lists for directories.

License

Open source software under the MIT license.

webview's People

Contributors

ouri028 avatar ttytm 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  avatar  avatar  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  avatar  avatar  avatar

Forkers

ouri028

webview's Issues

Open multiple windows

The program crashes when trying to create a secondary webview. So not sure if its possible or just a bug

Time-consuming functions will block application responsiveness.

vue

   window.get_big_index().then((data) => {
                    state.data = data
   })

vlang

// block
fn get_big_index(event_id &char, raw_args &char, mut app App) {
        mut data := app.api.big_index() or { return }
	app.w.result(event_id, .value, json.encode(data))
}

// no block, but the frontend also won't receive the response data.
fn get_big_index(event_id &char, raw_args &char, mut app App) {
	spawn fn [mut app, event_id] () {
		mut data := app.api.big_index() or { return }

		app.w.result(event_id, .value, json.encode(data))
	}()
}

feat: menu items on Mac (maybe also other OSs?)

Currently on Mac you need to click the close button to quit.
Would be useful to allow the usage of "standard" macOS shortcut for app actions (ex. CMD+Q, CMD+W, CMD+H, CMD+M, etc.).

Also, custom menu items support as Electron or Tauri would be fantastic.
Anyway, I tried looking in the Tauri source code but I was not able to understand the code (I'm not a Rust DEV), but I found the implementation should be here: https://github.com/tauri-apps/tauri/blob/dev/core/tauri-runtime/src/menu.rs

bug: Blank window from 'webkitgtk2-4.1' bug (with temporary fix)

Hello! Been using this module and it's wonderful!
I've encountered a very odd bug, and after some digging I've found a temporary workaround.
After an update to Manjaro install, building and running a ttytm.webview example project, I encountered the following error:

src/nv_gbm.c:99: GBM-DRV error (nv_gbm_bo_create): DRM_IOCTL_NVIDIA_GEM_ALLOC_NVKMS_MEMORY failed (ret=-1)

Failed to create GBM buffer of size 800x600: Invalid argument
src/nv_gbm.c:99: GBM-DRV error (nv_gbm_bo_create): DRM_IOCTL_NVIDIA_GEM_ALLOC_NVKMS_MEMORY failed (ret=-1)

Failed to create GBM buffer of size 800x600: Invalid argument
src/nv_gbm.c:99: GBM-DRV error (nv_gbm_bo_create): DRM_IOCTL_NVIDIA_GEM_ALLOC_NVKMS_MEMORY failed (ret=-1)

Failed to create GBM buffer of size 800x600: Invalid argument
Failed to create EGL images for DMABufs with file descriptors -1, -1 and -1

A window did show, but it was not rendering anything within.

Lots of Nvidia related things in here, and after some searching, I've found that this is also an issue with Tauri:
tauri-apps/tauri#7910
Seemed to also be discussed here on a GTK4 webkit bug post:
https://bugs.webkit.org/show_bug.cgi?id=228268
And here I found a work around posted by somebody:
wailsapp/wails#2977 (comment)

Solution:

The temporary workaround is running the following in a terminal session before running/building an app with :
export WEBKIT_DISABLE_DMABUF_RENDERER=1

Posting this incase anyone else has this issue when using this module/an updated webkit lib.

[Question] Change default icon of application

Hey,

So, first off great job with the bindings so far.

I had a question, I was successful in changing the default application icon and was wondering if this was something that you might like to add?

Currently it only works on Windows as it uses the Windows API to change the resource of the icon.

Here is a simple example.
It is using a 512x512 .ico file.

image

Please let me know if you would like for me to integrate this into the current bindings and potentially support Linux and MacOS at a later stage?

bug: debug not working on macOS

On macOS:

  • JS console.logs won't print to the terminal.
  • Inspecting webview console causes runtime warning.
2023-09-25 03:53:04.148 v-js-interop-simple[17757:5007724] NSWindow warning: adding an unknown subview: <WKInspectorWKWebView: 0x14b71b8b0>. Break on NSLog to debug.
2023-09-25 03:53:04.168 v-js-interop-simple[17757:5007724] Call stack:
(
        0   AppKit                              0x0000000195fb9790 -[NSThemeFrame addSubview:] + 116
        1   AppKit                              0x0000000195fb9470 -[NSView addSubview:positioned:relativeTo:] + 176
        2   AppKit                              0x0000000195fb933c -[NSThemeFrame addSubview:positioned:relativeTo:] + 52
        3   WebKit                              0x00000001b3deed10 _ZN6WebKit19WebInspectorUIProxy14platformAttachEv + 340
        4   WebKit                              0x00000001b3fcd874 _ZN6WebKit19WebInspectorUIProxy4openEv + 156
        5   WebKit                              0x00000001b4231948 _ZN6WebKit19WebInspectorUIProxy17didReceiveMessageERN3IPC10ConnectionERNS1_7DecoderE + 184
        6   WebKit                              0x00000001b432a210 _ZN3IPC18MessageReceiverMap15dispatchMessageERNS_10ConnectionERNS_7DecoderE + 264
        7   WebKit                              0x00000001b3f72fec _ZN6WebKit15WebProcessProxy17didReceiveMessageERN3IPC10ConnectionERNS1_7DecoderE + 40
        8   WebKit                              0x00000001b4325a00 _ZN3IPC10Connection15dispatchMessageENSt3__110unique_ptrINS_7DecoderENS1_14default_deleteIS3_EEEE + 332
        9   WebKit                              0x00000001b4325eac _ZN3IPC10Connection24dispatchIncomingMessagesEv + 284
        10  JavaScriptCore                      0x00000001ad18c0c4 _ZN3WTF7RunLoop11performWorkEv + 200
        11  JavaScriptCore                      0x00000001ad18cf90 _ZN3WTF7RunLoop11performWorkEPv + 36
        12  CoreFoundation                      0x0000000192d7263c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
        13  CoreFoundation                      0x0000000192d725d0 __CFRunLoopDoSource0 + 176
        14  CoreFoundation                      0x0000000192d72340 __CFRunLoopDoSources0 + 244
        15  CoreFoundation                      0x0000000192d70f48 __CFRunLoopRun + 828
        16  CoreFoundation                      0x0000000192d704b8 CFRunLoopRunSpecific + 612
        17  HIToolbox                           0x000000019c5c2df0 RunCurrentEventLoopInMode + 292
        18  HIToolbox                           0x000000019c5c2c2c ReceiveNextEventCommon + 648
        19  HIToolbox                           0x000000019c5c2984 _BlockUntilNextEventMatchingListInModeWithFilter + 76
        20  AppKit                              0x0000000195f9797c _DPSNextEvent + 636
        21  AppKit                              0x0000000195f96b18 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 716
        22  AppKit                              0x0000000195f8af7c -[NSApplication run] + 464
        23  v-js-interop-simple                 0x0000000100972d78 _ZN7webview6detail4objc6invokeIvPFvvEJP11objc_objectP13objc_selectorEEET_T0_DpT1_ + 40
        24  v-js-interop-simple                 0x000000010096f814 _ZN7webview6detail4objc8msg_sendIvJP11objc_objectP13objc_selectorEEET_DpT0_ + 40
        25  v-js-interop-simple                 0x000000010096d854 _ZN7webview6detail22cocoa_wkwebview_engine3runEv + 60
        26  v-js-interop-simple                 0x000000010096d80c webview_run + 24
        27  v-js-interop-simple                 0x00000001009a8918 webview__Webview_run + 24
        28  v-js-interop-simple                 0x00000001009aa028 main__main + 288
        29  v-js-interop-simple                 0x00000001009aa350 main + 88
        30  dyld                                0x000000019293bf28 start + 2236
)

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.