Skip to content

Commit

Permalink
nwggrid:
Browse files Browse the repository at this point in the history
  Fix searchbox omitting space
  Call `filter_view` on `signal_search_changed`, not all insertions
  Replace deprecated Gtk::Main with recommended Gtk::Application
  Remove obsolete `search_phrase` field
  Remove unused functions
  • Loading branch information
Siborgium committed Aug 6, 2020
1 parent a7e02ed commit 07fa861
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 88 deletions.
9 changes: 9 additions & 0 deletions common/nwg_classes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ void CommonWindow::check_screen() {
_SUPPORTS_ALPHA = (bool)visual;
}

void CommonWindow::quit() {
auto toplevel = dynamic_cast<Gtk::Window*>(this->get_toplevel());
if (!toplevel) {
std::cerr << "ERROR: Toplevel widget is not a window\n";
std::exit(EXIT_FAILURE);
}
toplevel->get_application()->quit();
}

AppBox::AppBox() {
this -> set_always_show_image(true);
}
Expand Down
1 change: 1 addition & 0 deletions common/nwg_classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CommonWindow : public Gtk::Window {
virtual ~CommonWindow();

void check_screen();
void quit();

protected:
bool on_draw(const ::Cairo::RefPtr< ::Cairo::Context>& cr) override;
Expand Down
6 changes: 2 additions & 4 deletions grid/grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ int main(int argc, char *argv[]) {
/* sort above by the 'name' field */
std::sort(desktop_entries.begin(), desktop_entries.end(), [](auto& a, auto& b) { return a.name < b.name; });

Gtk::Main kit(argc, argv);
auto app = Gtk::Application::create();

auto provider = Gtk::CssProvider::create();
auto display = Gdk::Display::get_default();
Expand All @@ -264,8 +264,6 @@ int main(int argc, char *argv[]) {

MainWindow window;

window.signal_button_press_event().connect(sigc::ptr_fun(&on_window_clicked));

window.show();

/* Detect focused display geometry: {x, y, width, height} */
Expand Down Expand Up @@ -465,7 +463,7 @@ int main(int argc, char *argv[]) {

std::cout << "Time: " << end_ms - start_ms << "ms\n";

Gtk::Main::run(window);
app->run(window);

return 0;
}
17 changes: 11 additions & 6 deletions grid/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,27 @@ class GridBox : public AppBox {
bool pinned;
};

class GridSearch : public Gtk::SearchEntry {
public:
GridSearch();
void prepare_to_insertion();
};

class MainWindow : public CommonWindow {
public:
MainWindow();

Gtk::SearchEntry searchbox; // This will stay insensitive, updated with search_phrase value only
GridSearch searchbox; // Search apps
Gtk::Label label_desc; // To display .desktop entry Comment field at the bottom
Glib::ustring search_phrase; // updated on key_press_event
Gtk::Grid apps_grid; // All application buttons grid
Gtk::Grid favs_grid; // Favourites grid above
Gtk::Grid pinned_grid; // Pinned entries grid above
Gtk::Separator separator; // between favs and all apps
Gtk::Separator separator1; // below pinned
std::list<GridBox> all_boxes {}; // attached to apps_grid unfiltered view
std::list<GridBox*> filtered_boxes {}; // attached to apps_grid filtered view
std::list<GridBox> fav_boxes {}; // attached to favs_grid
std::list<GridBox> pinned_boxes {}; // attached to pinned_grid
std::list<GridBox> all_boxes {}; // attached to apps_grid unfiltered view
std::list<GridBox*> filtered_boxes {}; // attached to apps_grid filtered view
std::list<GridBox> fav_boxes {}; // attached to favs_grid
std::list<GridBox> pinned_boxes {}; // attached to pinned_grid

private:
//Override default signal handler:
Expand Down
73 changes: 43 additions & 30 deletions grid/grid_classes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
#include "grid.h"

MainWindow::MainWindow(): CommonWindow("~nwggrid", "~nwggrid") {
searchbox.set_placeholder_text("Type to search");
searchbox.set_sensitive(false);
searchbox.set_name("searchbox");
search_phrase = "";
searchbox
.signal_search_changed()
.connect(sigc::mem_fun(*this, &MainWindow::filter_view));
apps_grid.set_column_spacing(5);
apps_grid.set_row_spacing(5);
apps_grid.set_column_homogeneous(true);
Expand Down Expand Up @@ -54,34 +53,40 @@ bool MainWindow::on_key_press_event(GdkEventKey* key_event) {
auto key_val = key_event -> keyval;
switch (key_val) {
case GDK_KEY_Escape:
Gtk::Main::quit();
return Gtk::Window::on_key_press_event(key_event);
this->quit();
break;
case GDK_KEY_Delete:
this -> search_phrase = "";
this -> searchbox.set_text(this -> search_phrase);
this -> filter_view();
return true;
this -> searchbox.set_text("");
break;
case GDK_KEY_Return:
case GDK_KEY_Left:
case GDK_KEY_Right:
case GDK_KEY_Up:
case GDK_KEY_Down:
break;
default:
this -> searchbox.handle_event(key_event);
this -> search_phrase = searchbox.get_text();
this -> filter_view();
return true;
// Focus the searchbox:
// because searchbox is now focused,
// Gtk::Window will delegate the event there
if (!this -> searchbox.is_focus()) {
this -> searchbox.grab_focus();
// when searchbox receives focus,
// its contents become selected
// and the incoming character will overwrite them
// so we make sure to drop the selection
this -> searchbox.prepare_to_insertion();
}
}
//if the event has not been handled, call the base class
// Delegate handling to the base class
return Gtk::Window::on_key_press_event(key_event);
}

void MainWindow::filter_view() {
if (this -> search_phrase.size() > 0) {
auto search_phrase = searchbox.get_text();
if (search_phrase.size() > 0) {
this -> filtered_boxes.clear();

auto phrase = this -> search_phrase.casefold();
auto phrase = search_phrase.casefold();
for (auto& box : this -> all_boxes) {
if (box.name.casefold().find(phrase) != Glib::ustring::npos ||
box.exec.casefold().find(phrase) != Glib::ustring::npos ||
Expand Down Expand Up @@ -121,6 +126,10 @@ void MainWindow::rebuild_grid(bool filtered) {
}
cnt++;
}
// Set keyboard focus to the first visible button
if (this -> fav_boxes.size() > 0) {
fav_boxes.front().grab_focus();
}
} else {
for (auto& box : this -> all_boxes) {
this -> apps_grid.attach(box, column, row, 1, 1);
Expand All @@ -132,20 +141,12 @@ void MainWindow::rebuild_grid(bool filtered) {
}
cnt++;
}
}
this -> apps_grid.thaw_child_notify();
// Set keyboard focus to the first visible button
if (this -> favs_grid.is_visible()) {
auto* first = favs_grid.get_child_at(0, 0);
if (first) {
first -> grab_focus();
}
} else {
auto* first = apps_grid.get_child_at(0, 0);
if (first) {
first -> grab_focus();
// Set keyboard focus to the first visible button
if (this -> all_boxes.size() > 0) {
all_boxes.front().grab_focus();
}
}
this -> apps_grid.thaw_child_notify();
}

GridBox::GridBox(Glib::ustring name, Glib::ustring exec, Glib::ustring comment, bool pinned)
Expand Down Expand Up @@ -188,5 +189,17 @@ void GridBox::on_enter() {
void GridBox::on_activate() {
exec.append(" &");
std::system(exec.data());
Gtk::Main::quit();
auto toplevel = dynamic_cast<MainWindow*>(this->get_toplevel());
toplevel->quit();
}

GridSearch::GridSearch() {
set_placeholder_text("Type to search");
set_sensitive(true);
set_name("searchbox");
}

void GridSearch::prepare_to_insertion() {
select_region(0, 0);
set_position(-1);
}
48 changes: 0 additions & 48 deletions grid/grid_tools.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,51 +256,3 @@ std::vector<CacheEntry> get_favourites(ns::json&& cache, int number) {
sorted_cache.erase(from, to);
return sorted_cache;
}

bool on_button_entered(GdkEventCrossing *event, const Glib::ustring& comment) {
(void) event; // suppress warning

description -> set_text(comment);
return true;
}

bool on_button_focused(GdkEventFocus *event, const Glib::ustring& comment) {
(void) event; // suppress warning

description -> set_text(comment);
return true;
}

void on_button_clicked(std::string cmd) {
int clicks = 0;
try {
clicks = cache[cmd];
clicks++;
} catch (...) {
clicks = 1;
}
cache[cmd] = clicks;
save_json(cache, cache_file);

cmd = cmd + " &";
const char *command = cmd.c_str();
std::system(command);

Gtk::Main::quit();
}

bool on_grid_button_press(GdkEventButton *event, const Glib::ustring& exec) {
if (pins && event -> button == 3) {
add_and_save_pinned(exec);
Gtk::Main::quit();
}
return true;
}

bool on_pinned_button_press(GdkEventButton *event, const Glib::ustring& exec) {
if (pins && event -> button == 3) {
remove_and_save_pinned(exec);
Gtk::Main::quit();
}
return true;
}

0 comments on commit 07fa861

Please sign in to comment.