Skip to content

Commit

Permalink
Fix property? macro for boolean properties.
Browse files Browse the repository at this point in the history
Fix #163
  • Loading branch information
hugopl committed Aug 6, 2024
1 parent c2658ae commit ef0f255
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
14 changes: 14 additions & 0 deletions spec/inheritance_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ private class UserObjectWithGProperties < GObject::Object
@[GObject::Property]
property signal_test : Bool = true

@[GObject::Property]
property? bool : Bool = false

# This declaration tests if the property is created as readonly if it only have getter?
@[GObject::Property]
getter? readonly_bool : Bool = true
Expand Down Expand Up @@ -153,6 +156,17 @@ describe "Classes inheriting GObject::Object" do
out_object = uninitialized Pointer(Void)
LibGObject.g_object_get(obj, "object", pointerof(out_object), Pointer(Void).null)
out_object.should eq(user_obj.to_unsafe)

obj.bool = true
out_bool = uninitialized Int32
LibGObject.g_object_get(obj, "bool", pointerof(out_bool), Pointer(Void).null)
GICrystal.to_bool(out_bool).should eq(true)
obj.bool?.should eq(true)

obj.bool = false
LibGObject.g_object_get(obj, "bool", pointerof(out_bool), Pointer(Void).null)
GICrystal.to_bool(out_bool).should eq(false)
obj.bool?.should eq(false)
end

it "emits notify signal on GObject properties access" do
Expand Down
32 changes: 31 additions & 1 deletion src/bindings/g_object/object.cr
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,15 @@ module GObject
{% if name.is_a?(TypeDeclaration) %}
@{{name.var.id}} : {{name.type}}?

def {{name.var.id}} : {{name.type}}
if (value = @{{name.var.id}}).nil?
@{{name.var.id}} = {{yield}}
_emit_notify_signal({{name.var.id}})
else
value
end
end

def {{name.var.id}}? : {{name.type}}
if (value = @{{name.var.id}}).nil?
@{{name.var.id}} = {{yield}}
Expand All @@ -369,10 +378,19 @@ module GObject
end
end

def {{name.var.id}}=(@{{name.var.id}} : \{{name.type}})
def {{name.var.id}}=(@{{name.var.id}} : {{name.type}})
_emit_notify_signal({{name.var.id}})
end
{% else %}
def {{name.id}}
if (value = @{{name.id}}).nil?
@{{name.id}} = {{yield}}
_emit_notify_signal({{name.id}})
else
value
end
end

def {{name.id}}?
if (value = @{{name.id}}).nil?
@{{name.id}} = {{yield}}
Expand All @@ -391,6 +409,10 @@ module GObject
{% if name.is_a?(TypeDeclaration) %}
@{{name}}

def {{name.var.id}} : {{name.type}}
@{{name.var.id}}
end

def {{name.var.id}}? : {{name.type}}
@{{name.var.id}}
end
Expand All @@ -401,6 +423,10 @@ module GObject
{% elsif name.is_a?(Assign) %}
@{{name}}

def {{name.target.id}}
@{{name.target.id}}
end

def {{name.target.id}}?
@{{name.target.id}}
end
Expand All @@ -409,6 +435,10 @@ module GObject
_emit_notify_signal({{name.target.id}})
end
{% else %}
def {{name.id}}
@{{name.id}}
end

def {{name.id}}?
@{{name.id}}
end
Expand Down

0 comments on commit ef0f255

Please sign in to comment.