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

Vfuncs #23

Merged
merged 4 commits into from
Jan 31, 2024
Merged

Vfuncs #23

merged 4 commits into from
Jan 31, 2024

Conversation

vixalien
Copy link
Collaborator

@vixalien vixalien commented Dec 26, 2023

This PR allows the setting of vfunc functions. These are "virtual functions" that will usually be called as the default callback for example, a signal or a method. By allowing setting, we effectively allow subclassing them.

This pattern can be used, for example, to allow adding a default handler for a GtkApplication using the following pattern:

class App extends Gtk.Application {
  // code...
}

const app = new App();

app.vfunc_activate = () => {
  console.log("activated...");
  this.show_main_window();
}

You can see that this is similar to adding a connect handler. This is like setting a function. For example, when we set the activate vfunc, it's as if we're setting the function that executes when the function is executed.

There is also a pattern typical in GJS that allows overriding vfuncs directly, but I still need to implement that.

class App extends Gtk.Application {
  // code...
  vfunc_activate() {
    // create and show the main window
    this.show_main_window();
  }
};

This PR will also help implement GtkBuilderScope's vfunc_create_closure, which is used to make sure callbacks from GtkBuilder template files (XML) are correctly attached to the JS-side callbacks. I will include the code in a future PR (probably #20)

I also need to log a warning or error if a vfunc cannot be set.

@vixalien vixalien force-pushed the vfuncs branch 2 times, most recently from 246ad35 to a123e2b Compare January 6, 2024 14:17
This was referenced Jan 15, 2024
@vixalien vixalien force-pushed the vfuncs branch 4 times, most recently from 44999f0 to 093f042 Compare January 22, 2024 13:27
@vixalien
Copy link
Collaborator Author

There is also a pattern typical in GJS that allows overriding vfuncs directly, but I still need to implement that.

I just realised that this cannot work currently, because if you try to subclass a type right now (for example, class App extends Gtk.Application) it won't work because the custom class has not been registered with GObject. I will include that in a separate PR, possibly #20 or another PR after that.

I also need to log a warning or error if a vfunc cannot be set.

Same as the one above, if you are trying to override your custom class' vfunc, the checking code will be included as part of the aforementioned future PR. However, setting a random vfunc_ like app.vfunc_blablabla will currently just work fine, and it will set a custom javascript property named like that on the object.

@vixalien vixalien marked this pull request as ready for review January 22, 2024 13:31
This is done manually as GIFieldInfo can't handle setting complex types like callbacks. This is useful when setting `GTypeInfo`'s `base_init`, `instance_init`... functions
use `cName` instead of `baseName`
@vixalien
Copy link
Collaborator Author

Please take a look

@ahgilak
Copy link
Owner

ahgilak commented Jan 31, 2024

LGTM. Thanks.

@ahgilak ahgilak merged commit 3452c42 into ahgilak:main Jan 31, 2024
@vixalien vixalien deleted the vfuncs branch January 31, 2024 18:01
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

Successfully merging this pull request may close these issues.

2 participants