Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type definitions #11

Merged
merged 10 commits into from
Jan 1, 2024
Merged

Improve type definitions #11

merged 10 commits into from
Jan 1, 2024

Conversation

swsnr
Copy link
Collaborator

@swsnr swsnr commented Dec 30, 2023

This pull request addresses a few issues I stumbled upon while trying to move from my hand-written types to the types in this repo. It's by no means complete (there were more issues), but I'm stuck in the process due to some other issues (see #10), so I figured I'd open a merge request with what I already got.

Recommended review is commit by commit, as every commit addresses one problem.

@swsnr swsnr marked this pull request as draft December 31, 2023 09:25
@swsnr
Copy link
Collaborator Author

swsnr commented Dec 31, 2023

I've turned this into a draft as now that I've overcome #10 I have a few more fixes ready.

@swsnr swsnr marked this pull request as ready for review December 31, 2023 11:26
@swsnr
Copy link
Collaborator Author

swsnr commented Dec 31, 2023

I think I've fixed all errors I've hit in my extension, see swsnr/gnome-shell-extension-picture-of-the-day#38

Copy link
Member

@schnz schnz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Looks good to me! Maybe you can comment on the readonly comment below and then it's ready to be merged :)


… and a happy new year 🥂!

packages/gnome-shell/src/types/extension-metadata.ts Outdated Show resolved Hide resolved
packages/gnome-shell/src/ui/main.d.ts Outdated Show resolved Hide resolved
swsnr added 10 commits January 1, 2024 08:28
Align types with documentation at
https://gjs.guide/extensions/topics/extension.html#extensionmetadata

Mark all properties as readonly according to documentation.

Make required properties name, description, and shell-version
non-optional, as GNOME Shell would not load an extension without these
properties anyway, see ExtensionManager.createExtensionObject.

Add missing dir and path properties which GNOME Shell adds
to the metadata when creating the extension instance, see
ExtensionManager._callExtensionInit

Add version-name metadata field, which extensions can set these days to
use a human-readable version instead of the auto-generated counter on
extensions.gnome.org
Add label properties for PopupMenuItem, PopupSeparatorMenuItem, and
PopupSubMenuMenuItem.

Make "text" argument to PopupSeparatorMenuItem optional because the
constructor handles the case of it being falsy as it initializes the
label with:

    this.label = new St.Label({text: text || ''});

Remove sensitive property from PopupMenuSection, because the
implementation does not enforce that sensitive is not changed.
PopupMenuBase.addAction explicitly checks whether `icon` is undefined, see
https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/2b72a3fd81bb98f042ce0a37da6b7949deabfdaa/js/ui/popupMenu.js#L560

    let menuItem;
    if (icon !== undefined)
        menuItem = new PopupImageMenuItem(title, icon);
    else
        menuItem = new PopupMenuItem(title);

As Javascript allows callers to omit arguments which then become
undefined this effectively makes icon an optional argument.
Notification handles the params in Notification.update which uses
Params.parse on params:

    params = Params.parse(params, {
        gicon: null,
        secondaryGIcon: null,
        bannerMarkup: false,
        clear: false,
        datetime: null,
        soundName: null,
        soundFile: null,
    });

See https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/2b72a3fd81bb98f042ce0a37da6b7949deabfdaa/js/ui/messageTray.js#L401

Params.parse in turn defines the first argument with a default value
of {}:

    export function parse(params = {}, defaults, allowExtras) { … }

See https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/2b72a3fd81bb98f042ce0a37da6b7949deabfdaa/js/misc/params.js#L20

Hence, passing undefined for params is possible, which effectively makes
params an optional parameter.
While the default implementation returns a Gio.ThemedIcon the icon is
effectively used to initialize an St.Icon via its gicon property:

    // Called to create a new icon actor.
    // Provides a sane default implementation, override if you need
    // something more fancy.
    createIcon(size) {
        return new St.Icon({
            gicon: this.getIcon(),
            icon_size: size,
        });
    }

    getIcon() {
        return new Gio.ThemedIcon({name: this.iconName});
    }

As such, getIcon can return any kind of Gio.Icon.
ModelDialog uses Params.parse to unpack the constructor parameters,
which implies that the params parameter itself as well as every single
of its fields are optional.
This allows to extend the signal types for custom base classes of any
of these types.

The interfaces are empty because I didn't care to figure out all signals
for the affected popup menu types, but still provided for forward
compatibility in order to allow signal types to be defined without
introducing new interfaces.
It not only accepts PopupBaseMenuItem but also a few other types which
it explicitly checks for with instanceof and which do not inherit from
PopupBaseMenuItem and thus haven't type-checked so far.  We mirror the
checked types in an explicit enum.

Same goes for firstMenuItem which the implementation also narrows down
to two specific types via _getMenuItems()
For ESNext module resolution relative imports must have a file
extension.
@swsnr
Copy link
Collaborator Author

swsnr commented Jan 1, 2024

@schnz Happy new year as well 🎆

I've addressed your remarks 🙂

@schnz schnz merged commit 537042d into gjsify:main Jan 1, 2024
@swsnr swsnr deleted the improve-type-definitions branch January 1, 2024 12:36
@swsnr
Copy link
Collaborator Author

swsnr commented Jan 1, 2024

Thank you ❤️

Would you be so kind as to push this change to npm with a new version? 😇

@schnz
Copy link
Member

schnz commented Jan 1, 2024

Thank you for the PR!

I had some trouble with figuring out a working ~/.yarnrc.yml config file, which is why I hadn't immediately pushed a new version.

The new version is available now :) (@girs/gnome-shell@45.0.0-beta6, or @latest)

@swsnr
Copy link
Collaborator Author

swsnr commented Jan 1, 2024

Thanks 🙏🏿

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Usage without bundling?
2 participants