11# julia Gtk interface
22module Gtk
33
4+ # Import binary definitions
5+ using GTK3_jll, Glib_jll, gdk_pixbuf_jll, adwaita_icon_theme_jll, hicolor_icon_theme_jll
6+ using Pkg. Artifacts
7+ const libgdk = libgdk3
8+ const libgtk = libgtk3
9+
10+
411const suffix = :Leaf
512include (" GLib/GLib.jl" )
613using . GLib
@@ -17,11 +24,7 @@ import Base: convert, show, run, size, resize!, length, getindex, setindex!,
1724 parent, isempty, empty!, first, last, in, popfirst!,
1825 eltype, copy, isvalid, string, sigatomic_begin, sigatomic_end, (:), iterate
1926
20- if VERSION < v " 1.0"
21- import Base: showall, select!, start
22- else
23- export showall, select!, start
24- end
27+ export showall, select!, start
2528
2629using Reexport
2730@reexport using Graphics
@@ -67,6 +70,59 @@ include("theme.jl")
6770include (" gio.jl" )
6871include (" application.jl" )
6972
73+ function __init__ ()
74+ # Set XDG_DATA_DIRS so that Gtk can find its icons and schemas
75+ ENV [" XDG_DATA_DIRS" ] = join (filter (x -> x != nothing , [
76+ dirname (adwaita_icons_dir),
77+ dirname (hicolor_icons_dir),
78+ joinpath (dirname (GTK3_jll. libgdk3_path), " .." , " share" ),
79+ get (ENV , " XDG_DATA_DIRS" , nothing ),
80+ ]), Sys. iswindows () ? " ;" : " :" )
81+
82+ # Next, ensure that gdk-pixbuf has its loaders.cache file; we generate a
83+ # MutableArtifacts.toml file that maps in a loaders.cache we dynamically
84+ # generate by running `gdk-pixbuf-query-loaders:`
85+ mutable_artifacts_toml = joinpath (dirname (@__DIR__ ), " MutableArtifacts.toml" )
86+ loaders_cache_name = " gdk-pixbuf-loaders-cache"
87+ loaders_cache_hash = artifact_hash (loaders_cache_name, mutable_artifacts_toml)
88+ if loaders_cache_hash === nothing
89+ # Run gdk-pixbuf-query-loaders, capture output,
90+ loader_cache_contents = gdk_pixbuf_query_loaders () do gpql
91+ withenv (" GDK_PIXBUF_MODULEDIR" => gdk_pixbuf_loaders_dir) do
92+ return String (read (` $gpql ` ))
93+ end
94+ end
95+
96+ # Write cache out to file in new artifact
97+ loaders_cache_hash = create_artifact () do art_dir
98+ open (joinpath (art_dir, " loaders.cache" ), " w" ) do io
99+ write (io, loader_cache_contents)
100+ end
101+ end
102+ bind_artifact! (mutable_artifacts_toml,
103+ loaders_cache_name,
104+ loaders_cache_hash;
105+ force= true
106+ )
107+ end
108+
109+ # Point gdk to our cached loaders
110+ ENV [" GDK_PIXBUF_MODULE_FILE" ] = joinpath (artifact_path (loaders_cache_hash), " loaders.cache" )
111+ ENV [" GDK_PIXBUF_MODULEDIR" ] = gdk_pixbuf_loaders_dir
112+
113+ GError () do error_check
114+ ccall ((:gtk_init_with_args , libgtk), Bool,
115+ (Ptr{Nothing}, Ptr{Nothing}, Ptr{UInt8}, Ptr{Nothing}, Ptr{UInt8}, Ptr{GError}),
116+ C_NULL , C_NULL , " Julia Gtk Bindings" , C_NULL , C_NULL , error_check)
117+ end
118+
119+ # if g_main_depth > 0, a glib main-loop is already running,
120+ # so we don't need to start a new one
121+ if ccall ((:g_main_depth , GLib. libglib), Cint, ()) == 0
122+ global gtk_main_task = schedule (Task (gtk_main))
123+ end
124+ end
125+
70126const ser_version = Serialization. ser_version
71127let cachedir = joinpath (splitdir (@__FILE__ )[1 ], " .." , " gen" )
72128 fastgtkcache = joinpath (cachedir, " gtk$(libgtk_version. major) _julia_ser$(ser_version) " )
0 commit comments