Skip to content

Commit

Permalink
Merge pull request linuxmint#5669 from claudiux/search-box_fix#5666
Browse files Browse the repository at this point in the history
[search-box@mtwebster] Fixes linuxmint#5666
  • Loading branch information
claudiux authored Apr 7, 2024
2 parents 70b393d + d059f6e commit 2d6740d
Show file tree
Hide file tree
Showing 16 changed files with 1,567 additions and 565 deletions.
5 changes: 3 additions & 2 deletions search-box@mtwebster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ You can right-click the applet to access additional settings - show or hide the

Installation
================

Install from the Cinnamon settings -> Applets (Download tab, then Manage tab), or:

1) Unzip, copy the folder "search-box@mtwebster" to "~/.local/share/cinnamon/applets/".

2) Enable the applet in cinnamon settings.
2) Enable the applet in Cinnamon settings.
276 changes: 276 additions & 0 deletions search-box@mtwebster/files/search-box@mtwebster/5.4/applet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
const Applet = imports.ui.applet;
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const Clutter = imports.gi.Clutter;
const St = imports.gi.St;
const PopupMenu = imports.ui.popupMenu;
const Main = imports.ui.main;
const Settings = imports.ui.settings;
const GLib = imports.gi.GLib;
const Util = imports.misc.util;

var prov_label = "";
var prov_url = "";

// l10n
const Gettext = imports.gettext;
const UUID = "search-box@mtwebster";
Gettext.bindtextdomain(UUID, GLib.get_home_dir() + "/.local/share/locale");

function _(str) {
return Gettext.dgettext(UUID, str);
}

function SearchBoxApplet(orientation, panel_height, instance_id) {
this._init(orientation, panel_height, instance_id);
}

