Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create notifiable property #11

Open
SVGAnimate opened this issue Nov 6, 2021 · 0 comments
Open

Create notifiable property #11

SVGAnimate opened this issue Nov 6, 2021 · 0 comments

Comments

@SVGAnimate
Copy link

SVGAnimate commented Nov 6, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant