Skip to content

Commit

Permalink
convert classes to es6
Browse files Browse the repository at this point in the history
suggested code in issue #95 not used
  • Loading branch information
jonian committed Oct 6, 2019
1 parent 0eeae79 commit 6023646
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 408 deletions.
130 changes: 64 additions & 66 deletions [email protected]/convenience.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,85 +4,83 @@ const Gio = imports.gi.Gio
const Config = imports.misc.config
const Unite = imports.misc.extensionUtils.getCurrentExtension()

var SettingsManager = new GObject.Class({
Name: 'UniteSettings',
Extends: Gio.Settings,

get DEFAULT_BINDING() {
return Gio.SettingsBindFlags.DEFAULT
},

get _types() {
return {
'autofocus-windows': 'boolean',
'hide-activities-button': 'enum',
'show-window-title': 'enum',
'show-desktop-name': 'boolean',
'desktop-name-text': 'string',
'extend-left-box': 'boolean',
'notifications-position': 'enum',
'use-system-fonts': 'boolean',
'show-legacy-tray': 'boolean',
'greyscale-tray-icons': 'boolean',
'show-window-buttons': 'enum',
'window-buttons-theme': 'enum',
'hide-window-titlebars': 'enum',
'window-buttons-placement': 'select',
'hide-dropdown-arrows': 'boolean',
'hide-aggregate-menu-arrow': 'boolean',
'hide-app-menu-arrow': 'boolean',
'hide-app-menu-icon': 'boolean',
'reduce-panel-spacing': 'boolean',
'restrict-to-primary-screen': 'boolean'
var SettingsManager = GObject.registerClass(
class UniteSettings extends Gio.Settings {
get DEFAULT_BINDING() {
return Gio.SettingsBindFlags.DEFAULT
}
},

exists(key) {
return Object.keys(this._types).includes(key)
},
get _types() {
return {
'autofocus-windows': 'boolean',
'hide-activities-button': 'enum',
'show-window-title': 'enum',
'show-desktop-name': 'boolean',
'desktop-name-text': 'string',
'extend-left-box': 'boolean',
'notifications-position': 'enum',
'use-system-fonts': 'boolean',
'show-legacy-tray': 'boolean',
'greyscale-tray-icons': 'boolean',
'show-window-buttons': 'enum',
'window-buttons-theme': 'enum',
'hide-window-titlebars': 'enum',
'window-buttons-placement': 'select',
'hide-dropdown-arrows': 'boolean',
'hide-aggregate-menu-arrow': 'boolean',
'hide-app-menu-arrow': 'boolean',
'hide-app-menu-icon': 'boolean',
'reduce-panel-spacing': 'boolean',
'restrict-to-primary-screen': 'boolean'
}
}

getSettingType(key) {
return this._types[key] || 'invalid'
},
exists(key) {
return Object.keys(this._types).includes(key)
}

getTypeSettings(type) {
return Object.keys(this._types).filter(key => this._types[key] == type)
},
getSettingType(key) {
return this._types[key] || 'invalid'
}

getSetting(key) {
if (!this.exists(key)) return
getTypeSettings(type) {
return Object.keys(this._types).filter(key => this._types[key] == type)
}

let boolean = this.getSettingType(key) == 'boolean'
return boolean ? this.get_boolean(key) : this.get_string(key)
}
})
getSetting(key) {
if (!this.exists(key)) return

var PreferencesManager = new GObject.Class({
Name: 'UnitePreferences',
Extends: Gio.Settings,
let boolean = this.getSettingType(key) == 'boolean'
return boolean ? this.get_boolean(key) : this.get_string(key)
}
}
)

get window_buttons_position() {
let setting = this.get_string('button-layout')
return /(close|minimize|maximize).*:/.test(setting) ? 'left' : 'right'
},
var PreferencesManager = GObject.registerClass(
class UnitePreferences extends Gio.Settings {
get window_buttons_position() {
let setting = this.get_string('button-layout')
return /(close|minimize|maximize).*:/.test(setting) ? 'left' : 'right'
}

get window_buttons_layout() {
let setting = this.get_string('button-layout')
return setting.match(/(close|minimize|maximize)/g)
},
get window_buttons_layout() {
let setting = this.get_string('button-layout')
return setting.match(/(close|minimize|maximize)/g)
}

exists(key) {
let fun = key.replace(/-/g, '_')
return (fun in this) || this.list_keys().includes(key)
},
exists(key) {
let fun = key.replace(/-/g, '_')
return (fun in this) || this.list_keys().includes(key)
}

getSetting(key) {
let fun = key.replace(/-/g, '_')
getSetting(key) {
let fun = key.replace(/-/g, '_')

if (this.exists(fun)) return this[fun]
if (this.exists(key)) return this.get_string(key)
if (this.exists(fun)) return this[fun]
if (this.exists(key)) return this.get_string(key)
}
}
})
)

function initTranslations(domain) {
let textDomain = domain || Unite.metadata['gettext-domain']
Expand Down
11 changes: 4 additions & 7 deletions [email protected]/extension.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const GObject = imports.gi.GObject
const Main = imports.ui.main
const Unite = imports.misc.extensionUtils.getCurrentExtension()
const ActivateWindow = Unite.imports.modules.activateWindow.ActivateWindow
Expand All @@ -12,10 +11,8 @@ const WindowDecoration = Unite.imports.modules.windowDecoration.WindowDecoration
const TopIcons = Unite.imports.modules.topIcons.TopIcons
const ThemeMods = Unite.imports.modules.themeMods.ThemeMods

var UniteExtension = new GObject.Class({
Name: 'UniteExtension',

_init() {
class UniteExtension {
constructor() {
this._activateWindow = new ActivateWindow()
this._extendLeftBox = new ExtendLeftBox()
this._desktopName = new DesktopName()
Expand All @@ -28,7 +25,7 @@ var UniteExtension = new GObject.Class({
this._themeMods = new ThemeMods()

Main.panel._addStyleClassName('unite-shell')
},
}

destroy() {
this._activateWindow.destroy()
Expand All @@ -44,7 +41,7 @@ var UniteExtension = new GObject.Class({

Main.panel._removeStyleClassName('unite-shell')
}
})
}

let uniteExtension

Expand Down
40 changes: 19 additions & 21 deletions [email protected]/handlers.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
const GObject = imports.gi.GObject
const Unite = imports.misc.extensionUtils.getCurrentExtension()
const Convenience = Unite.imports.convenience

var SignalsHandler = new GObject.Class({
Name: 'UniteSignalsHandler',
var SignalsHandler = class SignalsHandler {
constructor(context) {
this._init(context)
}

_init(context) {
this._signals = {}
this._context = context
},
}

_getCallbackFunction(callback) {
if (typeof callback == 'string')
callback = this._context[callback] || this._context[`_${callback}`]

return callback
},
}

_connectHandler(object, name, callbackObj) {
let callback = this._getCallbackFunction(callbackObj)
let signalId = object.connect(name, callback.bind(this._context))

return { object: object, signalId: signalId }
},
}

_addHandler(object, name, callback) {
let signalKey = `${object}[${name}#${callback}]`
Expand All @@ -31,72 +32,69 @@ var SignalsHandler = new GObject.Class({
this._signals[signalKey] = this._connectHandler(object, name, callback)

return signalKey
},
}

connect(object, name, callback) {
return this._addHandler(object, name, callback)
},
}

disconnect(signalKey) {
let signalData = this._signals[signalKey]
if (!signalData) return

signalData.object.disconnect(signalData.signalId)
delete this._signals[signalKey]
},
}

disconnectMany(signalKeys) {
signalKeys.forEach(signalKey => { this.disconnect(signalKey) })
},
}

disconnectAll() {
this.disconnectMany(Object.keys(this._signals))
}
})

var SettingsHandler = new GObject.Class({
Name: 'UniteSettingsHandler',
Extends: SignalsHandler,
}

var SettingsHandler = class SettingsHandler extends SignalsHandler {
_init(context) {
this._enabler = null
this._signals = {}
this._context = context
this._settings = Convenience.getSettings()
this._wmPrefs = Convenience.getPreferences()
},
}

_getSettingObject(settingKey) {
if (this._settings.exists(settingKey))
return this._settings

if (this._wmPrefs.exists(settingKey))
return this._wmPrefs
},
}

connect(name, callback) {
let object = this._getSettingObject(name)
return this._addHandler(object, `changed::${name}`, callback)
},
}

enable(name, callback) {
if (this._enabler) return

let signalObj = this._settings
this._enabler = this._connectHandler(signalObj, `changed::${name}`, callback)
},
}

disable() {
if (!this._enabler) return

this._settings.disconnect(this._enabler.signalId)
this._enabler = null
},
}

