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,22 +67,18 @@ 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
81+ void refreshNavigationHistoryState () override ;
8682 auto navigationHistoryState ()
8783 -> rpl::producer<NavigationHistoryState> override ;
8884
@@ -108,10 +104,13 @@ class Instance final : public Interface, public ::base::has_weak_ptr {
108104
109105 void startProcess ();
110106 void stopProcess ();
107+ void updateHistoryStates ();
111108
112109 void registerMasterMethodHandlers ();
113110 void registerHelperMethodHandlers ();
114111
112+ void *winId ();
113+
115114 bool _remoting = false ;
116115 bool _connected = false ;
117116 Master _master;
@@ -134,6 +133,7 @@ class Instance final : public Interface, public ::base::has_weak_ptr {
134133 std::function<bool (std::string,bool )> _navigationStartHandler;
135134 std::function<void (bool )> _navigationDoneHandler;
136135 std::function<DialogResult(DialogArgs)> _dialogHandler;
136+ rpl::variable<NavigationHistoryState> _navigationHistoryState;
137137 bool _loadFailed = false ;
138138
139139};
@@ -350,6 +350,26 @@ bool Instance::create(Config config) {
350350 instance->loadChanged (loadEvent);
351351 }),
352352 this );
353+ g_signal_connect_swapped (
354+ _webview,
355+ " notify::uri" ,
356+ G_CALLBACK (+[](
357+ Instance *instance,
358+ GParamSpec *pspec) -> gboolean {
359+ instance->updateHistoryStates ();
360+ return true ;
361+ }),
362+ this );
363+ g_signal_connect_swapped (
364+ _webview,
365+ " notify::title" ,
366+ G_CALLBACK (+[](
367+ Instance *instance,
368+ GParamSpec *pspec) -> gboolean {
369+ instance->updateHistoryStates ();
370+ return true ;
371+ }),
372+ this );
353373 g_signal_connect_swapped (
354374 _webview,
355375 " decide-policy" ,
@@ -447,6 +467,7 @@ void Instance::loadChanged(WebKitLoadEvent loadEvent) {
447467 _master.call_navigation_done (!_loadFailed, nullptr );
448468 }
449469 }
470+ updateHistoryStates ();
450471}
451472
452473bool Instance::decidePolicy (
@@ -564,10 +585,6 @@ ResolveResult Instance::resolve() {
564585 return Resolve (_wayland);
565586}
566587
567- bool Instance::finishEmbedding () {
568- return true ;
569- }
570-
571588void Instance::navigate (std::string url) {
572589 if (_remoting) {
573590 if (!_helper) {
@@ -651,6 +668,9 @@ void Instance::eval(std::string js) {
651668}
652669
653670void Instance::focus () {
671+ if (const auto widget = _widget.get ()) {
672+ widget->activateWindow ();
673+ }
654674}
655675
656676QWidget *Instance::widget () {
@@ -689,9 +709,13 @@ void *Instance::winId() {
689709 return reinterpret_cast <void *>(gtk_plug_get_id (GTK_PLUG (_window)));
690710}
691711
712+ void Instance::refreshNavigationHistoryState () {
713+ // Not needed here, there are events.
714+ }
715+
692716auto Instance::navigationHistoryState ()
693717-> rpl::producer<NavigationHistoryState> {
694- return rpl::single ( NavigationHistoryState () );
718+ return _navigationHistoryState. value ( );
695719}
696720
697721void Instance::setOpaqueBg (QColor opaqueBg) {
@@ -728,9 +752,6 @@ void Instance::setOpaqueBg(QColor opaqueBg) {
728752 }
729753}
730754
731- void Instance::resizeToWindow () {
732- }
733-
734755void Instance::startProcess () {
735756 auto loop = GLib::MainLoop::new_ ();
736757
@@ -864,6 +885,17 @@ void Instance::stopProcess() {
864885 }
865886}
866887
888+ void Instance::updateHistoryStates () {
889+ const auto url = webkit_web_view_get_uri (_webview);
890+ const auto title = webkit_web_view_get_title (_webview);
891+ _master.call_navigation_state_update (
892+ url ? url : " " ,
893+ title ? title : " " ,
894+ webkit_web_view_can_go_back (_webview),
895+ webkit_web_view_can_go_forward (_webview),
896+ nullptr );
897+ }
898+
867899void Instance::registerMasterMethodHandlers () {
868900 if (!_master) {
869901 return ;
@@ -970,6 +1002,22 @@ void Instance::registerMasterMethodHandlers() {
9701002
9711003 return true ;
9721004 });
1005+
1006+ _master.signal_handle_navigation_state_update ().connect ([=](
1007+ Master,
1008+ Gio::DBusMethodInvocation invocation,
1009+ const std::string &url,
1010+ const std::string &title,
1011+ bool canGoBack,
1012+ bool canGoForward) {
1013+ _navigationHistoryState = NavigationHistoryState{
1014+ .url = url,
1015+ .title = title,
1016+ .canGoBack = canGoBack,
1017+ .canGoForward = canGoForward,
1018+ };
1019+ return true ;
1020+ });
9731021}
9741022
9751023int Instance::exec () {
0 commit comments