[webkit-gtk] WebView transparent background not working

Rena hyperhacker at gmail.com
Mon Feb 25 20:34:23 PST 2013


I wrote a simple app using the GTK Webkit widget set to be
transparent. It was working on my old PC, but I've bought a new one,
and here the Webkit view is not transparent, whether I set a
transparent background in CSS or no background at all - I see either
the colour I set, or the default window background colour, fully
opaque.

The app adds a label to the window as well, and that appears
transparent, but the webview has a solid background, even if it's the
only widget in the window (i.e. not in a GtkBox or GtkScrolledWindow).
The app doesn't report any errors, and webview:get_transparent()
returns true in the window's draw handler.



#!/usr/bin/env lua5.1
lgi = require('lgi')
gtk = lgi.require('Gtk', '3.0')
gdk = lgi.require('Gdk', '3.0')
gio = lgi.require('Gio', '2.0')
webkit = lgi.require('WebKit', '3.0') --requires libwebkitgtk-3.0-dev
glib = lgi.GLib
gobject = lgi.GObject
pango = lgi.Pango
cairo = lgi.cairo --lowercase for some reason
soup = lgi.Soup


--create the main window and webview
webkit.set_cache_model(webkit.CacheModel.DOCUMENT_VIEWER)
local webview = webkit.WebView.new()
local window = gtk.Window {
	title = "Web View",
	resizable = true,
	type = gtk.WindowType.TOPLEVEL,
	type_hint = gdk.WindowTypeHint.NORMAL,
	default_width = 200,
	default_height = 200,
	decorated = true,
	resizable = true,
	gtk.Box {
		orientation = 'VERTICAL',
		gtk.ScrolledWindow { expand = true, webview },
		gtk.Label { label = 'A Label' },
	},
	on_destroy = function() os.exit(0) end,
}

--configure the webview
local settings = webkit.WebSettings.new()
settings.enable_accelerated_compositing = true
settings.enable_webaudio = true --enable some experimental JS APIs
settings.enable_webgl = true

--required to allow scripts in file:///* to access http://* and our custom
--protocols used for JS/Lua communication
settings.enable_universal_access_from_file_uris = true
webview:set_settings(settings)
webview:set_transparent(true)
--window:set_opacity(0.5) --this affects the entire window, not just BG


--give the window a transparent background
local screen_supports_alpha = false
window:set_app_paintable(true)
window.on_draw = function(widget, cr, event)
	if screen_supports_alpha then cr:set_source_rgba(0, 0, 0, 0.5)
	else cr:set_source_rgb(0, 0, 0)
	end
	cr.operator = cairo.Operator.SOURCE
	cr:paint()
	return false
end

local function check_screen_supports_alpha()
	local screen = window:get_screen()
	local visual = screen:get_rgba_visual()
	if visual then
		screen_supports_alpha = true
		print("Alpha supported on this screen")
	else
		screen_supports_alpha = false
		print("Alpha not supported on this screen")
		visual = screen:get_default_visual()
	end
	window:set_visual(visual)
end

window.on_screen_changed = function(widget, old_screen)
	check_screen_supports_alpha()
end
check_screen_supports_alpha()


--load main page
webview:load_uri('file://' .. os.getenv('PWD') .. '/data/main.html')


--blastoff
window:show_all()
local ok, err = xpcall(gtk.main, debug.traceback)
if not ok then
	io.stderr:write(tostring(err) .. '\n')
	os.exit(1)
end


-- 
Sent from my Game Boy.


More information about the webkit-gtk mailing list