Skip to content

Truly independent web browser

LibWeb+WebContent+UI/Qt: Add a picker for date and time inputs

Andreas Klingee1b53568a2f
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index 9d79dc085bd..6d8ec6c1777 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -459,6 +459,11 @@ static void show_the_picker_if_applicable(HTMLInputElement& element)
auto weak_element = GC::Weak<HTMLInputElement> { element };
element.set_is_open(true);
element.document().page().did_request_color_picker(weak_element, element.value_as_color());
+ } else if (element.has_date_picker()) {
+ auto weak_element = GC::Weak<HTMLInputElement> { element };
+ element.set_is_open(true);
+ element.document().page().did_request_date_picker(weak_element, element.type().to_utf16_string(), element.value(),
+ element.get_attribute(AttributeNames::min).value_or({}), element.get_attribute(AttributeNames::max).value_or({}));
}
}
}
@@ -1228,7 +1233,7 @@ void HTMLInputElement::create_text_input_shadow_tree()
MUST(shadow_root->append_child(element));
auto text_container = element;
- if (type_state() == TypeAttributeState::Number) {
+ if (type_state() == TypeAttributeState::Number || has_date_picker()) {
text_container = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
{
static auto& style = *new GC::Root<CSS::CSSStyleProperties>;
@@ -1351,6 +1356,36 @@ void HTMLInputElement::create_text_input_shadow_tree()
m_down_button_element->add_event_listener_without_options(UIEvents::EventNames::mouseup, DOM::IDLEventListener::create(mouseup_callback));
}
+ if (has_date_picker()) {
+ // NB: The picker indicator opens the picker, as the field itself is for typing a value.
+ auto picker_indicator = MUST(DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML));
+ picker_indicator->set_attribute_value(HTML::AttributeNames::style, "display: flex; align-items: center; padding-inline-start: 4px; opacity: 0.7; cursor: default;"_utf16);
+ MUST(element->append_child(picker_indicator));
+
+ auto icon = MUST(DOM::create_element(document(), SVG::TagNames::svg, Namespace::SVG));
+ icon->set_attribute_value(HTML::AttributeNames::style, "width: 1em; height: 1em;"_utf16);
+ icon->set_attribute_value(SVG::AttributeNames::xmlns, Namespace::SVG.to_utf16_string());
+ icon->set_attribute_value(SVG::AttributeNames::viewBox, "0 0 24 24"_utf16);
+ MUST(picker_indicator->append_child(icon));
+
+ auto icon_path = MUST(DOM::create_element(document(), SVG::TagNames::path, Namespace::SVG));
+ icon_path->set_attribute_value(SVG::AttributeNames::fill, "currentColor"_utf16);
+ if (type_state() == TypeAttributeState::Time)
+ icon_path->set_attribute_value(SVG::AttributeNames::d, "M12,2A10,10 0 1,0 22,12A10,10 0 0,0 12,2M12,20A8,8 0 1,1 20,12A8,8 0 0,1 12,20M12.5,7H11V13L16.2,16.2L17,14.9L12.5,12.2V7Z"_utf16);
+ else
+ icon_path->set_attribute_value(SVG::AttributeNames::d, "M19,4H18V2H16V4H8V2H6V4H5A2,2 0 0,0 3,6V20A2,2 0 0,0 5,22H19A2,2 0 0,0 21,20V6A2,2 0 0,0 19,4M19,20H5V10H19V20M19,8H5V6H19V8M7,12H12V17H7V12Z"_utf16);
+ MUST(icon->append_child(icon_path));
+
+ auto open_picker_function = JS::NativeFunction::create(
+ HTML::relevant_realm(*this), [this](JS::VM&) {
+ show_the_picker_if_applicable(*this);
+ return JS::js_undefined();
+ },
+ 0, Utf16FlyString {}, &HTML::relevant_realm(*this));
+ auto open_picker_callback = GC::Heap::the().allocate<WebIDL::CallbackType>(*open_picker_function, HTML::relevant_realm(*this));
+ picker_indicator->add_event_listener_without_options(UIEvents::EventNames::click, DOM::IDLEventListener::create(open_picker_callback));
+ }
+
update_text_input_shadow_tree();
}
@@ -4350,7 +4385,44 @@ bool HTMLInputElement::supports_a_picker() const
// The input element can support a picker. A picker is a user interface element that allows the end user to choose a value.
// Whether an input element supports a picker depends on the type attribute state and implementation-defined behavior.
// An input element must support a picker when its type attribute is in the File Upload state.
- return first_is_one_of(type_state(), TypeAttributeState::FileUpload, TypeAttributeState::Color);
+ return first_is_one_of(type_state(), TypeAttributeState::FileUpload, TypeAttributeState::Color) || has_date_picker();
+}
+
+bool HTMLInputElement::has_date_picker() const
+{
+ return first_is_one_of(type_state(), TypeAttributeState::Date, TypeAttributeState::Time, TypeAttributeState::LocalDateAndTime, TypeAttributeState::Month, TypeAttributeState::Week);
+}
+
+void HTMLInputElement::did_pick_date(Optional<Utf16String> picked_value)
+{
+ set_is_open(false);
+ if (!has_date_picker() || !picked_value.has_value() || !is_mutable())
+ return;
+
+ auto old_value = m_value;
+ // then when the user changes the element's value
+ m_dirty_value = true;
+ m_value = value_sanitization_algorithm(*picked_value);
+ if (m_value == old_value)
+ return;
+ if (m_text_node) {
+ m_text_node->set_data(m_value);
+ update_placeholder_visibility();
+ }
+ relevant_value_was_changed();
+
+ // the user agent must queue an element task on the user interaction task source
+ user_interaction_did_change_input_value();
+
+ // https://html.spec.whatwg.org/multipage/input.html#common-input-element-events
+ // [...] any time the user commits the change, [...] set its user validity to true
+ m_user_validity = true;
+ CSS::Invalidation::invalidate_style_after_validity_change(*this);
+
+ // and fire an event named change at the input element, with the bubbles attribute initialized to true.
+ auto change_event = create_event_for_element(*this, HTML::EventNames::change);
+ change_event->set_bubbles(true);
+ dispatch_event(*change_event);
}
void HTMLInputElement::set_is_open(bool is_open)
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.h b/Libraries/LibWeb/HTML/HTMLInputElement.h
index 8ba934aa57f..af405894ee4 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.h
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.h
@@ -125,6 +125,8 @@ public:
void suggestions_source_element_did_change();
void did_pick_color(Optional<Color> picked_color, ColorPickerUpdateState state);
+ void did_pick_date(Optional<Utf16String> picked_value);
+ bool has_date_picker() const;
enum class MultipleHandling {
Replace,
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 50311bcdb1e..dc88b121e16 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -1315,6 +1315,37 @@ void Page::color_picker_update(Optional<Color> picked_color, HTML::ColorPickerUp
}
}
+void Page::did_request_date_picker(GC::Weak<HTML::HTMLInputElement> target, Utf16String input_type, Utf16String value, Utf16String minimum, Utf16String maximum)
+{
+ if (m_pending_non_blocking_dialog == PendingNonBlockingDialog::None) {
+ m_pending_non_blocking_dialog = PendingNonBlockingDialog::DatePicker;
+ m_pending_non_blocking_dialog_target = move(target);
+ m_pending_date_picker_input_type = input_type;
+
+ m_client->page_did_request_date_picker(move(input_type), move(value), move(minimum), move(maximum));
+ } else if (target && target.ptr() != m_pending_non_blocking_dialog_target.ptr()) {
+ // NB: Another dialog is showing, so no picker opens for this input.
+ target->set_is_open(false);
+ }
+}
+
+void Page::date_picker_closed(Optional<Utf16String> picked_value)
+{
+ if (m_pending_non_blocking_dialog != PendingNonBlockingDialog::DatePicker)
+ return;
+ m_pending_non_blocking_dialog = PendingNonBlockingDialog::None;
+ auto requested_input_type = move(m_pending_date_picker_input_type);
+
+ if (m_pending_non_blocking_dialog_target) {
+ auto& input_element = as<HTML::HTMLInputElement>(*m_pending_non_blocking_dialog_target);
+ m_pending_non_blocking_dialog_target = nullptr;
+ // NB: A picker opened for another type of input gives a value that does not apply to this one.
+ if (input_element.type() != requested_input_type)
+ picked_value = {};
+ input_element.did_pick_date(move(picked_value));
+ }
+}
+
void Page::did_request_file_picker(GC::Weak<HTML::HTMLInputElement> target, HTML::FileFilter const& accepted_file_types, HTML::AllowMultipleFiles allow_multiple_files)
{
if (m_pending_non_blocking_dialog == PendingNonBlockingDialog::None) {
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index 767b9aa574f..1a15e661acb 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -306,6 +306,9 @@ public:
void did_request_color_picker(GC::Weak<HTML::HTMLInputElement> target, Color current_color);
void color_picker_update(Optional<Color> picked_color, HTML::ColorPickerUpdateState state);
+ void did_request_date_picker(GC::Weak<HTML::HTMLInputElement> target, Utf16String input_type, Utf16String value, Utf16String minimum, Utf16String maximum);
+ void date_picker_closed(Optional<Utf16String> picked_value);
+
void did_request_file_picker(GC::Weak<HTML::HTMLInputElement> target, HTML::FileFilter const& accepted_file_types, HTML::AllowMultipleFiles);
void file_picker_closed(Span<HTML::SelectedFile> selected_files);
@@ -367,6 +370,7 @@ public:
enum class PendingNonBlockingDialog {
None,
ColorPicker,
+ DatePicker,
FilePicker,
Select,
};
@@ -562,6 +566,7 @@ private:
PendingNonBlockingDialog m_pending_non_blocking_dialog { PendingNonBlockingDialog::None };
GC::Weak<HTML::HTMLElement> m_pending_non_blocking_dialog_target;
+ Utf16String m_pending_date_picker_input_type;
HashMap<u64, ClipboardRequest> m_pending_clipboard_requests;
u64 m_next_clipboard_request_id { 0 };
@@ -876,6 +881,7 @@ public:
// https://html.spec.whatwg.org/multipage/input.html#show-the-picker,-if-applicable
virtual void page_did_request_color_picker([[maybe_unused]] Color current_color) { }
+ virtual void page_did_request_date_picker([[maybe_unused]] Utf16String input_type, [[maybe_unused]] Utf16String value, [[maybe_unused]] Utf16String minimum, [[maybe_unused]] Utf16String maximum) { }
virtual void page_did_request_file_picker([[maybe_unused]] HTML::FileFilter const& accepted_file_types, Web::HTML::AllowMultipleFiles) { }
virtual void page_did_request_select_dropdown([[maybe_unused]] HTML::CrossProcessId local_root_id, [[maybe_unused]] Web::CSSPixelPoint content_position, [[maybe_unused]] Web::CSSPixels minimum_width, [[maybe_unused]] Vector<Web::HTML::SelectItem> items) { }
virtual void page_did_request_geolocation_position([[maybe_unused]] u64 request_id, [[maybe_unused]] HTML::CrossProcessId requesting_document_id) { }
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index a43ca394fe1..f558be2ee5a 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -2761,6 +2761,23 @@ void ViewImplementation::color_picker_update(Optional<Color> picked_color, Web::
target->async_color_picker_update(picked_color, state);
}
+void ViewImplementation::did_request_date_picker(Badge<WebContentPage>, WebContentPage& requesting_page, Utf16String input_type, Utf16String value, Utf16String minimum, Utf16String maximum)
+{
+ if (another_page_awaits(m_date_picker_page, requesting_page) || !on_request_date_picker) {
+ requesting_page.async_date_picker_closed({});
+ return;
+ }
+ m_date_picker_page = requesting_page;
+ on_request_date_picker(input_type, value, minimum, maximum);
+}
+
+void ViewImplementation::date_picker_closed(Optional<Utf16String> picked_value)
+{
+ NonnullRefPtr<WebContentPage> target = m_date_picker_page ? *m_date_picker_page : page();
+ m_date_picker_page.clear();
+ target->async_date_picker_closed(move(picked_value));
+}
+
void ViewImplementation::did_request_file_picker(Badge<WebContentPage>, WebContentPage& requesting_page, Web::HTML::FileFilter const& accepted_file_types, Web::HTML::AllowMultipleFiles allow_multiple_files)
{
if (another_page_awaits(m_file_picker_page, requesting_page)) {
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index e2ef1061806..76ced77f644 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -448,6 +448,7 @@ public:
void confirm_closed(bool accepted);
void prompt_closed(Optional<Utf16String> const& response);
void color_picker_update(Optional<Color> picked_color, Web::HTML::ColorPickerUpdateState state);
+ void date_picker_closed(Optional<Utf16String> picked_value);
void file_picker_closed(Vector<Web::HTML::SelectedFile> selected_files);
void select_dropdown_closed(Optional<u32> const& selected_item_id);
@@ -620,6 +621,7 @@ public:
Function<void()> on_fullscreen_window;
Function<void()> on_exit_fullscreen_window;
Function<void(Color current_color)> on_request_color_picker;
+ Function<void(Utf16String const& input_type, Utf16String const& value, Utf16String const& minimum, Utf16String const& maximum)> on_request_date_picker;
Function<void(WebContentPage& requesting_page, u64 request_id, Web::HTML::CrossProcessId document_id)> on_request_geolocation_position;
Function<void(WebContentPage& requesting_page, u64 request_id)> on_cancel_geolocation_position_request;
Function<void(WebContentPage& requesting_page, u64 request_id, Web::HTML::CrossProcessId document_id)> on_start_geolocation_position_watch;
@@ -673,6 +675,7 @@ public:
void send_to_media_context_menu_page(Function<void(WebContentPage&)> const&);
void did_request_color_picker(Badge<WebContentPage>, WebContentPage& requesting_page, Color current_color);
+ void did_request_date_picker(Badge<WebContentPage>, WebContentPage& requesting_page, Utf16String input_type, Utf16String value, Utf16String minimum, Utf16String maximum);
void did_request_file_picker(Badge<WebContentPage>, WebContentPage& requesting_page, Web::HTML::FileFilter const& accepted_file_types, Web::HTML::AllowMultipleFiles);
void did_request_select_dropdown(Badge<WebContentPage>, WebContentPage& requesting_page, Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> items);
@@ -1134,6 +1137,7 @@ protected:
Web::ViewportIsFullscreen m_is_fullscreen { Web::ViewportIsFullscreen::No };
RefPtr<WebContentPage> m_color_picker_page;
+ RefPtr<WebContentPage> m_date_picker_page;
RefPtr<WebContentPage> m_file_picker_page;
RefPtr<WebContentPage> m_select_dropdown_page;
RefPtr<WebContentPage> m_media_context_menu_page;
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 25d6e1a4661..d3f845ecb0e 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -1613,6 +1613,11 @@ void WebContentPage::did_request_color_picker(Color current_color)
view().did_request_color_picker({}, *this, current_color);
}
+void WebContentPage::did_request_date_picker(Utf16String input_type, Utf16String value, Utf16String minimum, Utf16String maximum)
+{
+ view().did_request_date_picker({}, *this, move(input_type), move(value), move(minimum), move(maximum));
+}
+
static Optional<Web::Permissions::PermissionName> permission_name_from(Utf16String const& name)
{
if (name == "geolocation"_utf16)
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index f3722883013..1bd3e14897c 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -276,6 +276,7 @@ private:
virtual void did_change_picture_in_picture_player_controls_visibility(bool shown) override;
virtual void did_request_file(ByteString path, i32 request_id) override;
virtual void did_request_color_picker(Color current_color) override;
+ virtual void did_request_date_picker(Utf16String input_type, Utf16String value, Utf16String minimum, Utf16String maximum) override;
virtual void did_request_geolocation_position(u64 request_id, Web::HTML::CrossProcessId document_id) override;
virtual void did_cancel_geolocation_position_request(u64 request_id) override;
virtual void did_start_geolocation_position_watch(u64 request_id, Web::HTML::CrossProcessId document_id) override;
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index 11bdbfdc7ec..de03dc22146 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -3454,6 +3454,12 @@ void ConnectionFromClient::color_picker_update(Web::PageId page_id, Optional<Col
page->page().color_picker_update(picked_color, state);
}
+void ConnectionFromClient::date_picker_closed(Web::PageId page_id, Optional<Utf16String> picked_value)
+{
+ if (auto page = this->page(page_id); page.has_value())
+ page->page().date_picker_closed(move(picked_value));
+}
+
void ConnectionFromClient::file_picker_closed(Web::PageId page_id, Vector<Web::HTML::SelectedFile> selected_files)
{
if (auto page = this->page(page_id); page.has_value())
diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.h
index eb64b86df37..fb3f77cbb4d 100644
--- a/Services/WebContent/ConnectionFromClient.h
+++ b/Services/WebContent/ConnectionFromClient.h
@@ -286,6 +286,7 @@ private:
virtual void confirm_closed(Web::PageId page_id, bool accepted) override;
virtual void prompt_closed(Web::PageId page_id, Optional<Utf16String> response) override;
virtual void color_picker_update(Web::PageId page_id, Optional<Color> picked_color, Web::HTML::ColorPickerUpdateState state) override;
+ virtual void date_picker_closed(Web::PageId page_id, Optional<Utf16String> picked_value) override;
virtual void file_picker_closed(Web::PageId page_id, Vector<Web::HTML::SelectedFile> selected_files) override;
virtual void select_dropdown_closed(Web::PageId page_id, Optional<u32> selected_item_id) override;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 6fef8477297..0d9db56dc84 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -1985,6 +1985,11 @@ void PageClient::page_did_request_color_picker(Color current_color)
client().async_did_request_color_picker(m_id, current_color);
}
+void PageClient::page_did_request_date_picker(Utf16String input_type, Utf16String value, Utf16String minimum, Utf16String maximum)
+{
+ client().async_did_request_date_picker(m_id, move(input_type), move(value), move(minimum), move(maximum));
+}
+
void PageClient::page_did_request_file_picker(Web::HTML::FileFilter const& accepted_file_types, Web::HTML::AllowMultipleFiles allow_multiple_files)
{
client().async_did_request_file_picker(m_id, accepted_file_types, allow_multiple_files);
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index 4728095b554..a6ea298aca6 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -347,6 +347,7 @@ private:
virtual void page_did_retain_blob_url_token(Web::HTML::CrossProcessId navigable_id, URL::BlobURLEntry::Token) override;
virtual Optional<Web::FileAPI::SerializedBlobURLEntry> page_did_request_blob_url_entry(Utf16String const& url, Optional<URL::BlobURLEntry::Token> token) override;
virtual void page_did_request_color_picker(Color current_color) override;
+ virtual void page_did_request_date_picker(Utf16String input_type, Utf16String value, Utf16String minimum, Utf16String maximum) override;
virtual void page_did_request_file_picker(Web::HTML::FileFilter const& accepted_file_types, Web::HTML::AllowMultipleFiles) override;
virtual void page_did_request_select_dropdown(Web::HTML::CrossProcessId local_root_id, Web::CSSPixelPoint content_position, Web::CSSPixels minimum_width, Vector<Web::HTML::SelectItem> items) override;
virtual void page_did_request_geolocation_position(u64 request_id, Web::HTML::CrossProcessId requesting_document_id) override;
diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipc
index 0e93a547149..7e8fcb5508a 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -234,6 +234,7 @@ endpoint WebContentClient routed by Web::PageId page_id
did_change_picture_in_picture_player_controls_visibility(Web::PageId page_id, bool shown) =|
did_request_file(Web::PageId page_id, ByteString path, i32 request_id) =|
did_request_color_picker(Web::PageId page_id, Color current_color) =|
+ did_request_date_picker(Web::PageId page_id, Utf16String input_type, Utf16String value, Utf16String minimum, Utf16String maximum) =|
did_request_geolocation_position(Web::PageId page_id, u64 request_id, Web::HTML::CrossProcessId document_id) =|
did_cancel_geolocation_position_request(Web::PageId page_id, u64 request_id) =|
did_start_geolocation_position_watch(Web::PageId page_id, u64 request_id, Web::HTML::CrossProcessId document_id) =|
diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipc
index ef9237c35ba..ec64cc0dbae 100644
--- a/Services/WebContent/WebContentServer.ipc
+++ b/Services/WebContent/WebContentServer.ipc
@@ -293,6 +293,7 @@ endpoint WebContentServer routed by Web::PageId page_id
confirm_closed(Web::PageId page_id, bool accepted) =|
prompt_closed(Web::PageId page_id, Optional<Utf16String> response) =|
color_picker_update(Web::PageId page_id, Optional<Color> picked_color, Web::HTML::ColorPickerUpdateState state) =|
+ date_picker_closed(Web::PageId page_id, Optional<Utf16String> picked_value) =|
file_picker_closed(Web::PageId page_id, Vector<Web::HTML::SelectedFile> selected_files) =|
select_dropdown_closed(Web::PageId page_id, Optional<u32> selected_item_id) =|
diff --git a/Tests/LibWeb/Layout/expected/input-time.txt b/Tests/LibWeb/Layout/expected/input-time.txt
index 8a5172e7d72..2e0552c3f88 100644
--- a/Tests/LibWeb/Layout/expected/input-time.txt
+++ b/Tests/LibWeb/Layout/expected/input-time.txt
@@ -4,10 +4,15 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
frag 0 from TextInputBox start: 0, length: 0, rect: [13,11 240x20] baseline: 19
TextInputBox <input> at [13,11] inline-block [0+2+3 240 3+2+0] [0+2+1 20 1+2+0] [BFC] children: not-inline
Box <div> at [13,11] flex-container(row) [0+0+0 240 0+0+0] [0+0+0 20 0+0+0] [FFC] children: not-inline
- BlockContainer <div> at [13,11] flex-item [0+0+0 240 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 0, rect: [13,11 0x20] baseline: 16
- TextNode <#text> (not painted)
+ Box <div> at [13,11] flex-container(row) flex-item [0+0+0 216 0+0+0] [0+0+0 20 0+0+0] [FFC] children: not-inline
+ BlockContainer <div> at [13,11] flex-item [0+0+0 216 0+0+0] [0+0+0 20 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 0, rect: [13,11 0x20] baseline: 16
+ TextNode <#text> (not painted)
+ Box <div> at [233,11] flex-container(row) flex-item [0+0+4 20 0+0+0] [0+0+0 20 0+0+0] [FFC] children: not-inline
+ SVGSVGBox <svg> at [233,11] flex-item [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] viewport-transform=[0.8333333 0 0 0.8333333 0 0] [SVG] children: not-inline
+ SVGGeometryBox <path> at [2,2] [0+0+0 20 0+0+0] [0+0+0 20 0+0+0] children: not-inline
TextNode <#text> (not painted)
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
SC for BlockContainer<HTML> [0,0 800x42] (z-index: auto)
+ SC for Box<DIV> [233,11 20x20] (z-index: auto)
diff --git a/Tests/LibWeb/Text/expected/HTML/input-date-picker-dismissed.txt b/Tests/LibWeb/Text/expected/HTML/input-date-picker-dismissed.txt
new file mode 100644
index 00000000000..0cd88bfe34b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/input-date-picker-dismissed.txt
@@ -0,0 +1,8 @@
+date: open during click=true
+date: value=2026-10-31 open=false
+date: open during click=true
+date: value=2026-10-31 open=false
+time: open during click=true
+time: value=13:37 open=false
+time: open during click=true
+time: value=13:37 open=false
diff --git a/Tests/LibWeb/Text/input/HTML/input-date-picker-dismissed.html b/Tests/LibWeb/Text/input/HTML/input-date-picker-dismissed.html
new file mode 100644
index 00000000000..6b1941683a7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/input-date-picker-dismissed.html
@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+ input {
+ width: 200px;
+ }
+</style>
+<input id="date" type="date" value="2026-10-31">
+<input id="time" type="time" value="13:37">
+<script>
+ asyncTest(async done => {
+ for (const input of document.querySelectorAll("input")) {
+ input.addEventListener("change", () => println(`${input.id}: change`));
+ input.addEventListener("click", () => println(`${input.id}: open during click=${input.matches(":open")}`));
+ const rect = input.getBoundingClientRect();
+ for (let i = 0; i < 2; ++i) {
+ internals.click(rect.right - 8, rect.top + rect.height / 2);
+ // The UI dismisses the picker asynchronously.
+ while (input.matches(":open"))
+ await new Promise(resolve => requestAnimationFrame(resolve));
+ println(`${input.id}: value=${input.value} open=${input.matches(":open")}`);
+ }
+ }
+ done();
+ });
+</script>
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index 97ef2fce93d..b93ce75d4c4 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -42,9 +42,11 @@
#include <LibWebView/TabPerformanceMonitor.h>
#include <QApplication>
+#include <QCalendarWidget>
#include <QClipboard>
#include <QColorDialog>
#include <QCursor>
+#include <QDialogButtonBox>
#include <QDynamicPropertyChangeEvent>
#include <QFileDialog>
#include <QFont>
@@ -52,19 +54,23 @@
#include <QFrame>
#include <QHBoxLayout>
#include <QImage>
+#include <QLocale>
#include <QMenu>
#include <QMessageBox>
#include <QMimeData>
#include <QMimeDatabase>
#include <QPainter>
#include <QPainterPath>
+#include <QPixmap>
#include <QProgressBar>
#include <QPushButton>
#include <QResizeEvent>
#include <QScrollArea>
#include <QStyleOptionToolButton>
#include <QStylePainter>
+#include <QTimeEdit>
#include <QTimer>
+#include <QToolButton>
#include <QVBoxLayout>
#include <QVariantAnimation>
#include <UI/Qt/PerformanceMonitorWidget.h>
@@ -76,6 +82,34 @@ static constexpr int HOVER_TEXT_CORNER_RADIUS = 4;
static constexpr auto WINDOW_DRAG_REGION_PROPERTY = "LadybirdWindowDragRegion";
static constexpr int BOOKMARKS_BAR_HOVER_COLLAPSE_POLL_INTERVAL_MS = 250;
+// The calendar's month buttons get chevrons in the palette's text color, since the style's own arrows stay dark in dark
+// mode.
+enum class ChevronDirection {
+ Left,
+ Right,
+};
+
+static QIcon calendar_month_chevron_icon(QWidget const& widget, ChevronDirection direction)
+{
+ bool const points_left = direction == ChevronDirection::Left;
+ static constexpr int size = 16;
+ auto device_pixel_ratio = widget.devicePixelRatioF();
+ QPixmap pixmap(QSize(size, size) * device_pixel_ratio);
+ pixmap.setDevicePixelRatio(device_pixel_ratio);
+ pixmap.fill(Qt::transparent);
+
+ QPainterPath chevron;
+ chevron.moveTo(points_left ? 10 : 6, 4);
+ chevron.lineTo(points_left ? 6 : 10, 8);
+ chevron.lineTo(points_left ? 10 : 6, 12);
+
+ QPainter painter(&pixmap);
+ painter.setRenderHint(QPainter::Antialiasing);
+ painter.setPen(QPen(widget.palette().color(QPalette::WindowText), 1.75, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
+ painter.drawPath(chevron);
+ return QIcon(pixmap);
+}
+
static Web::HTML::ActivateTab activate_tab_for_modifiers(Qt::KeyboardModifiers modifiers)
{
return modifiers.testFlag(Qt::ShiftModifier) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No;
@@ -1566,6 +1600,13 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)
dialog->close();
dialog->deleteLater();
}
+ if (m_date_picker_dialog) {
+ auto* dialog = m_date_picker_dialog.data();
+ m_date_picker_dialog = nullptr;
+ QObject::disconnect(dialog, nullptr, this, nullptr);
+ dialog->close();
+ dialog->deleteLater();
+ }
if (m_file_picker_dialog) {
auto* dialog = m_file_picker_dialog.data();
m_file_picker_dialog = nullptr;
@@ -1677,6 +1718,124 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)
m_color_picker_dialog->open();
};
+ view().on_request_date_picker = [this](Utf16String const& input_type, Utf16String const& value, Utf16String const& minimum, Utf16String const& maximum) {
+ if (m_date_picker_dialog) {
+ view().date_picker_closed({});
+ return;
+ }
+
+ // NB: The values come from the page, so anything that does not parse leaves the picker at its defaults. The
+ // page sanitizes the value the picker sends back.
+ auto type = qstring_from_utf16_string(input_type);
+ bool const picks_date = type != "time";
+ bool const picks_time = type == "time" || type == "datetime-local";
+
+ auto parse_date = [&](QString const& string) -> QDate {
+ if (type == "month")
+ return QDate::fromString(string + "-01", "yyyy-MM-dd");
+ if (type == "week") {
+ auto parts = string.split("-W");
+ if (parts.size() != 2)
+ return {};
+ bool year_ok = false;
+ bool week_ok = false;
+ auto year = parts[0].toInt(&year_ok);
+ auto week = parts[1].toInt(&week_ok);
+ if (!year_ok || !week_ok)
+ return {};
+ // The first ISO week of a year holds January 4th.
+ auto january_fourth = QDate(year, 1, 4);
+ return january_fourth.addDays(1 - january_fourth.dayOfWeek() + (week - 1) * 7);
+ }
+ return QDate::fromString(string.left(10), "yyyy-MM-dd");
+ };
+ auto current_value = qstring_from_utf16_string(value);
+ auto current_time = type == "datetime-local" ? current_value.mid(11) : current_value;
+ // NB: A value with seconds or a fraction of a second keeps them, so that confirming the picker does not drop
+ // them. The "z" format reads and writes one to three digits of the fraction.
+ bool const picks_seconds = current_time.length() > 5;
+ bool const picks_milliseconds = current_time.contains('.');
+ auto const time_format = picks_milliseconds ? QStringLiteral("HH:mm:ss.z")
+ : picks_seconds ? QStringLiteral("HH:mm:ss")
+ : QStringLiteral("HH:mm");
+ auto* dialog = new QDialog(&view());
+ m_date_picker_dialog = dialog;
+ dialog->setWindowTitle("Ladybird");
+ auto* layout = new QVBoxLayout(dialog);
+
+ QCalendarWidget* calendar = nullptr;
+ if (picks_date) {
+ calendar = new QCalendarWidget(dialog);
+ calendar->setGridVisible(false);
+ calendar->setVerticalHeaderFormat(type == "week" ? QCalendarWidget::ISOWeekNumbers : QCalendarWidget::NoVerticalHeader);
+ calendar->setFirstDayOfWeek(type == "week" ? Qt::Monday : QLocale().firstDayOfWeek());
+ // Draw weekends like other days, rather than in Qt's default red.
+ calendar->setWeekdayTextFormat(Qt::Saturday, {});
+ calendar->setWeekdayTextFormat(Qt::Sunday, {});
+ calendar->setStyleSheet(
+ "#qt_calendar_navigationbar { background: transparent; }"
+ "#qt_calendar_navigationbar QToolButton { background: transparent; border: none; padding: 2px 6px; font-weight: 600; }"
+ "#qt_calendar_navigationbar QToolButton::menu-indicator { image: none; width: 0; }"
+ "#qt_calendar_navigationbar QToolButton:hover { background: palette(midlight); border-radius: 4px; }"
+ "QCalendarWidget QAbstractItemView { selection-background-color: palette(accent); selection-color: palette(highlighted-text); }");
+ if (auto* previous_month = calendar->findChild<QToolButton*>("qt_calendar_prevmonth"))
+ previous_month->setIcon(calendar_month_chevron_icon(*calendar, ChevronDirection::Left));
+ if (auto* next_month = calendar->findChild<QToolButton*>("qt_calendar_nextmonth"))
+ next_month->setIcon(calendar_month_chevron_icon(*calendar, ChevronDirection::Right));
+ if (auto date = parse_date(current_value); date.isValid())
+ calendar->setSelectedDate(date);
+ if (auto date = parse_date(qstring_from_utf16_string(minimum)); date.isValid())
+ calendar->setMinimumDate(date);
+ if (auto date = parse_date(qstring_from_utf16_string(maximum)); date.isValid())
+ calendar->setMaximumDate(date);
+ layout->addWidget(calendar);
+ }
+
+ QTimeEdit* time_edit = nullptr;
+ if (picks_time) {
+ time_edit = new QTimeEdit(dialog);
+ time_edit->setDisplayFormat(picks_milliseconds ? QStringLiteral("HH:mm:ss.zzz") : time_format);
+ if (auto time = QTime::fromString(current_time, time_format); time.isValid())
+ time_edit->setTime(time);
+ layout->addWidget(time_edit);
+ }
+
+ auto* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, dialog);
+ QObject::connect(buttons, &QDialogButtonBox::accepted, dialog, &QDialog::accept);
+ QObject::connect(buttons, &QDialogButtonBox::rejected, dialog, &QDialog::reject);
+ layout->addWidget(buttons);
+
+ QObject::connect(dialog, &QDialog::finished, this, [this, dialog, calendar, time_edit, type, time_format](int result) {
+ Optional<Utf16String> picked_value;
+ if (result == QDialog::Accepted) {
+ QString picked;
+ if (calendar) {
+ auto date = calendar->selectedDate();
+ if (type == "month") {
+ picked = date.toString("yyyy-MM");
+ } else if (type == "week") {
+ int week_year = 0;
+ auto week = date.weekNumber(&week_year);
+ picked = QString("%1-W%2").arg(week_year, 4, 10, QChar('0')).arg(week, 2, 10, QChar('0'));
+ } else {
+ picked = date.toString("yyyy-MM-dd");
+ }
+ }
+ if (time_edit) {
+ if (!picked.isEmpty())
+ picked += "T";
+ picked += time_edit->time().toString(time_format);
+ }
+ picked_value = utf16_string_from_qstring(picked);
+ }
+ m_date_picker_dialog = nullptr;
+ dialog->deleteLater();
+ view().date_picker_closed(AK::move(picked_value));
+ });
+
+ dialog->open();
+ };
+
view().on_request_file_picker = [this](auto const& accepted_file_types, auto allow_multiple_files) {
QStringList accepted_file_filters;
QMimeDatabase mime_database;
diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.h
index 78918289d83..1b64f064a44 100644
--- a/UI/Qt/Tab.h
+++ b/UI/Qt/Tab.h
@@ -36,6 +36,7 @@
#include <QWidget>
class QColorDialog;
+class QDialog;
class QFileDialog;
class QMessageBox;
class QTimer;
@@ -326,6 +327,7 @@ private:
JavaScriptDialog* m_javascript_dialog { nullptr };
Function<void(bool)> m_before_unload_completion;
QPointer<QColorDialog> m_color_picker_dialog;
+ QPointer<QDialog> m_date_picker_dialog;
QPointer<QFileDialog> m_file_picker_dialog;
QPointer<QMessageBox> m_external_url_confirmation_dialog;
PermissionPromptQueue m_permission_prompts;