diff options
Diffstat (limited to 'extensions/44/vertical-workspaces/lib/recentFilesSearchProvider.js')
-rw-r--r-- | extensions/44/vertical-workspaces/lib/recentFilesSearchProvider.js | 292 |
1 files changed, 145 insertions, 147 deletions
diff --git a/extensions/44/vertical-workspaces/lib/recentFilesSearchProvider.js b/extensions/44/vertical-workspaces/lib/recentFilesSearchProvider.js index 86e38f4..b567ff2 100644 --- a/extensions/44/vertical-workspaces/lib/recentFilesSearchProvider.js +++ b/extensions/44/vertical-workspaces/lib/recentFilesSearchProvider.js @@ -1,5 +1,5 @@ /** - * Vertical Workspaces +* V-Shell (Vertical Workspaces) * recentFilesSearchProvider.js * * @author GdH <G-dH@github.com> @@ -9,116 +9,146 @@ 'use strict'; -const { GLib, Gio, Meta, St, Shell, Gtk } = imports.gi; +const Gio = imports.gi.Gio; +const GLib = imports.gi.GLib; +const RecentManager = imports.gi.Gtk.RecentManager; +const St = imports.gi.St; +const Shell = imports.gi.Shell; const Main = imports.ui.main; -const ExtensionUtils = imports.misc.extensionUtils; -const Me = ExtensionUtils.getCurrentExtension(); -const Settings = Me.imports.lib.settings; -const _Util = Me.imports.lib.util; +let Me; +let opt; // gettext -const _ = Settings._; - -const shellVersion = Settings.shellVersion; - -const ModifierType = imports.gi.Clutter.ModifierType; - -let recentFilesSearchProvider; -let _enableTimeoutId = 0; +let _; // prefix helps to eliminate results from other search providers // so it needs to be something less common // needs to be accessible from vw module -var prefix = 'fq//'; +const PREFIX = 'fq//'; + +var RecentFilesSearchProviderModule = class { + // export for other modules + static _PREFIX = PREFIX; + constructor(me) { + Me = me; + opt = Me.opt; + _ = Me.gettext; + + this._firstActivation = true; + this.moduleEnabled = false; + this._recentFilesSearchProvider = null; + this._enableTimeoutId = 0; + } -var opt; + cleanGlobals() { + Me = null; + opt = null; + _ = null; + } -function getOverviewSearchResult() { - return Main.overview._overview.controls._searchController._searchResults; -} + update(reset) { + this.moduleEnabled = opt.get('recentFilesSearchProviderModule'); + reset = reset || !this.moduleEnabled; -function update(reset = false) { - opt = Me.imports.lib.settings.opt; - if (!reset && opt.RECENT_FILES_SEARCH_PROVIDER_ENABLED && !recentFilesSearchProvider) { - enable(); - } else if (reset || !opt.RECENT_FILES_SEARCH_PROVIDER_ENABLED) { - disable(); - opt = null; + if (reset && !this._firstActivation) { + this._disableModule(); + } else if (!reset) { + this._firstActivation = false; + this._activateModule(); + } + if (reset && this._firstActivation) + console.debug(' RecentFilesSearchProviderModule - Keeping untouched'); } -} -function enable() { - // delay because Fedora had problem to register a new provider soon after Shell restarts - _enableTimeoutId = GLib.timeout_add( - GLib.PRIORITY_DEFAULT, - 2000, - () => { - if (!recentFilesSearchProvider) { - recentFilesSearchProvider = new RecentFilesSearchProvider(opt); - getOverviewSearchResult()._registerProvider(recentFilesSearchProvider); + _activateModule() { + // delay because Fedora had problem to register a new provider soon after Shell restarts + this._enableTimeoutId = GLib.timeout_add( + GLib.PRIORITY_DEFAULT, + 2000, + () => { + if (!this._recentFilesSearchProvider) { + this._recentFilesSearchProvider = new RecentFilesSearchProvider(opt); + this._getOverviewSearchResult()._registerProvider(this._recentFilesSearchProvider); + } + this._enableTimeoutId = 0; + return GLib.SOURCE_REMOVE; } - _enableTimeoutId = 0; - return GLib.SOURCE_REMOVE; - } - ); -} + ); -function disable() { - if (recentFilesSearchProvider) { - getOverviewSearchResult()._unregisterProvider(recentFilesSearchProvider); - recentFilesSearchProvider = null; - } - if (_enableTimeoutId) { - GLib.source_remove(_enableTimeoutId); - _enableTimeoutId = 0; + console.debug(' RecentFilesSearchProviderModule - Activated'); } -} -function makeResult(window, i) { - const app = Shell.WindowTracker.get_default().get_window_app(window); - const appName = app ? app.get_name() : 'Unknown'; - const windowTitle = window.get_title(); - const wsIndex = window.get_workspace().index(); - - return { - 'id': i, - // convert all accented chars to their basic form and lower case for search - 'name': `${wsIndex + 1}: ${windowTitle} ${appName}`.normalize('NFD').replace(/[\u0300-\u036f]/g, '').toLowerCase(), - appName, - windowTitle, - window, - }; -} + _disableModule() { + if (this._recentFilesSearchProvider) { + this._getOverviewSearchResult()._unregisterProvider(this._recentFilesSearchProvider); + this._recentFilesSearchProvider = null; + } + if (this._enableTimeoutId) { + GLib.source_remove(this._enableTimeoutId); + this._enableTimeoutId = 0; + } -const closeSelectedRegex = /^\/x!$/; -const closeAllResultsRegex = /^\/xa!$/; -const moveToWsRegex = /^\/m[0-9]+$/; -const moveAllToWsRegex = /^\/ma[0-9]+$/; + console.debug(' RecentFilesSearchProviderModule - Disabled'); + } -const RecentFilesSearchProvider = class RecentFilesSearchProvider { - constructor() { - this.id = 'org.gnome.Nautilus.desktop'; - this.appInfo = Gio.AppInfo.create_from_commandline('/usr/bin/nautilus -ws recent:///', 'Recent Files', null); - // this.appInfo = Shell.AppSystem.get_default().lookup_app('org.gnome.Nautilus.desktop').appInfo; - this.appInfo.get_description = () => _('Search recent files'); - this.appInfo.get_name = () => _('Recent Files'); - this.appInfo.get_id = () => this.id; - this.appInfo.get_icon = () => Gio.icon_new_for_string('document-open-recent-symbolic'); - this.appInfo.should_show = () => true; + _getOverviewSearchResult() { + return Main.overview._overview.controls._searchController._searchResults; + } +}; +class RecentFilesSearchProvider { + constructor() { + this.id = 'recent-files'; + const appSystem = Shell.AppSystem.get_default(); + let appInfo = appSystem.lookup_app('org.gnome.Nautilus.desktop')?.get_app_info(); + if (!appInfo) + appInfo = Gio.AppInfo.create_from_commandline('/usr/bin/nautilus -w', _('Recent Files'), null); + appInfo.get_description = () => _('Search recent files'); + appInfo.get_name = () => _('Recent Files'); + appInfo.get_id = () => 'org.gnome.Nautilus.desktop'; + appInfo.get_icon = () => Gio.icon_new_for_string('document-open-recent-symbolic'); + appInfo.should_show = () => true; + + this.appInfo = appInfo; this.canLaunchSearch = true; this.isRemoteProvider = false; } + getInitialResultSet(terms, callback /* , cancellable = null*/) { + // In GS 43 callback arg has been removed + /* if (Me.shellVersion >= 43) + cancellable = callback; */ + + const filesDict = {}; + let files = []; + if (terms[0].startsWith(PREFIX)) + files = RecentManager.get_default().get_items(); + + // Detect whether time stamps are in int, or in GLib.DateTime object + this._timeNeedsConversion = files[0]?.get_modified().to_unix; + + for (let file of files) + filesDict[file.get_uri()] = file; + + this.files = filesDict; + + if (Me.shellVersion >= 43) + return new Promise(resolve => resolve(this._getResultSet(terms))); + else + callback(this._getResultSet(terms)); + + return null; + } + _getResultSet(terms) { - if (!terms[0].startsWith(prefix)) + if (!terms[0].startsWith(PREFIX)) return []; // do not modify original terms let termsCopy = [...terms]; // search for terms without prefix - termsCopy[0] = termsCopy[0].replace(prefix, ''); + termsCopy[0] = termsCopy[0].replace(PREFIX, ''); const candidates = this.files; const _terms = [].concat(termsCopy); @@ -135,15 +165,18 @@ const RecentFilesSearchProvider = class RecentFilesSearchProvider { const file = this.files[id]; const name = `${file.get_age()}d: ${file.get_display_name()} ${file.get_uri_display().replace(`/${file.get_display_name()}`, '')}`; if (opt.SEARCH_FUZZY) - m = _Util.fuzzyMatch(term, name); + m = Me.Util.fuzzyMatch(term, name); else - m = _Util.strictMatch(term, name); + m = Me.Util.strictMatch(term, name); if (m !== -1) results.push({ weight: m, id }); } - results.sort((a, b) => this.files[a.id].get_visited() < this.files[b.id].get_visited()); + if (this._timeNeedsConversion) + results.sort((a, b) => this.files[a.id].get_modified().to_unix() < this.files[b.id].get_modified().to_unix()); + else + results.sort((a, b) => this.files[a.id].get_modified() < this.files[b.id].get_modified()); this.resultIds = results.map(item => item.id); return this.resultIds; @@ -151,7 +184,7 @@ const RecentFilesSearchProvider = class RecentFilesSearchProvider { getResultMetas(resultIds, callback = null) { const metas = resultIds.map(id => this.getResultMeta(id)); - if (shellVersion >= 43) + if (Me.shellVersion >= 43) return new Promise(resolve => resolve(metas)); else if (callback) callback(metas); @@ -172,89 +205,54 @@ const RecentFilesSearchProvider = class RecentFilesSearchProvider { } getIcon(result, size) { - let file = Gio.File.new_for_uri(result.get_uri()); - let info = file.query_info(Gio.FILE_ATTRIBUTE_THUMBNAIL_PATH, - Gio.FileQueryInfoFlags.NONE, null); - let path = info.get_attribute_byte_string( - Gio.FILE_ATTRIBUTE_THUMBNAIL_PATH); - let icon, gicon; - if (path) { - gicon = Gio.FileIcon.new(Gio.File.new_for_path(path)); - } else { - const appInfo = Gio.AppInfo.get_default_for_type(result.get_mime_type(), false); - if (appInfo) - gicon = appInfo.get_icon(); - } + const appInfo = Gio.AppInfo.get_default_for_type(result.get_mime_type(), false); + if (appInfo) + gicon = appInfo.get_icon(); if (gicon) icon = new St.Icon({ gicon, icon_size: size }); else icon = new St.Icon({ icon_name: 'icon-missing', icon_size: size }); - return icon; } - launchSearch(/* terms, timeStamp */) { - this._openNautilus('recent:///'); + launchSearch(terms, timeStamp) { + const appInfo = Gio.AppInfo.create_from_commandline('/usr/bin/nautilus -w recent:///', 'Nautilus', null); + appInfo.launch([], global.create_app_launch_context(timeStamp, -1)); } - _openNautilus(uri) { - try { - GLib.spawn_command_line_async(`nautilus -ws ${uri}`); - } catch (e) { - log(e); - } - } - - activateResult(resultId /* , terms, timeStamp */) { - const file = this.files[resultId]; - - if (_Util.isShiftPressed()) { + activateResult(resultId, terms, timeStamp) { + const uri = resultId; + const context = global.create_app_launch_context(timeStamp, -1); + if (Me.Util.isShiftPressed()) { Main.overview.toggle(); - this._openNautilus(file.get_uri()); + this.appInfo.launch_uris([uri], context); + } else if (Gio.app_info_launch_default_for_uri(uri, context)) { + // update recent list after (hopefully) successful activation + const recentManager = RecentManager.get_default(); + recentManager.add_item(resultId); } else { - const appInfo = Gio.AppInfo.get_default_for_type(file.get_mime_type(), false); - if (!(appInfo && appInfo.launch_uris([file.get_uri()], null))) - this._openNautilus(file.get_uri()); + this.appInfo.launch_uris([uri], context); } } - getInitialResultSet(terms, callback /* , cancellable = null*/) { - // In GS 43 callback arg has been removed - /* if (shellVersion >= 43) - cancellable = callback; */ - - const filesDict = {}; - const files = Gtk.RecentManager.get_default().get_items().filter(f => f.exists()); - - for (let file of files) - filesDict[file.get_uri()] = file; - - - this.files = filesDict; - - if (shellVersion >= 43) - return new Promise(resolve => resolve(this._getResultSet(terms))); - else - callback(this._getResultSet(terms)); - - return null; + filterResults(results /* , maxResults*/) { + // return results.slice(0, maxResults); + return results.slice(0, 20); } - filterResults(results, maxResults) { - return results.slice(0, 20); - // return results.slice(0, maxResults); + getSubsearchResultSet(previousResults, terms, callback) { + if (Me.shellVersion < 43) { + this.getSubsearchResultSet42(terms, callback); + return null; + } + return this.getInitialResultSet(terms); } - getSubsearchResultSet(previousResults, terms, callback /* , cancellable*/) { - // if we return previous results, quick typers get non-actual results + getSubsearchResultSet42(terms, callback) { callback(this._getResultSet(terms)); } - - /* createResultObject(resultMeta) { - return this.files[resultMeta.id]; - }*/ -}; +} |