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 all 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 @@ -19,6 +19,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')!
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 @@ -69,7 +69,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')!
// 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;
}
#elif __linux__
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);
#elif __linux__
#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,12 +1,18 @@
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 @VMODROOT/src/webview.o
#include "@VMODROOT/src/webview.h"

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

$if linux {
#pkgconfig gtk+-3.0
#pkgconfig webkit2gtk-4.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
19 changes: 19 additions & 0 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 @@ -59,6 +60,7 @@ pub const no_result = unsafe { nil }
// Depending on the platform, a GtkWindow, NSWindow or HWND pointer can be passed here.
// Returns null on failure. Creation can fail for various reasons such as when required runtime
// dependencies are missing or when window creation fails.

pub fn create(opts CreateOptions) &Webview {
return C.webview_create(int(opts.debug), opts.window)
}
Expand Down Expand Up @@ -103,6 +105,23 @@ pub fn (w &Webview) get_window() voidptr {
return C.webview_get_window(w)
}

// set_icon updates the icon for the native window. It supports Windows HWND and Linux GTK windows
// under X11 - Under Wayland, window application mapping is based on the desktop file entry name.
// TODO: add macOS support
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 icon.')
}
} $else $if linux {
if !C.set_icon_linux(w.get_window(), &char(icon_file_path.str)) {
return error('Failed to set icon.')
}
} $else {
return error('Failed to set icon. Unsupported OS.')
}
}

// 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
2 changes: 1 addition & 1 deletion tests/v_fn_call_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn test_timeout() {
// We check if the V functions are called and windows closed successfully within 30 seconds.
// Otherwise, windows can remain open and the test can run indefinitely.
spawn fn () {
time.sleep(30 * time.second)
time.sleep(60 * time.second)
eprintln('Timeout trying to call V-functions from JS.')
assert false
}()
Expand Down