summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/InProcessWebView.cpp
blob: d5afb5c005c3b1655280ef6f70db68230fb9452b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/URL.h>
#include <LibCore/File.h>
#include <LibCore/MimeData.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/Clipboard.h>
#include <LibGUI/InputBox.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGUI/Scrollbar.h>
#include <LibGUI/Window.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/Parser/HTMLDocumentParser.h>
#include <LibWeb/InProcessWebView.h>
#include <LibWeb/Layout/InitialContainingBlockBox.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/Page/Frame.h>
#include <LibWeb/Painting/PaintContext.h>
#include <LibWeb/UIEvents/MouseEvent.h>

REGISTER_WIDGET(Web, InProcessWebView)

namespace Web {

InProcessWebView::InProcessWebView()
    : m_page(make<Page>(*this))
{
    set_should_hide_unnecessary_scrollbars(true);
    set_background_role(ColorRole::Base);
    set_focus_policy(GUI::FocusPolicy::StrongFocus);

    m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) {
        GUI::Clipboard::the().set_plain_text(selected_text());
    });

    m_select_all_action = GUI::CommonActions::make_select_all_action([this](auto&) {
        select_all();
    });
}

InProcessWebView::~InProcessWebView()
{
}

void InProcessWebView::select_all()
{
    auto* layout_root = this->layout_root();
    if (!layout_root)
        return;

    const Layout::Node* first_layout_node = layout_root;

    for (;;) {
        auto* next = first_layout_node->next_in_pre_order();
        if (!next)
            break;
        first_layout_node = next;
        if (is<Layout::TextNode>(*first_layout_node))
            break;
    }

    const Layout::Node* last_layout_node = first_layout_node;

    for (const Layout::Node* layout_node = first_layout_node; layout_node; layout_node = layout_node->next_in_pre_order()) {
        if (is<Layout::TextNode>(*layout_node))
            last_layout_node = layout_node;
    }

    VERIFY(first_layout_node);
    VERIFY(last_layout_node);

    int last_layout_node_index_in_node = 0;
    if (is<Layout::TextNode>(*last_layout_node))
        last_layout_node_index_in_node = downcast<Layout::TextNode>(*last_layout_node).text_for_rendering().length() - 1;

    layout_root->set_selection({ { first_layout_node, 0 }, { last_layout_node, last_layout_node_index_in_node } });
    update();
}

String InProcessWebView::selected_text() const
{
    return page().focused_frame().selected_text();
}

void InProcessWebView::page_did_layout()
{
    VERIFY(layout_root());
    set_content_size(layout_root()->size().to_type<int>());
}

void InProcessWebView::page_did_change_title(const String& title)
{
    if (on_title_change)
        on_title_change(title);
}

void InProcessWebView::page_did_set_document_in_main_frame(DOM::Document* document)
{
    if (on_set_document)
        on_set_document(document);
    layout_and_sync_size();
    scroll_to_top();
    update();
}

void InProcessWebView::page_did_start_loading(const URL& url)
{
    if (on_load_start)
        on_load_start(url);
}

void InProcessWebView::page_did_finish_loading(const URL& url)
{
    if (on_load_finish)
        on_load_finish(url);
}

void InProcessWebView::page_did_change_selection()
{
    update();
}

void InProcessWebView::page_did_request_cursor_change(Gfx::StandardCursor cursor)
{
    set_override_cursor(cursor);
}

void InProcessWebView::page_did_request_context_menu(const Gfx::IntPoint& content_position)
{
    if (on_context_menu_request)
        on_context_menu_request(screen_relative_rect().location().translated(to_widget_position(content_position)));
}

void InProcessWebView::page_did_request_link_context_menu(const Gfx::IntPoint& content_position, const URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers)
{
    if (on_link_context_menu_request)
        on_link_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)));
}

void InProcessWebView::page_did_request_image_context_menu(const Gfx::IntPoint& content_position, const URL& url, [[maybe_unused]] const String& target, [[maybe_unused]] unsigned modifiers, const Gfx::Bitmap* bitmap)
{
    if (!on_image_context_menu_request)
        return;
    Gfx::ShareableBitmap shareable_bitmap;
    if (bitmap)
        shareable_bitmap = Gfx::ShareableBitmap(*bitmap);
    on_image_context_menu_request(url, screen_relative_rect().location().translated(to_widget_position(content_position)), shareable_bitmap);
}

