Skip to content

Commit

Permalink
Add Requires property to MetaInfo, fixes #166
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Nov 8, 2024
1 parent 9a294b3 commit 2b28915
Showing 1 changed file with 74 additions and 66 deletions.
140 changes: 74 additions & 66 deletions packages/generator-typescript/templates/gobject-2.0.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// A few things here are inspired by gi.ts
// A few things here are inspired by gi.ts
// See https://gitlab.gnome.org/ewlsh/gi.ts/-/blob/master/packages/lib/src/generators/dts/gobject.ts
// Copyright Evan Welsh

// __type__ forces all GTypes to not match structurally.
export type GType<T = unknown> = {
__type__(arg: never): T
name: string
};
}

// Extra interfaces used to help define GObject classes in js; these
// aren't part of gi.
Expand All @@ -27,17 +27,18 @@ export interface MetaInfo<Props, Interfaces, Sigs> {
Template?: Uint8Array | GLib.Bytes | string
Children?: string[]
InternalChildren?: string[]
Requires?: Object[]
}

// Correctly types interface checks.
export function type_is_a<T extends Object>(obj: Object, is_a_type: { $gtype: GType<T> }): obj is T;
export function type_is_a<T extends Object>(obj: Object, is_a_type: { $gtype: GType<T> }): obj is T

export class Interface<T = unknown> {
static _classInit: (cls: any) => any;
__name__: string;
_construct: (params: any, ...otherArgs: any[]) => any;
_init: (params: any) => void;
$gtype?: GType<T>;
static _classInit: (cls: any) => any
__name__: string
_construct: (params: any, ...otherArgs: any[]) => any
_init: (params: any) => void
$gtype?: GType<T>
}

/**
Expand All @@ -48,60 +49,60 @@ export class NotImplementedError extends Error {
get name(): 'NotImplementedError'
}

export const __gtkCssName__: unique symbol;
export const __gtkTemplate__: unique symbol;
export const __gtkChildren__: unique symbol;
export const __gtkInternalChildren__: unique symbol;
export const __gtkCssName__: unique symbol
export const __gtkTemplate__: unique symbol
export const __gtkChildren__: unique symbol
export const __gtkInternalChildren__: unique symbol

// Expose GObject static properties for ES6 classes

export const GTypeName: unique symbol;
export const requires: unique symbol;
export const interfaces: unique symbol;
export const properties: unique symbol;
export const signals: unique symbol;
export const GTypeName: unique symbol
export const requires: unique symbol
export const interfaces: unique symbol
export const properties: unique symbol
export const signals: unique symbol

export let gtypeNameBasedOnJSPath: boolean;
export let gtypeNameBasedOnJSPath: boolean

export let TYPE_BOOLEAN: GType<boolean>;
export let Boolean: BooleanConstructor;
export let TYPE_BOOLEAN: GType<boolean>
export let Boolean: BooleanConstructor

export let TYPE_ENUM: GType<number>;
export let TYPE_FLAGS: GType<number>;
export let TYPE_ENUM: GType<number>
export let TYPE_FLAGS: GType<number>

export let TYPE_DOUBLE: GType<number>;
export let Double: NumberConstructor;
export let TYPE_DOUBLE: GType<number>
export let Double: NumberConstructor

export let TYPE_STRING: GType<string>;
export let String: StringConstructor;
export let TYPE_STRING: GType<string>
export let String: StringConstructor

export let TYPE_NONE: GType<undefined>;
export let TYPE_POINTER: GType<undefined>;
export let TYPE_BOXED: GType<unknown>;
export let TYPE_PARAM: GType<unknown>;
export let TYPE_INTERFACE: GType<unknown>;
export let TYPE_OBJECT: GType<object>;
export let TYPE_JSOBJECT: GType<Object>;
export let TYPE_VARIANT: GType<GLib.Variant>;
export let TYPE_INT: GType<number>;
export let TYPE_UINT: GType<number>;
export let TYPE_INT64: GType<number>;
export let TYPE_UINT64: GType<number>;
export let TYPE_NONE: GType<undefined>
export let TYPE_POINTER: GType<undefined>
export let TYPE_BOXED: GType<unknown>
export let TYPE_PARAM: GType<unknown>
export let TYPE_INTERFACE: GType<unknown>
export let TYPE_OBJECT: GType<object>
export let TYPE_JSOBJECT: GType<Object>
export let TYPE_VARIANT: GType<GLib.Variant>
export let TYPE_INT: GType<number>
export let TYPE_UINT: GType<number>
export let TYPE_INT64: GType<number>
export let TYPE_UINT64: GType<number>

// fake enum for signal accumulators, keep in sync with gi/object.c
export enum AccumulatorType {
NONE=0,
FIRST_WINS=1,
TRUE_HANDLED=2,
NONE = 0,
FIRST_WINS = 1,
TRUE_HANDLED = 2,
}

// A simple workaround if you have a class with .connect, .disconnect or .emit
// methods (such as Gio.Socket.connect or NMClient.Device.disconnect)
// The original g_signal_* functions are not introspectable anyway, because
// we need our own handling of signal argument marshalling
export function signal_connect(object: Object, name: string, handler: (...args: any[]) => any): number;
export function signal_connect_after(object: Object, name: string, handler: (...args: any[]) => any): number;
export function signal_emit_by_name(object: Object, name: string, ...args: any[]): void;
export function signal_connect(object: Object, name: string, handler: (...args: any[]) => any): number
export function signal_connect_after(object: Object, name: string, handler: (...args: any[]) => any): number
export function signal_emit_by_name(object: Object, name: string, ...args: any[]): void

/**
* Finds the first signal handler that matches certain selection criteria.
Expand All @@ -116,7 +117,10 @@ export function signal_emit_by_name(object: Object, name: string, ...args: any[]
* @param match.func the callback function the handler will invoke.
* @returns A valid non-0 signal handler ID for a successful match.
*/
export function signal_handler_find(instance: Object, match: {signalId: string, detail: string, func: (...args: any[]) => any}): number | bigint | object | null
export function signal_handler_find(
instance: Object,
match: { signalId: string; detail: string; func: (...args: any[]) => any },
): number | bigint | object | null

