mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-13 18:00:40 +00:00
Browser: Open links with target="_blank" in new tab
This commit is contained in:
parent
061205b3b3
commit
896decd4d5
Notes:
sideshowbarker
2024-07-19 07:20:43 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/896decd4d5c Pull-request: https://github.com/SerenityOS/serenity/pull/1939
3 changed files with 28 additions and 21 deletions
|
@ -133,11 +133,16 @@ Tab::Tab()
|
||||||
update_bookmark_button(url.to_string());
|
update_bookmark_button(url.to_string());
|
||||||
};
|
};
|
||||||
|
|
||||||
m_html_widget->on_link_click = [this](auto& url, auto&) {
|
m_html_widget->on_link_click = [this](auto& href, auto& target) {
|
||||||
if (url.starts_with("#")) {
|
if (href.starts_with("#")) {
|
||||||
m_html_widget->scroll_to_anchor(url.substring_view(1, url.length() - 1));
|
auto anchor = href.substring_view(1, href.length() - 1);
|
||||||
|
m_html_widget->scroll_to_anchor(anchor);
|
||||||
} else {
|
} else {
|
||||||
m_html_widget->load(m_html_widget->document()->complete_url(url));
|
auto url = m_html_widget->document()->complete_url(href);
|
||||||
|
if (target == "_blank")
|
||||||
|
on_tab_open_request(url);
|
||||||
|
else
|
||||||
|
m_html_widget->load(url);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -44,6 +44,7 @@ public:
|
||||||
void did_become_active();
|
void did_become_active();
|
||||||
|
|
||||||
Function<void(String)> on_title_change;
|
Function<void(String)> on_title_change;
|
||||||
|
Function<void(URL&)> on_tab_open_request;
|
||||||
Function<void(Tab&)> on_tab_close_request;
|
Function<void(Tab&)> on_tab_close_request;
|
||||||
|
|
||||||
const String& title() const { return m_title; }
|
const String& title() const { return m_title; }
|
||||||
|
|
|
@ -24,9 +24,9 @@
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "WindowActions.h"
|
|
||||||
#include "InspectorWidget.h"
|
#include "InspectorWidget.h"
|
||||||
#include "Tab.h"
|
#include "Tab.h"
|
||||||
|
#include "WindowActions.h"
|
||||||
#include <LibCore/File.h>
|
#include <LibCore/File.h>
|
||||||
#include <LibGUI/Application.h>
|
#include <LibGUI/Application.h>
|
||||||
#include <LibGUI/BoxLayout.h>
|
#include <LibGUI/BoxLayout.h>
|
||||||
|
@ -75,6 +75,8 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
auto window = GUI::Window::construct();
|
auto window = GUI::Window::construct();
|
||||||
window->set_rect(100, 100, 640, 480);
|
window->set_rect(100, 100, 640, 480);
|
||||||
|
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
|
||||||
|
window->set_title("Browser");
|
||||||
|
|
||||||
auto& widget = window->set_main_widget<GUI::Widget>();
|
auto& widget = window->set_main_widget<GUI::Widget>();
|
||||||
widget.set_fill_with_background_color(true);
|
widget.set_fill_with_background_color(true);
|
||||||
|
@ -91,7 +93,8 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
Browser::WindowActions window_actions(*window);
|
Browser::WindowActions window_actions(*window);
|
||||||
|
|
||||||
auto create_new_tab = [&](bool activate = true) {
|
Function<void(URL url, bool activate)> create_new_tab;
|
||||||
|
create_new_tab = [&](auto url, auto activate) {
|
||||||
auto& new_tab = tab_widget.add_tab<Browser::Tab>("New tab");
|
auto& new_tab = tab_widget.add_tab<Browser::Tab>("New tab");
|
||||||
|
|
||||||
new_tab.on_title_change = [&](auto title) {
|
new_tab.on_title_change = [&](auto title) {
|
||||||
|
@ -100,6 +103,10 @@ int main(int argc, char** argv)
|
||||||
window->set_title(String::format("%s - Browser", title.characters()));
|
window->set_title(String::format("%s - Browser", title.characters()));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
new_tab.on_tab_open_request = [&](auto& url) {
|
||||||
|
create_new_tab(url, true);
|
||||||
|
};
|
||||||
|
|
||||||
new_tab.on_tab_close_request = [&](auto& tab) {
|
new_tab.on_tab_close_request = [&](auto& tab) {
|
||||||
tab_widget.deferred_invoke([&](auto&) {
|
tab_widget.deferred_invoke([&](auto&) {
|
||||||
tab_widget.remove_tab(tab);
|
tab_widget.remove_tab(tab);
|
||||||
|
@ -108,27 +115,20 @@ int main(int argc, char** argv)
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
|
new_tab.load(url);
|
||||||
|
|
||||||
window->set_title("Browser");
|
dbg() << "Added new tab " << &new_tab << ", loading " << url;
|
||||||
window->show();
|
|
||||||
|
|
||||||
URL url_to_load = home_url;
|
|
||||||
|
|
||||||
if (app.args().size() >= 1) {
|
|
||||||
url_to_load = URL::create_with_url_or_path(app.args()[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
new_tab.load(url_to_load);
|
|
||||||
|
|
||||||
dbg() << "Added new tab " << &new_tab << ", loading " << url_to_load;
|
|
||||||
|
|
||||||
if (activate)
|
if (activate)
|
||||||
tab_widget.set_active_widget(&new_tab);
|
tab_widget.set_active_widget(&new_tab);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
URL default_url = home_url;
|
||||||
|
if (app.args().size() >= 1)
|
||||||
|
default_url = URL::create_with_url_or_path(app.args()[0]);
|
||||||
|
|
||||||
window_actions.on_create_new_tab = [&] {
|
window_actions.on_create_new_tab = [&] {
|
||||||
create_new_tab();
|
create_new_tab(default_url, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
window_actions.on_next_tab = [&] {
|
window_actions.on_next_tab = [&] {
|
||||||
|
@ -139,7 +139,8 @@ int main(int argc, char** argv)
|
||||||
tab_widget.activate_previous_tab();
|
tab_widget.activate_previous_tab();
|
||||||
};
|
};
|
||||||
|
|
||||||
create_new_tab();
|
create_new_tab(default_url, true);
|
||||||
|
window->show();
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue