cleanup: Port non-GObject classes to JS6 classes
ES6 finally adds standard class syntax to the language, so we can replace our custom Lang.Class framework with the new syntax. Any classes that inherit from GObject will need special treatment, so limit the port to regular javascript classes for now. https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/361
This commit is contained in:

committed by
Georges Basile Stavracas Neto

parent
99ce3deeb0
commit
bacfdbbb03
@ -4,7 +4,6 @@ const Clutter = imports.gi.Clutter;
|
||||
const Gio = imports.gi.Gio;
|
||||
const GLib = imports.gi.GLib;
|
||||
const Gtk = imports.gi.Gtk;
|
||||
const Lang = imports.lang;
|
||||
const St = imports.gi.St;
|
||||
const Signals = imports.signals;
|
||||
const Shell = imports.gi.Shell;
|
||||
@ -89,46 +88,42 @@ function _getCalendarDayAbbreviation(dayNumber) {
|
||||
|
||||
// Abstraction for an appointment/event in a calendar
|
||||
|
||||
var CalendarEvent = new Lang.Class({
|
||||
Name: 'CalendarEvent',
|
||||
|
||||
_init(id, date, end, summary, allDay) {
|
||||
var CalendarEvent = class CalendarEvent {
|
||||
constructor(id, date, end, summary, allDay) {
|
||||
this.id = id;
|
||||
this.date = date;
|
||||
this.end = end;
|
||||
this.summary = summary;
|
||||
this.allDay = allDay;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// Interface for appointments/events - e.g. the contents of a calendar
|
||||
//
|
||||
|
||||
// First, an implementation with no events
|
||||
var EmptyEventSource = new Lang.Class({
|
||||
Name: 'EmptyEventSource',
|
||||
|
||||
_init() {
|
||||
var EmptyEventSource = class EmptyEventSource {
|
||||
constructor() {
|
||||
this.isLoading = false;
|
||||
this.isDummy = true;
|
||||
this.hasCalendars = false;
|
||||
},
|
||||
}
|
||||
|
||||
destroy() {
|
||||
},
|
||||
}
|
||||
|
||||
requestRange(begin, end) {
|
||||
},
|
||||
}
|
||||
|
||||
getEvents(begin, end) {
|
||||
let result = [];
|
||||
return result;
|
||||
},
|
||||
}
|
||||
|
||||
hasEvents(day) {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
};
|
||||
Signals.addSignalMethods(EmptyEventSource.prototype);
|
||||
|
||||
const CalendarServerIface = `
|
||||
@ -174,10 +169,8 @@ function _dateIntervalsOverlap(a0, a1, b0, b1)
|
||||
}
|
||||
|
||||
// an implementation that reads data from a session bus service
|
||||
var DBusEventSource = new Lang.Class({
|
||||
Name: 'DBusEventSource',
|
||||
|
||||
_init() {
|
||||
var DBusEventSource = class DBusEventSource {
|
||||
constructor() {
|
||||
this._resetCache();
|
||||
this.isLoading = false;
|
||||
this.isDummy = false;
|
||||
@ -224,39 +217,39 @@ var DBusEventSource = new Lang.Class({
|
||||
this._onNameAppeared();
|
||||
}
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this._dbusProxy.run_dispose();
|
||||
},
|
||||
}
|
||||
|
||||
get hasCalendars() {
|
||||
if (this._initialized)
|
||||
return this._dbusProxy.HasCalendars;
|
||||
else
|
||||
return false;
|
||||
},
|
||||
}
|
||||
|
||||
_resetCache() {
|
||||
this._events = [];
|
||||
this._lastRequestBegin = null;
|
||||
this._lastRequestEnd = null;
|
||||
},
|
||||
}
|
||||
|
||||
_onNameAppeared(owner) {
|
||||
this._initialized = true;
|
||||
this._resetCache();
|
||||
this._loadEvents(true);
|
||||
},
|
||||
}
|
||||
|
||||
_onNameVanished(oldOwner) {
|
||||
this._resetCache();
|
||||
this.emit('changed');
|
||||
},
|
||||
}
|
||||
|
||||
_onChanged() {
|
||||
this._loadEvents(false);
|
||||
},
|
||||
}
|
||||
|
||||
_onEventsReceived(results, error) {
|
||||
let newEvents = [];
|
||||
@ -278,7 +271,7 @@ var DBusEventSource = new Lang.Class({
|
||||
this._events = newEvents;
|
||||
this.isLoading = false;
|
||||
this.emit('changed');
|
||||
},
|
||||
}
|
||||
|
||||
_loadEvents(forceReload) {
|
||||
// Ignore while loading
|
||||
@ -292,7 +285,7 @@ var DBusEventSource = new Lang.Class({
|
||||
this._onEventsReceived.bind(this),
|
||||
Gio.DBusCallFlags.NONE);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
requestRange(begin, end) {
|
||||
if (!(_datesEqual(begin, this._lastRequestBegin) && _datesEqual(end, this._lastRequestEnd))) {
|
||||
@ -303,7 +296,7 @@ var DBusEventSource = new Lang.Class({
|
||||
this._curRequestEnd = end;
|
||||
this._loadEvents(false);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
getEvents(begin, end) {
|
||||
let result = [];
|
||||
@ -321,7 +314,7 @@ var DBusEventSource = new Lang.Class({
|
||||
return d1.getTime() - d2.getTime();
|
||||
});
|
||||
return result;
|
||||
},
|
||||
}
|
||||
|
||||
hasEvents(day) {
|
||||
let dayBegin = _getBeginningOfDay(day);
|
||||
@ -334,13 +327,11 @@ var DBusEventSource = new Lang.Class({
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
};
|
||||
Signals.addSignalMethods(DBusEventSource.prototype);
|
||||
|
||||
var Calendar = new Lang.Class({
|
||||
Name: 'Calendar',
|
||||
|
||||
_init() {
|
||||
var Calendar = class Calendar {
|
||||
constructor() {
|
||||
this._weekStart = Shell.util_get_week_start();
|
||||
this._settings = new Gio.Settings({ schema_id: 'org.gnome.desktop.calendar' });
|
||||
|
||||
@ -378,7 +369,7 @@ var Calendar = new Lang.Class({
|
||||
this._onScroll.bind(this));
|
||||
|
||||
this._buildHeader ();
|
||||
},
|
||||
}
|
||||
|
||||
// @eventSource: is an object implementing the EventSource API, e.g. the
|
||||
// requestRange(), getEvents(), hasEvents() methods and the ::changed signal.
|
||||
@ -390,7 +381,7 @@ var Calendar = new Lang.Class({
|
||||
});
|
||||
this._rebuildCalendar();
|
||||
this._update();
|
||||
},
|
||||
}
|
||||
|
||||
// Sets the calendar to show a specific date
|
||||
setDate(date) {
|
||||
@ -400,14 +391,14 @@ var Calendar = new Lang.Class({
|
||||
this._selectedDate = date;
|
||||
this._update();
|
||||
this.emit('selected-date-changed', new Date(this._selectedDate));
|
||||
},
|
||||
}
|
||||
|
||||
updateTimeZone() {
|
||||
// The calendar need to be rebuilt after a time zone update because
|
||||
// the date might have changed.
|
||||
this._rebuildCalendar();
|
||||
this._update();
|
||||
},
|
||||
}
|
||||
|
||||
_buildHeader() {
|
||||
let layout = this.actor.layout_manager;
|
||||
@ -464,7 +455,7 @@ var Calendar = new Lang.Class({
|
||||
|
||||
// All the children after this are days, and get removed when we update the calendar
|
||||
this._firstDayIndex = this.actor.get_n_children();
|
||||
},
|
||||
}
|
||||
|
||||
_onScroll(actor, event) {
|
||||
switch (event.get_scroll_direction()) {
|
||||
@ -478,7 +469,7 @@ var Calendar = new Lang.Class({
|
||||
break;
|
||||
}
|
||||
return Clutter.EVENT_PROPAGATE;
|
||||
},
|
||||
}
|
||||
|
||||
_onPrevMonthButtonClicked() {
|
||||
let newDate = new Date(this._selectedDate);
|
||||
@ -502,7 +493,7 @@ var Calendar = new Lang.Class({
|
||||
this._backButton.grab_key_focus();
|
||||
|
||||
this.setDate(newDate);
|
||||
},
|
||||
}
|
||||
|
||||
_onNextMonthButtonClicked() {
|
||||
let newDate = new Date(this._selectedDate);
|
||||
@ -526,14 +517,14 @@ var Calendar = new Lang.Class({
|
||||
this._forwardButton.grab_key_focus();
|
||||
|
||||
this.setDate(newDate);
|
||||
},
|
||||
}
|
||||
|
||||
_onSettingsChange() {
|
||||
this._useWeekdate = this._settings.get_boolean(SHOW_WEEKDATE_KEY);
|
||||
this._buildHeader();
|
||||
this._rebuildCalendar();
|
||||
this._update();
|
||||
},
|
||||
}
|
||||
|
||||
_rebuildCalendar() {
|
||||
let now = new Date();
|
||||
@ -654,7 +645,7 @@ var Calendar = new Lang.Class({
|
||||
// Signal to the event source that we are interested in events
|
||||
// only from this date range
|
||||
this._eventSource.requestRange(beginDate, iter);
|
||||
},
|
||||
}
|
||||
|
||||
_update() {
|
||||
let now = new Date();
|
||||
@ -677,18 +668,17 @@ var Calendar = new Lang.Class({
|
||||
button.remove_style_pseudo_class('selected');
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
Signals.addSignalMethods(Calendar.prototype);
|
||||
|
||||
var EventMessage = new Lang.Class({
|
||||
Name: 'EventMessage',
|
||||
Extends: MessageList.Message,
|
||||
var EventMessage = class EventMessage extends MessageList.Message {
|
||||
constructor(event, date) {
|
||||
super('', event.summary);
|
||||
|
||||
_init(event, date) {
|
||||
this._event = event;
|
||||
this._date = date;
|
||||
|
||||
this.parent(this._formatEventTime(), event.summary);
|
||||
this.setTitle(this._formatEventTime());
|
||||
|
||||
this._icon = new St.Icon({ icon_name: 'x-office-calendar-symbolic' });
|
||||
this.setIcon(this._icon);
|
||||
@ -697,7 +687,7 @@ var EventMessage = new Lang.Class({
|
||||
let iconVisible = this.actor.get_parent().has_style_pseudo_class('first-child');
|
||||
this._icon.opacity = (iconVisible ? 255 : 0);
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
_formatEventTime() {
|
||||
let periodBegin = _getBeginningOfDay(this._date);
|
||||
@ -731,18 +721,16 @@ var EventMessage = new Lang.Class({
|
||||
}
|
||||
return title;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var NotificationMessage = new Lang.Class({
|
||||
Name: 'NotificationMessage',
|
||||
Extends: MessageList.Message,
|
||||
|
||||
_init(notification) {
|
||||
this.notification = notification;
|
||||
|
||||
this.parent(notification.title, notification.bannerBodyText);
|
||||
var NotificationMessage =
|
||||
class NotificationMessage extends MessageList.Message {
|
||||
constructor(notification) {
|
||||
super(notification.title, notification.bannerBodyText);
|
||||
this.setUseBodyMarkup(notification.bannerBodyMarkup);
|
||||
|
||||
this.notification = notification;
|
||||
|
||||
this.setIcon(this._getIcon());
|
||||
|
||||
this.connect('close', () => {
|
||||
@ -758,7 +746,7 @@ var NotificationMessage = new Lang.Class({
|
||||
});
|
||||
this._updatedId = notification.connect('updated',
|
||||
this._onUpdated.bind(this));
|
||||
},
|
||||
}
|
||||
|
||||
_getIcon() {
|
||||
if (this.notification.gicon)
|
||||
@ -766,23 +754,23 @@ var NotificationMessage = new Lang.Class({
|
||||
icon_size: MESSAGE_ICON_SIZE });
|
||||
else
|
||||
return this.notification.source.createIcon(MESSAGE_ICON_SIZE);
|
||||
},
|
||||
}
|
||||
|
||||
_onUpdated(n, clear) {
|
||||
this.setIcon(this._getIcon());
|
||||
this.setTitle(n.title);
|
||||
this.setBody(n.bannerBodyText);
|
||||
this.setUseBodyMarkup(n.bannerBodyMarkup);
|
||||
},
|
||||
}
|
||||
|
||||
_onClicked() {
|
||||
this.notification.activate();
|
||||
},
|
||||
}
|
||||
|
||||
_onDestroy() {
|
||||
this.parent();
|
||||
super._onDestroy();
|
||||
this._disconnectNotificationSignals();
|
||||
},
|
||||
}
|
||||
|
||||
_disconnectNotificationSignals() {
|
||||
if (this._updatedId)
|
||||
@ -792,26 +780,23 @@ var NotificationMessage = new Lang.Class({
|
||||
if (this._destroyId)
|
||||
this.notification.disconnect(this._destroyId);
|
||||
this._destroyId = 0;
|
||||
},
|
||||
}
|
||||
|
||||
canClose() {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var EventsSection = new Lang.Class({
|
||||
Name: 'EventsSection',
|
||||
Extends: MessageList.MessageListSection,
|
||||
var EventsSection = class EventsSection extends MessageList.MessageListSection {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
_init() {
|
||||
this._desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.interface' });
|
||||
this._desktopSettings.connect('changed', this._reloadEvents.bind(this));
|
||||
this._eventSource = new EmptyEventSource();
|
||||
|
||||
this._messageById = new Map();
|
||||
|
||||
this.parent();
|
||||
|
||||
this._title = new St.Button({ style_class: 'events-section-title',
|
||||
label: '',
|
||||
x_align: St.Align.START,
|
||||
@ -824,16 +809,16 @@ var EventsSection = new Lang.Class({
|
||||
Shell.AppSystem.get_default().connect('installed-changed',
|
||||
this._appInstalledChanged.bind(this));
|
||||
this._appInstalledChanged();
|
||||
},
|
||||
}
|
||||
|
||||
setEventSource(eventSource) {
|
||||
this._eventSource = eventSource;
|
||||
this._eventSource.connect('changed', this._reloadEvents.bind(this));
|
||||
},
|
||||
}
|
||||
|
||||
get allowed() {
|
||||
return Main.sessionMode.showCalendarEvents;
|
||||
},
|
||||
}
|
||||
|
||||
_updateTitle() {
|
||||
this._title.visible = !isToday(this._date);
|
||||
@ -852,7 +837,7 @@ var EventsSection = new Lang.Class({
|
||||
dayFormat = Shell.util_translate_time_string(NC_("calendar heading",
|
||||
"%A, %B %-d, %Y"));
|
||||
this._title.label = this._date.toLocaleFormat(dayFormat);
|
||||
},
|
||||
}
|
||||
|
||||
_reloadEvents() {
|
||||
if (this._eventSource.isLoading)
|
||||
@ -887,12 +872,12 @@ var EventsSection = new Lang.Class({
|
||||
|
||||
this._reloading = false;
|
||||
this._sync();
|
||||
},
|
||||
}
|
||||
|
||||
_appInstalledChanged() {
|
||||
this._calendarApp = undefined;
|
||||
this._title.reactive = (this._getCalendarApp() != null);
|
||||
},
|
||||
}
|
||||
|
||||
_getCalendarApp() {
|
||||
if (this._calendarApp !== undefined)
|
||||
@ -907,7 +892,7 @@ var EventsSection = new Lang.Class({
|
||||
this._calendarApp = null;
|
||||
}
|
||||
return this._calendarApp;
|
||||
},
|
||||
}
|
||||
|
||||
_onTitleClicked() {
|
||||
Main.overview.hide();
|
||||
@ -917,32 +902,30 @@ var EventsSection = new Lang.Class({
|
||||
if (app.get_id() == 'evolution.desktop')
|
||||
app = Gio.DesktopAppInfo.new('evolution-calendar.desktop');
|
||||
app.launch([], global.create_app_launch_context(0, -1));
|
||||
},
|
||||
}
|
||||
|
||||
setDate(date) {
|
||||
this.parent(date);
|
||||
super.setDate(date);
|
||||
this._updateTitle();
|
||||
this._reloadEvents();
|
||||
},
|
||||
}
|
||||
|
||||
_shouldShow() {
|
||||
return !this.empty || !isToday(this._date);
|
||||
},
|
||||
}
|
||||
|
||||
_sync() {
|
||||
if (this._reloading)
|
||||
return;
|
||||
|
||||
this.parent();
|
||||
super._sync();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var NotificationSection = new Lang.Class({
|
||||
Name: 'NotificationSection',
|
||||
Extends: MessageList.MessageListSection,
|
||||
|
||||
_init() {
|
||||
this.parent();
|
||||
var NotificationSection =
|
||||
class NotificationSection extends MessageList.MessageListSection {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this._sources = new Map();
|
||||
this._nUrgent = 0;
|
||||
@ -953,12 +936,12 @@ var NotificationSection = new Lang.Class({
|
||||
});
|
||||
|
||||
this.actor.connect('notify::mapped', this._onMapped.bind(this));
|
||||
},
|
||||
}
|
||||
|
||||
get allowed() {
|
||||
return Main.sessionMode.hasNotifications &&
|
||||
!Main.sessionMode.isGreeter;
|
||||
},
|
||||
}
|
||||
|
||||
_createTimeLabel(datetime) {
|
||||
let label = new St.Label({ style_class: 'event-time',
|
||||
@ -969,7 +952,7 @@ var NotificationSection = new Lang.Class({
|
||||
label.text = Util.formatTimeSpan(datetime);
|
||||
});
|
||||
return label;
|
||||
},
|
||||
}
|
||||
|
||||
_sourceAdded(tray, source) {
|
||||
let obj = {
|
||||
@ -984,7 +967,7 @@ var NotificationSection = new Lang.Class({
|
||||
this._onNotificationAdded.bind(this));
|
||||
|
||||
this._sources.set(source, obj);
|
||||
},
|
||||
}
|
||||
|
||||
_onNotificationAdded(source, notification) {
|
||||
let message = new NotificationMessage(notification);
|
||||
@ -1015,14 +998,14 @@ var NotificationSection = new Lang.Class({
|
||||
|
||||
let index = isUrgent ? 0 : this._nUrgent;
|
||||
this.addMessageAtIndex(message, index, this.actor.mapped);
|
||||
},
|
||||
}
|
||||
|
||||
_onSourceDestroy(source, obj) {
|
||||
source.disconnect(obj.destroyId);
|
||||
source.disconnect(obj.notificationAddedId);
|
||||
|
||||
this._sources.delete(source);
|
||||
},
|
||||
}
|
||||
|
||||
_onMapped() {
|
||||
if (!this.actor.mapped)
|
||||
@ -1031,17 +1014,15 @@ var NotificationSection = new Lang.Class({
|
||||
for (let message of this._messages.keys())
|
||||
if (message.notification.urgency != MessageTray.Urgency.CRITICAL)
|
||||
message.notification.acknowledged = true;
|
||||
},
|
||||
}
|
||||
|
||||
_shouldShow() {
|
||||
return !this.empty && isToday(this._date);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var Placeholder = new Lang.Class({
|
||||
Name: 'Placeholder',
|
||||
|
||||
_init() {
|
||||
var Placeholder = class Placeholder {
|
||||
constructor() {
|
||||
this.actor = new St.BoxLayout({ style_class: 'message-list-placeholder',
|
||||
vertical: true });
|
||||
|
||||
@ -1059,14 +1040,14 @@ var Placeholder = new Lang.Class({
|
||||
this.actor.add_actor(this._label);
|
||||
|
||||
this._sync();
|
||||
},
|
||||
}
|
||||
|
||||
setDate(date) {
|
||||
if (sameDay(this._date, date))
|
||||
return;
|
||||
this._date = date;
|
||||
this._sync();
|
||||
},
|
||||
}
|
||||
|
||||
_sync() {
|
||||
let today = isToday(this._date);
|
||||
@ -1083,12 +1064,10 @@ var Placeholder = new Lang.Class({
|
||||
this._label.text = _("No Events");
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
var CalendarMessageList = new Lang.Class({
|
||||
Name: 'CalendarMessageList',
|
||||
|
||||
_init() {
|
||||
var CalendarMessageList = class CalendarMessageList {
|
||||
constructor() {
|
||||
this.actor = new St.Widget({ style_class: 'message-list',
|
||||
layout_manager: new Clutter.BinLayout(),
|
||||
x_expand: true, y_expand: true });
|
||||
@ -1134,7 +1113,7 @@ var CalendarMessageList = new Lang.Class({
|
||||
this._addSection(this._eventsSection);
|
||||
|
||||
Main.sessionMode.connect('updated', this._sync.bind(this));
|
||||
},
|
||||
}
|
||||
|
||||
_addSection(section) {
|
||||
let obj = {
|
||||
@ -1159,7 +1138,7 @@ var CalendarMessageList = new Lang.Class({
|
||||
this._sections.set(section, obj);
|
||||
this._sectionList.add_actor(section.actor);
|
||||
this._sync();
|
||||
},
|
||||
}
|
||||
|
||||
_removeSection(section) {
|
||||
let obj = this._sections.get(section);
|
||||
@ -1172,11 +1151,11 @@ var CalendarMessageList = new Lang.Class({
|
||||
this._sections.delete(section);
|
||||
this._sectionList.remove_actor(section.actor);
|
||||
this._sync();
|
||||
},
|
||||
}
|
||||
|
||||
_onKeyFocusIn(section, actor) {
|
||||
Util.ensureActorVisibleInScrollView(this._scrollView, actor);
|
||||
},
|
||||
}
|
||||
|
||||
_sync() {
|
||||
let sections = [...this._sections.keys()];
|
||||
@ -1191,15 +1170,15 @@ var CalendarMessageList = new Lang.Class({
|
||||
|
||||
let canClear = sections.some(s => s.canClear && s.actor.visible);
|
||||
this._clearButton.reactive = canClear;
|
||||
},
|
||||
}
|
||||
|
||||
setEventSource(eventSource) {
|
||||
this._eventsSection.setEventSource(eventSource);
|
||||
},
|
||||
}
|
||||
|
||||
setDate(date) {
|
||||
for (let section of this._sections.keys())
|
||||
section.setDate(date);
|
||||
this._placeholder.setDate(date);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
Reference in New Issue
Block a user