Skip to content

Commit

Permalink
fix(example): Ensure application exits properly on window close, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpLink committed Nov 6, 2024
1 parent d3644fd commit 84baa3a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 82 deletions.
27 changes: 12 additions & 15 deletions examples/adw-1-hello/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@
* @see https://gitlab.gnome.org/GNOME/libadwaita/-/blob/main/examples/hello-world/hello.c
*/

import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import Gtk from 'gi://Gtk?version=4.0';
import Adw from 'gi://Adw';
import Gio from 'gi://Gio'
import GLib from 'gi://GLib'
import Gtk from 'gi://Gtk?version=4.0'
import Adw from 'gi://Adw'

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

const app = new Adw.Application({
applicationId: 'com.github.jumplink.gjs.adw-1-hello',
flags: Gio.ApplicationFlags.FLAGS_NONE
});

const onQuit = () => {
loop.quit()
app.quit()
}
flags: Gio.ApplicationFlags.FLAGS_NONE,
})

const onActivate = (app: Adw.Application) => {
// Should support both camelCase and default property names, see https://github.com/gjsify/ts-for-gir/issues/138
const label = new Gtk.Label({
label: "Hello World",
label: 'Hello World',
marginBottom: 11,
marginTop: 11,
})
Expand All @@ -38,12 +33,14 @@ const onActivate = (app: Adw.Application) => {
window.set_title('Hello')
window.set_default_size(200, 200)

window.connect('close-request', onQuit)
window.connect('close-request', () => {
app.quit()
return false
})

window.set_child(label)
window.present()
}

app.connect('activate', onActivate)
app.run([imports.system.programInvocationName].concat(ARGV));
loop.run()
app.run([imports.system.programInvocationName].concat(ARGV))
125 changes: 58 additions & 67 deletions examples/gtk-4-template-vite/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,68 @@
// SPDX-FileCopyrightText: 2021 Andy Holmes <[email protected]>
// Based on https://gitlab.gnome.org/GNOME/gjs/-/blob/master/examples/gtk4-template.js

import GObject from 'gi://GObject?version=2.0';
import GLib from 'gi://GLib?version=2.0';
import Gtk from 'gi://Gtk?version=4.0';
import Gdk from 'gi://Gdk?version=4.0';

import Template from './gtk4-template.ui?raw';
import Style from './gtk4-template.css?inline';

Gtk.init();

const ExampleWindow = GObject.registerClass({
GTypeName: 'ExampleWindow',
Template: Template,
Children: [
'box',
],
InternalChildren: [
'button',
],
}, class ExampleWindow extends Gtk.Window {

box: Gtk.Box | null = null;
_button: Gtk.Button | null = null;

constructor() {
super();

// The template has been initialized and you can access the children
if (this.box) this.box.visible = true;

// Internal children are set on the instance prefixed with a `_`
if (this._button) this._button.visible = true;

this.initStyles()
}

// The signal handler bound in the UI file
_onButtonClicked(button: Gtk.Button) {
if (this instanceof Gtk.Window)
log('Callback scope is bound to `ExampleWindow`');

button.label = 'Button was clicked!';
}

/** Load the stylesheet in a CssProvider and add it to the Gtk.StyleContext */
protected initStyles() {
const provider = new Gtk.CssProvider();
console.log("Style", Style)
provider.load_from_string(Style)
const display = Gdk.Display.get_default()

if (!display) {
console.error('No display found')
return
import GObject from 'gi://GObject?version=2.0'
import GLib from 'gi://GLib?version=2.0'
import Gtk from 'gi://Gtk?version=4.0'
import Gdk from 'gi://Gdk?version=4.0'

import Template from './gtk4-template.ui?raw'
import Style from './gtk4-template.css?inline'

Gtk.init()

const ExampleWindow = GObject.registerClass(
{
GTypeName: 'ExampleWindow',
Template: Template,
Children: ['box'],
InternalChildren: ['button'],
},
class ExampleWindow extends Gtk.Window {
box: Gtk.Box | null = null
_button: Gtk.Button | null = null

constructor() {
super()

// The template has been initialized and you can access the children
if (this.box) this.box.visible = true

// Internal children are set on the instance prefixed with a `_`
if (this._button) this._button.visible = true

this.initStyles()
}

Gtk.StyleContext.add_provider_for_display(
display,
provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
);
}
});
// The signal handler bound in the UI file
_onButtonClicked(button: Gtk.Button) {
if (this instanceof Gtk.Window) log('Callback scope is bound to `ExampleWindow`')

button.label = 'Button was clicked!'
}

// Create a window that stops the program when it is closed
const loop = GLib.MainLoop.new(null, false);
/** Load the stylesheet in a CssProvider and add it to the Gtk.StyleContext */
protected initStyles() {
const provider = new Gtk.CssProvider()
console.log('Style', Style)
provider.load_from_string(Style)
const display = Gdk.Display.get_default()

if (!display) {
console.error('No display found')
return
}

const win = new ExampleWindow();
win.connect('close-request', () => loop.quit());
win.present();
Gtk.StyleContext.add_provider_for_display(display, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
}
},
)

// Create a window that stops the program when it is closed
const loop = GLib.MainLoop.new(null, false)

loop.run();
const win = new ExampleWindow()
win.connect('close-request', () => loop.quit())
win.present()

loop.run()

0 comments on commit 84baa3a

Please sign in to comment.