/**
* Blocks all handlers on an instance that match certain selection criteria.
Expand All @@ -132,7 +136,10 @@ export function signal_handler_find(instance: Object, match: {signalId: string,
* @param match.func the callback function the handler will invoke.
* @returns The number of handlers that matched.
*/
export function signal_handlers_block_matched(instance: Object, match: {signalId: string, detail: string, func: (...args: any[]) => any}): number
export function signal_handlers_block_matched(
instance: Object,
match: { signalId: string; detail: string; func: (...args: any[]) => any },
): number

/**
* Disconnects all handlers on an instance that match certain selection
Expand All @@ -150,7 +157,10 @@ export function signal_handlers_block_matched(instance: Object, match: {signalId
* @param match.func the callback function the handler will invoke.
* @returns The number of handlers that matched.
*/
export function signal_handlers_unblock_matched(instance: Object, match: {signalId: string, detail: string, func: (...args: any[]) => any}): number
export function signal_handlers_unblock_matched(
instance: Object,
match: { signalId: string; detail: string; func: (...args: any[]) => any },
): number

/**
* Disconnects all handlers on an instance that match certain selection
Expand All @@ -168,7 +178,10 @@ export function signal_handlers_unblock_matched(instance: Object, match: {signal
* @param match.func the callback function the handler will invoke.
* @returns The number of handlers that matched.
*/
export function signal_handlers_disconnect_matched(instance: Object, match: {signalId: string, detail: string, func: (...args: any[]) => any}): number
export function signal_handlers_disconnect_matched(
instance: Object,
match: { signalId: string; detail: string; func: (...args: any[]) => any },
): number

// Also match the macros used in C APIs, even though they're not introspected

Expand All @@ -179,7 +192,7 @@ export function signal_handlers_disconnect_matched(instance: Object, match: {sig
* @param func the callback function the handler will invoke.
* @returns The number of handlers that matched.
*/
export function signal_handlers_block_by_func(instance: Object, func: (...args: any[]) => any): number;
export function signal_handlers_block_by_func(instance: Object, func: (...args: any[]) => any): number

/**
* Unblocks all handlers on an instance that match `func`.
Expand All @@ -189,7 +202,7 @@ export function signal_handlers_block_by_func(instance: Object, func: (...args:
* @param func the callback function the handler will invoke.
* @returns The number of handlers that matched.
*/
export function signal_handlers_unblock_by_func(instance: Object, func: (...args: any[]) => any): number;
export function signal_handlers_unblock_by_func(instance: Object, func: (...args: any[]) => any): number

/**
* Disconnects all handlers on an instance that match `func`.
Expand All @@ -198,31 +211,26 @@ export function signal_handlers_unblock_by_func(instance: Object, func: (...args
* @param func the callback function the handler will invoke.
* @returns The number of handlers that matched.
*/
export function signal_handlers_disconnect_by_func(instance: Object, func: (...args: any[]) => any): number;
export function signal_handlers_disconnect_by_data(): void;
export function signal_handlers_disconnect_by_func(instance: Object, func: (...args: any[]) => any): number
export function signal_handlers_disconnect_by_data(): void

export type Property<K extends ParamSpec> = K extends ParamSpec<infer T>
? T
: any;
export type Property<K extends ParamSpec> = K extends ParamSpec<infer T> ? T : any

// TODO: What about the generated class Closure
export type TClosure<R = any, P = any> = (...args: P[]) => R;
// TODO: What about the generated class Closure
export type TClosure<R = any, P = any> = (...args: P[]) => R

type ObjectConstructor = { new(...args: any[]): Object };
type ObjectConstructor = { new (...args: any[]): Object }

export function registerClass<
T extends ObjectConstructor,
Props extends { [key: string]: ParamSpec },
Interfaces extends { $gtype: GType }[],
Sigs extends {
[key: string]: {
param_types?: readonly GType[];
[key: string]: any;
};
}
>(
options: MetaInfo<Props, Interfaces, Sigs>,
cls: T
): T;
param_types?: readonly GType[]
[key: string]: any
}
},
>(options: MetaInfo<Props, Interfaces, Sigs>, cls: T): T

export function registerClass<T extends ObjectConstructor>(cls: T): T

0 comments on commit 2b28915

Please sign in to comment.