void InProcessWebView::page_did_click_link(const URL& url, const String& target, unsigned modifiers)
{
    if (on_link_click)
        on_link_click(url, target, modifiers);
}

void InProcessWebView::page_did_middle_click_link(const URL& url, const String& target, unsigned modifiers)
{
    if (on_link_middle_click)
        on_link_middle_click(url, target, modifiers);
}

void InProcessWebView::page_did_enter_tooltip_area([[maybe_unused]] const Gfx::IntPoint& content_position, const String& title)
{
    GUI::Application::the()->show_tooltip(title, nullptr);
}

void InProcessWebView::page_did_leave_tooltip_area()
{
    GUI::Application::the()->hide_tooltip();
}

void InProcessWebView::page_did_hover_link(const URL& url)
{
    if (on_link_hover)
        on_link_hover(url);
}

void InProcessWebView::page_did_unhover_link()
{
    if (on_link_hover)
        on_link_hover({});
}

void InProcessWebView::page_did_invalidate(const Gfx::IntRect&)
{
    update();
}

void InProcessWebView::page_did_change_favicon(const Gfx::Bitmap& bitmap)
{
    if (on_favicon_change)
        on_favicon_change(bitmap);
}

void InProcessWebView::layout_and_sync_size()
{
    if (!document())
        return;

    bool had_vertical_scrollbar = vertical_scrollbar().is_visible();
    bool had_horizontal_scrollbar = horizontal_scrollbar().is_visible();

    page().main_frame().set_size(available_size());
    set_content_size(layout_root()->size().to_type<int>());

    // NOTE: If layout caused us to gain or lose scrollbars, we have to lay out again
    //       since the scrollbars now take up some of the available space.
    if (had_vertical_scrollbar != vertical_scrollbar().is_visible() || had_horizontal_scrollbar != horizontal_scrollbar().is_visible()) {
        page().main_frame().set_size(available_size());
        set_content_size(layout_root()->size().to_type<int>());
    }

    page().main_frame().set_viewport_scroll_offset({ horizontal_scrollbar().value(), vertical_scrollbar().value() });
}

void InProcessWebView::resize_event(GUI::ResizeEvent& event)
{
    GUI::ScrollableWidget::resize_event(event);
    layout_and_sync_size();
}

void InProcessWebView::paint_event(GUI::PaintEvent& event)
{
    GUI::Frame::paint_event(event);

    GUI::Painter painter(*this);
    painter.add_clip_rect(widget_inner_rect());
    painter.add_clip_rect(event.rect());

    if (!layout_root()) {
        painter.fill_rect(event.rect(), palette().color(background_role()));
        return;
    }

    painter.translate(frame_thickness(), frame_thickness());

    PaintContext context(painter, palette(), { horizontal_scrollbar().value(), vertical_scrollbar().value() });
    context.set_should_show_line_box_borders(m_should_show_line_box_borders);
    context.set_viewport_rect(viewport_rect_in_content_coordinates());
    context.set_has_focus(is_focused());
    layout_root()->paint_all_phases(context);
}

void InProcessWebView::mousemove_event(GUI::MouseEvent& event)
{
    page().handle_mousemove(to_content_position(event.position()), event.buttons(), event.modifiers());
    GUI::ScrollableWidget::mousemove_event(event);
}

void InProcessWebView::mousedown_event(GUI::MouseEvent& event)
{
    page().handle_mousedown(to_content_position(event.position()), event.button(), event.modifiers());
    GUI::ScrollableWidget::mousedown_event(event);
}

void InProcessWebView::mouseup_event(GUI::MouseEvent& event)
{
    page().handle_mouseup(to_content_position(event.position()), event.button(), event.modifiers());
    GUI::ScrollableWidget::mouseup_event(event);
}

void InProcessWebView::mousewheel_event(GUI::MouseEvent& event)
{
    page().handle_mousewheel(to_content_position(event.position()), event.button(), event.modifiers(), event.wheel_delta());
    GUI::ScrollableWidget::mousewheel_event(event);
}

