Skip to content

Create notifiable property #11

@SVGAnimate

Description

@SVGAnimate

Hello,

How would you like to create a notifiable property for GObject ?

Use :

function my_callback(MyObject $object, GParamSpec $param, mixed $user_data) {
    echo $param->name . " changed", PHP_EOL;
}
$object = new MyObject();
$object->connect("signal::notify::zoom", "my_callback", "my_data", NULL);

$object->zoom = 100; // this emit signal "notify::zoom" who call the callback of each connection.

Output :
zoom changed


Example 1 :

Add class property after declaration and before his instantiation.

<?php
class MyObject extends GObject {
	$zoom;
}
$spec = GParamSpecUint("zoom", "Zoom prop", "Set zoom", 0, 100, 51, GParam::CONSTRUCT_ONLY);
GObjectClass::InstallProperty(MyObject::class, 1, $spec);
//unset($spec)

A possible alternative( That I don't like) :

<?php
class MyObject extends GObject {
	$zoom;
        public function __construct() {
                parent::__contrust();
                static $installed_class = 0;
                if (!$installed_class) {
                        $spec = GParamSpecUint("zoom", "Zoom prop", "Set zoom", 0, 100, 51, GParam::CONSTRUCT_ONLY);
                        GObjectClass::InstallProperty(MyObject::class, 1, $spec);
                        $installed_class = 1;
                }
        }
}

Example 2 :

Add class property using annotations.

<?php
class MyObject extends GObject {
	/**
	 * @g_param_spec_uint(
	 *     name: "zoom",
	 *     nick: "Zoom prop",
	 *     "Set zoom",
	 *     0, 100, 51,
	 *     G_PARAM_CONSTRUCT | G_PARAM_READWRITE)
	 */
	$zoom;
}

A possible alternative :

<?php
class MyObject extends GObject {
	/** Set zoom
	 * @property(readwrite) uint $zoom Zoom property
	 * @range(0, 100)
	 */
	int $zoom = 51;
}

And even like that

<?php
class MyObject extends GObject {
	#[GPropertyUint("zoom", "Zoom prop", "Set zoom", 0, 100, 51, GParam::CONSTRUCT | GParam::READWRITE)]
	int $zoom;
}

Example 3 :

All members of GObject child classes are notifiable automatically properties.

<?php
class MyObject extends GObject {
	public int $zoom = 51;// Type declaration required
}

What do you think of this topic?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions