Skip to content

Commit

Permalink
code review II
Browse files Browse the repository at this point in the history
  • Loading branch information
cinatic committed Oct 22, 2023
1 parent 85581c2 commit 4c23dd0
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 32 deletions.
3 changes: 1 addition & 2 deletions [email protected]/components/buttons/buttonGroup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Clutter from 'gi://Clutter'
import GObject from 'gi://GObject'
import St from 'gi://St'
import Gtk from 'gi://Gtk'
import Pango from 'gi://Pango'

export const ButtonGroup = GObject.registerClass({
Expand All @@ -19,7 +18,7 @@ export const ButtonGroup = GObject.registerClass({
})

if (!enableScrollbar) {
this.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.NEVER)
this.set_policy(St.PolicyType.NEVER, St.PolicyType.NEVER)
}

this._selectedButton = buttons.find(item => item.selected)
Expand Down
5 changes: 2 additions & 3 deletions [email protected]/components/flatList/flatList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Clutter from 'gi://Clutter'
import GObject from 'gi://GObject'
import St from 'gi://St'
import Graphene from 'gi://Graphene'
import Gtk from 'gi://Gtk'

import { ScaleLayout } from '../scaleLayout/scaleLayout.js';
import { Translations } from '../../helpers/translations.js';
Expand All @@ -24,8 +23,8 @@ export const FlatList = GObject.registerClass({
x_expand: true,
y_expand: true,
clip_to_allocation: true,
hscrollbar_policy: Gtk.PolicyType.NEVER,
vscrollbar_policy: Gtk.PolicyType.AUTOMATIC
hscrollbar_policy: St.PolicyType.NEVER,
vscrollbar_policy: St.PolicyType.AUTOMATIC
})

this._content = new St.BoxLayout({
Expand Down
6 changes: 3 additions & 3 deletions [email protected]/components/searchBar/searchBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import GObject from 'gi://GObject'
import Clutter from 'gi://Clutter'
import St from 'gi://St'

import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import { SettingsHandler } from '../../helpers/settings.js'

import { IconButton } from '../buttons/iconButton.js';
import { Translations } from '../../helpers/translations.js';
Expand Down Expand Up @@ -98,9 +98,9 @@ export const SearchBar = GObject.registerClass({
icon_name: 'emblem-system-symbolic',
icon_size: 18,
onClick: () => {
const extensionObject = Extension.lookupByURL(import.meta.url);
const settings = new SettingsHandler()
this._mainEventHandler.emit('hide-panel')
extensionObject.openPreferences();
settings.extensionObject.openPreferences();
}
})

Expand Down
18 changes: 17 additions & 1 deletion [email protected]/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import { ScreenWrapper } from './components/screenWrapper/screenWrapper.js'

import * as ComponentsHelper from './helpers/components.js'
import { EventHandler } from './helpers/eventHandler.js'
import { SettingsHandler } from './helpers/settings.js'
import { initSettings, SettingsHandler } from './helpers/settings.js'
import { CLEANUP_PROCEDURES as SUBPROCESS_CLEANUP_PROCEDURES } from './helpers/subprocess.js'

const MenuPosition = {
CENTER: 0,
Expand Down Expand Up @@ -132,15 +133,30 @@ let _kubectlMenu = null

export default class KubectlExtension extends Extension {
enable () {
initSettings(this)
_kubectlMenu = new KubectlMenuButton()
Main.panel.addToStatusArea('kubectlMenu', _kubectlMenu)
_kubectlMenu.checkPositionInPanel()
}

disable () {
if (_kubectlMenu) {
this.cleanUp()
_kubectlMenu.destroy()
_kubectlMenu = null
}
}

cleanUp () {
[SUBPROCESS_CLEANUP_PROCEDURES].forEach(procedureMap => {
Object.keys(procedureMap).forEach(timeoutId => {
try {
clearTimeout(timeoutId)
procedureMap[timeoutId].call()
} catch (e) {

}
})
})
}
}
11 changes: 6 additions & 5 deletions [email protected]/helpers/components.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Gio from 'gi://Gio'

import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import * as MessageTray from 'resource:///org/gnome/shell/ui/messageTray.js';
import { SettingsHandler } from './settings.js'

const iconCache = {}

Expand All @@ -10,9 +11,9 @@ export const getCustomIconPath = iconName => {
return iconCache[iconName]
}

const extensionObject = Extension.lookupByURL(import.meta.url);
const settings = new SettingsHandler()

const newIcon = Gio.icon_new_for_string(extensionObject.dir.get_child('icons').get_path() + '/' + iconName + '.svg')
const newIcon = Gio.icon_new_for_string(settings.extensionObject.path + '/icons/' + iconName + '.svg')
iconCache[iconName] = newIcon

return newIcon
Expand All @@ -30,8 +31,8 @@ export const showNotification = (title, message, dialogType, transient) => {
break
}

const source = new Main.MessageTray.Source('kubectl-extension', icon)
const notification = new Main.MessageTray.Notification(source, title, message)
const source = new MessageTray.Source('kubectl-extension', icon)
const notification = new MessageTray.Notification(source, title, message)
notification.setTransient(transient)

Main.messageTray.add(source)
Expand Down
28 changes: 18 additions & 10 deletions [email protected]/helpers/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
let _settings = null
let _extensionObject = {}

export const initSettings = extensionObject => {
_extensionObject = extensionObject
}

export const POSITION_IN_PANEL_KEY = 'position-in-panel'
export const KUBECTL_NAMESPACE = 'namespace'
Expand All @@ -9,15 +13,6 @@ export const KUBECTL_RESOURCE = 'resource'
export const SETTINGS_SCHEMA_DOMAIN = 'org.gnome.shell.extensions.kubectl'

export const SettingsHandler = class SettingsHandler {
constructor () {
this._extensionObject = Extension.lookupByURL(import.meta.url)
this._settings = this._extensionObject.getSettings()
}

get extensionObject() {
return this._extensionObject
}

get position_in_panel () {
return this._settings.get_enum(POSITION_IN_PANEL_KEY)
}
Expand Down Expand Up @@ -46,6 +41,19 @@ export const SettingsHandler = class SettingsHandler {
this._settings.set_string(KUBECTL_RESOURCE, value)
}

get extensionObject () {
return _extensionObject
}

get _settings () {
if (!_settings) {
_settings = this.extensionObject.getSettings()
}

return _settings
}


connect (identifier, onChange) {
return this._settings.connect(identifier, onChange)
}
Expand Down
8 changes: 6 additions & 2 deletions [email protected]/helpers/subprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import GLib from 'gi://GLib'

import { tryJsonParse } from './data.js'

export const CLEANUP_PROCEDURES = {}

// partially copied from https://wiki.gnome.org/AndyHolmes/Sandbox/SpawningProcesses
export const run = async ({ command, asJson = true, input = null, timeout = 10 }) => {
try {
Expand All @@ -28,6 +30,7 @@ export const run = async ({ command, asJson = true, input = null, timeout = 10 }
proc.init(cancellable)

const cancelTimeOutId = setTimeout(() => cancellable.cancel(), timeout * 1000)
CLEANUP_PROCEDURES[cancelTimeOutId] = () => cancellable.cancel()

const result = await new Promise((resolve, reject) => {
proc.communicate_utf8_async(input, cancellable, (proc, res) => {
Expand All @@ -44,12 +47,13 @@ export const run = async ({ command, asJson = true, input = null, timeout = 10 }
resolve({ error: stderr || stdout })
}

clearTimeout(cancelTimeOutId)
resolve({ output: stdout })
} catch (e) {
clearTimeout(cancelTimeOutId)
logError(e)
reject(e)
} finally {
clearTimeout(cancelTimeOutId)
delete CLEANUP_PROCEDURES[cancelTimeOutId]
}
})
})
Expand Down
4 changes: 2 additions & 2 deletions [email protected]/po/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2023-10-20 17:55+0200\n"
"POT-Creation-Date: 2023-10-22 12:47+0200\n"
"PO-Revision-Date: 2020-11-30 14:42+0100\n"
"Last-Translator: [email protected]\n"
"Language-Team: German\n"
Expand All @@ -19,7 +19,7 @@ msgstr ""
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 2.4.1\n"

#: [email protected]/extension.js:56
#: [email protected]/extension.js:57
msgid "Kubectl"
msgstr ""

Expand Down
4 changes: 2 additions & 2 deletions [email protected]/po/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: gnome-shell-extension-kubectl\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2023-10-20 18:13+0200\n"
"POT-Creation-Date: 2023-10-22 12:57+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand All @@ -17,7 +17,7 @@ msgstr ""
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: [email protected]/extension.js:56
#: [email protected]/extension.js:57
msgid "Kubectl"
msgstr ""

Expand Down
4 changes: 2 additions & 2 deletions [email protected]/po/nl.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: gnome-shell-extension-kubectl\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2023-10-20 17:55+0200\n"
"POT-Creation-Date: 2023-10-22 12:47+0200\n"
"PO-Revision-Date: 2021-07-16 14:19+0200\n"
"Last-Translator: Heimen Stoffels <[email protected]>\n"
"Language-Team: \n"
Expand All @@ -18,7 +18,7 @@ msgstr ""
"X-Generator: Poedit 3.0\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: [email protected]/extension.js:56
#: [email protected]/extension.js:57
msgid "Kubectl"
msgstr "Kubectl"

Expand Down
4 changes: 4 additions & 0 deletions [email protected]/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Gtk from 'gi://Gtk'

import { ExtensionPreferences } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';

import { initSettings } from './helpers/settings.js'

export const PrefsWidget = GObject.registerClass({
GTypeName: 'KubectlExtension_PrefsWidget'
}, class Widget extends Gtk.Box {
Expand Down Expand Up @@ -123,6 +125,8 @@ const isGnome4 = () => true

export default class KubectlExtensionPreferences extends ExtensionPreferences {
getPreferencesWidget() {
initSettings(this)

const widget = new PrefsWidget(this.getSettings(), this.path)

widget.Settings = this.getSettings();
Expand Down

0 comments on commit 4c23dd0

Please sign in to comment.