-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(example): Ensure application exits properly on window close, fixes …
- Loading branch information
Showing
2 changed files
with
70 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() |