mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 01:02:45 +00:00 
			
		
		
		
	LibWeb: Allow focusing individual (focusable) elements with Tab key
You can now cycle through focusable elements (currently only hyperlinks are focusable) with the Tab key. The focus outline is rendered in a new FocusOutline paint phase.
This commit is contained in:
		
							parent
							
								
									5939af14d4
								
							
						
					
					
						commit
						01022eb5d6
					
				
					 11 changed files with 92 additions and 2 deletions
				
			
		|  | @ -508,4 +508,18 @@ bool Document::is_editable() const | ||||||
|     return m_editable; |     return m_editable; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void Document::set_focused_element(Element* element) | ||||||
|  | { | ||||||
|  |     if (m_focused_element == element) | ||||||
|  |         return; | ||||||
|  | 
 | ||||||
|  |     if (element) | ||||||
|  |         m_focused_element = element->make_weak_ptr(); | ||||||
|  |     else | ||||||
|  |         m_focused_element = nullptr; | ||||||
|  | 
 | ||||||
|  |     if (m_layout_root) | ||||||
|  |         m_layout_root->set_needs_display(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -161,6 +161,11 @@ public: | ||||||
|     void set_editable(bool editable) { m_editable = editable; } |     void set_editable(bool editable) { m_editable = editable; } | ||||||
|     virtual bool is_editable() const final; |     virtual bool is_editable() const final; | ||||||
| 
 | 
 | ||||||
|  |     Element* focused_element() { return m_focused_element; } | ||||||
|  |     const Element* focused_element() const { return m_focused_element; } | ||||||
|  | 
 | ||||||
|  |     void set_focused_element(Element*); | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
|     virtual RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style) override; |     virtual RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style) override; | ||||||
| 
 | 
 | ||||||
|  | @ -191,6 +196,8 @@ private: | ||||||
| 
 | 
 | ||||||
|     QuirksMode m_quirks_mode { QuirksMode::No }; |     QuirksMode m_quirks_mode { QuirksMode::No }; | ||||||
|     bool m_editable { false }; |     bool m_editable { false }; | ||||||
|  | 
 | ||||||