void InProcessWebView::keydown_event(GUI::KeyEvent& event)
{
    bool page_accepted_event = page().handle_keydown(event.key(), event.modifiers(), event.code_point());

    if (event.modifiers() == 0) {
        switch (event.key()) {
        case Key_Home:
            vertical_scrollbar().set_value(0);
            break;
        case Key_End:
            vertical_scrollbar().set_value(vertical_scrollbar().max());
            break;
        case Key_Down:
            vertical_scrollbar().set_value(vertical_scrollbar().value() + vertical_scrollbar().step());
            break;
        case Key_Up:
            vertical_scrollbar().set_value(vertical_scrollbar().value() - vertical_scrollbar().step());
            break;
        case Key_Left:
            horizontal_scrollbar().set_value(horizontal_scrollbar().value() + horizontal_scrollbar().step());
            break;
        case Key_Right:
            horizontal_scrollbar().set_value(horizontal_scrollbar().value() - horizontal_scrollbar().step());
            break;
        case Key_PageDown:
            vertical_scrollbar().set_value(vertical_scrollbar().value() + frame_inner_rect().height());
            break;
        case Key_PageUp:
            vertical_scrollbar().set_value(vertical_scrollbar().value() - frame_inner_rect().height());
            break;
        default:
            if (!page_accepted_event) {
                ScrollableWidget::keydown_event(event);
                return;
            }
            break;
        }
    }

    event.accept();
}

URL InProcessWebView::url() const
{
    if (!page().main_frame().document())
        return {};
    return page().main_frame().document()->url();
}

void InProcessWebView::reload()
{
    load(url());
}

void InProcessWebView::load_html(const StringView& html, const URL& url)
{
    page().main_frame().loader().load_html(html, url);
}

bool InProcessWebView::load(const URL& url)
{
    set_override_cursor(Gfx::StandardCursor::None);
    return page().main_frame().loader().load(url, FrameLoader::Type::Navigation);
}

const Layout::InitialContainingBlockBox* InProcessWebView::layout_root() const
{
    return document() ? document()->layout_node() : nullptr;
}

Layout::InitialContainingBlockBox* InProcessWebView::layout_root()
{
    if (!document())
        return nullptr;
    return const_cast<Layout::InitialContainingBlockBox*>(document()->layout_node());
}

void InProcessWebView::page_did_request_scroll_into_view(const Gfx::IntRect& rect)
{
    scroll_into_view(rect, true, true);
    set_override_cursor(Gfx::StandardCursor::None);
}

void InProcessWebView::load_empty_document()
{
    page().main_frame().set_document(nullptr);
}

DOM::Document* InProcessWebView::document()
{
    return page().main_frame().document();
}

const DOM::Document* InProcessWebView::document() const
{
    return page().main_frame().document();
}

void InProcessWebView::set_document(DOM::Document* document)
{
    page().main_frame().set_document(document);
}

void InProcessWebView::did_scroll()
{
    page().main_frame().set_viewport_scroll_offset({ horizontal_scrollbar().value(), vertical_scrollbar().value() });
}

void InProcessWebView::drop_event(GUI::DropEvent& event)
{
    if (event.mime_data().has_urls()) {
        if (on_url_drop) {
            on_url_drop(event.mime_data().urls().first());
            return;
        }
    }
    ScrollableWidget::drop_event(event);
}

void InProcessWebView::page_did_request_alert(const String& message)
{
    GUI::MessageBox::show(window(), message, "Alert", GUI::MessageBox::Type::Information);
}

bool InProcessWebView::page_did_request_confirm(const String& message)
{
    auto confirm_result = GUI::MessageBox::show(window(), message, "Confirm", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::OKCancel);
    return confirm_result == GUI::Dialog::ExecResult::ExecOK;
}

String InProcessWebView::page_did_request_prompt(const String& message, const String& default_)
{
    String value { default_ };
    if (GUI::InputBox::show(window(), value, message, "Prompt") == GUI::InputBox::ExecOK)
        return value;
    return {};
}

String InProcessWebView::page_did_request_cookie(const URL& url, Cookie::Source source)
{
    if (on_get_cookie)
        return on_get_cookie(url, source);
    return {};
}

void InProcessWebView::page_did_set_cookie(const URL& url, const Cookie::ParsedCookie& cookie, Cookie::Source source)
{
    if (on_set_cookie)
        on_set_cookie(url, cookie, source);
}

}