From 970f8f6497326bceabe9c236ea5dac7192d0d89f Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 14 Apr 2024 21:18:42 +0300 Subject: [PATCH] implement history actions tray, add history search feature --- config.json | 30 ++++++ src/Entity/Tab/History.php | 194 +++++++++++++++++++++++++++++++++++-- src/Model/Database.php | 13 ++- 3 files changed, 225 insertions(+), 12 deletions(-) diff --git a/config.json b/config.json index 83c07625..7b2b28b1 100644 --- a/config.json +++ b/config.json @@ -142,6 +142,36 @@ "time": { "format":"c" + }, + "header": + { + "margin":8, + "button": + { + "open": + { + "visible":true, + "label":"Open" + }, + "clear": + { + "visible":true, + "label":"Clear" + }, + "go": + { + "visible":true, + "label":"Go" + } + }, + "search": + { + "placeholder":"Search in history..." + } + }, + "body": + { + "margin":8 } } } diff --git a/src/Entity/Tab/History.php b/src/Entity/Tab/History.php index 5c4bf926..9cb2e309 100644 --- a/src/Entity/Tab/History.php +++ b/src/Entity/Tab/History.php @@ -8,7 +8,16 @@ class History { public \Yggverse\Yoda\Entity\App $app; - public \GtkBox $box; + public \GtkBox $box, + $header, + $body; + + public \GtkButton $open, + $clear, + $go; + + public \GtkEntry $search; + public \GtkListStore $list; public \GtkTreeView $treeview; public \GtkScrolledWindow $container; @@ -24,6 +33,129 @@ class History // Init config $this->config = \Yggverse\Yoda\Model\File::getConfig()->app->tab->history; + // Compose header + $this->header = new \GtkBox( + \GtkOrientation::HORIZONTAL + ); + + $this->header->set_margin_top( + $this->config->header->margin + ); + + $this->header->set_margin_bottom( + $this->config->header->margin + ); + + $this->header->set_margin_start( + $this->config->header->margin + ); + + $this->header->set_margin_end( + $this->config->header->margin + ); + + $this->header->set_spacing( + $this->config->header->margin + ); + + // Open button + $this->open = \GtkButton::new_with_label( + $this->config->header->button->open->label + ); + + $this->open->set_sensitive( + false + ); + + $this->open->connect( + 'clicked', + function () + { + // @TODO + + $this->refresh(); + } + ); + + if ($this->config->header->button->open->visible) + { + $this->header->add( + $this->open + ); + } + + // Clear button + $this->clear = \GtkButton::new_with_label( + $this->config->header->button->clear->label + ); + + $this->clear->set_sensitive( + false + ); + + $this->clear->connect( + 'clicked', + function () + { + // @TODO + + $this->refresh(); + } + ); + + if ($this->config->header->button->clear->visible) + { + $this->header->add( + $this->clear + ); + } + + // Search field + $this->search = new \GtkEntry; + + $this->search->set_placeholder_text( + $this->config->header->search->placeholder + ); + + $this->search->connect( + 'activate', + function ($entry) + { + $this->refresh( + $entry->get_text() + ); + } + ); + + $this->header->pack_start( + $this->search, + true, + true, + 0 + ); + + // Go button + $this->go = \GtkButton::new_with_label( + $this->config->header->button->go->label + ); + + $this->go->connect( + 'clicked', + function () + { + $this->refresh( + $this->search->get_text() + ); + } + ); + + if ($this->config->header->button->go->visible) + { + $this->header->add( + $this->go + ); + } + // Build history list $this->treeview = new \GtkTreeView(); @@ -45,7 +177,7 @@ class History ) ); - // Create list storage + // Init list storage $this->list = new \GtkListStore( \GObject::TYPE_STRING, \GObject::TYPE_STRING @@ -55,11 +187,8 @@ class History $this->list ); - // Build history list from database records - $this->refresh(); - - // Compose page - $this->box = new \GtkBox( + // Compose body + $this->body = new \GtkBox( \GtkOrientation::VERTICAL ); @@ -69,19 +198,64 @@ class History $this->treeview ); - $this->box->pack_start( + $this->body->set_margin_start( + $this->config->body->margin + ); + + $this->body->pack_start( $this->container, true, true, 0 ); + + // Compose page + $this->box = new \GtkBox( + \GtkOrientation::VERTICAL + ); + + $this->box->add( + $this->header + ); + + $this->box->pack_start( + $this->body, + true, + true, + 0 + ); + + // Refresh history + $this->refresh(); + + // Activate events + $this->treeview->connect( + 'row-activated', + function () + { + // @TODO + } + ); } - public function refresh(): void + public function refresh( + string $search = '' + ): void { + // Reset previous state $this->list->clear(); - foreach ($this->app->database->getHistory() as $record) + // Update buttons sensibility + $this->open->set_sensitive( + false + ); + + $this->clear->set_sensitive( + false + ); + + // Build history list from database records + foreach ($this->app->database->getHistory($search) as $record) { $this->list->append( [ diff --git a/src/Model/Database.php b/src/Model/Database.php index 1c4be565..d2c4efac 100644 --- a/src/Model/Database.php +++ b/src/Model/Database.php @@ -74,17 +74,26 @@ class Database } public function getHistory( + string $search = '', int $start = 0, int $limit = 1000 ): array { - $query = $this->_database->query( + $query = $this->_database->prepare( sprintf( - 'SELECT * FROM `history` ORDER BY `id` DESC LIMIT %d,%d', + 'SELECT * FROM `history` WHERE `url` LIKE :search ORDER BY `id` DESC LIMIT %d,%d', $start, $limit ) + ); + $query->execute( + [ + ':search' => sprintf( + '%%%s%%', + $search + ) + ] ); return $query->fetchAll();