RandomWallpaperGnome3/randomwallpaper@iflow.space/extension.js

214 lines
5.8 KiB
JavaScript
Raw Normal View History

const Lang = imports.lang;
2016-04-08 12:50:25 +00:00
const GLib = imports.gi.GLib;
const Shell = imports.gi.Shell;
2014-08-20 00:25:47 +00:00
//self
const Self = imports.misc.extensionUtils.getCurrentExtension();
const WallpaperController = Self.imports.wallpaperController;
2017-07-20 11:48:13 +00:00
const LoggerModule = Self.imports.logger;
// UI Imports
2014-08-20 00:25:47 +00:00
const Main = imports.ui.main;
const St = imports.gi.St;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
2017-07-19 13:08:50 +00:00
const CustomElements = Self.imports.elements;
2014-10-22 13:17:46 +00:00
const Tweener = imports.ui.tweener;
// Filesystem
const Gio = imports.gi.Gio;
2016-04-08 16:40:52 +00:00
// Settings
const Convenience = Self.imports.convenience;
let wallpaperController;
let extensionMeta;
function init(metaData) {
extensionMeta = metaData;
wallpaperController = new WallpaperController.WallpaperController(metaData);
2014-08-20 00:25:47 +00:00
}
let panelEntry;
2014-08-20 00:25:47 +00:00
let RandomWallpaperEntry = new Lang.Class({
Extends: PanelMenu.Button,
Name: "RandomWallpaperEntry",
2017-07-20 11:48:13 +00:00
logger: null,
2014-08-20 00:25:47 +00:00
_init: function(menuAlignment, nameText) {
this.parent(menuAlignment, nameText);
2017-07-20 11:48:13 +00:00
this.logger = new LoggerModule.Logger('RWG3', 'RandomWallpaperEntry');
// Panelmenu Icon
2014-10-22 13:17:46 +00:00
this.statusIcon = new CustomElements.StatusElement();
this.actor.add_child(this.statusIcon);
// new wallpaper button
2017-02-24 22:31:35 +00:00
this.newWallpaperItem = new CustomElements.NewWallpaperElement();
this.menu.addMenuItem(this.newWallpaperItem);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
2017-07-20 19:49:54 +00:00
// current background section
this.currentBackgroundSection = new PopupMenu.PopupMenuSection();
this.menu.addMenuItem(this.currentBackgroundSection);
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// history section
this.historySection = new PopupMenu.PopupMenuSection();
this.menu.addMenuItem(this.historySection);
this.setHistoryList();
this.menu.addMenuItem(new PopupMenu.PopupSeparatorMenuItem());
// clear history button
this.clearHistoryItem = new PopupMenu.PopupMenuItem('Clear History');
this.menu.addMenuItem(this.clearHistoryItem);
2016-04-08 12:50:25 +00:00
// open wallpaper folder button
this.openFolder = new PopupMenu.PopupMenuItem('Open Wallpaper Folder');
this.menu.addMenuItem(this.openFolder);
2017-07-20 12:16:08 +00:00
// settings button
this.openSettings = new PopupMenu.PopupMenuItem('Settings');
this.menu.addMenuItem(this.openSettings);
/*
add eventlistener
*/
2017-02-24 22:31:35 +00:00
wallpaperController.registerStartLoadingHook(this.statusIcon.startLoading.bind(this.statusIcon));
wallpaperController.registerStopLoadingHook(this.statusIcon.stopLoading.bind(this.statusIcon));
wallpaperController.registerStopLoadingHook(this.setHistoryList.bind(this));
// new wallpaper event
2014-10-23 16:59:15 +00:00
this.newWallpaperItem.connect('activate', function() {
2017-02-24 22:31:35 +00:00
wallpaperController.fetchNewWallpaper();
});
2014-08-20 00:25:47 +00:00
// clear history event
2014-10-23 16:59:15 +00:00
this.clearHistoryItem.connect('activate', function() {
wallpaperController.deleteHistory();
});
2016-04-08 12:50:25 +00:00
// Open Wallpaper Folder
this.openFolder.connect('activate', function(event) {
let uri = GLib.filename_to_uri(wallpaperController.wallpaperlocation, "");
Gio.AppInfo.launch_default_for_uri(uri, global.create_app_launch_context(0, -1))
});
2017-07-20 12:16:08 +00:00
this.openSettings.connect("activate", function(){
// call gnome settings tool for this extension
let app = Shell.AppSystem.get_default().lookup_app("gnome-shell-extension-prefs.desktop");
if( app!=null ) {
// only works in Gnome >= 3.12
let info = app.get_app_info();
let timestamp = global.display.get_current_time_roundtrip();
info.launch_uris([Self.uuid], global.create_app_launch_context(timestamp, -1));
}
});
this.menu.actor.connect('show', function() {
2017-02-24 22:31:35 +00:00
this.newWallpaperItem.show();
wallpaperController.menuShowHook();
2017-02-24 22:31:35 +00:00
}.bind(this));
2016-04-08 12:50:25 +00:00
// when the popupmenu disapears, check if the wallpaper is the original and
// reset it if needed
2017-02-24 22:31:35 +00:00
this.menu.actor.connect('hide', () => {
wallpaperController.resetWallpaper();
2017-02-24 22:31:35 +00:00
this.setHistoryList();
});
this.menu.actor.connect('leave-event', function(e, t, a) {
wallpaperController.resetWallpaper();
});
},
2017-07-20 19:49:54 +00:00
setCurrentBackgroundElement: function () {
this.currentBackgroundSection.removeAll();
let historyController = wallpaperController.getHistoryController();
let history = historyController.history;
if (history.length > 0) {
let currentImage = new CustomElements.CurrentImageElement(history[0]);
this.currentBackgroundSection.addMenuItem(currentImage);
}
},
setHistoryList: function() {
2017-07-20 19:49:54 +00:00
this.setCurrentBackgroundElement();
this.historySection.removeAll();
2017-07-20 11:48:13 +00:00
let historyController = wallpaperController.getHistoryController();
let history = historyController.history;
if (history.length <= 1) {
this.clearHistoryList();
return;
2017-07-20 11:48:13 +00:00
}
2017-07-20 11:48:13 +00:00
for (let i = 1; i < history.length; i++) {
let historyid = history[i].id;
let tmp = new CustomElements.HistoryElement(history[i], i);
tmp.actor.connect('key-focus-in', onEnter);
2014-10-23 16:59:15 +00:00
tmp.actor.connect('key-focus-out', onLeave);
tmp.actor.connect('enter-event', onEnter);
2014-10-23 16:59:15 +00:00
tmp.connect('activate', onSelect);
2014-10-22 13:17:46 +00:00
this.historySection.addMenuItem(tmp);
2017-07-20 11:48:13 +00:00
}
let _this = this;
function onLeave(actor) {
wallpaperController.resetWallpaper();
}
function onEnter(actor) {
wallpaperController.previewWallpaper(actor.historyId);
}
function onSelect(actor) {
wallpaperController.setWallpaper(actor.historyEntry.id);
}
},
clearHistoryList: function() {
this.historySection.removeAll();
let empty = new PopupMenu.PopupMenuItem('No recent wallpaper ...', {
activate: false,
hover: false,
style_class: 'rwg-recent-lable',
can_focus: false
});
this.historySection.addMenuItem(empty);
},
});
2014-08-20 00:25:47 +00:00
function enable() {
2016-04-08 10:27:38 +00:00
// Extension enabled
2017-02-04 23:48:52 +00:00
// UI
panelEntry = new RandomWallpaperEntry(0, "Random wallpaper");
// add to panel
Main.panel.addToStatusArea("random-wallpaper-menu", panelEntry);
2014-08-20 00:25:47 +00:00
}
function disable() {
2016-04-08 10:27:38 +00:00
// Extension disabled
panelEntry.destroy();
}