Skip to content

Commit

Permalink
Use static GObject.registerClass syntax for st-15-layout-manager-example
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Nov 13, 2024
1 parent 0e1e75d commit aff812f
Showing 1 changed file with 42 additions and 50 deletions.
92 changes: 42 additions & 50 deletions examples/st-15-layout-manager/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,49 @@
import Clutter from 'gi://Clutter';
import St from 'gi://St';
import GObject from 'gi://GObject';
import GLib from 'gi://GLib';

// Define a custom widget class with GridLayout
const GridLayoutWidget = GObject.registerClass(
class GridLayoutWidget extends St.Widget<Clutter.GridLayout> {
constructor() {
super({
layout_manager: new Clutter.GridLayout()
});

// Create and add labels in a grid pattern
const labels = [
'Top Left', 'Top Right',
'Bottom Left', 'Bottom Right'
];

labels.forEach((text, index) => {
const label = new St.Label({ text });
this.layout_manager.attach(
label,
index % 2, // column
Math.floor(index / 2), // row
1, 1
);
});
}
export class GridLayoutWidget extends St.Widget<Clutter.GridLayout> {
static {
GObject.registerClass({
GTypeName: 'GridLayoutWidget',
}, this);
}
);

// Main loop
const loop = new GLib.MainLoop(null, false);

// Create main window
const stage = new Clutter.Stage({
width: 300,
height: 200,
});

// Add our widget
const widget = new GridLayoutWidget();
stage.add_child(widget);

// Center the widget
widget.set_position(
stage.width / 2 - widget.width / 2,
stage.height / 2 - widget.height / 2
);

// Connect signals
stage.connect('destroy', () => loop.quit());
stage.show();
constructor() {
super({
layout_manager: new Clutter.GridLayout()
});

// Create and add labels in a grid pattern
const labels = [
'Top Left', 'Top Right',
'Bottom Left', 'Bottom Right'
];

labels.forEach((text, index) => {
const label = new St.Label({ text });
this.layout_manager.attach(
label,
index % 2, // column
Math.floor(index / 2), // row
1, 1
);
});
}
}

// Start the main loop
loop.run();
/**
* Example usage in your extension:
*
* class Extension {
* enable() {
* this._widget = new GridLayoutWidget();
* Main.uiGroup.add_child(this._widget);
* }
*
* disable() {
* this._widget.destroy();
* this._widget = null;
* }
* }
*/

0 comments on commit aff812f

Please sign in to comment.