Skip to content

Commit

Permalink
Merge pull request #18 from s0/type-improvements
Browse files Browse the repository at this point in the history
Introduce type-definitions for `node-gtk`
  • Loading branch information
JezerM authored Mar 14, 2022
2 parents 57609aa + 335eada commit f7b5463
Show file tree
Hide file tree
Showing 7 changed files with 299 additions and 39 deletions.
52 changes: 26 additions & 26 deletions ts/bridge/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { dialog, ipcMain } from "electron";
// @ts-ignore Until there's a @types/node-gtk
import * as gi from "node-gtk";
import * as fs from "fs";
import * as os from "os";
Expand Down Expand Up @@ -150,15 +149,15 @@ export class Greeter {
* @readonly
* @deprecated Use `battery_data`
*/
public get batteryData(): LightDMBattery | object {
public get batteryData(): LightDMBattery | null {
return battery_to_obj(this._battery);
}

/**
* Gets the battery data.
* @readonly
*/
public get battery_data(): LightDMBattery | object {
public get battery_data(): LightDMBattery | null {
return battery_to_obj(this._battery);
}

Expand Down Expand Up @@ -276,7 +275,7 @@ export class Greeter {
* The current language or "null" if no language.
* @readonly
*/
public get language(): LightDMLanguage | object {
public get language(): LightDMLanguage | null {
return language_to_obj(LightDM.getLanguage());
}

Expand All @@ -285,30 +284,31 @@ export class Greeter {
* @readonly
*/
public get languages(): LightDMLanguage[] {
return reduceArray(
LightDM.getLanguages(),
language_to_obj
) as LightDMLanguage[];
return reduceArray(LightDM.getLanguages(), language_to_obj).filter(
isDefined
);
}

/**
* The currently active layout for the selected user.
*/
public get layout(): LightDMLayout | object {
public get layout(): LightDMLayout | null {
return layout_to_obj(LightDM.getLayout());
}

public set layout(layout: LightDMLayout | object) {
LightDM.getLayout();
LightDM.setLayout(new LightDM.Layout(layout));
public set layout(layout: LightDMLayout | null) {
if (layout) {
LightDM.getLayout();
LightDM.setLayout(new LightDM.Layout(layout));
}
}

/**
* A list of keyboard layouts to present to the user.
* @readonly
*/
public get layouts(): LightDMLayout[] {
return reduceArray(LightDM.getLayouts(), layout_to_obj) as LightDMLayout[];
return reduceArray(LightDM.getLayouts(), layout_to_obj).filter(isDefined);
}

/**
Expand All @@ -324,10 +324,9 @@ export class Greeter {
* @readonly
*/
public get remote_sessions(): LightDMSession[] {
return reduceArray(
LightDM.getRemoteSessions(),
session_to_obj
) as LightDMSession[];
return reduceArray(LightDM.getRemoteSessions(), session_to_obj).filter(
isDefined
);
}

/**
Expand All @@ -351,10 +350,7 @@ export class Greeter {
* @readonly
*/
public get sessions(): LightDMSession[] {
return reduceArray(
LightDM.getSessions(),
session_to_obj
) as LightDMSession[];
return reduceArray(LightDM.getSessions(), session_to_obj).filter(isDefined);
}

/**
Expand Down Expand Up @@ -390,7 +386,7 @@ export class Greeter {
* @readonly
*/
public get users(): LightDMUser[] {
return reduceArray(LightDMUsers.getUsers(), user_to_obj) as LightDMUser[];
return reduceArray(LightDMUsers.getUsers(), user_to_obj).filter(isDefined);
}

/**
Expand Down Expand Up @@ -553,8 +549,8 @@ function get_layouts(config_layouts: string[]): LightDMLayout[] {
for (let conf_lay of config_layouts) {
conf_lay = conf_lay.replace(/\s/g, "\t");
if (ldm_lay.getName() == conf_lay) {
const lays_chips = layout_to_obj(ldm_lay) as LightDMLayout;
if (Object.keys(lays_chips).length == 0) continue;
const lays_chips = layout_to_obj(ldm_lay);
if (!lays_chips) continue;
final.push(lays_chips);
}
}
Expand Down Expand Up @@ -711,15 +707,19 @@ export class ThemeUtils {
}
}

function reduceArray<T>(arr: unknown[], func: (arg: unknown) => T): T[] {
function reduceArray<I, O>(arr: I[], func: (arg: I) => O): O[] {
if (!Array.isArray(arr)) return [];
return arr.reduce((acc: T[], val) => {
return arr.reduce((acc: O[], val) => {
const v = func(val);
acc.push(v);
return acc;
}, []);
}

function isDefined<T>(val: T | null | undefined): val is T {
return val !== null && val !== undefined;
}

function hasKey<T>(obj: T, key: PropertyKey): key is keyof T {
return key in obj;
}
Expand Down
29 changes: 18 additions & 11 deletions ts/bridge/bridge_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ import {
LightDMUser,
} from "../ldm_interfaces";

import { LightDM } from "node-gtk";
import { Battery } from "../utils/battery";

// eslint-disable-next-line
function session_to_obj(session: any): LightDMSession | object {
if (!session) return {};
function session_to_obj(
session: LightDM.LightDMSession
): LightDMSession | null {
if (!session) return null;
return {
comment: session.getComment(),
key: session.getKey(),
Expand All @@ -18,8 +23,8 @@ function session_to_obj(session: any): LightDMSession | object {
}

// eslint-disable-next-line
function user_to_obj(user: any): LightDMUser | object {
if (!user) return {};
function user_to_obj(user: LightDM.LightDMUser): LightDMUser | null {
if (!user) return null;
return {
background: user.getBackground(),
display_name: user.getDisplayName(),
Expand All @@ -35,8 +40,10 @@ function user_to_obj(user: any): LightDMUser | object {
}

// eslint-disable-next-line
function language_to_obj(lang: any): LightDMLanguage | object {
if (!lang) return {};
function language_to_obj(
lang: LightDM.LightDMLanguage
): LightDMLanguage | null {
if (!lang) return null;
return {
code: lang.getCode(),
name: lang.getName(),
Expand All @@ -45,8 +52,8 @@ function language_to_obj(lang: any): LightDMLanguage | object {
}

// eslint-disable-next-line
function layout_to_obj(layout: any): LightDMLayout | object {
if (!layout) return {};
function layout_to_obj(layout: LightDM.LightDMLayout): LightDMLayout | null {
if (!layout) return null;
return {
description: layout.getDescription(),
name: layout.getName(),
Expand All @@ -55,9 +62,9 @@ function layout_to_obj(layout: any): LightDMLayout | object {
}

// eslint-disable-next-line
function battery_to_obj(battery: any): LightDMBattery | object {
if (!battery) return {};
if (battery._batteries.length == 0) return {};
function battery_to_obj(battery: Battery): LightDMBattery | null {
if (!battery) return null;
if (battery._batteries.length == 0) return null;
return {
name: battery.name,
level: battery.level,
Expand Down
16 changes: 16 additions & 0 deletions ts/lib/node-gtk-gio.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
declare module "node-gtk" {
export namespace Gio {
abstract class AsyncResult {}
class Task implements AsyncResult {}
class SimpleAsyncResult implements AsyncResult {}
class Cancellable {}

export interface Gio {
Cancellable: typeof Cancellable;
Task: typeof Task;
SimpleAsyncResult: typeof SimpleAsyncResult;
AsyncResult: typeof AsyncResult;
}
}
export function require(name: "Gio", version: "2.0"): Gio.Gio;
}
10 changes: 10 additions & 0 deletions ts/lib/node-gtk-gobject.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
declare module "node-gtk" {
export namespace GObject {
class Object {}

export interface GObject {
Object: typeof Object;
}
}
export function require(name: "GObject", version: "2.0"): GObject.GObject;
}
Loading

0 comments on commit f7b5463

Please sign in to comment.