diff --git a/config.json b/config.json index 02be1cf9..a0a9f9d7 100644 --- a/config.json +++ b/config.json @@ -129,6 +129,31 @@ "length": { "max":1024 + }, + "autocomplete": + { + "enabled":true, + "inline": + { + "completion":true, + "selection":true + }, + "key": + { + "length":1 + }, + "ignore": + { + "keycode": + [ + 111, + 116 + ] + }, + "result": + { + "limit":15 + } } } } diff --git a/src/Entity/Tab/Page.php b/src/Entity/Tab/Page.php index c7d5a752..f1781e19 100644 --- a/src/Entity/Tab/Page.php +++ b/src/Entity/Tab/Page.php @@ -30,6 +30,10 @@ class Page public \GtkProgressBar $progressbar; + public \GtkEntryCompletion $completion; + + public \GtkListStore $suggestion; + public object $config; public function __construct( @@ -202,6 +206,70 @@ class Page 0 ); + // Init autocomplete + if ($this->config->header->entry->request->autocomplete->enabled) + { + $this->completion = new \GtkEntryCompletion(); + + $this->completion->set_inline_completion( + $this->config->header->entry->request->autocomplete->inline->completion + ); + + $this->completion->set_inline_selection( + $this->config->header->entry->request->autocomplete->inline->selection + ); + + $this->completion->set_minimum_key_length( + $this->config->header->entry->request->autocomplete->key->length + ); + + $this->completion->set_text_column( + 0 + ); + + $this->suggestion = new \GtkListStore( + \GObject::TYPE_STRING + ); + + $this->completion->set_model( + $this->suggestion + ); + + $this->request->connect( + 'key-release-event', + function ($entry, $event) + { + if ( + mb_strlen($entry->get_text()) >= $this->config->header->entry->request->autocomplete->key->length + && + isset($event->key->keycode) + && + !in_array( + $event->key->keycode, + $this->config->header->entry->request->autocomplete->ignore->keycode + ) + ) { + $this->suggestion->clear(); + + foreach ($this->app->database->getHistory( + $entry->get_text(), 0, $this->config->header->entry->request->autocomplete->result->limit + ) as $suggestion) + { + $this->suggestion->append( + [ + $suggestion->url + ] + ); + } + + $this->request->set_completion( + $this->completion + ); + } + } + ); + } + // Go button $this->go = \GtkButton::new_with_label( $this->config->header->button->go->label