From 47dba83d301a176f79eb87ef4393f02d578e2255 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Sat, 17 Apr 2021 23:08:06 +0300 Subject: [PATCH] Browser: Display full UserAgent string in status bar on menu hover This uses the new on_action_enter & on_action_leave APIs to display the full useragent string when hovering over one of the useragent spoof menu options. --- Userland/Applications/Browser/Tab.cpp | 28 ++++++++++++++++++++++++-- Userland/Applications/Browser/Tab.h | 3 +++ Userland/Applications/Browser/main.cpp | 14 +++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 6a15326c59..95aa973d0b 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -486,9 +486,10 @@ Tab::Tab(Type type) m_web_content_view->debug_request("spoof-user-agent", Web::default_user_agent); } }); - m_disable_user_agent_spoofing->set_checked(true); + m_disable_user_agent_spoofing->set_long_text(Web::default_user_agent); spoof_user_agent_menu.add_action(*m_disable_user_agent_spoofing); m_user_agent_spoof_actions.add_action(*m_disable_user_agent_spoofing); + m_disable_user_agent_spoofing->set_checked(true); auto add_user_agent = [&](auto& name, auto& user_agent) { auto action = GUI::Action::create_checkable(name, [&](auto&) { @@ -498,6 +499,7 @@ Tab::Tab(Type type) m_web_content_view->debug_request("spoof-user-agent", user_agent); } }); + action->set_long_text(user_agent); spoof_user_agent_menu.add_action(action); m_user_agent_spoof_actions.add_action(action); }; @@ -508,7 +510,7 @@ Tab::Tab(Type type) add_user_agent("Firefox Android Mobile", "Mozilla/5.0 (Android 11; Mobile; rv:68.0) Gecko/68.0 Firefox/86.0"); add_user_agent("Safari iOS Mobile", "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1"); - auto custom_user_agent = GUI::Action::create_checkable("Custom", [&](auto&) { + auto custom_user_agent = GUI::Action::create_checkable("Custom", [&](auto& action) { String user_agent; if (GUI::InputBox::show(window(), user_agent, "Enter User Agent:", "Custom User Agent") != GUI::InputBox::ExecOK || user_agent.is_empty() || user_agent.is_null()) { m_disable_user_agent_spoofing->activate(); @@ -519,6 +521,7 @@ Tab::Tab(Type type) } else { m_web_content_view->debug_request("spoof-user-agent", user_agent); } + action.set_long_text(user_agent); }); spoof_user_agent_menu.add_action(custom_user_agent); m_user_agent_spoof_actions.add_action(custom_user_agent); @@ -653,4 +656,25 @@ Web::WebViewHooks& Tab::hooks() return *m_web_content_view; } +void Tab::action_entered(GUI::Action& action) +{ + m_user_agent_spoof_actions.for_each_action([&](GUI::Action& user_agent_action) { + if (&action != &user_agent_action) + return IterationDecision::Continue; + if (!user_agent_action.long_text().is_empty()) + m_statusbar->set_override_text(user_agent_action.long_text()); + return IterationDecision::Break; + }); +} + +void Tab::action_left(GUI::Action& action) +{ + m_user_agent_spoof_actions.for_each_action([&](auto& user_agent_action) { + if (&action != &user_agent_action) + return IterationDecision::Continue; + m_statusbar->set_override_text({}); + return IterationDecision::Break; + }); +} + } diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index 9e0add02be..a9d38d48b3 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -67,6 +67,9 @@ public: void did_become_active(); void context_menu_requested(const Gfx::IntPoint& screen_position); + void action_entered(GUI::Action&); + void action_left(GUI::Action&); + Function on_title_change; Function on_tab_open_request; Function on_tab_close_request; diff --git a/Userland/Applications/Browser/main.cpp b/Userland/Applications/Browser/main.cpp index 4f4a73c75a..b99e685f07 100644 --- a/Userland/Applications/Browser/main.cpp +++ b/Userland/Applications/Browser/main.cpp @@ -248,6 +248,20 @@ int main(int argc, char** argv) } } + app->on_action_enter = [&](GUI::Action& action) { + auto* tab = static_cast(tab_widget.active_widget()); + if (!tab) + return; + tab->action_entered(action); + }; + + app->on_action_leave = [&](auto& action) { + auto* tab = static_cast(tab_widget.active_widget()); + if (!tab) + return; + tab->action_left(action); + }; + window_actions.on_create_new_tab = [&] { create_new_tab(Browser::g_home_url, true); };