From 23c5de9b650703e367237afca312c04e50f16e33 Mon Sep 17 00:00:00 2001 From: Angelo Verlain Date: Mon, 19 Feb 2024 23:56:49 +0200 Subject: [PATCH] chore: eslint --- src/application.ts | 4 +-- src/drag-overlay.ts | 7 ++--- src/error.ts | 4 +-- src/mpris.ts | 45 +++++++++++++++---------------- src/playback-rate-button.ts | 11 +++----- src/player.ts | 37 +++++++------------------- src/stream.ts | 47 +++++++++++++++++--------------- src/volume-button.ts | 8 +++--- src/waveform.ts | 20 ++++++-------- src/window.ts | 34 +++++++++++++----------- tsconfig.json | 4 +-- types/ambient.d.ts | 53 ++++++++++++++++++++++--------------- 12 files changed, 130 insertions(+), 144 deletions(-) diff --git a/src/application.ts b/src/application.ts index 3e59dd2..d3c4d33 100644 --- a/src/application.ts +++ b/src/application.ts @@ -70,13 +70,13 @@ export class Application extends Adw.Application { this.present_main_window(); } - vfunc_open(files: Gio.FilePrototype[], hint: string): void { + vfunc_open(files: Gio.FilePrototype[]): void { this.present_main_window(); const window = this.get_active_window(); if (window && window instanceof Window && files.length > 0) { - window.load_file(files[0]); + void window.load_file(files[0]); } } } diff --git a/src/drag-overlay.ts b/src/drag-overlay.ts index 3a2d46a..e6f2425 100644 --- a/src/drag-overlay.ts +++ b/src/drag-overlay.ts @@ -37,10 +37,7 @@ export class APDragOverlay extends Adw.Bin { this._notify_current_drop_cb.bind(this), ); - this._drop_target.connect( - "drop", - this._drop_target_drop_cb.bind(this), - ); + this._drop_target.connect("drop", this._drop_target_drop_cb.bind(this)); this.add_controller(this._drop_target); } @@ -75,7 +72,7 @@ export class APDragOverlay extends Adw.Bin { return false; } - this.get_window().load_file(file); + void this.get_window().load_file(file); return true; } diff --git a/src/error.ts b/src/error.ts index 6733865..ca34451 100644 --- a/src/error.ts +++ b/src/error.ts @@ -29,7 +29,7 @@ export class APErrorState extends Adw.Bin { this._statusPage.set_description(message); } - show_error(title: string, error: any) { + show_error(title: string, error: unknown) { this._statusPage.title = title; if (error instanceof Error) { @@ -39,7 +39,7 @@ export class APErrorState extends Adw.Bin { } else { console.error("error: ", error); this.show_message( - error ? error.toString() : _("An unknown error happened"), + error ? (error as Error).toString() : _("An unknown error happened"), ); } } diff --git a/src/mpris.ts b/src/mpris.ts index 0723d11..4ce86ef 100644 --- a/src/mpris.ts +++ b/src/mpris.ts @@ -93,7 +93,10 @@ const MPRIS_XML = ` export class DBusInterface { connection!: Gio.DBusConnection; - constructor(private name: string, private path: string) { + constructor( + private name: string, + private path: string, + ) { Gio.bus_get(Gio.BusType.SESSION, null) .then(this.got_bus.bind(this)) .catch((e: GLib.Error) => { @@ -160,7 +163,7 @@ export class DBusInterface { parameters: GLib.Variant, invocation: Gio.DBusMethodInvocation, ) { - const args = parameters.unpack() as any[]; + const args = parameters.unpack() as unknown[]; this.method_inargs.get(method_name)!.forEach((sig, i) => { if (sig === "h") { @@ -174,8 +177,10 @@ export class DBusInterface { let result; + type Method = (...args: unknown[]) => unknown; + try { - result = (this[method_snake_name as keyof this] as any)(...args); + result = (this[method_snake_name as keyof this] as Method)(...args); } catch (error) { invocation.return_dbus_error( interface_name, @@ -196,7 +201,7 @@ export class DBusInterface { } } - _dbus_emit_signal(signal_name: string, values: Record) { + _dbus_emit_signal(signal_name: string, values: Record) { if (this.signals.size === 0) return; const signal = this.signals.get(signal_name)!; @@ -208,6 +213,8 @@ export class DBusInterface { parameters.push(GLib.Variant.new(signature, value)); } + // TODO: the type is incorrect + // eslint-disable-next-line @typescript-eslint/no-explicit-any const variant = GLib.Variant.new_tuple(parameters as any); this.connection.emit_signal( @@ -259,7 +266,7 @@ export class MPRIS extends DBusInterface { this.stream.connect("notify::seeking", () => { if (!this.stream.seeking) { - this._on_seek_finished(this as any, this.stream.timestamp); + this._on_seek_finished(this, this.stream.timestamp); } }); } @@ -340,7 +347,7 @@ export class MPRIS extends DBusInterface { ); } - private _on_seek_finished(_: Gtk.Widget, position: number) { + private _on_seek_finished(_: unknown, position: number) { this._seeked(Math.trunc(position)); } @@ -437,7 +444,7 @@ export class MPRIS extends DBusInterface { * * Not implemented */ - open_uri(_uri: string) { + open_uri() { return; } @@ -471,7 +478,7 @@ export class MPRIS extends DBusInterface { const iface = interface_name.get_string()[0]; switch (iface) { - case this.MEDIA_PLAYER2_IFACE: + case this.MEDIA_PLAYER2_IFACE: { const application_id = this.app.get_application_id() ?? ""; return { @@ -485,7 +492,8 @@ export class MPRIS extends DBusInterface { SupportedUriSchemes: GLib.Variant.new_strv([]), SupportedMimeTypes: GLib.Variant.new_strv([]), }; - case this.MEDIA_PLAYER2_PLAYER_IFACE: + } + case this.MEDIA_PLAYER2_PLAYER_IFACE: { const position_msecond = Math.trunc(this.stream.timestamp); const playback_status = this._get_playback_status(); @@ -505,6 +513,7 @@ export class MPRIS extends DBusInterface { CanSeek: GLib.Variant.new_boolean(true), CanControl: GLib.Variant.new_boolean(true), }; + } case "org.freedesktop.DBus.Properties": return {}; case "org.freedesktop.DBus.Introspectable": @@ -550,14 +559,16 @@ export class MPRIS extends DBusInterface { break; default: console.warn( - `MPRIS can not set, as it does not implement ${interface_name}`, + `MPRIS can not set, as it does not implement ${ + interface_name.get_string()[0] + }`, ); } } _properties_changed( interface_name: string, - changed_properties: Record>, + changed_properties: Record, invalidated_properties: string[], ) { this._dbus_emit_signal("PropertiesChanged", { @@ -571,15 +582,3 @@ export class MPRIS extends DBusInterface { return MPRIS_XML; } } - -function hex_encode(string: string) { - var hex, i; - - var result = ""; - for (i = 0; i < string.length; i++) { - hex = string.charCodeAt(i).toString(16); - result += ("000" + hex).slice(-4); - } - - return result; -} diff --git a/src/playback-rate-button.ts b/src/playback-rate-button.ts index d4e488e..d1b50e7 100644 --- a/src/playback-rate-button.ts +++ b/src/playback-rate-button.ts @@ -13,10 +13,7 @@ export class APPlaybackRateButton extends Adw.Bin { { GTypeName: "APPlaybackRateButton", Template: "resource:///com/vixalien/decibels/playback-rate-button.ui", - InternalChildren: [ - "adjustment", - "label" - ], + InternalChildren: ["adjustment", "label"], Properties: {}, }, this, @@ -39,7 +36,7 @@ export class APPlaybackRateButton extends Adw.Bin { GObject.BindingFlags.SYNC_CREATE, ); - // @ts-ignore GObject.BindingTransformFunc return arguments are not correctly typed + // @ts-expect-error GObject.BindingTransformFunc return arguments are not correctly typed window.stream.bind_property_full( "rate", this._label, @@ -52,14 +49,14 @@ export class APPlaybackRateButton extends Adw.Bin { null, ); - // @ts-ignore GObject.BindingTransformFunc return arguments are not correctly typed + // @ts-expect-error GObject.BindingTransformFunc return arguments are not correctly typed window.stream.bind_property_full( "rate", this._label, "visible", GObject.BindingFlags.SYNC_CREATE, (_binding, from: number) => { - const rounded = Math.round(from * 10) / 10 + const rounded = Math.round(from * 10) / 10; return [true, rounded !== 1]; }, null, diff --git a/src/player.ts b/src/player.ts index 2f7ee18..58ab712 100644 --- a/src/player.ts +++ b/src/player.ts @@ -39,9 +39,7 @@ export class APPlayerState extends Adw.Bin { "waveform", "scale", ], - Children: [ - "headerbar", - ], + Children: ["headerbar"], }, this, ); @@ -63,7 +61,7 @@ export class APPlayerState extends Adw.Bin { GObject.BindingFlags.SYNC_CREATE, ); - // @ts-ignore GObject.BindingTransformFunc return arguments are not correctly typed + // @ts-expect-error GObject.BindingTransformFunc return arguments are not correctly typed window.stream.bind_property_full( "duration", this._duration_label, @@ -76,13 +74,13 @@ export class APPlayerState extends Adw.Bin { null, ); - // @ts-ignore GObject.BindingTransformFunc return arguments are not correctly typed + // @ts-expect-error GObject.BindingTransformFunc return arguments are not correctly typed window.stream.bind_property_full( "timestamp", this._scale_adjustment, "value", GObject.BindingFlags.SYNC_CREATE, - (_binding, from: number) => { + () => { if ((this._scale.get_state_flags() & Gtk.StateFlags.ACTIVE) != 0) { return [false, null]; } @@ -92,7 +90,7 @@ export class APPlayerState extends Adw.Bin { null, ); - // @ts-ignore GObject.BindingTransformFunc return arguments are not correctly typed + // @ts-expect-error GObject.BindingTransformFunc return arguments are not correctly typed window.stream.bind_property_full( "timestamp", this._timestamp_label, @@ -111,24 +109,19 @@ export class APPlayerState extends Adw.Bin { GObject.BindingFlags.SYNC_CREATE | GObject.BindingFlags.BIDIRECTIONAL, ); - // @ts-ignore GObject.BindingTransformFunc return arguments are not correctly typed + // @ts-expect-error GObject.BindingTransformFunc return arguments are not correctly typed window.stream.bind_property_full( "playing", this._playback_image, "icon-name", GObject.BindingFlags.SYNC_CREATE, (_binding, from: boolean) => { - return [ - true, - from - ? "pause-symbolic" - : "play-symbolic", - ]; + return [true, from ? "pause-symbolic" : "play-symbolic"]; }, null, ); - // @ts-ignore GObject.BindingTransformFunc return arguments are not correctly typed + // @ts-expect-error GObject.BindingTransformFunc return arguments are not correctly typed window.stream.bind_property_full( "playing", this._playback_button, @@ -140,7 +133,7 @@ export class APPlayerState extends Adw.Bin { null, ); - // @ts-ignore GObject.BindingTransformFunc return arguments are not correctly typed + // @ts-expect-error GObject.BindingTransformFunc return arguments are not correctly typed window.stream.bind_property_full( "timestamp", this._waveform, @@ -191,11 +184,7 @@ export class APPlayerState extends Adw.Bin { }); } - private scale_change_value_cb( - _scale: Gtk.Scale, - _scroll: Gtk.ScrollType, - _value: number, - ) { + private scale_change_value_cb() { const window = this.get_root() as Window; const stream = window?.stream; @@ -229,8 +218,6 @@ export class APPlayerState extends Adw.Bin { private key_pressed_cb( _controller: Gtk.EventControllerKey, keyval: number, - _keycode: number, - _modifier: Gdk.ModifierType, ): boolean { const window = this.get_root() as Window; const stream = window?.stream; @@ -272,10 +259,6 @@ export class APPlayerState extends Adw.Bin { } } -function get_window() { - return (Gtk.Application.get_default() as Gtk.Application).get_active_window(); -} - function seconds_to_string(seconds: number) { // show the duration in the format "mm:ss" // show hours if the duration is longer than an hour diff --git a/src/stream.ts b/src/stream.ts index 3e5e560..5ed0eb2 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -16,7 +16,8 @@ if (!Gst.is_initialized()) { Gst.init(null); } -type GTypeToType = Y extends GObject.GType ? T +type GTypeToType = Y extends GObject.GType + ? T : never; type GTypeArrayToTypeArray = { @@ -115,16 +116,18 @@ class APPlaySignalAdapter extends GObject.Object { case GstPlay.PlayMessage.END_OF_STREAM: this.emit_message("end-of-stream", []); break; - case GstPlay.PlayMessage.ERROR: + case GstPlay.PlayMessage.ERROR: { const error = GstPlay.play_message_parse_error(message); this.emit_message("error", [error[0]!, error[1]!]); break; - case GstPlay.PlayMessage.WARNING: + } + case GstPlay.PlayMessage.WARNING: { const warning = GstPlay.play_message_parse_warning(message); this.emit_message("warning", [warning[0]!, warning[1]!]); break; + } case GstPlay.PlayMessage.VIDEO_DIMENSIONS_CHANGED: this.emit_message( "video-dimensions-changed", @@ -251,17 +254,20 @@ export class APMediaStream extends Gtk.MediaStream { this.tags = info.get_tags(); switch (info.get_result()) { - case GstPbUtils.DiscovererResult.OK: + case GstPbUtils.DiscovererResult.OK: { const uri = info.get_uri(); this._play.set_uri(uri); this.peaks_generator.generate_peaks_async(uri); return; + } case GstPbUtils.DiscovererResult.MISSING_PLUGINS: this.gerror( GLib.Error.new_literal( GstPlay.PlayError, GstPlay.PlayError.FAILED, - _("File uses a format that cannot be played. Additional media codecs may be required."), + _( + "File uses a format that cannot be played. Additional media codecs may be required.", + ), ), ); return; @@ -271,7 +277,9 @@ export class APMediaStream extends Gtk.MediaStream { GLib.Error.new_literal( GstPlay.PlayError, GstPlay.PlayError.FAILED, - _("An error happened while trying to get information about the file. Please try again."), + _( + "An error happened while trying to get information about the file. Please try again.", + ), ), ); return; @@ -291,7 +299,9 @@ export class APMediaStream extends Gtk.MediaStream { GLib.Error.new_literal( GstPlay.PlayError, GstPlay.PlayError.FAILED, - _("Reading the file's information timed out. Please try again."), + _( + "Reading the file's information timed out. Please try again.", + ), ), ); return; @@ -473,7 +483,9 @@ export class APMediaStream extends Gtk.MediaStream { get_duration() { if (!this._play.media_info) return 0; - return get_safe_duration(this._play.media_info.get_duration()) / Gst.USECOND; + return ( + get_safe_duration(this._play.media_info.get_duration()) / Gst.USECOND + ); } // property: error @@ -549,7 +561,7 @@ export class APMediaStream extends Gtk.MediaStream { // handlers - private uri_loaded_cb(_play: GstPlay.Play, uri: string): void { + private uri_loaded_cb(): void { this.emit("loaded"); this.notify("rate"); this.play(); @@ -581,7 +593,7 @@ export class APMediaStream extends Gtk.MediaStream { } } - private duration_changed_cb(_play: GstPlay.Play): void { + private duration_changed_cb(): void { this.notify("duration"); } @@ -602,7 +614,7 @@ export class APMediaStream extends Gtk.MediaStream { this.gerror(error); } - protected eos_cb(_play: GstPlay.Play): void { + protected eos_cb(): void { this.pause(); this.seek(0); @@ -639,12 +651,12 @@ export class APMediaStream extends Gtk.MediaStream { } } - private volume_changed_cb(_play: GstPlay.Play): void { + private volume_changed_cb(): void { this.notify("volume"); this.notify("cubic-volume"); } - private mute_changed_cb(_play: GstPlay.Play): void { + private mute_changed_cb(): void { this.notify("muted"); } @@ -697,9 +709,7 @@ export class APMediaStream extends Gtk.MediaStream { } skip_seconds(seconds: number) { - this.seek( - Math.max(this.timestamp + seconds * Gst.MSECOND, 0), - ); + this.seek(Math.max(this.timestamp + seconds * Gst.MSECOND, 0)); } get_action_group() { @@ -743,11 +753,6 @@ export class APMediaStream extends Gtk.MediaStream { } } -// compare numbers of different precisions -function compare_numbers(a: number, b: number): boolean { - return Math.abs(Math.fround(a) - Math.fround(b)) < 0.00001; -} - export function get_linear_volume(value: number) { return GstAudio.stream_volume_convert_volume( GstAudio.StreamVolumeFormat.CUBIC, diff --git a/src/volume-button.ts b/src/volume-button.ts index cd1b727..06649d4 100644 --- a/src/volume-button.ts +++ b/src/volume-button.ts @@ -11,10 +11,7 @@ export class APVolumeButton extends Adw.Bin { { GTypeName: "APVolumeButton", Template: "resource:///com/vixalien/decibels/volume-button.ui", - InternalChildren: [ - "adjustment", - "menu_button", - ], + InternalChildren: ["adjustment", "menu_button"], Properties: { value: GObject.param_spec_double( "value", @@ -23,7 +20,8 @@ export class APVolumeButton extends Adw.Bin { 0, 1.0, 0.5, - GObject.ParamFlags.READABLE | GObject.ParamFlags.WRITABLE | + GObject.ParamFlags.READABLE | + GObject.ParamFlags.WRITABLE | GObject.ParamFlags.EXPLICIT_NOTIFY, ), }, diff --git a/src/waveform.ts b/src/waveform.ts index 8491f98..a43df6c 100644 --- a/src/waveform.ts +++ b/src/waveform.ts @@ -61,7 +61,7 @@ export class APWaveForm extends Gtk.DrawingArea { "peaks", "Peaks", "The peaks of the currently playing song", - (Object as any).$gtype, + Object.$gtype, GObject.ParamFlags.READWRITE, ), }, @@ -109,8 +109,8 @@ export class APWaveForm extends Gtk.DrawingArea { private dragUpdate(_gesture: Gtk.GestureDrag, offset_x: number): void { if (this.drag_start_position != null) { - const after = this.drag_start_position - - (offset_x / (this.peaks.length * GUTTER)); + const after = + this.drag_start_position - offset_x / (this.peaks.length * GUTTER); this._position = Math.max(Math.min(after, 1), 0); this.queue_draw(); @@ -132,7 +132,7 @@ export class APWaveForm extends Gtk.DrawingArea { const vertiCenter = height / 2; const horizCenter = width / 2; - let pointer = horizCenter - (this._position * peaks.length * GUTTER); + let pointer = horizCenter - this._position * peaks.length * GUTTER; const styleContext = this.get_style_context(); @@ -166,9 +166,7 @@ export class APWaveForm extends Gtk.DrawingArea { pointer = pointer + invisible_peaks; } - for ( - const peak of peaks.slice(invisible_peaks / GUTTER) - ) { + for (const peak of peaks.slice(invisible_peaks / GUTTER)) { // this shouldn't happen, but just in case if (pointer < 0) { pointer += GUTTER; @@ -230,7 +228,7 @@ export class APWaveForm extends Gtk.DrawingArea { const ok = lookupColor[0]; if (ok) return lookupColor[1]; return styleContext.get_color(); - } + } } export class APPeaksGenerator extends GObject.Object { @@ -245,7 +243,7 @@ export class APPeaksGenerator extends GObject.Object { "peaks", "Peaks", "The peaks of the currently playing song", - (Object as any).$gtype, + Object.$gtype, GObject.ParamFlags.READABLE, ), }, @@ -299,9 +297,7 @@ export class APPeaksGenerator extends GObject.Object { case Gst.MessageType.ELEMENT: { const s = message.get_structure(); if (s && s.has_name("level")) { - const peakVal = s.get_value( - "rms", - ) as unknown as GObject.ValueArray; + const peakVal = s.get_value("rms") as unknown as GObject.ValueArray; if (peakVal) { const peak = peakVal.get_nth(0) as number; diff --git a/src/window.ts b/src/window.ts index 8797f8c..de718a0 100644 --- a/src/window.ts +++ b/src/window.ts @@ -142,7 +142,7 @@ export class Window extends Adw.ApplicationWindow { this.mpris = new MPRIS(this.stream); - this.stream.connect("error", (_source, error) => { + this.stream.connect("error", (_source, error: GLib.Error) => { console.error( "error during playback", error.toString(), @@ -171,7 +171,7 @@ export class Window extends Adw.ApplicationWindow { (this.add_action_entries as AddActionEntries)([ { name: "open-file", - activate: (_source, _param) => { + activate: () => { this.open_file(); }, }, @@ -183,18 +183,20 @@ export class Window extends Adw.ApplicationWindow { } async load_file(file: Gio.File) { - const fileInfo = await file.query_info_async( - "standard::*", - Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, - GLib.PRIORITY_DEFAULT, - null, - ).catch(() => { - this.show_error( - _("File Cannot Be Played"), - _("No available audio file found"), - ); - return null; - }); + const fileInfo = await file + .query_info_async( + "standard::*", + Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, + GLib.PRIORITY_DEFAULT, + null, + ) + .catch(() => { + this.show_error( + _("File Cannot Be Played"), + _("No available audio file found"), + ); + return null; + }); if (!fileInfo) return; @@ -222,7 +224,7 @@ export class Window extends Adw.ApplicationWindow { (this.file_dialog.open(this, null) as unknown as Promise) .then((file) => { if (file) { - this.load_file(file); + void this.load_file(file); } else { this.show_error( _("File Cannot Be Played"), @@ -246,7 +248,7 @@ export class Window extends Adw.ApplicationWindow { this._stack.visible_child_name = page; } - show_error(title: string, error: any) { + show_error(title: string, error: unknown) { this.stream.stop(); this.show_stack_page("error"); diff --git a/tsconfig.json b/tsconfig.json index 8e3e1e1..4c4ea15 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -28,5 +28,5 @@ "gi-types/gi.d.ts", "types/ambient.d.ts", "src" - ] -} \ No newline at end of file + ], +} diff --git a/types/ambient.d.ts b/types/ambient.d.ts index b9bbc27..dca9424 100644 --- a/types/ambient.d.ts +++ b/types/ambient.d.ts @@ -1,26 +1,35 @@ -declare function _(id: string): string; -declare function C_(ctx: string, id: string): string; -declare function print(args: string): void; -declare function log(obj: object, others?: object[]): void; -declare function log(msg: string, substitutions?: any[]): void; +import GObject from "gi://GObject"; -declare const pkg: { - version: string; - name: string; -}; -declare module console { - export function error(...args: any[]): void; - export function log(...args: any[]): void; - export function warn(...args: any[]): void; - export function info(...args: any[]): void; - export function debug(...args: any[]): void; -} - -declare module imports { - const format: { - format(this: String, ...args: any[]): string; - printf(fmt: string, ...args: any[]): string; - vprintf(fmt: string, args: any[]): string; +declare global { + const pkg: { + version: string; + name: string; }; + + function _(id: string): string; + function C_(ctx: string, id: string): string; + function print(args: string): void; + function log(obj: object, others?: object[]): void; + function log(msg: string, substitutions?: any[]): void; + + const console: { + error(...args: any[]): void; + log(...args: any[]): void, + warn(...args: any[]): void; + info(...args: any[]): void; + debug(...args: any[]): void; + } + + const imports: { + format: { + format(this: String, ...args: any[]): string; + printf(fmt: string, ...args: any[]): string; + vprintf(fmt: string, args: any[]): string; + }; + } + + interface ObjectConstructor { + $gtype: GObject.GType; + } }