|
| 1 | +const Desklet = imports.ui.desklet; |
| 2 | +const GLib = imports.gi.GLib; |
| 3 | +const Mainloop = imports.mainloop; |
| 4 | +const St = imports.gi.St; |
| 5 | +const Gettext = imports.gettext; |
| 6 | +const Util = imports.misc.util; |
| 7 | +const Settings = imports.ui.settings; |
| 8 | +const Tooltips = imports.ui.tooltips; |
| 9 | + |
| 10 | +const UUID = "github-contribution-grid@KopfdesDaemons"; |
| 11 | + |
| 12 | +const { GitHubHelper } = require("./helpers/github.helper"); |
| 13 | + |
| 14 | +Gettext.bindtextdomain(UUID, GLib.get_home_dir() + "/.local/share/locale"); |
| 15 | + |
| 16 | +function _(str) { |
| 17 | + return Gettext.dgettext(UUID, str); |
| 18 | +} |
| 19 | + |
| 20 | +class MyDesklet extends Desklet.Desklet { |
| 21 | + constructor(metadata, deskletId) { |
| 22 | + super(metadata, deskletId); |
| 23 | + this.githubUsername = ""; |
| 24 | + this.githubToken = ""; |
| 25 | + this.blockSize = 11; |
| 26 | + this.refreshInterval = 15; |
| 27 | + this.backgroundColor = "rgba(255, 255, 255, 0)"; |
| 28 | + this.showContributionCount = false; |
| 29 | + this.showUsername = true; |
| 30 | + this.timeoutId = null; |
| 31 | + this.contributionData = null; |
| 32 | + this.mainContainer = null; |
| 33 | + |
| 34 | + this.bindSettings(metadata, deskletId); |
| 35 | + |
| 36 | + this.setHeader(_("GitHub Contribution Grid")); |
| 37 | + |
| 38 | + this.mainContainer = new St.BoxLayout({ vertical: true, style_class: "github-contribution-grid-main-container" }); |
| 39 | + this.mainContainer.add_child(this._createHeader()); |
| 40 | + this.setContent(this.mainContainer); |
| 41 | + |
| 42 | + this._updateLoop(); |
| 43 | + |
| 44 | + // The first request after system start will fail |
| 45 | + // Delay to ensure network services are ready and try again |
| 46 | + Mainloop.timeout_add_seconds(10, () => { |
| 47 | + this._setupContributionData(); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + bindSettings(metadata, deskletId) { |
| 52 | + this.settings = new Settings.DeskletSettings(this, metadata["uuid"], deskletId); |
| 53 | + this.settings.bindProperty(Settings.BindingDirection.IN, "github-username", "githubUsername", this.onDataSettingChanged); |
| 54 | + this.settings.bindProperty(Settings.BindingDirection.IN, "github-token", "githubToken", this.onDataSettingChanged); |
| 55 | + this.settings.bindProperty(Settings.BindingDirection.IN, "refresh-interval", "refreshInterval", this.onDataSettingChanged); |
| 56 | + this.settings.bindProperty(Settings.BindingDirection.IN, "block-size", "blockSize", this.onStyleSettingChanged); |
| 57 | + this.settings.bindProperty(Settings.BindingDirection.IN, "background-color", "backgroundColor", this.onStyleSettingChanged); |
| 58 | + this.settings.bindProperty(Settings.BindingDirection.IN, "show-username", "showUsername", this.onStyleSettingChanged); |
| 59 | + this.settings.bindProperty(Settings.BindingDirection.IN, "show-contribution-count", "showContributionCount", this.onStyleSettingChanged); |
| 60 | + } |
| 61 | + |
| 62 | + on_desklet_removed() { |
| 63 | + if (this.timeoutId) { |
| 64 | + Mainloop.source_remove(this.timeoutId); |
| 65 | + this.timeoutId = null; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + onDataSettingChanged = () => { |
| 70 | + this.mainContainer.remove_all_children(); |
| 71 | + this.mainContainer.add_child(this._createHeader()); |
| 72 | + this._setupContributionData(); |
| 73 | + }; |
| 74 | + |
| 75 | + onStyleSettingChanged = () => { |
| 76 | + this.mainContainer.remove_all_children(); |
| 77 | + this.mainContainer.add_child(this._createHeader()); |
| 78 | + this._renderContent(this.contributionData); |
| 79 | + }; |
| 80 | + |
| 81 | + async _setupContributionData() { |
| 82 | + this.contributionData = null; |
| 83 | + |
| 84 | + if (!this.githubUsername || !this.githubToken) { |
| 85 | + this._renderContent(null, _("Please configure username and token in settings.")); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + try { |
| 90 | + const response = await GitHubHelper.getContributionData(this.githubUsername, this.githubToken); |
| 91 | + this.contributionData = response; |
| 92 | + this._renderContent(this.contributionData); |
| 93 | + } catch (e) { |
| 94 | + global.logError(`[${UUID}] Error fetching contribution data: ${e}`); |
| 95 | + this._renderContent(null, e.message); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + _createHeader() { |
| 100 | + const headerContainer = new St.BoxLayout({ style_class: "github-contribution-grid-header-container" }); |
| 101 | + |
| 102 | + // Reload button |
| 103 | + const reloadButton = new St.Button({ style_class: "github-contribution-grid-reload-bin" }); |
| 104 | + reloadButton.connect("button-press-event", () => this._setupContributionData()); |
| 105 | + const reloadIcon = new St.Icon({ |
| 106 | + icon_name: "view-refresh-symbolic", |
| 107 | + icon_type: St.IconType.SYMBOLIC, |
| 108 | + icon_size: 16, |
| 109 | + }); |
| 110 | + new Tooltips.Tooltip(reloadButton, _("Reload")); |
| 111 | + reloadButton.set_child(reloadIcon); |
| 112 | + headerContainer.add_child(reloadButton); |
| 113 | + |
| 114 | + // Username |
| 115 | + if (this.showUsername) { |
| 116 | + const usernameButton = new St.Button({ label: this.githubUsername, style_class: "github-contribution-grid-label-bin" }); |
| 117 | + usernameButton.connect("button-press-event", () => Util.spawnCommandLine(`xdg-open "https://github.com/${this.githubUsername}"`)); |
| 118 | + new Tooltips.Tooltip(usernameButton, _("Open GitHub profile")); |
| 119 | + headerContainer.add_child(usernameButton); |
| 120 | + } |
| 121 | + |
| 122 | + return headerContainer; |
| 123 | + } |
| 124 | + |
| 125 | + _renderContent(weeks, error = null) { |
| 126 | + if (this.contentContainer) { |
| 127 | + this.mainContainer.remove_child(this.contentContainer); |
| 128 | + this.contentContainer.destroy(); |
| 129 | + } |
| 130 | + |
| 131 | + this.contentContainer = new St.BoxLayout({ |
| 132 | + style_class: "github-contribution-grid-container", |
| 133 | + x_expand: true, |
| 134 | + style: `background-color: ${this.backgroundColor};`, |
| 135 | + }); |
| 136 | + |
| 137 | + if (!this.githubUsername || !this.githubToken) { |
| 138 | + // UI for Desklet Setup |
| 139 | + const setupBox = new St.BoxLayout({ vertical: true, style_class: "github-contribution-grid-setup-container" }); |
| 140 | + setupBox.add_child(new St.Label({ text: "GitHub Contribution Grid", style_class: "github-contribution-grid-setup-headline" })); |
| 141 | + setupBox.add_child(new St.Label({ text: _("Please configure username and token in settings.") })); |
| 142 | + |
| 143 | + const createTokenButton = new St.Button({ style_class: "github-contribution-grid-link", label: _("Create a GitHub token") }); |
| 144 | + createTokenButton.connect("clicked", () => Util.spawnCommandLine(`xdg-open "${GitHubHelper.gitHubTokenCrationURL}"`)); |
| 145 | + setupBox.add_child(createTokenButton); |
| 146 | + |
| 147 | + this.contentContainer.add_child(setupBox); |
| 148 | + } else if (error) { |
| 149 | + // Error UI |
| 150 | + const errorBox = new St.BoxLayout({ vertical: true, style_class: "github-contribution-grid-error-container" }); |
| 151 | + errorBox.add_child(new St.Label({ text: "GitHub Contribution Grid", style_class: "github-contribution-grid-error-headline" })); |
| 152 | + errorBox.add_child(new St.Label({ text: _("Error:") })); |
| 153 | + errorBox.add_child(new St.Label({ text: error, style_class: "github-contribution-grid-error-message" })); |
| 154 | + const reloadButton = new St.Button({ style_class: "github-contribution-grid-error-reload-button", label: _("Reload") }); |
| 155 | + reloadButton.connect("clicked", () => this._setupContributionData()); |
| 156 | + errorBox.add_child(reloadButton); |
| 157 | + this.contentContainer.add_child(errorBox); |
| 158 | + } else if (weeks) { |
| 159 | + // Render GitHub Grid |
| 160 | + const gridBox = new St.BoxLayout({ style_class: "github-contribution-grid-grid-box" }); |
| 161 | + |
| 162 | + for (const week of weeks) { |
| 163 | + const weekBox = new St.BoxLayout({ vertical: true, style_class: "week-container" }); |
| 164 | + |
| 165 | + for (const day of week.contributionDays) { |
| 166 | + const dayBin = new St.Bin({ |
| 167 | + style_class: "day-bin", |
| 168 | + reactive: true, |
| 169 | + track_hover: true, |
| 170 | + style: `font-size: ${this.blockSize}px; background-color: ${GitHubHelper.getContributionColor(day.contributionCount)};`, |
| 171 | + }); |
| 172 | + |
| 173 | + new Tooltips.Tooltip(dayBin, `${day.date} ${day.contributionCount} ` + _("contributions")); |
| 174 | + |
| 175 | + if (this.showContributionCount) { |
| 176 | + const countLabel = new St.Label({ text: day.contributionCount.toString() }); |
| 177 | + dayBin.set_child(countLabel); |
| 178 | + } |
| 179 | + weekBox.add_child(dayBin); |
| 180 | + } |
| 181 | + gridBox.add_child(weekBox); |
| 182 | + } |
| 183 | + this.contentContainer.add_child(gridBox); |
| 184 | + } |
| 185 | + |
| 186 | + this.mainContainer.add_child(this.contentContainer); |
| 187 | + } |
| 188 | + |
| 189 | + _updateLoop() { |
| 190 | + this._setupContributionData().finally(() => { |
| 191 | + this.timeoutId = Mainloop.timeout_add_seconds(this.refreshInterval * 60, () => { |
| 192 | + this._updateLoop(); |
| 193 | + }); |
| 194 | + }); |
| 195 | + } |
| 196 | +} |
| 197 | + |
| 198 | +function main(metadata, deskletId) { |
| 199 | + return new MyDesklet(metadata, deskletId); |
| 200 | +} |
0 commit comments