-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from gjsify/add_action_entries
fix(gio): Correct types for overridden `ActionMap.add_action_entries` method
- Loading branch information
Showing
6 changed files
with
218 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import Gio from 'gi://Gio'; | ||
import GLib from 'gi://GLib'; | ||
import GObject from 'gi://GObject'; | ||
|
||
export class ExampleApplication extends Gio.Application { | ||
static { | ||
GObject.registerClass({ | ||
GTypeName: 'ExampleApplication' | ||
}, this); | ||
} | ||
|
||
constructor() { | ||
super({ | ||
application_id: 'org.example.ActionEntries', | ||
flags: Gio.ApplicationFlags.FLAGS_NONE, | ||
}); | ||
this.initActions(); | ||
} | ||
|
||
private initActions() { | ||
// Define multiple actions at once using ActionEntryObj type | ||
const actions: Gio.ActionEntryObj[] = [ | ||
// Simple action without state or parameter | ||
{ | ||
name: 'quit', | ||
activate: () => { | ||
console.log('Quitting application...'); | ||
this.quit(); | ||
}, | ||
}, | ||
|
||
// Action with parameter | ||
{ | ||
name: 'greet', | ||
parameter_type: 's', // GVariant type string for string parameter | ||
activate: (_action, parameter) => { | ||
if (parameter) { | ||
const name = parameter.get_string()[0]; | ||
console.log(`Hello, ${name}!`); | ||
} | ||
}, | ||
}, | ||
|
||
// Stateful toggle action | ||
{ | ||
name: 'dark-mode', | ||
state: 'false', // Initial state as string | ||
change_state: (_action, value) => { | ||
if (value) { | ||
const isDark = value.get_boolean(); | ||
console.log(`Dark mode ${isDark ? 'enabled' : 'disabled'}`); | ||
// Get the action and set its state | ||
const action = this.lookup_action('dark-mode') as Gio.SimpleAction; | ||
action.set_state(value); | ||
} | ||
}, | ||
}, | ||
|
||
// Action with optional parameter | ||
{ | ||
name: 'save', | ||
parameter_type: '(sb)', // Tuple of string and boolean | ||
activate: (_action, parameter) => { | ||
if (parameter) { | ||
const [filename, backup] = parameter.deep_unpack() as [string, boolean]; | ||
console.log(`Saving ${filename} (backup: ${backup})`); | ||
} else { | ||
console.log('Quick save'); | ||
} | ||
}, | ||
}, | ||
]; | ||
|
||
// Add all actions at once | ||
this.add_action_entries(actions); | ||
} | ||
|
||
vfunc_activate() { | ||
// Test the actions | ||
this.activate_action('greet', GLib.Variant.new_string('World')); | ||
|
||
// For stateful actions, we should use change-state instead of activate | ||
const darkModeAction = this.lookup_action('dark-mode') as Gio.SimpleAction; | ||
darkModeAction.change_state(GLib.Variant.new_boolean(true)); | ||
|
||
this.activate_action('save', GLib.Variant.new('(sb)', ['document.txt', true])); | ||
this.activate_action('quit', null); | ||
} | ||
} | ||
|
||
// Run the application | ||
const app = new ExampleApplication(); | ||
app.run([]); |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@ts-for-gir-example/gio-2-action-entries", | ||
"version": "4.0.0-beta.19", | ||
"description": "Example demonstrating Gio.ActionEntry", | ||
"type": "module", | ||
"private": true, | ||
"scripts": { | ||
"build:app": "tsc", | ||
"build": "yarn build:app", | ||
"start:app": "gjs -m dist/main.js", | ||
"start": "yarn build && yarn start:app", | ||
"validate": "yarn validate:types", | ||
"validate:types": "tsc --noEmit", | ||
"clear": "rm -rf dist" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.6.3" | ||
}, | ||
"dependencies": { | ||
"@girs/gio-2.0": "workspace:^", | ||
"@girs/gjs": "workspace:^", | ||
"@girs/glib-2.0": "workspace:^" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["ESNext"], | ||
"types": ["@girs/gjs", "@girs/gjs/dom", "@girs/gio-2.0", "@girs/glib-2.0"], | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleResolution": "bundler", | ||
"strict": true, | ||
"noImplicitAny": true, | ||
"strictNullChecks": true, | ||
"noImplicitThis": true, | ||
"alwaysStrict": true, | ||
"outDir": "./dist" | ||
}, | ||
"files": [ | ||
"main.ts" | ||
] | ||
} |
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
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