diff --git a/src/Controller/Tab.php b/src/Controller/Tab.php new file mode 100644 index 00000000..9c24da7b --- /dev/null +++ b/src/Controller/Tab.php @@ -0,0 +1,173 @@ +window = $window; + + $this->config = \Yggverse\Yoda\Model\File::getConfig(); + + $this->memory = new \Yggverse\Yoda\Model\Memory(); + + $this->tab = new \Yggverse\Yoda\Entity\Box\Tab(); + + $this->tab->navigation->address->entry->connect( + 'activate', + function ($entry) + { + $this->navigate( + $entry->get_text() + ); + } + ); + + $this->tab->navigation->go->button->connect( + 'released', + function ($entry) + { + $this->navigate( + $this->tab->navigation->address->entry->get_text() + ); + } + ); + + $this->tab->navigation->reload->button->connect( + 'released', + function ($entry) + { + $this->navigate( + $this->tab->navigation->address->entry->get_text() + ); + } + ); + + if ($this->config->homepage) + { + $this->tab->navigation->home->button->connect( + 'released', + function ($entry) + { + $this->tab->navigation->address->entry->set_text( + $this->config->homepage + ); + + $this->navigate( + $this->config->homepage + ); + } + ); + } + + // @TODO back, forward buttons + } + + public function navigate(string $url) + { + $this->tab->tray->label->set_text( + sprintf( + 'Open %s...', + urldecode( + $url + ) + ) + ); + + $start = microtime(true); + + $host = null; + + if ($this->config->resolver->enabled) + { + $address = new \Yggverse\Net\Address( + $url + ); + + $name = $address->getHost(); + + if (!$host = $this->memory->get($name)) + { + $resolve = new \Yggverse\Net\Resolve( + $this->config->resolver->request->record, + $this->config->resolver->request->host, + $this->config->resolver->request->timeout, + $this->config->resolver->result->shuffle + ); + + $resolved = $resolve->address( + $address + ); + + if ($resolved) + { + $host = $resolved->getHost(); + + $this->memory->set( + $name, + $host + ); + } + } + } + + $request = new \Yggverse\Gemini\Client\Request( + $url, + $host + ); + + $raw = $request->getResponse(); + + $end = microtime(true); + + $response = new \Yggverse\Gemini\Client\Response( + $raw + ); + + $this->tab->content->label->set_markup( + $response->getBody() + ); + + $body = new \Yggverse\Gemini\Gemtext\Body( + $response->getBody() + ); + + if ($h1 = $body->getH1()) + { + $this->window->set_title( + sprintf( + '%s - Yoda', + empty($h1[0]) ? $address->getHost() : $h1[0] + ) + ); + } + + $this->tab->tray->label->set_text( + sprintf( + '%s | %s | %d bytes | %s seconds', + date('c'), + $response->getMeta() ? $response->getMeta() : $response->getCode(), + number_format( + mb_strlen( + $raw + ) + ), + round( + $end - $start, 2 + ) + ) + ); + } +} \ No newline at end of file diff --git a/src/Entity/Box/Tab.php b/src/Entity/Box/Tab.php index c4fa155f..b3aea528 100644 --- a/src/Entity/Box/Tab.php +++ b/src/Entity/Box/Tab.php @@ -8,30 +8,14 @@ class Tab { public \GtkBox $box; - public \GtkWindow $window; - public \Yggverse\Yoda\Entity\Box\Menu $menu; public \Yggverse\Yoda\Entity\Box\Navigation $navigation; public \Yggverse\Yoda\Entity\Label\Content $content; public \Yggverse\Yoda\Entity\Label\Tray $tray; - public \Yggverse\Yoda\Model\Memory $memory; - - public object $config; - public function __construct( - \GtkWindow $window, string $name = 'boxTab' ) { - // Init window - $this->window = $window; - - // Init config - $this->config = \Yggverse\Yoda\Model\File::getConfig(); - - // Init memory - $this->memory = new \Yggverse\Yoda\Model\Memory(); - // Init container $this->box = new \GtkBox( \GtkOrientation::VERTICAL @@ -83,152 +67,5 @@ class Tab true, 0 ); - - // Init listeners - $this->navigation->address->entry->connect( - 'activate', - function ($entry) - { - $this->navigate( - $entry->get_text() - ); - } - ); - - $this->navigation->go->button->connect( - 'released', - function ($entry) - { - $this->navigate( - $this->navigation->address->entry->get_text() - ); - } - ); - - $this->navigation->reload->button->connect( - 'released', - function ($entry) - { - $this->navigate( - $this->navigation->address->entry->get_text() - ); - } - ); - - if ($this->config->homepage) - { - $this->navigation->home->button->connect( - 'released', - function ($entry) - { - $this->navigation->address->entry->set_text( - $this->config->homepage - ); - - $this->navigate( - $this->config->homepage - ); - } - ); - } - - // @TODO back, forward buttons - } - - // Actions - public function navigate(string $url) - { - $this->tray->label->set_text( - sprintf( - 'Open %s...', - urldecode( - $url - ) - ) - ); - - $start = microtime(true); - - $host = null; - - if ($this->config->resolver->enabled) - { - $address = new \Yggverse\Net\Address( - $url - ); - - $name = $address->getHost(); - - if (!$host = $this->memory->get($name)) - { - $resolve = new \Yggverse\Net\Resolve( - $this->config->resolver->request->record, - $this->config->resolver->request->host, - $this->config->resolver->request->timeout, - $this->config->resolver->result->shuffle - ); - - $resolved = $resolve->address( - $address - ); - - if ($resolved) - { - $host = $resolved->getHost(); - - $this->memory->set( - $name, - $host - ); - } - } - } - - $request = new \Yggverse\Gemini\Client\Request( - $url, - $host - ); - - $raw = $request->getResponse(); - - $end = microtime(true); - - $response = new \Yggverse\Gemini\Client\Response( - $raw - ); - - $this->content->label->set_markup( - $response->getBody() - ); - - $body = new \Yggverse\Gemini\Gemtext\Body( - $response->getBody() - ); - - if ($h1 = $body->getH1()) - { - $this->window->set_title( - sprintf( - '%s - Yoda', - empty($h1[0]) ? $address->getHost() : $h1[0] - ) - ); - } - - $this->tray->label->set_text( - sprintf( - '%s | %s | %d bytes | %s seconds', - date('c'), - $response->getMeta() ? $response->getMeta() : $response->getCode(), - number_format( - mb_strlen( - $raw - ) - ), - round( - $end - $start, 2 - ) - ) - ); } } \ No newline at end of file diff --git a/src/Yoda.php b/src/Yoda.php index cb91f0c6..0510553c 100644 --- a/src/Yoda.php +++ b/src/Yoda.php @@ -61,12 +61,12 @@ $window->connect( } ); -$tab = new \Yggverse\Yoda\Entity\Box\Tab( +$controller = new \Yggverse\Yoda\Controller\Tab( $window ); $window->add( - $tab->box + $controller->tab->box ); $window->show_all();