Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic icon support for webview #24

Merged
merged 14 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples/project-structure/assets/icon.ico
Binary file not shown.
1 change: 1 addition & 0 deletions examples/project-structure/src/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn main() {
w.set_title('V webview examples')
w.set_size(800, 600, .@none)
w.navigate('file://${@VMODROOT}/ui/index.html')
w.set_icon('${@VMODROOT}/assets/icon.ico') or { panic(err) }
w.run()
w.destroy()
}
Binary file added examples/v-js-interop-app/icon.ico
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/v-js-interop-app/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn main() {
w := webview.create(debug: true)
w.set_title('V webview examples')
w.set_size(800, 600, .@none)

w.set_icon('${@VMODROOT}/icon.ico') or { panic(err) }
// The first string argument is the functions name in the JS frontend.
// Use JS's `camelCase` convention or distinct identifiers if you prefer it.
w.bind('get_settings', app.get_settings)
Expand Down
48 changes: 48 additions & 0 deletions src/icon/icon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "icon.h"

#ifdef _WIN32

BOOL set_icon_win32(const void *ptr, const wchar_t *iconFilePath)
{
// Load the icon from the specified file
HICON hIcon = LoadImageW(NULL, iconFilePath, IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
if (hIcon == NULL)
{
fprintf(stderr, "Failed to load icon from file!\n");
return FALSE;
}
HWND hwnd = ((const HWND *)ptr);
if (hwnd == NULL)
{
DestroyIcon(hIcon);
fprintf(stderr, "Failed to find the application window!\n");
return FALSE;
}
// Set the application icon
SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
// Cleanup
DestroyIcon(hIcon);
return TRUE;
}
#else
bool set_icon_linux(const void *ptr, const char *iconFilePath)
{
// Linux code to set the icon for a GTK window
GtkWidget *window = (GtkWidget *)ptr;
if (window == NULL)
{
fprintf(stderr, "Failed to find the application window!\n");
return FALSE;
}
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(iconFilePath, NULL);
if (pixbuf == NULL)
{
fprintf(stderr, "Failed to load icon from file!\n");
return FALSE;
}
GtkWindow *gtkWindow = GTK_WINDOW(window);
gtk_window_set_icon(GTK_WINDOW(gtkWindow), pixbuf);
g_object_unref(pixbuf);
return TRUE;
}
#endif
12 changes: 12 additions & 0 deletions src/icon/icon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <stdio.h>

#ifdef _WIN32
#include <windows.h>
#include <tchar.h>
BOOL set_icon_win32(const void *ptr, const wchar_t *iconFilePath);
#else
#include <gtk/gtk.h>
#include <stdbool.h>
bool set_icon_linux(const void *ptr, const char *iconFilePath);
#endif
10 changes: 10 additions & 0 deletions src/lib.c.v
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
module webview

import builtin.wchar

#flag linux -DWEBVIEW_GTK -lstdc++
#flag darwin -DWEBVIEW_COCOA -framework WebKit -stdlib=libc++ -lstdc++
#flag windows -DWEBVIEW_EDGE -static -ladvapi32 -lole32 -lshell32 -lshlwapi -luser32 -lversion -lstdc++

#flag -I @VMODROOT/src/icon/icon.h
#flag @VMODROOT/src/icon/icon.c

#flag @VMODROOT/src/webview.o
#include "@VMODROOT/src/webview.h"
#include "@VMODROOT/src/icon/icon.h"

$if linux {
#pkgconfig gtk+-3.0
Expand All @@ -26,6 +32,10 @@ fn C.webview_dispatch(w &C.webview_t, func fn (w &C.webview_t, ctx voidptr), ctx

fn C.webview_get_window(w &C.webview_t) voidptr

fn C.set_icon_win32(w voidptr, ico_file_path &wchar.Character) bool

fn C.set_icon_linux(w voidptr, ico_file_path &char) bool

fn C.webview_set_title(w &C.webview_t, title &char)

fn C.webview_set_size(w &C.webview_t, width int, height int, hints int)
Expand Down
23 changes: 21 additions & 2 deletions src/lib.v
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Source webview C library: https://github.com/webview/webview
module webview

import json
import builtin.wchar

pub type Webview = C.webview_t

Expand Down Expand Up @@ -45,11 +46,15 @@ pub enum Hint {
// Window size can not be changed by a user
fixed = C.WEBVIEW_HINT_FIXED
// Width and height are minimum bounds
min = C.WEBVIEW_HINT_MIN
min = C.WEBVIEW_HINT_MIN
// Width and height are maximum bounds
max = C.WEBVIEW_HINT_MAX
max = C.WEBVIEW_HINT_MAX
}

const (
default_title = 'webview'
)

// create creates a new webview instance. If `debug` is `true` - developer tools
// will be enabled (if the platform supports them). The `window` parameter can be
// a pointer to the native window handle. If it's non-null - then child WebView
Expand Down Expand Up @@ -102,6 +107,20 @@ pub fn (w &Webview) get_window() voidptr {
return C.webview_get_window(w)
}

// set_icon change the default icon for webview.
// Currently only Windows is supported.
pub fn (w &Webview) set_icon(icon_file_path string) ! {
$if windows {
if !C.set_icon_win32(w.get_window(), wchar.from_string(icon_file_path)) {
return error('Failed to set custom icon.')
}
} $else {
if !C.set_icon_linux(w.get_window(), &char(icon_file_path.str)) {
return error('Failed to set custom icon.')
}
}
}

// set_title updates the title of the native window. Must be called from the UI thread.
pub fn (w &Webview) set_title(title string) {
C.webview_set_title(w, &char(title.str))
Expand Down
Binary file added tests/icon.ico
Binary file not shown.
15 changes: 15 additions & 0 deletions tests/icon_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import webview

fn test_set_icon() {
mut w := webview.create(debug: false)
w.set_size(600, 400, .@none)
w.set_title('testing icon change')
w.set_html('<html style="background: #1B2845; color: #eee">
<samp>${@FILE}</samp>
<h2>Testing set_icon</h2>
</html>')
w.set_icon('${@VMODROOT}/tests/icon.ico') or { panic(err) }
w.run()

w.destroy()
}
4 changes: 2 additions & 2 deletions tests/v_fn_call_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ fn test_fn_call() {
assert false
}()

w.bind_ctx('v_fn', fn (e &webview.Event, mut fns_called &int) {
w.bind_ctx('v_fn', fn (e &Event, mut fns_called &int) {
fns_called++
assert e.string(0) == 'foo'
}, fns_called)
w.bind_ctx('v_fn_with_obj_arg', fn (e &webview.Event, mut fns_called &int) {
w.bind_ctx('v_fn_with_obj_arg', fn (e &Event, mut fns_called &int) {
fns_called++
p := e.decode[Person](0) or {
eprintln(err)
Expand Down