diff --git a/src/Abstract/Entity/Browser/Container/Page/Content/Markup.php b/src/Abstract/Entity/Browser/Container/Page/Content/Markup.php new file mode 100644 index 00000000..9596d1aa --- /dev/null +++ b/src/Abstract/Entity/Browser/Container/Page/Content/Markup.php @@ -0,0 +1,77 @@ +content = $content; + + // Init markup label + $this->gtk = new \GtkLabel; + + $this->gtk->set_use_markup( + true + ); + + $this->gtk->set_selectable( + true + ); + + $this->gtk->set_can_focus( + false + ); + + $this->gtk->set_track_visited_links( + true + ); + + $this->gtk->set_xalign( + 0 + ); + + $this->gtk->set_yalign( + 0 + ); + + $this->gtk->show(); + + // Init events + $this->gtk->connect( + 'activate-link', + function( + \GtkLabel $label, + string $href + ) { + $this->_onActivateLink( + $label, + $href + ); + } + ); + } + + abstract protected function _onActivateLink( + \GtkLabel $label, + string $href + ); + + abstract public function setSource( + string $value + ): void; +} diff --git a/src/Entity/Browser/Container/Page.php b/src/Entity/Browser/Container/Page.php index 25f039c2..1af5df71 100644 --- a/src/Entity/Browser/Container/Page.php +++ b/src/Entity/Browser/Container/Page.php @@ -11,7 +11,6 @@ use \Yggverse\Yoda\Entity\Browser\Container\Page\Content; use \Yggverse\Yoda\Entity\Browser\Container\Page\Response; use \Yggverse\Yoda\Model\Connection; -use \Yggverse\Yoda\Model\Filesystem; class Page { @@ -200,57 +199,10 @@ class Page ); // Update content - switch ($connection->getMime()) - { - case Filesystem::MIME_TEXT_GEMINI: - - $title = null; - - $this->content->setGemtext( - (string) $connection->getData(), - $title - ); - - if ($title) - { - $this->title->setValue( - $title - ); - } - - break; - - case Filesystem::MIME_TEXT_PLAIN: - - $this->content->setPlain( - (string) $connection->getData() - ); - - break; - - /* @TODO - case 'image/gif': - case 'image/jpeg': - case 'image/png': - case 'image/webp': - - $this->content->setImage( - (string) $connection->getData() - ); - - break; - */ - - default: - - $this->title->setValue( - _('Oops!') - ); - - $this->content->setPlain( - _('MIME type not supported') - ); - } + $this->content->set( + $connection->getData(), + $connection->getMime() + ); // Hide progressbar $this->progressbar->hide(); @@ -271,7 +223,7 @@ class Page ); // Update content - $this->content->setGemtext( + $this->content->set( _('Response time reached') ); diff --git a/src/Entity/Browser/Container/Page/Content.php b/src/Entity/Browser/Container/Page/Content.php index 560ee9f0..151a42dc 100644 --- a/src/Entity/Browser/Container/Page/Content.php +++ b/src/Entity/Browser/Container/Page/Content.php @@ -4,29 +4,37 @@ declare(strict_types=1); namespace Yggverse\Yoda\Entity\Browser\Container\Page; -use \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Data; +use \Yggverse\Yoda\Entity\Browser\Container\Page; + +use \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Gemtext; +use \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Image; +use \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Plain; use \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Viewport; +use \Yggverse\Yoda\Model\Filesystem; + class Content { public \GtkScrolledWindow $gtk; // Dependencies - public \Yggverse\Yoda\Entity\Browser\Container\Page $page; + public Page $page; // Requirements - public \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Data $data; - public \Yggverse\Yoda\Entity\Browser\Container\Page\Content\Viewport $viewport; + public Viewport $viewport; // Defaults private int $_margin = 8; + // Extras + private ?string $_source = null; + public function __construct( - \Yggverse\Yoda\Entity\Browser\Container\Page $page + Page $page ) { $this->page = $page; - // Init container + // Init scrolled window container $this->gtk = new \GtkScrolledWindow; $this->gtk->set_margin_start( @@ -41,21 +49,11 @@ class Content $this->_margin ); - // Init viewport - // to integrate scrolled window features for data label + // Init scrolled window viewport $this->viewport = new Viewport( $this ); - // Init data label - $this->data = new Data( - $this - ); - - $this->viewport->gtk->add( - $this->data->gtk - ); - $this->gtk->add( $this->viewport->gtk ); @@ -64,28 +62,73 @@ class Content $this->gtk->show(); } - public function refresh() + public function set( + ?string $data, + ?string $mime + ): void { - // @TODO + $this->_source = $data; + + switch ($mime) + { + case Filesystem::MIME_TEXT_GEMINI: + + $document = new Gemtext( + $this + ); + + $document->setSource( + $data + ); + + break; + + case Filesystem::MIME_TEXT_PLAIN: + + $document = new Plain( + $this + ); + + $document->setSource( + $data + ); + + break; + + /* @TODO + case 'image/gif': + case 'image/jpeg': + case 'image/png': + case 'image/webp': + + break; + */ + + default: + + $document = new Plain( + $this + ); + + $document->setSource( + _('MIME type not supported') + ); + } + + $this->viewport->gtk->add( + $document->gtk + ); + + //$this->gtk->show_all(); } - public function setGemtext( - ?string $data = null, - ?string &$title = null - ): void + public function getSource(): ?string { - $this->data->setGemtext( - $data, - $title - ); + return $this->_source; } - public function setPlain( - ?string $data = null - ): void + public function refresh() { - $this->data->setPlain( - $data - ); + // @TODO } } \ No newline at end of file diff --git a/src/Entity/Browser/Container/Page/Content/Data.php b/src/Entity/Browser/Container/Page/Content/Gemtext.php similarity index 77% rename from src/Entity/Browser/Container/Page/Content/Data.php rename to src/Entity/Browser/Container/Page/Content/Gemtext.php index a2d063f2..0ff24b6a 100644 --- a/src/Entity/Browser/Container/Page/Content/Data.php +++ b/src/Entity/Browser/Container/Page/Content/Gemtext.php @@ -7,115 +7,16 @@ namespace Yggverse\Yoda\Entity\Browser\Container\Page\Content; use \Yggverse\Gemtext\Document; use \Yggverse\Net\Address; -class Data +class Gemtext extends \Yggverse\Yoda\Abstract\Entity\Browser\Container\Page\Content\Markup { - public \GtkLabel $gtk; - - // Extras - public ?string $raw = null; - - // Dependencies - public \Yggverse\Yoda\Entity\Browser\Container\Page\Content $content; - - // Defaults - private int $_wrap = 140; - - public function __construct( - \Yggverse\Yoda\Entity\Browser\Container\Page\Content $content - ) { - // Init dependency - $this->content = $content; - - // Init markup label - $this->gtk = new \GtkLabel; - - $this->gtk->set_use_markup( - true - ); - - $this->gtk->set_selectable( - true - ); - - $this->gtk->set_can_focus( - false - ); - - $this->gtk->set_track_visited_links( - true - ); - - $this->gtk->set_xalign( - 0 - ); - - $this->gtk->set_yalign( - 0 - ); - - // Render - $this->gtk->show(); - - // Init events - $this->gtk->connect( - 'activate-link', - function( - \GtkLabel $label, - string $href - ) { - // Format URL - $url = $this->_url( - $href - ); - - // Update request entry - $this->content->page->navbar->request->setValue( - $this->_url( - $href - ) - ); - - // Update page - $this->content->page->update(); - - // Prevent propagation for supported protocols - if (in_array( - parse_url( - $url, - PHP_URL_SCHEME - ), - [ - 'nex', - 'gemini', - 'file' - ]) - ) return true; - } - ); - } - - public function setPlain( - string $value - ): void - { - $this->gtk->set_markup( - sprintf( - '%s', - htmlspecialchars( - $value - ) - ) - ); - - $this->raw = $value; - } - - public function setGemtext( + public function setSource( string $value, string | null &$title = null, bool $preformatted = false ): void { + $this->_source = $value; + $document = new Document( $value ); @@ -330,8 +231,39 @@ class Data $line ) ); + } + + protected function _onActivateLink( + \GtkLabel $label, + string $href + ) { + // Format URL + $url = $this->_url( + $href + ); + + // Update request entry + $this->content->page->navbar->request->setValue( + $this->_url( + $href + ) + ); - $this->raw = $value; + // Update page + $this->content->page->update(); + + // Prevent propagation for supported protocols + if (in_array( + parse_url( + $url, + PHP_URL_SCHEME + ), + [ + 'nex', + 'gemini', + 'file' + ]) + ) return true; } private function _wrap( diff --git a/src/Entity/Browser/Container/Page/Content/Plain.php b/src/Entity/Browser/Container/Page/Content/Plain.php new file mode 100644 index 00000000..9bfcda93 --- /dev/null +++ b/src/Entity/Browser/Container/Page/Content/Plain.php @@ -0,0 +1,24 @@ +_source = $value; + + $this->gtk->set_markup( + sprintf( + '%s', + htmlspecialchars( + $value + ) + ) + ); + } +} \ No newline at end of file diff --git a/src/Entity/Browser/Menu/File/Save.php b/src/Entity/Browser/Menu/File/Save.php index f6f5ce1f..0153f186 100644 --- a/src/Entity/Browser/Menu/File/Save.php +++ b/src/Entity/Browser/Menu/File/Save.php @@ -62,7 +62,7 @@ class Save { file_put_contents( $dialog->get_filename(), - $page->content->data->raw + $page->content->getSource() ); } }