|  |     WeakPtr<Element> m_focused_element; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -295,4 +295,9 @@ String Element::inner_html() const | ||||||
|     return builder.to_string(); |     return builder.to_string(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | bool Element::is_focused() const | ||||||
|  | { | ||||||
|  |     return document().focused_element() == this; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -87,6 +87,9 @@ public: | ||||||
|     String inner_html() const; |     String inner_html() const; | ||||||
|     void set_inner_html(StringView); |     void set_inner_html(StringView); | ||||||
| 
 | 
 | ||||||
|  |     bool is_focused() const; | ||||||
|  |     virtual bool is_focusable() const { return false; } | ||||||
|  | 
 | ||||||
| protected: | protected: | ||||||
|     RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style) override; |     RefPtr<LayoutNode> create_layout_node(const CSS::StyleProperties* parent_style) override; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -39,6 +39,8 @@ public: | ||||||
| 
 | 
 | ||||||
|     String href() const { return attribute(HTML::AttributeNames::href); } |     String href() const { return attribute(HTML::AttributeNames::href); } | ||||||
|     String target() const { return attribute(HTML::AttributeNames::target); } |     String target() const { return attribute(HTML::AttributeNames::target); } | ||||||
|  | 
 | ||||||
|  |     virtual bool is_focusable() const override { return has_attribute(HTML::AttributeNames::href); } | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -715,6 +715,23 @@ void LayoutBlock::paint(PaintContext& context, PaintPhase phase) | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     if (phase == PaintPhase::FocusOutline) { | ||||||
|  |         if (children_are_inline()) { | ||||||
|  |             for (auto& line_box : m_line_boxes) { | ||||||
|  |                 for (auto& fragment : line_box.fragments()) { | ||||||
|  |                     auto* node = fragment.layout_node().node(); | ||||||
|  |                     if (!node) | ||||||
|  |                         continue; | ||||||
|  |                     auto* parent = node->parent_element(); | ||||||
|  |                     if (!parent) | ||||||
|  |                         continue; | ||||||
|  |                     if (parent->is_focused()) | ||||||
|  |                         context.painter().draw_rect(enclosing_int_rect(fragment.absolute_rect()), context.palette().focus_outline()); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| HitTestResult LayoutBlock::hit_test(const Gfx::IntPoint& position, HitTestType type) const | HitTestResult LayoutBlock::hit_test(const Gfx::IntPoint& position, HitTestType type) const | ||||||
|  |  | ||||||
|  | @ -27,9 +27,9 @@ | ||||||
| #include <LibGUI/Painter.h> | #include <LibGUI/Painter.h> | ||||||
| #include <LibWeb/DOM/Document.h> | #include <LibWeb/DOM/Document.h> | ||||||
| #include <LibWeb/HTML/HTMLBodyElement.h> | #include <LibWeb/HTML/HTMLBodyElement.h> | ||||||
| #include <LibWeb/Page/Frame.h> |  | ||||||
| #include <LibWeb/Layout/LayoutBlock.h> | #include <LibWeb/Layout/LayoutBlock.h> | ||||||
| #include <LibWeb/Layout/LayoutBox.h> | #include <LibWeb/Layout/LayoutBox.h> | ||||||
|  | #include <LibWeb/Page/Frame.h> | ||||||
| 
 | 
 | ||||||
| namespace Web { | namespace Web { | ||||||
| 
 | 
 | ||||||
|  | @ -222,6 +222,10 @@ void LayoutBox::paint(PaintContext& context, PaintPhase phase) | ||||||
|         context.painter().draw_rect(enclosing_int_rect(padded_rect), Color::Cyan); |         context.painter().draw_rect(enclosing_int_rect(padded_rect), Color::Cyan); | ||||||
|         context.painter().draw_rect(enclosing_int_rect(content_rect), Color::Magenta); |         context.painter().draw_rect(enclosing_int_rect(content_rect), Color::Magenta); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     if (phase == PaintPhase::FocusOutline && node() && node()->is_element() && downcast<DOM::Element>(*node()).is_focused()) { | ||||||
|  |         context.painter().draw_rect(enclosing_int_rect(absolute_rect()), context.palette().focus_outline()); | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| HitTestResult LayoutBox::hit_test(const Gfx::IntPoint& position, HitTestType type) const | HitTestResult LayoutBox::hit_test(const Gfx::IntPoint& position, HitTestType type) const | ||||||
|  |  | ||||||
|  | @ -105,6 +105,7 @@ void LayoutDocument::paint_all_phases(PaintContext& context) | ||||||
|     paint(context, PaintPhase::Background); |     paint(context, PaintPhase::Background); | ||||||
|     paint(context, PaintPhase::Border); |     paint(context, PaintPhase::Border); | ||||||
|     paint(context, PaintPhase::Foreground); |     paint(context, PaintPhase::Foreground); | ||||||
|  |     paint(context, PaintPhase::FocusOutline); | ||||||
|     paint(context, PaintPhase::Overlay); |     paint(context, PaintPhase::Overlay); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -103,6 +103,7 @@ public: | ||||||
|         Background, |         Background, | ||||||
|         Border, |         Border, | ||||||
|         Foreground, |         Foreground, | ||||||
|  |         FocusOutline, | ||||||
|         Overlay, |         Overlay, | ||||||
|     }; |     }; | ||||||
|     virtual void paint(PaintContext&, PaintPhase); |     virtual void paint(PaintContext&, PaintPhase); | ||||||
|  |  | ||||||
|  | @ -237,8 +237,41 @@ void EventHandler::dump_selection(const char* event_name) const | ||||||
| #endif | #endif | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| bool EventHandler::handle_keydown(KeyCode key, unsigned, u32 code_point) | bool EventHandler::focus_next_element() | ||||||
| { | { | ||||||
|  |     if (!m_frame.document()) | ||||||
|  |         return false; | ||||||
|  |     auto* element = m_frame.document()->focused_element(); | ||||||
|  |     if (!element) { | ||||||
|  |         element = m_frame.document()->first_child_of_type<DOM::Element>(); | ||||||
|  |         if (element && element->is_focusable()) { | ||||||
|  |             m_frame.document()->set_focused_element(element); | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order()) | ||||||
|  |         ; | ||||||
|  | 
 | ||||||
|  |     m_frame.document()->set_focused_element(element); | ||||||
|  |     return element; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool EventHandler::focus_previous_element() | ||||||
|  | { | ||||||
|  |     // FIXME: Implement Shift-Tab cycling backwards through focusable elements!
 | ||||||
|  |     return false; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point) | ||||||
|  | { | ||||||
|  |     if (key == KeyCode::Key_Tab) { | ||||||
|  |         if (modifiers & KeyModifier::Mod_Shift) | ||||||
|  |             return focus_previous_element(); | ||||||
|  |         else | ||||||
|  |             return focus_next_element(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     if (m_frame.cursor_position().node() && m_frame.cursor_position().node()->is_editable()) { |     if (m_frame.cursor_position().node() && m_frame.cursor_position().node()->is_editable()) { | ||||||
|         // FIXME: Support backspacing across DOM node boundaries.
 |         // FIXME: Support backspacing across DOM node boundaries.
 | ||||||
|         if (key == KeyCode::Key_Backspace && m_frame.cursor_position().offset() > 0) { |         if (key == KeyCode::Key_Backspace && m_frame.cursor_position().offset() > 0) { | ||||||
|  |  | ||||||
|  | @ -48,6 +48,9 @@ public: | ||||||
|     bool handle_keydown(KeyCode, unsigned modifiers, u32 code_point); |     bool handle_keydown(KeyCode, unsigned modifiers, u32 code_point); | ||||||
| 
 | 
 | ||||||
| private: | private: | ||||||
|  |     bool focus_next_element(); | ||||||
|  |     bool focus_previous_element(); | ||||||
|  | 
 | ||||||
|     LayoutDocument* layout_root(); |     LayoutDocument* layout_root(); | ||||||
|     const LayoutDocument* layout_root() const; |     const LayoutDocument* layout_root() const; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling