[gnome-characters: 3/10] Use ES6 classes
- From: Daiki Ueno <dueno src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-characters: 3/10] Use ES6 classes
- Date: Wed, 20 Jun 2018 04:15:23 +0000 (UTC)
commit 23bf268f00776e28b3b47fdd0288a11033e39869
Author: tommy <tommyshem485 gmail com>
Date: Mon Jun 18 23:02:01 2018 +0100
Use ES6 classes
src/characterList.js | 241 ++++++++++++++++++++++++---------------------------
1 file changed, 113 insertions(+), 128 deletions(-)
---
diff --git a/src/characterList.js b/src/characterList.js
index b65e8ed..f2c582b 100644
--- a/src/characterList.js
+++ b/src/characterList.js
@@ -18,15 +18,10 @@
const Lang = imports.lang;
const Params = imports.params;
-const Gio = imports.gi.Gio;
-const GLib = imports.gi.GLib;
-const GObject = imports.gi.GObject;
-const Gtk = imports.gi.Gtk;
-const Gdk = imports.gi.Gdk;
+const {Gc, Gdk, GLib, Gio,GObject,Gtk, Pango, PangoCairo} = imports.gi;
+
const Cairo = imports.cairo;
-const Pango = imports.gi.Pango;
-const PangoCairo = imports.gi.PangoCairo;
-const Gc = imports.gi.Gc;
+
const Main = imports.main;
const Util = imports.util;
@@ -43,22 +38,20 @@ function getCellSize(fontDescription) {
return fontDescription.get_size() * 2 / Pango.SCALE;
}
-const CharacterListRow = new Lang.Class({
- Name: 'CharacterListRow',
- Extends: GObject.Object,
-
- _init: function(params) {
- let filtered = Params.filter(params, { characters: null,
+const CharacterListRow = GObject.registerClass({
+}, class CharacterListRow extends GObject.Object {
+ _init(params) {
+ const filtered = Params.filter(params, { characters: null,
fontDescription: null,
overlayFontDescription: null });
params = Params.fill(params, {});
- this.parent(params);
+ super._init(params);
this._characters = filtered.characters;
this._fontDescription = filtered.fontDescription;
this._overlayFontDescription = filtered.overlayFontDescription;
- },
+ }
- draw: function(cr, x, y, width, height) {
+ draw(cr, x, y, width, height) {
let layout = PangoCairo.create_layout(cr);
layout.set_font_description(this._fontDescription);
@@ -97,9 +90,9 @@ const CharacterListRow = new Lang.Class({
}
}
}
- },
+ }
- _computeBoundingBox: function(cr, cellRect, uc) {
+ _computeBoundingBox(cr, cellRect, uc) {
let layout = PangoCairo.create_layout(cr);
layout.set_font_description(this._fontDescription);
layout.set_text(uc, -1);
@@ -139,9 +132,9 @@ const CharacterListRow = new Lang.Class({
shapeRect.width = shapeRect.width / Pango.SCALE;
shapeRect.height = shapeRect.height / Pango.SCALE;
return shapeRect;
- },
+ }
- _drawBoundingBox: function(cr, cellRect, uc) {
+ _drawBoundingBox(cr, cellRect, uc) {
cr.save();
cr.rectangle(cellRect.x, cellRect.y, cellRect.width, cellRect.height);
cr.clip();
@@ -160,9 +153,9 @@ const CharacterListRow = new Lang.Class({
cr.fill();
cr.restore();
- },
+ }
- _drawCharacterName: function(cr, cellRect, uc) {
+ _drawCharacterName(cr, cellRect, uc) {
cr.save();
cr.rectangle(cellRect.x, cellRect.y, cellRect.width, cellRect.height);
cr.clip();
@@ -189,21 +182,19 @@ const CharacterListRow = new Lang.Class({
}
});
-const CharacterListWidget = new Lang.Class({
- Name: 'CharacterListWidget',
- Extends: Gtk.DrawingArea,
+const CharacterListWidget = GObject.registerClass({
Signals: {
'character-selected': { param_types: [ GObject.TYPE_STRING ] }
},
-
- _init: function(params) {
- let filtered = Params.filter(params, {
+}, class CharacterListWidget extends Gtk.DrawingArea {
+ _init(params) {
+ const filtered = Params.filter(params, {
fontDescription: null,
numRows: NUM_ROWS
});
params = Params.fill(params, {});
- this.parent(params);
- let context = this.get_style_context();
+ super._init(params);
+ const context = this.get_style_context();
context.add_class('character-list');
context.save();
this._cellsPerRow = CELLS_PER_ROW;
@@ -218,9 +209,9 @@ const CharacterListWidget = new Lang.Class({
null,
Gdk.DragAction.COPY);
this.drag_source_add_text_targets();
- },
+ }
- vfunc_drag_begin: function(context) {
+ vfunc_drag_begin(context) {
let cellSize = getCellSize(this._fontDescription);
this._dragSurface = new Cairo.ImageSurface(Cairo.Format.ARGB32,
cellSize,
@@ -232,14 +223,14 @@ const CharacterListWidget = new Lang.Class({
let row = this._createCharacterListRow([this._character]);
row.draw(cr, 0, 0, cellSize, cellSize);
Gtk.drag_set_icon_surface(context, this._dragSurface, 0, 0);
- },
+ }
- vfunc_drag_data_get: function(context, data, info, time) {
+ vfunc_drag_data_get(context, data, info, time) {
if (this._character != null)
data.set_text(this._character, -1);
- },
+ }
- vfunc_button_press_event: function(event) {
+ vfunc_button_press_event(event) {
let allocation = this.get_allocation();
let cellSize = getCellSize(this._fontDescription);
let x = Math.floor(event.x / cellSize);
@@ -250,42 +241,42 @@ const CharacterListWidget = new Lang.Class({
else
this._character = null;
return false;
- },
+ }
- vfunc_button_release_event: function(event) {
+ vfunc_button_release_event(event) {
if (this._character)
this.emit('character-selected', this._character);
return false;
- },
+ }
- vfunc_get_request_mode: function() {
+ vfunc_get_request_mode() {
return Gtk.SizeRequestMode.HEIGHT_FOR_WIDTH;
- },
+ }
- vfunc_get_preferred_height: function() {
+ vfunc_get_preferred_height() {
let [minWidth, natWidth] = this.vfunc_get_preferred_width();
return this.vfunc_get_preferred_height_for_width(minWidth);
- },
+ }
- vfunc_get_preferred_height_for_width: function(width) {
+ vfunc_get_preferred_height_for_width(width) {
let height = Math.max(this._rows.length, this._numRows) *
getCellSize(this._fontDescription);
return [height, height];
- },
+ }
- vfunc_get_preferred_width: function() {
+ vfunc_get_preferred_width() {
return this.vfunc_get_preferred_width_for_height(0);
- },
+ }
- vfunc_get_preferred_width_for_height: function(height) {
+ vfunc_get_preferred_width_for_height(height) {
let cellSize = getCellSize(this._fontDescription);
let minWidth = NUM_COLUMNS * cellSize;
let natWidth = Math.max(this._cellsPerRow, NUM_COLUMNS) * cellSize;
return [minWidth, natWidth];
- },
+ }
- vfunc_size_allocate: function(allocation) {
- this.parent(allocation);
+ vfunc_size_allocate(allocation) {
+ super.vfunc_size_allocate(allocation);
let cellSize = getCellSize(this._fontDescription);
let cellsPerRow = Math.floor(allocation.width / cellSize);
@@ -294,9 +285,9 @@ const CharacterListWidget = new Lang.Class({
this._cellsPerRow = cellsPerRow;
this.setCharacters(this._characters);
}
- },
+ }
- _createCharacterListRow: function(characters) {
+ _createCharacterListRow(characters) {
var context = this.get_pango_context();
var fontDescription = context.get_font_description();
fontDescription.set_size(fontDescription.get_size() * 0.8);
@@ -306,13 +297,13 @@ const CharacterListWidget = new Lang.Class({
overlayFontDescription: fontDescription
});
return row;
- },
+ }
- setFontDescription: function(fontDescription) {
+ setFontDescription(fontDescription) {
this._fontDescription = fontDescription;
- },
+ }
- setCharacters: function(characters) {
+ setCharacters(characters) {
this._rows = [];
this._characters = characters;
@@ -333,9 +324,9 @@ const CharacterListWidget = new Lang.Class({
this.queue_resize();
this.queue_draw();
- },
+ }
- vfunc_draw: function(cr) {
+ vfunc_draw(cr) {
// Clear the canvas.
let context = this.get_style_context();
let fg = context.get_color(Gtk.StateFlags.NORMAL);
@@ -364,9 +355,7 @@ const CharacterListWidget = new Lang.Class({
const MAX_SEARCH_RESULTS = 100;
-var FontFilter = new Lang.Class({
- Name: 'FontFilter',
- Extends: GObject.Object,
+var FontFilter = GObject.registerClass({
Properties: {
'font': GObject.ParamSpec.string(
'font', '', '',
@@ -376,10 +365,10 @@ var FontFilter = new Lang.Class({
Signals: {
'filter-set': { param_types: [] }
},
-
+}, class FontFilter extends GObject.Object {
get font() {
return this._font;
- },
+ }
set font(v) {
let fontDescription = Pango.FontDescription.from_string(v);
@@ -392,25 +381,25 @@ var FontFilter = new Lang.Class({
this._font = v;
this._fontDescription = fontDescription;
- },
+ }
get fontDescription() {
if (this._filterFontDescription)
return this._filterFontDescription;
return this._fontDescription;
- },
+ }
- _init: function(params) {
+ _init(params) {
params = Params.fill(params, {});
- this.parent(params);
+ super._init(params);
this._fontDescription = null;
this._filterFontDescription = null;
Main.settings.bind('font', this, 'font', Gio.SettingsBindFlags.DEFAULT);
- },
+ }
- setFilterFont: function(v) {
+ setFilterFont(v) {
let fontDescription;
if (v == null) {
fontDescription = null;
@@ -426,9 +415,9 @@ var FontFilter = new Lang.Class({
this._filterFontDescription = fontDescription;
this.emit('filter-set');
}
- },
+ }
- apply: function(widget, characters) {
+ apply(widget, characters) {
let fontDescription = this._fontDescription;
if (this._filterFontDescription) {
let context = widget.get_pango_context();
@@ -444,27 +433,25 @@ var FontFilter = new Lang.Class({
}
return [fontDescription, characters];
- },
+ }
});
-var CharacterListView = new Lang.Class({
- Name: 'CharacterListView',
- Extends: Gtk.Stack,
+var CharacterListView = GObject.registerClass({
Template: 'resource:///org/gnome/Characters/characterlist.ui',
InternalChildren: ['loading-spinner'],
Signals: {
'character-selected': { param_types: [ GObject.TYPE_STRING ] }
},
-
- _init: function(params) {
- let filtered = Params.filter(params, {
- fontFilter: null
+}, class CharacterListView extends Gtk.Stack {
+ _init(params) {
+ const filtered = Params.filter(params, {
+ fontFilter: null,
});
params = Params.fill(params, {
hexpand: true, vexpand: true,
transition_type: Gtk.StackTransitionType.CROSSFADE
});
- this.parent(params);
+ super._init(params);
this._fontFilter = filtered.fontFilter;
this._characterList = new CharacterListWidget({
@@ -502,9 +489,9 @@ var CharacterListView = new Lang.Class({
}));
scroll.connect('edge-reached', Lang.bind(this, this._onEdgeReached));
scroll.connect('size-allocate', Lang.bind(this, this._onSizeAllocate));
- },
+ }
- _startSpinner: function() {
+ _startSpinner() {
this._stopSpinner();
this._spinnerTimeoutId =
GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000,
@@ -513,31 +500,31 @@ var CharacterListView = new Lang.Class({
this.visible_child_name = 'loading';
this.show_all();
}));
- },
+ }
- _stopSpinner: function() {
+ _stopSpinner() {
if (this._spinnerTimeoutId > 0) {
GLib.source_remove(this._spinnerTimeoutId);
this._spinnerTimeoutId = 0;
this._loading_spinner.stop();
}
- },
+ }
- _finishSearch: function(result) {
+ _finishSearch(result) {
this._stopSpinner();
let characters = Util.searchResultToArray(result);
this.setCharacters(characters);
- },
+ }
- setCharacters: function(characters) {
+ setCharacters(characters) {
this._characters = characters;
this._updateCharacterList();
- },
+ }
- _updateCharacterList: function() {
- let [fontDescription, characters] = this._fontFilter.apply(this, this._characters);
+ _updateCharacterList() {
+ const [fontDescription, characters] = this._fontFilter.apply(this, this._characters);
this._characterList.setFontDescription(fontDescription);
this._characterList.setCharacters(characters);
if (characters.length == 0) {
@@ -546,19 +533,19 @@ var CharacterListView = new Lang.Class({
this.visible_child_name = 'character-list';
}
this.show_all();
- },
+ }
_maybeLoadMore() {
if (this._searchContext != null && !this._searchContext.is_finished()) {
this._searchWithContext(this._searchContext, MAX_SEARCH_RESULTS);
}
- },
+ }
- _onEdgeReached: function(scrolled, pos) {
+ _onEdgeReached(scrolled, pos) {
if (pos == Gtk.PositionType.BOTTOM) {
this._maybeLoadMore();
}
- },
+ }
get initialSearchCount() {
// Use our parents allocation; we aren't visible before we do the
@@ -574,20 +561,20 @@ var CharacterListView = new Lang.Class({
let heightInRows = Math.ceil((allocation.height + 1) / cellSize);
return Math.max(MAX_SEARCH_RESULTS, heightInRows * cellsPerRow);
- },
+ }
- _onSizeAllocate: function(scrolled, allocation) {
+ _onSizeAllocate(scrolled, allocation) {
if (this._characters.length < this.initialSearchCount) {
this._maybeLoadMore();
}
- },
+ }
- _addSearchResult: function(result) {
- let characters = Util.searchResultToArray(result);
+ _addSearchResult(result) {
+ const characters = Util.searchResultToArray(result);
this.setCharacters(this._characters.concat(characters));
- },
+ }
- _searchWithContext: function(context, count) {
+ _searchWithContext(context, count) {
this._startSpinner();
context.search(
count,
@@ -598,12 +585,12 @@ var CharacterListView = new Lang.Class({
let result = context.search_finish(res);
this._addSearchResult(result);
} catch (e) {
- log("Failed to search: " + e.message);
+ log(`Failed to search: ${e.message}`);
}
}));
- },
+ }
- searchByCategory: function(category) {
+ searchByCategory(category) {
if ('scripts' in category) {
this.searchByScripts(category.scripts);
return;
@@ -612,45 +599,43 @@ var CharacterListView = new Lang.Class({
let criteria = Gc.SearchCriteria.new_category(category.category);
this._searchContext = new Gc.SearchContext({ criteria: criteria });
this._searchWithContext(this._searchContext, this.initialSearchCount);
- },
+ }
- searchByKeywords: function(keywords) {
- let criteria = Gc.SearchCriteria.new_keywords(keywords);
+ searchByKeywords(keywords) {
+ const criteria = Gc.SearchCriteria.new_keywords(keywords);
this._searchContext = new Gc.SearchContext({
criteria: criteria,
flags: Gc.SearchFlag.WORD
});
this._searchWithContext(this._searchContext, this.initialSearchCount);
- },
+ }
- searchByScripts: function(scripts) {
+ searchByScripts(scripts) {
var criteria = Gc.SearchCriteria.new_scripts(scripts);
this._searchContext = new Gc.SearchContext({ criteria: criteria });
this._searchWithContext(this._searchContext, this.initialSearchCount);
- },
+ }
- cancelSearch: function() {
+ cancelSearch() {
this._cancellable.cancel();
this._cancellable.reset();
}
});
-var RecentCharacterListView = new Lang.Class({
- Name: 'RecentCharacterListView',
- Extends: Gtk.Bin,
+var RecentCharacterListView = GObject.registerClass({
Signals: {
- 'character-selected': { param_types: [ GObject.TYPE_STRING ] }
+ 'character-selected': { param_types: [ GObject.TYPE_STRING ] },
},
-
- _init: function(params) {
- let filtered = Params.filter(params, {
+}, class RecentCharacterListView extends Gtk.Bin {
+ _init(params) {
+ const filtered = Params.filter(params, {
category: null,
fontFilter: null
});
params = Params.fill(params, {
hexpand: true, vexpand: false
});
- this.parent(params);
+ super._init(params);
this._fontFilter = filtered.fontFilter;
this._characterList = new CharacterListWidget({
@@ -670,16 +655,16 @@ var RecentCharacterListView = new Lang.Class({
this._category = filtered.category;
this._characters = [];
- },
+ }
- setCharacters: function(characters) {
- let result = Gc.filter_characters(this._category, characters);
+ setCharacters(characters) {
+ const result = Gc.filter_characters(this._category, characters);
this._characters = Util.searchResultToArray(result);
this._updateCharacterList();
- },
+ }
- _updateCharacterList: function() {
- let [fontDescription, characters] = this._fontFilter.apply(this, this._characters);
+ _updateCharacterList() {
+ const [fontDescription, characters] = this._fontFilter.apply(this, this._characters);
this._characterList.setFontDescription(fontDescription);
this._characterList.setCharacters(characters);
this.show_all();
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]