Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert key switches case sensitivity in nwgdmenu #59

Merged
merged 2 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions dmenu/dmenu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ std::vector<Glib::ustring> all_commands {};

bool dmenu_run = false;
bool show_searchbox = true;
bool case_sensitive = true;

int main(int argc, char *argv[]) {
std::string custom_css_file {"style.css"};

pid_t pid = getpid();
std::string mypid = std::to_string(pid);

std::string pid_file = "/var/run/user/" + std::to_string(getuid()) + "/nwgdmenu.pid";

int saved_pid {};
if (std::ifstream(pid_file)) {
try {
Expand Down Expand Up @@ -70,11 +71,11 @@ int main(int argc, char *argv[]) {

// We will build dmenu out of commands found in $PATH if nothing has been passed by stdin
dmenu_run = isatty(fileno(stdin)) == 1;

if (input.cmdOptionExists("-run")){
dmenu_run = true;
}

// Otherwise let's build from stdin input
if (!dmenu_run) {
all_commands = {};
Expand Down Expand Up @@ -293,7 +294,7 @@ int main(int argc, char *argv[]) {
break;
}
}

menu.set_reserve_toggle_size(false);
menu.set_property("width_request", (int)(w / 8));

Expand Down
2 changes: 2 additions & 0 deletions dmenu/dmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <fstream>
#include <filesystem>
#include <regex>
#include <algorithm>

#include <gtkmm.h>
#include <glibmm/ustring.h>
Expand All @@ -36,6 +37,7 @@ extern std::vector<Glib::ustring> all_commands;

extern bool dmenu_run;
extern bool show_searchbox;
extern bool case_sensitive;

class DMenu : public Gtk::Menu {
public:
Expand Down
46 changes: 41 additions & 5 deletions dmenu/dmenu_classes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ bool Anchor::on_focus_in_event(GdkEventFocus* focus_event) {
}

DMenu::DMenu() {
searchbox.set_text("Type to search");
if (case_sensitive) {
searchbox.set_text("Type To Search");
} else {
searchbox.set_text("TYPE TO SEARCH");
}
searchbox.set_sensitive(false);
searchbox.set_name("searchbox");
search_phrase = "";
Expand All @@ -71,6 +75,9 @@ bool DMenu::on_key_press_event(GdkEventKey* key_event) {
&& key_event->type == GDK_KEY_PRESS) {

char character = key_event -> keyval;
if (!case_sensitive) {
character = toupper(character);
}
this -> search_phrase += character;

this -> searchbox.set_text(this -> search_phrase);
Expand All @@ -87,6 +94,11 @@ bool DMenu::on_key_press_event(GdkEventKey* key_event) {
this -> searchbox.set_text(this -> search_phrase);
this -> filter_view();
return true;
} else if (key_event -> keyval == GDK_KEY_Insert) {
this -> search_phrase = "";
case_sensitive = !case_sensitive;
this -> filter_view();
return true;
}
}
//if the event has not been handled, call the base class
Expand Down Expand Up @@ -116,7 +128,17 @@ void DMenu::filter_view() {
int cnt = 0;
bool limit_exhausted = false;
for (Glib::ustring command : all_commands) {
if (command.find(this -> search_phrase) == 0) {
std::string sf = this -> search_phrase;
std::string cm = command;
if (!case_sensitive) {
for(unsigned int l = 0; l < sf.length(); l++) {
sf[l] = toupper(sf[l]);
}
for(unsigned int l = 0; l < cm.length(); l++) {
cm[l] = toupper(cm[l]);
}
}
if (cm.find(sf) == 0) {
Gtk::MenuItem *item = new Gtk::MenuItem();
item -> set_label(command);
item -> signal_activate().connect(sigc::bind<Glib::ustring>(sigc::mem_fun
Expand All @@ -131,11 +153,21 @@ void DMenu::filter_view() {
}
if (!limit_exhausted) {
for (Glib::ustring command : all_commands) {
if (command.find(this -> search_phrase) != std::string::npos && command.find(this -> search_phrase) != 0) {
std::string sf = this -> search_phrase;
std::string cm = command;
if (!case_sensitive) {
for(unsigned int l = 0; l < sf.length(); l++) {
sf[l] = toupper(sf[l]);
}
for(unsigned int l = 0; l < cm.length(); l++) {
cm[l] = toupper(cm[l]);
}
}
if (cm.find(sf) != std::string::npos && cm.find(sf) != 0) {
Gtk::MenuItem *item = new Gtk::MenuItem();
item -> set_label(command);
item -> signal_activate().connect(sigc::bind<Glib::ustring>(sigc::mem_fun
(*this, &DMenu::on_item_clicked), command));
(*this, &DMenu::on_item_clicked), cm));
this -> append(*item);
cnt++;
if (cnt > rows - 1) {
Expand All @@ -147,7 +179,11 @@ void DMenu::filter_view() {
this -> show_all();

} else {
this -> searchbox.set_text("Type to search");
if (case_sensitive) {
this -> searchbox.set_text("Type To Search");
} else {
this -> searchbox.set_text("TYPE TO SEARCH");
}
// remove all items except searchbox
for (auto item : this -> get_children()) {
if (item -> get_name() != "search_item") {
Expand Down