2323
2424#include < webview/webview.hpp>
2525#include < crl/crl.h>
26- #include < rpl/range .h>
26+ #include < rpl/rpl .h>
2727#include < regex>
2828
2929namespace Webview ::WebKitGTK {
@@ -67,21 +67,16 @@ class Instance final : public Interface, public ::base::has_weak_ptr {
6767
6868 ResolveResult resolve ();
6969
70- bool finishEmbedding () override ;
71-
7270 void navigate (std::string url) override ;
7371 void navigateToData (std::string id) override ;
7472 void reload () override ;
7573
76- void resizeToWindow () override ;
77-
7874 void init (std::string js) override ;
7975 void eval (std::string js) override ;
8076
8177 void focus () override ;
8278
8379 QWidget *widget () override ;
84- void *winId () override ;
8580
8681 auto navigationHistoryState ()
8782 -> rpl::producer<NavigationHistoryState> override ;
@@ -108,10 +103,13 @@ class Instance final : public Interface, public ::base::has_weak_ptr {
108103
109104 void startProcess ();
110105 void stopProcess ();
106+ void updateHistoryStates ();
111107
112108 void registerMasterMethodHandlers ();
113109 void registerHelperMethodHandlers ();
114110
111+ void *winId ();
112+
115113 bool _remoting = false ;
116114 bool _connected = false ;
117115 Master _master;
@@ -134,6 +132,7 @@ class Instance final : public Interface, public ::base::has_weak_ptr {
134132 std::function<bool (std::string,bool )> _navigationStartHandler;
135133 std::function<void (bool )> _navigationDoneHandler;
136134 std::function<DialogResult(DialogArgs)> _dialogHandler;
135+ rpl::variable<NavigationHistoryState> _navigationHistoryState;
137136 bool _loadFailed = false ;
138137
139138};
@@ -350,6 +349,26 @@ bool Instance::create(Config config) {
350349 instance->loadChanged (loadEvent);
351350 }),
352351 this );
352+ g_signal_connect_swapped (
353+ _webview,
354+ " notify::uri" ,
355+ G_CALLBACK (+[](
356+ Instance *instance,
357+ GParamSpec *pspec) -> gboolean {
358+ instance->updateHistoryStates ();
359+ return true ;
360+ }),
361+ this );
362+ g_signal_connect_swapped (
363+ _webview,
364+ " notify::title" ,
365+ G_CALLBACK (+[](
366+ Instance *instance,
367+ GParamSpec *pspec) -> gboolean {
368+ instance->updateHistoryStates ();
369+ return true ;
370+ }),
371+ this );
353372 g_signal_connect_swapped (
354373 _webview,
355374 " decide-policy" ,
@@ -447,6 +466,7 @@ void Instance::loadChanged(WebKitLoadEvent loadEvent) {
447466 _master.call_navigation_done (!_loadFailed, nullptr );
448467 }
449468 }
469+ updateHistoryStates ();
450470}
451471
452472bool Instance::decidePolicy (
@@ -564,10 +584,6 @@ ResolveResult Instance::resolve() {
564584 return Resolve (_wayland);
565585}
566586
567- bool Instance::finishEmbedding () {
568- return true ;
569- }
570-
571587void Instance::navigate (std::string url) {
572588 if (_remoting) {
573589 if (!_helper) {
@@ -651,6 +667,9 @@ void Instance::eval(std::string js) {
651667}
652668
653669void Instance::focus () {
670+ if (const auto widget = _widget.get ()) {
671+ widget->activateWindow ();
672+ }
654673}
655674
656675QWidget *Instance::widget () {
@@ -691,7 +710,7 @@ void *Instance::winId() {
691710
692711auto Instance::navigationHistoryState ()
693712-> rpl::producer<NavigationHistoryState> {
694- return rpl::single ( NavigationHistoryState () );
713+ return _navigationHistoryState. value ( );
695714}
696715
697716void Instance::setOpaqueBg (QColor opaqueBg) {
@@ -728,9 +747,6 @@ void Instance::setOpaqueBg(QColor opaqueBg) {
728747 }
729748}
730749
731- void Instance::resizeToWindow () {
732- }
733-
734750void Instance::startProcess () {
735751 auto loop = GLib::MainLoop::new_ ();
736752
@@ -864,6 +880,17 @@ void Instance::stopProcess() {
864880 }
865881}
866882
883+ void Instance::updateHistoryStates () {
884+ const auto url = webkit_web_view_get_uri (_webview);
885+ const auto title = webkit_web_view_get_title (_webview);
886+ _master.call_navigation_state_update (
887+ url ? url : " " ,
888+ title ? title : " " ,
889+ webkit_web_view_can_go_back (_webview),
890+ webkit_web_view_can_go_forward (_webview),
891+ nullptr );
892+ }
893+
867894void Instance::registerMasterMethodHandlers () {
868895 if (!_master) {
869896 return ;
@@ -970,6 +997,22 @@ void Instance::registerMasterMethodHandlers() {
970997
971998 return true ;
972999 });
1000+
1001+ _master.signal_handle_navigation_state_update ().connect ([=](
1002+ Master,
1003+ Gio::DBusMethodInvocation invocation,
1004+ const std::string &url,
1005+ const std::string &title,
1006+ bool canGoBack,
1007+ bool canGoForward) {
1008+ _navigationHistoryState = NavigationHistoryState{
1009+ .url = url,
1010+ .title = title,
1011+ .canGoBack = canGoBack,
1012+ .canGoForward = canGoForward,
1013+ };
1014+ return true ;
1015+ });
9731016}
9741017
9751018int Instance::exec () {
0 commit comments