SearchBoxApplet.prototype = {
__proto__: Applet.TextIconApplet.prototype,

_init: function(orientation, panel_height, instance_id) {
Applet.TextIconApplet.prototype._init.call(this, orientation, panel_height, instance_id);

try {
this.setAllowedLayout(Applet.AllowedLayout.BOTH);

this.settings = new Settings.AppletSettings(this, UUID, instance_id);
this._searchInactiveIcon = new St.Icon({ style_class: "menu-search-entry-icon",
icon_name: "edit-find",
icon_type: St.IconType.SYMBOLIC });
this._searchActiveIcon = new St.Icon({ style_class: "menu-search-entry-icon",
icon_name: "edit-clear",
icon_type: St.IconType.SYMBOLIC });
this.searchIcon = new St.Icon({icon_name: "edit-find", icon_size: 24, icon_type: St.IconType.FULLCOLOR});
this._searchIconClickedId = 0;

this.settings.bind("show-provider", "show_provider", this._reload);
this.settings.bind("selected-provider", "selected_provider", this._reload);
this.already_changed = false;
this.settings.bind("providers", "providers", this._on_providers_changed);
this.settings.bind("reset-after-search", "reset_after_search", this._reload);
this.settings.bind("use-custom-provider", "use_custom_provider");
this.settings.bind("custom-provider-label", "custom_provider_label");
this.settings.bind("custom-provider-url", "custom_provider_url");
this.settings.bind("custom-keybinding", "custom_keybinding", this.on_keybinding_changed);

this.set_applet_icon_symbolic_name("edit-find-symbolic");
this._orientation = orientation;

this.populate_search_engines_menu();
this.set_applet_tooltip(_("Right-click to select or add your search engine"));

this.menuManager = new PopupMenu.PopupMenuManager(this);
this.menu = new Applet.AppletPopupMenu(this, this._orientation);
this.menuManager.addMenu(this.menu);

this._searchArea = new St.BoxLayout({name: "searchArea" });

this.menu.addActor(this._searchArea);

this.searchBox = new St.BoxLayout({ style_class: "menu-search-box" });
this._searchArea.add(this.searchBox);

this.buttonbox = new St.BoxLayout();
let button = new St.Button({ child: this.searchIcon });
button.connect("clicked", Lang.bind(this, this._search));
this.buttonbox.add_actor(button);
this._searchArea.add(this.buttonbox);
this.searchEntry = new St.Entry({ name: "menu-search-entry",
hint_text: _("Type to search..."),
track_hover: true,
can_focus: true });
this.searchEntry.set_secondary_icon(this._searchInactiveIcon);
this.searchBox.add_actor(this.searchEntry);
this.searchActive = false;
this.searchEntryText = this.searchEntry.clutter_text;
this.searchEntryText.connect("text-changed", Lang.bind(this, this._onSearchTextChanged));
this.searchEntryText.connect("key-press-event", Lang.bind(this, this._onMenuKeyPress));
this._previousSearchPattern = "";

this.on_keybinding_changed();
this.update_label_visible();
this._on_providers_changed();
}
catch (e) {
global.logError(e);
}
},

populate_search_engines_menu: function() {
if (this.context_menu_item_search_engines)
this.context_menu_item_search_engines.destroy();

this.context_menu_item_search_engines = new PopupMenu.PopupSubMenuMenuItem(_("Select your Search Engine:"));
this._applet_context_menu.addMenuItem(this.context_menu_item_search_engines);

for (let p of this.providers) {
let item = new PopupMenu.PopupMenuItem(p.name, {
reactive: true
});
item.connect("activate", () => {
this.selected_provider = ""+p.name;
this.update_label_visible();
});
this.context_menu_item_search_engines.menu.addMenuItem(item);
}
this.context_menu_item_search_engines.menu.open();
},

_on_providers_changed: function() {
if (this.already_changed) return;

if (this.custom_provider_label.length > 0 && this.custom_provider_url.length > 0) {
this.providers.push({"name": ""+this.custom_provider_label, "url": ""+this.custom_provider_url});
this.use_custom_provider = false;
this.custom_provider_label = "";
this.custom_provider_url = "";
}

this._reload();
},

_reload: function() {
prov_label = this.selected_provider;
prov_url = "";
for (let p of this.providers) {
if (p["name"] == prov_label) {
prov_url = p["url"];
break
}
}
if (prov_url.length === 0) {
prov_label = "Google";
prov_url = "https://google.com/search?q=";
}

if (this.show_provider) {
this.set_applet_label(prov_label);
this.hide_applet_label(false);
}
else {
this.hide_applet_label(true);
}
this.already_changed = false;
},

_onMenuKeyPress: function(actor, event) {
let symbol = event.get_key_symbol();
if (symbol==Clutter.KEY_Return && this.menu.isOpen) {
this._search();
return true;
}
return false;
},

_search: function() {
var _entry = this.searchEntry.get_text().replace(/'/g,"%27");
let brief = _entry.split(" ", 1)[0];
if (brief.length === 3) {
for (let p of this.providers) {
if (p.short == brief) {
prov_label = p.name;
this.selected_provider = prov_label;
prov_url = p.url;
_entry = _entry.slice(4);
break
}
}
}
Util.spawnCommandLineAsync("xdg-open " + prov_url + "'" + _entry + "'");
if (this.reset_after_search) {
this.searchEntry.set_text("");
this.searchActive = false;
}
this.menu.close();
},

resetSearch: function(){
this.searchEntry.set_text("");
this.searchActive = false;
global.stage.set_key_focus(this.searchEntry);
},

_onSearchTextChanged: function () {
this.searchActive = this.searchEntry.get_text().length > 0; // != "";
if (this.searchActive) {
this.searchEntry.set_secondary_icon(this._searchActiveIcon);

if (this._searchIconClickedId == 0) {
this._searchIconClickedId = this.searchEntry.connect("secondary-icon-clicked",
Lang.bind(this, function() {
this.resetSearch();
}));
}
} else {
if (this._searchIconClickedId > 0)
this.searchEntry.disconnect(this._searchIconClickedId);
this._searchIconClickedId = 0;

this.searchEntry.set_secondary_icon(this._searchInactiveIcon);

}
},

on_keybinding_changed: function() {
Main.keybindingManager.addHotKey("must-be-unique-id", this.custom_keybinding, Lang.bind(this, this.on_hotkey_triggered));
},

on_hotkey_triggered: function() {
this.on_applet_clicked();
},

on_applet_clicked: function(event) {
this.menu.toggle();
global.stage.set_key_focus(this.searchEntry);
},

_onButtonPressEvent: function(actor, event) {
let button = event.get_button();
if (button < 3) {
if (!this._draggable.inhibit) {
return false;
} else {
if (this._applet_context_menu.isOpen) {
this._applet_context_menu.close();
}
}
}

if (button === 1) {
this.on_applet_clicked(event);
} else if (button === 2) {
return false; //this.on_applet_middle_clicked(event);
} else if (button === 3) {
//this.finalizeContextMenu();
if (this._applet_context_menu._getMenuItems().length > 0) {
this._applet_context_menu.toggle();
if (this._applet_context_menu.isOpen) {
this.populate_search_engines_menu();
this.context_menu_item_search_engines.menu.open();
}
}
}

return true;
},

on_orientation_changed: function (orientation) {
this._orientation = orientation;

this.update_label_visible();
},

update_label_visible: function () {
if (this._orientation == St.Side.LEFT || this._orientation == St.Side.RIGHT)
this.hide_applet_label(true);
else
this.hide_applet_label(false);
}

};

function main(metadata, orientation, panel_height, instance_id) {
let searchBoxApplet = new SearchBoxApplet(orientation, panel_height, instance_id);
return searchBoxApplet;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"header-provider" : {
"type" : "header",
"description" : "Search Provider Settings"
},
"show-provider" : {
"type" : "switch",
"default" : true,
"description": "Show provider",
"tooltip": "Show selected provider in the panel"
},
"reset-after-search" : {
"type" : "switch",
"default" : true,
"description": "Reset query after search",
"tooltip": "Reset query after search? This may not be useful if you want to be able to perform the same query on other search engines afterwards."
},
"providers": {
"type": "list",
"description": "Providers",
"height": 300,
"show-buttons": true,
"reorderable": true,
"columns": [
{
"id": "name",
"title": "Name",
"type": "string",
"expand-width": false,
"align": 0.0,
"default": ""
},
{
"id": "url",
"title": "Query URL",
"type": "string",
"expand-width": true,
"align": 0.0,
"default": ""
},
{
"id": "short",
"title": "Short name\n(3 lower-case letters)",
"type": "string",
"expand-width": false,
"align": 0.0,
"default": ""
}
],
"default": [
{"name": "Ask", "url": "https://www.ask.com/web?q=", "short": "ask"},
{"name": "Bing", "url": "https://www.bing.com/search?q=", "short": "bng"},
{"name": "DuckDuckGo", "url": "https://duckduckgo.com/?q=", "short": "ddg"},
{"name": "Google", "url": "https://google.com/search?q=", "short": "ggl"},
{"name": "Lilo", "url": "https://search.lilo.org/?plugin=lilose&q=", "short": "llo"},
{"name": "RefSeek", "url": "https://www.refseek.com/search?q=", "short": "rsk"},
{"name": "Yahoo", "url": "https://search.yahoo.com/search?p=", "short": "yoo"}
],
"tooltip": "If your query begins with the 3 letters of the short name, it will be processed by the relevant search engine.\nExample: ddg linux mint iso"
},
"selected-provider" : {
"type": "generic",
"default" : "Google"
},
"use-custom-provider" : {
"type" : "generic",
"default" : false
},
"custom-provider-label" : {
"type" : "generic",
"default" : ""
},
"custom-provider-url" : {
"type" : "generic",
"default" : ""
},
"header-keybinding" : {
"type" : "header",
"description" : "Keybinding"
},
"custom-keybinding" : {
"type" : "keybinding",
"description" : "Keybinding, which opens the search box",
"default" : "<Primary><Alt>f"
}
}
Loading

0 comments on commit 2d6740d

Please sign in to comment.