Skip to content

Commit 7332028

Browse files
committed
minor type stability fixes, docstrings, fix GVariant getter for string
1 parent bc82cf8 commit 7332028

12 files changed

+103
-17
lines changed

src/GLib/actions.jl

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# actions
2+
"""
3+
activate(a::GAction, par = nothing)
4+
5+
Activates an action, optionally with a parameter `par`, which if given should be a GVariant.
6+
"""
7+
activate(a::GAction, par = nothing) = G_.activate(a, par)
28
GSimpleAction(name::AbstractString; kwargs...) = GSimpleAction(name, nothing; kwargs...)
39

410
function GSimpleAction(name::AbstractString, parameter::Type{T}; kwargs...) where T
@@ -22,12 +28,26 @@ function GSimpleAction(name::AbstractString, parameter::Type{T},
2228
GSimpleAction(name, pvtype, GVariant(initial_state))
2329
end
2430

31+
activate(a::GSimpleAction, par = nothing) = G_.activate(GAction(a), par)
32+
33+
"""
34+
set_state(m::GSimpleAction, v)
35+
36+
Set the state of a stateful action.
37+
"""
38+
set_state(m::GSimpleAction, v) = set_state(m, GVariant(v))
2539
set_state(m::GSimpleAction, v::GVariant) = G_.set_state(m,v)
2640

2741
# action maps
2842
push!(m::GActionMap, a::GAction) = (G_.add_action(m,a); m)
2943
delete!(m::GActionMap, a::AbstractString) = (G_.remove_action(m, a); m)
44+
getindex(m::GActionMap, name::AbstractString) = G_.lookup_action(m, name)
3045

46+
"""
47+
add_action(m::GActionMap, name::AbstractString, parameter::Type{T}, handler::Function)
48+
49+
Add an action with `name` and a parameter of type `T` to a `GActionMap`. Also connect a `handler` for the action's "activate" signal.
50+
"""
3151
function add_action(m::GActionMap, name::AbstractString,
3252
parameter::Type{T}, handler::Function) where T
3353
action = GSimpleAction(name, parameter)
@@ -36,23 +56,33 @@ function add_action(m::GActionMap, name::AbstractString,
3656
action
3757
end
3858

59+
"""
60+
add_action(m::GActionMap, name::AbstractString, handler::Function)
61+
62+
Add an action with `name` to a `GActionMap`. Also connect a `handler` for the action's "activate" signal.
63+
"""
3964
function add_action(m::GActionMap, name::AbstractString, handler::Function)
4065
add_action(m, name, Nothing, handler)
4166
end
4267

4368
function add_action(m::GActionMap, name::AbstractString,
44-
parameter::Type{T}, cb, user_data) where T
69+
parameter::Type{T}, cb::Function, user_data) where T
4570
action = GSimpleAction(name, parameter)
4671
push!(m,GAction(action))
4772
signal_connect(cb, action, :activate, Nothing,
4873
(Ptr{GVariant},), false, user_data)
4974
action
5075
end
5176

52-
function add_action(m::GActionMap, name::AbstractString, cb, user_data)
77+
function add_action(m::GActionMap, name::AbstractString, cb::Function, user_data)
5378
add_action(m, name, Nothing, cb, user_data)
5479
end
5580

81+
"""
82+
add_stateful_action(m::GActionMap, name::AbstractString, parameter::Type{T}, initial_state, handler::Function)
83+
84+
Add a stateful action with `name`, a parameter of type `T`, and an initial state to a `GActionMap`. Also connect a `handler` for the action's "change-state" signal.
85+
"""
5686
function add_stateful_action(m::GActionMap, name::AbstractString,
5787
parameter::Type{T}, initial_state,
5888
handler::Function) where T
@@ -62,14 +92,19 @@ function add_stateful_action(m::GActionMap, name::AbstractString,
6292
action
6393
end
6494

95+
"""
96+
add_stateful_action(m::GActionMap, name::AbstractString, initial_state, handler::Function)
97+
98+
Add a stateful action with `name` and an initial state to a `GActionMap`. Also connect a `handler` for the action's "change-state" signal.
99+
"""
65100
function add_stateful_action(m::GActionMap, name::AbstractString, initial_state,
66101
handler::Function)
67102
add_stateful_action(m, name, Nothing, initial_state, handler)
68103
end
69104

70105
function add_stateful_action(m::GActionMap, name::AbstractString,
71106
parameter::Type{T}, initial_state,
72-
cb, user_data) where T
107+
cb::Function, user_data) where T
73108
action = GSimpleAction(name, parameter, initial_state)
74109
push!(m,GAction(action))
75110
signal_connect(cb, action, :change_state, Nothing,
@@ -78,20 +113,22 @@ function add_stateful_action(m::GActionMap, name::AbstractString,
78113
end
79114

80115
function add_stateful_action(m::GActionMap, name::AbstractString, initial_state,
81-
cb, user_data)
116+
cb::Function, user_data)
82117
add_stateful_action(m, name, Nothing, initial_state, cb, user_data)
83118
end
84119

85120
# action groups
86121
push!(g::GSimpleActionGroup, a) = (push!(GActionMap(g), GAction(a)); g)
87122
delete!(g::GSimpleActionGroup, a::AbstractString) = (delete!(GActionMap(g), a); g)
123+
getindex(g::GSimpleActionGroup, name::AbstractString) = getindex(GActionMap(g), name)
88124
list_actions(g) = G_.list_actions(GActionGroup(g))
89125

90126
function GDBusActionGroup(app::GApplication, bus_name, object_path)
91127
conn = G_.get_dbus_connection(app)
92128
G_.get(conn, bus_name, object_path)
93129
end
94130

131+
# menus
95132
function GMenu(i::GMenuItem)
96133
m = GMenu()
97134
G_.set_submenu(i,m)

src/GLib/gvalues.jl

+30
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ function getindex(gv::Base.Ref{GValue}, ::Type{Any})
252252
end
253253
#end
254254

255+
"""
256+
get_gtk_property(w::GObject, name::AbstractString, ::Type{T})
257+
258+
Get a GObject property's value as type `T`.
259+
"""
255260
get_gtk_property(w::GObject, name::AbstractString, ::Type{T}) where T = get_gtk_property(w, String(name)::String, T)
256261
get_gtk_property(w::GObject, name::Symbol, ::Type{T}) where T = get_gtk_property(w, String(name), T)
257262
function get_gtk_property(w::GObject, name::String, ::Type{T}) where T
@@ -265,6 +270,16 @@ function get_gtk_property(w::GObject, name::String, ::Type{T}) where T
265270
end
266271
return val
267272
end
273+
274+
"""
275+
get_gtk_property(w::GObject, name::AbstractString)
276+
277+
Get a GObject property's value. The type of the returned value depends on the property, so this function's output is type unstable.
278+
279+
GObject properties are mapped onto Julia instance properties, so this function is equivalent to the syntax `w.name`.
280+
281+
See also [`set_gtk_property!`](@ref).
282+
"""
268283
get_gtk_property(w::GObject, name::AbstractString) = get_gtk_property(w, String(name)::String)
269284
get_gtk_property(w::GObject, name::Symbol) = get_gtk_property(w, String(name))
270285
function get_gtk_property(w::GObject, name::String)
@@ -279,7 +294,22 @@ function get_gtk_property(w::GObject, name::String)
279294
return val
280295
end
281296

297+
"""
298+
set_gtk_property!(w::GObject, name, ::Type{T}, value)
299+
300+
Set a GObject property `name` (which can be a string or symbol) to `value` converted to type `T`.
301+
"""
282302
set_gtk_property!(w::GObject, name, ::Type{T}, value) where T = set_gtk_property!(w, name, convert(T, value))
303+
304+
"""
305+
set_gtk_property!(w::GObject, name, value)
306+
307+
Set a GObject property `name` (which can be a string or symbol) to `value`. The type of `value` will be converted to match the property type, if possible.
308+
309+
GObject properties are mapped onto Julia instance properties, so note that this function is equivalent to the more convenient syntax `w.name = value`.
310+
311+
See also [`get_gtk_property`](@ref).
312+
"""
283313
set_gtk_property!(w::GObject, name::AbstractString, value) = set_gtk_property!(w::GObject, String(name)::String, value)
284314
set_gtk_property!(w::GObject, name::Symbol, value) = set_gtk_property!(w::GObject, String(name), value)
285315
function set_gtk_property!(w::GObject, name::String, value)

src/GLib/gvariant.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ let variant_fns = Expr(:block)
66
(name, ctype, juliatype, g_value_fn, g_variant_fn) = fundamental_types[i]
77
if g_variant_fn !== :error && juliatype != Union{}
88
push!(variant_fns.args, :( GVariant(x::T) where {T <: $juliatype} = G_.$(Symbol("Variant_new_", g_variant_fn))(x)))
9-
push!(variant_fns.args, :( getindex(gv::GVariant, ::Type{T}) where {T <: $juliatype} = G_.$(Symbol("get_", g_variant_fn))(gv)))
9+
if g_variant_fn === :string
10+
push!(variant_fns.args, :( getindex(gv::GVariant, ::Type{T}) where {T <: $juliatype} = G_.get_string(gv)[1]))
11+
else
12+
push!(variant_fns.args, :( getindex(gv::GVariant, ::Type{T}) where {T <: $juliatype} = G_.$(Symbol("get_", g_variant_fn))(gv)))
13+
end
1014
end
1115
end
1216
Core.eval(GLib, variant_fns)

src/GLib/loop.jl

+4-3
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,14 @@ end
155155
"""
156156
pause_main_loop(f)
157157
158-
Pauses the GLib eventloop around a function. Restores the original state of the eventloop after
159-
calling the function.
158+
Pauses the GLib event loop around a function. Restores the original state of the event loop after calling the function. This function does not pause the event loop if it is being run by a `GApplication`.
160159
"""
161160
function pause_main_loop(f)
162161
was_running = is_loop_running()
163162
if was_running && g_main_running[] == false
164-
error("Main loop is running, but not via `glib_main`. Pausing the main loop inside a GApplication is not currently supported.")
163+
warn("GLib main loop is running, but not via `glib_main`. Pausing the main loop inside a GApplication is not currently supported, so the function will be called without pausing.")
164+
f()
165+
return
165166
end
166167
was_running && stop_main_loop(true)
167168
try

src/GLib/signals.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ the given `id`.
159159
signal_handler_is_connected(w::GObject, handler_id::Culong) =
160160
ccall((:g_signal_handler_is_connected, libgobject), Cint, (Ptr{GObject}, Culong), w, handler_id) == 1
161161

162+
"""
163+
signal_emit(w::GObject, sig::AbstractStringLike, ::Type{RT}, args...) where RT
164+
165+
Cause an object signal to be emitted. The return type `RT` and the correct number of arguments (of the correct type) must be provided. The argument list should exclude the `user_data` argument.
166+
"""
162167
function signal_emit(w::GObject, sig::AbstractStringLike, ::Type{RT}, args...) where RT
163168
i = isa(sig, AbstractString) ? something(findfirst("::", sig), 0:-1) : (0:-1)
164169
if !isempty(i)
@@ -422,7 +427,7 @@ end
422427
423428
Connect a callback `f` to the object's "notify::property" signal that will be
424429
called whenever the property changes. The callback signature should be
425-
`f(::Ptr, param::Ptr{GParamSpec}, user_data)` and should return `nothing`.
430+
`f(::Ptr, param::Ptr{GParamSpec}, user_data)` and the function should return `nothing`.
426431
"""
427432
function on_notify(f, object::GObject, property::AbstractString, user_data = object, after = false)
428433
signal_connect_generic(f, object, "notify::$property", Nothing, (Ptr{GParamSpec},), after, user_data)

src/Gdk4.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function GdkRGBA(rgba::AbstractString)
1313
r=GdkRGBA(0,0,0,0)
1414
b=G_.parse(r,rgba)
1515
if !b
16-
error("Unable to parse into a color")
16+
error("Unable to parse $rgba into a GdkRGBA")
1717
end
1818
r
1919
end

src/Gtk4.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ end
6464

6565
end
6666

67-
import .GLib: set_gtk_property!, get_gtk_property, run,
67+
import .GLib: set_gtk_property!, get_gtk_property, run, activate,
6868
signal_handler_is_connected, signalnames,
6969
GListModel, start_main_loop, stop_main_loop
7070

src/base.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ Gets the `GdkMonitor` where `w` is displayed, or `nothing` if the widget is not
152152
part of a widget hierarchy.
153153
"""
154154
function monitor(w::GtkWidget)
155-
d = display(w)
155+
d = display(w)::GdkDisplayLeaf
156156
tl = toplevel(w)
157157
tl === nothing && return nothing
158-
s = G_.get_surface(GtkNative(tl))
158+
s = G_.get_surface(GtkNative(tl))::GdkSurfaceLeaf
159159
# sometimes `get_monitor_at_surface` returns NULL when it shouldn't
160160
# should be unnecessary in a future version of GTK4_jll: https://gitlab.gnome.org/GNOME/gtk/-/merge_requests/4917
161161
try

src/displays.jl

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Returns a tuple `(width,height)` that gives the primary monitor size for the
55
display where `widget` is being displayed, or the default display if `widget` is
66
unrealized or not given.
77
"""
8-
function screen_size(widget=nothing)
8+
function screen_size(widget=nothing)::Tuple{Int32,Int32}
99
if widget!== nothing && G_.get_realized(widget)
1010
d=G_.get_display(widget)
1111
else
@@ -15,8 +15,10 @@ function screen_size(widget=nothing)
1515

1616
m=G_.get_monitors(d)
1717
m===nothing && error("Unable to get list of monitors")
18-
length(m)==0 && error("No monitors found")
19-
size(m[1])
18+
ml=m::GListStoreLeaf
19+
length(ml)==0 && error("No monitors found")
20+
m1=ml[1]::GdkMonitorLeaf
21+
size(m1)
2022
end
2123

2224
# GtkImage (for fixed-size images, such as icons)

src/events.jl

+2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ Adds a shortcut specified by a string like "<Control>S" for an action (such as
5454
"""
5555
function add_action_shortcut(scc::GtkShortcutController,trigger::AbstractString,action::AbstractString)
5656
t = GtkShortcutTrigger(trigger)
57+
t === nothing && error("unable to parse $trigger into a GtkShortcutTrigger")
5758
a = GtkShortcutAction("action($action)")
59+
a === nothing && error("unable to parse action($action) into a GtkShortcutAction")
5860
sc = GtkShortcut(t,a)
5961
Gtk4.G_.add_shortcut(scc,sc)
6062
end

src/lists.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ widget's model is a `GtkStringList`.
7676
"""
7777
function selected_string!(d::GtkDropDown, s::AbstractString)
7878
sl = G_.get_model(d)
79+
isnothing(sl) && error("No model set in GtkDropDown")
7980
isa(sl, GtkStringList) || error("This method only works if the model is a GtkStringList")
8081
i = findfirst(==(s), sl)
8182
isnothing(i) && error("String not found in model")
@@ -120,7 +121,7 @@ function GtkTreeListModel(root, passthrough, autoexpand, create_func)
120121
ref, deref = GLib.gc_ref_closure(create_func)
121122
GLib.glib_ref(rootlm)
122123
ret = ccall(("gtk_tree_list_model_new", libgtk4), Ptr{GObject}, (Ptr{GObject}, Cint, Cint, Ptr{Nothing}, Ptr{Nothing}, Ptr{Nothing}), rootlm, passthrough, autoexpand, create_cfunc, ref, deref)
123-
convert(GtkTreeListModel, ret, true)
124+
GtkTreeListModelLeaf(ret, true)
124125
end
125126

126127
"""

test/action-group.jl

+4
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ gv2 = GLib.GVariant(UInt8,2)
222222
@test gv2 > gv1
223223
@test gv2 >= gv1
224224

225+
# test string
226+
gvs = GVariant("test")
227+
@test gvs[String] == "test"
228+
225229
# test tuples
226230
gvt = GLib.GVariant((true,3,6.5))
227231
@test GLib.GVariantType(Tuple{Bool,Int,Float64}) == GLib.G_.get_type(gvt)

0 commit comments

Comments
 (0)