get(settingKey) {
if (settingKey == null) return

let object = this._getSettingObject(settingKey)
if (object) return object.getSetting(settingKey)
}
})
}
28 changes: 14 additions & 14 deletions [email protected]/module.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const GObject = imports.gi.GObject
const Unite = imports.misc.extensionUtils.getCurrentExtension()
const Signals = Unite.imports.handlers.SignalsHandler
const Settings = Unite.imports.handlers.SettingsHandler

var BaseModule = new GObject.Class({
Name: 'UniteBaseModule',
var BaseModule = class UniteBaseModule {
constructor() {
this._setup()
this._init()
}

_setup() {
this._enableKey = null
this._enableValue = null
this._disableValue = null

this._runCallback('_onSetup')
},
}

_init() {
this._setup()

this._signals = new Signals(this)
this._settings = new Settings(this)
this._setting = this._settings.get(this._enableKey)
Expand All @@ -25,7 +25,7 @@ var BaseModule = new GObject.Class({
this._activate()

this._settings.enable(this._enableKey, 'reload')
},
}

get _enabled() {
if (this._enableKey == null)
Expand All @@ -36,28 +36,28 @@ var BaseModule = new GObject.Class({

if (this._disableValue != null)
return this._setting != this._disableValue
},
}

_hasCallback(name) {
return typeof(this[name]) === 'function'
},
}

_runCallback(name) {
if (this._hasCallback(name))
this[name]()
},
}

_activate() {
if (this._enabled)
this._runCallback('_onActivate')
},
}

_deactivate() {
this._runCallback('_onDeactivate')

this._settings.disconnectAll()
this._signals.disconnectAll()
},
}

_reload() {
let prevState = this._enabled
Expand All @@ -71,12 +71,12 @@ var BaseModule = new GObject.Class({

this._runCallback('_onReload')
}
},
}

destroy() {
this._deactivate()
this._runCallback('_onDestroy')

this._settings.disable()
}
})
}
Loading

0 comments on commit 6023646

Please sign in to comment.