Skip to content

Commit 2885166

Browse files
committed
menu applet: more code reduction
Remove more code that can be done on the base class through the constructor, remove RecentClearButton class.
1 parent dcb4758 commit 2885166

File tree

1 file changed

+42
-94
lines changed
  • files/usr/share/cinnamon/applets/menu@cinnamon.org

1 file changed

+42
-94
lines changed

files/usr/share/cinnamon/applets/menu@cinnamon.org/applet.js

Lines changed: 42 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,21 @@ class VisibleChildIterator {
110110
}
111111

112112
class SimpleMenuItem {
113-
constructor(reactive=true, activatable=true) {
113+
constructor(reactive=true, activatable=true, name="", description="", styleClass="") {
114114
this._signals = new SignalManager.SignalManager();
115115

116116
this.actor = new St.BoxLayout({ style_class: 'popup-menu-item',
117117
reactive: reactive,
118118
accessible_role: Atk.Role.MENU_ITEM });
119+
if (styleClass)
120+
this.actor.add_style_class_name(styleClass);
121+
119122
this.actor._delegate = this;
120123

121124
this.label = null;
122125
this.icon = null;
123-
this.name = "";
124-
this.description = "";
126+
this.name = name;
127+
this.description = description;
125128

126129
if (reactive && activatable) {
127130
this._signals.connect(this.actor, 'button-release-event', Lang.bind(this, this._onButtonReleaseEvent));
@@ -279,16 +282,12 @@ class ApplicationContextMenuItem extends PopupMenu.PopupBaseMenuItem {
279282
}
280283

281284
class GenericApplicationButton extends SimpleMenuItem {
282-
constructor(applet, app, withMenu) {
283-
super();
285+
constructor(applet, app, withMenu=false, styleClass="") {
286+
let desc = app.get_description() || "";
287+
super(true, true, app.get_name(), desc.split("\n")[0], styleClass);
284288
this.app = app;
285289
this.applet = applet;
286290

287-
this.name = this.app.get_name();
288-
let desc = app.get_description();
289-
if (desc)
290-
this.description = desc.split("\n")[0];
291-
292291
this.withMenu = withMenu;
293292
if (this.withMenu){
294293
this.menu = new PopupMenu.PopupSubMenu(this.actor);
@@ -395,10 +394,7 @@ class GenericApplicationButton extends SimpleMenuItem {
395394

396395
class TransientButton extends SimpleMenuItem {
397396
constructor(applet, pathOrCommand) {
398-
super();
399-
this.name = "";
400-
this.description = pathOrCommand;
401-
397+
super(true, true, "", pathOrCommand, 'menu-application-button');
402398
if (pathOrCommand.charAt(0) == '~') {
403399
pathOrCommand = pathOrCommand.slice(1);
404400
pathOrCommand = GLib.get_home_dir() + pathOrCommand;
@@ -422,7 +418,7 @@ class TransientButton extends SimpleMenuItem {
422418
this.app = {
423419
get_app_info: {
424420
get_filename() {
425-
return pathOrCommand;
421+
return this.name;
426422
}
427423
},
428424
get_id() {
@@ -451,7 +447,6 @@ class TransientButton extends SimpleMenuItem {
451447
this.icon = new St.Icon({icon_name: iconName, icon_size: APPLICATION_ICON_SIZE, icon_type: St.IconType.FULLCOLOR});
452448
// @todo Would be nice to indicate we don't have a handler for this file.
453449
}
454-
this.actor.set_style_class_name('menu-application-button');
455450

456451
this.addActor(this.icon);
457452

@@ -482,9 +477,8 @@ class TransientButton extends SimpleMenuItem {
482477

483478
class ApplicationButton extends GenericApplicationButton {
484479
constructor(applet, app) {
485-
super(applet, app, true);
480+
super(applet, app, true, 'menu-application-button');
486481
this.category = [];
487-
this.actor.set_style_class_name('menu-application-button');
488482

489483
if (applet.showApplicationIcons) {
490484
this.icon = this.app.create_icon_texture(APPLICATION_ICON_SIZE);
@@ -523,15 +517,10 @@ class ApplicationButton extends GenericApplicationButton {
523517

524518
class SearchProviderResultButton extends SimpleMenuItem {
525519
constructor(applet, provider, result) {
526-
super();
520+
super(true, true, result.label, result.description, 'menu-application-button');
527521
this.provider = provider;
528522
this.result = result;
529-
530-
this.name = result.label;
531-
this.description = result.description;
532-
533523
this.applet = applet;
534-
this.actor.set_style_class_name('menu-application-button');
535524

536525
if (result.icon) {
537526
this.icon = result.icon;
@@ -567,19 +556,14 @@ class SearchProviderResultButton extends SimpleMenuItem {
567556

568557
class PlaceButton extends SimpleMenuItem {
569558
constructor(applet, place) {
570-
super();
571-
this.applet = applet;
572-
this.place = place;
573-
574-
this.name = place.name;
575-
let selectedAppId = place.idDecoded;
576-
selectedAppId = selectedAppId.substr(selectedAppId.indexOf(':') + 1);
559+
let selectedAppId = place.idDecoded.substr(place.idDecoded.indexOf(':') + 1);
577560
let fileIndex = selectedAppId.indexOf('file:///');
578561
if (fileIndex !== -1)
579562
selectedAppId = selectedAppId.substr(fileIndex + 7);
580-
this.description = selectedAppId;
581563

582-
this.actor.set_style_class_name('menu-application-button');
564+
super(true, true, place.name, selectedAppId, 'menu-application-button');
565+
this.applet = applet;
566+
this.place = place;
583567

584568
if (applet.showApplicationIcons) {
585569
this.icon = place.iconFactory(APPLICATION_ICON_SIZE);
@@ -624,23 +608,18 @@ class RecentContextMenuItem extends PopupMenu.PopupBaseMenuItem {
624608

625609
class RecentButton extends SimpleMenuItem {
626610
constructor(applet, file) {
627-
super();
611+
let fileIndex = file.uriDecoded.indexOf("file:///");
612+
let selectedAppUri = fileIndex === -1 ? "" : file.uriDecoded.substr(fileIndex + 7);
613+
614+
super(true, true, file.name, selectedAppUri, 'menu-application-button');
615+
628616
this.mimeType = file.mimeType;
629617
this.uri = file.uri;
630618
this.uriDecoded = file.uriDecoded;
631619
this.applet = applet;
632620

633-
this.name = file.name;
634-
let selectedAppUri = file.uriDecoded;
635-
let fileIndex = selectedAppUri.indexOf("file:///");
636-
if (fileIndex !== -1)
637-
selectedAppUri = selectedAppUri.substr(fileIndex + 7);
638-
this.description = selectedAppUri;
639-
640621
this.menu = null;
641622

642-
this.actor.set_style_class_name('menu-application-button');
643-
644623
if (applet.showApplicationIcons) {
645624
this.icon = file.createIcon(APPLICATION_ICON_SIZE);
646625
this.addActor(this.icon);
@@ -786,30 +765,6 @@ class RecentButton extends SimpleMenuItem {
786765
}
787766
}
788767

789-
class RecentClearButton extends SimpleMenuItem {
790-
constructor(applet) {
791-
super();
792-
this.applet = applet;
793-
this.actor.set_style_class_name('menu-application-button');
794-
795-
this.name = _("Clear list");
796-
this.description = _("Clear all recent documents");
797-
798-
this.addIcon(APPLICATION_ICON_SIZE, 'edit-clear', null, true);
799-
this.addLabel(this.name, 'menu-application-button-label');
800-
}
801-
802-
activate() {
803-
this.applet.menu.close();
804-
(new Gtk.RecentManager()).purge_items();
805-
}
806-
807-
destroy() {
808-
delete this.applet;
809-
super.destroy();
810-
}
811-
}
812-
813768
class CategoryButton extends SimpleMenuItem {
814769
constructor(category, showIcon) {
815770
super(true, false);
@@ -831,30 +786,25 @@ class CategoryButton extends SimpleMenuItem {
831786

832787
class PlaceCategoryButton extends SimpleMenuItem {
833788
constructor(showIcon) {
834-
super(true, false);
835-
this.actor.set_style_class_name('menu-category-button');
789+
super(true, false, _('Places'), '', 'menu-category-button');
836790
if (showIcon)
837791
this.addIcon(CATEGORY_ICON_SIZE, 'folder');
838-
this.addLabel(_("Places"), 'menu-category-button-label');
792+
this.addLabel(this.name, 'menu-category-button-label');
839793
}
840794
}
841795

842796
class RecentCategoryButton extends SimpleMenuItem {
843797
constructor(showIcon) {
844-
super(true, false);
845-
this.actor.set_style_class_name('menu-category-button');
798+
super(true, false, _('Recent Files'), '', 'menu-category-button');
846799
if (showIcon)
847800
this.addIcon(CATEGORY_ICON_SIZE, 'folder-recent');
848-
this.addLabel(_("Recent Files"), 'menu-category-button-label');
801+
this.addLabel(this.name, 'menu-category-button-label');
849802
}
850803
}
851804

852805
class FavoritesButton extends GenericApplicationButton {
853806
constructor(applet, app) {
854-
super(applet, app);
855-
856-
this.actor.add_style_class_name('menu-favorites-button');
857-
807+
super(applet, app, false, 'menu-favorites-button');
858808
this.icon = app.create_icon_texture(getFavIconSize());
859809
this.addActor(this.icon);
860810

@@ -889,12 +839,7 @@ class FavoritesButton extends GenericApplicationButton {
889839

890840
class SystemButton extends SimpleMenuItem {
891841
constructor(iconName, name, desc) {
892-
super();
893-
this.name = name;
894-
this.description = desc;
895-
896-
this.actor.add_style_class_name('menu-favorites-button');
897-
842+
super(true, true, name, desc, 'menu-favorites-button');
898843
this.addIcon(getFavIconSize(), iconName);
899844
}
900845
}
@@ -1136,6 +1081,7 @@ class CinnamonMenuApplet extends Applet.TextIconApplet {
11361081
this.privacy_settings = new Gio.Settings( {schema_id: PRIVACY_SCHEMA} );
11371082
this.noRecentDocuments = true;
11381083
this._noRecentDocsButton = null;
1084+
this._recentClearButton = null;
11391085
this._activeContextMenuParent = null;
11401086
this._activeContextMenuItem = null;
11411087
this._display();
@@ -2012,7 +1958,7 @@ class CinnamonMenuApplet extends Applet.TextIconApplet {
20121958
this._previousSelectedActor._delegate instanceof RecentButton ||
20131959
this._previousSelectedActor._delegate instanceof SearchProviderResultButton ||
20141960
this._previousSelectedActor._delegate instanceof PlaceButton ||
2015-
this._previousSelectedActor._delegate instanceof RecentClearButton ||
1961+
this._previousSelectedActor._delegate === this._recentClearButton ||
20161962
this._previousSelectedActor._delegate instanceof TransientButton)
20171963
this._previousSelectedActor.style_class = "menu-application-button";
20181964
else if (this._previousSelectedActor._delegate instanceof FavoritesButton ||
@@ -2313,12 +2259,14 @@ class CinnamonMenuApplet extends Applet.TextIconApplet {
23132259
id++;
23142260
}
23152261

2316-
let recent_clear_button = null;
2317-
2318-
recent_clear_button = this._recentButtons.find(button => (button instanceof RecentClearButton));
2319-
2320-
if (recent_clear_button == undefined) {
2321-
let button = new RecentClearButton(this);
2262+
if (!this._recentClearButton) {
2263+
let button = new SimpleMenuItem(true, true, _("Clear list"), ("Clear all recent documents"), 'menu-application-button');
2264+
button.addIcon(APPLICATION_ICON_SIZE, 'edit-clear', null, true);
2265+
button.addLabel(button.name, 'menu-application-button-label');
2266+
button.activate = () => {
2267+
this.menu.close();
2268+
(new Gtk.RecentManager()).purge_items();
2269+
};
23222270
this._addEnterEvent(button, Lang.bind(this, function() {
23232271
this._clearPrevSelection(button.actor);
23242272
button.actor.style_class = "menu-application-button-selected";
@@ -2328,10 +2276,10 @@ class CinnamonMenuApplet extends Applet.TextIconApplet {
23282276
this._previousSelectedActor = button.actor;
23292277
}));
23302278

2331-
recent_clear_button = button;
2279+
this._recentClearButton = button;
23322280
}
23332281

2334-
new_recents.push(recent_clear_button);
2282+
new_recents.push(this._recentClearButton);
23352283

23362284
this.noRecentDocuments = false;
23372285
} else {
@@ -2377,7 +2325,7 @@ class CinnamonMenuApplet extends Applet.TextIconApplet {
23772325
let children = this.applicationsBox.get_children();
23782326
for (let i = children.length - 1; i > 0; i--) {
23792327
if ((children[i]._delegate instanceof RecentButton) ||
2380-
(children[i]._delegate instanceof RecentClearButton) ||
2328+
(children[i]._delegate === this._recentClearButton) ||
23812329
(i == children.length - 1)) {
23822330
placeholder = children[i - 1];
23832331
break;
@@ -3192,7 +3140,7 @@ class CinnamonMenuApplet extends Applet.TextIconApplet {
31923140

31933141
let recentResults = [];
31943142
for (let i = 0; i < this._recentButtons.length; i++) {
3195-
if (!(this._recentButtons[i] instanceof RecentClearButton) && this._recentButtons[i].name.toLowerCase().indexOf(pattern) != -1)
3143+
if (!(this._recentButtons[i] === this._recentClearButton) && this._recentButtons[i].name.toLowerCase().indexOf(pattern) != -1)
31963144
recentResults.push(this._recentButtons[i].name);
31973145
}
31983146

0 commit comments

Comments
 (0)