瀏覽代碼

LibWeb: Add "focused frame" concept, one focused Frame per Page

Focus currently only moves when doing a mousedown in a frame.
Andreas Kling 5 年之前
父節點
當前提交
6b4a7d1ee3

+ 1 - 0
Libraries/LibWeb/Forward.h

@@ -130,6 +130,7 @@ class ImageData;
 }
 
 namespace Web {
+class EventHandler;
 class Frame;
 class LayoutBlock;
 class LayoutDocument;

+ 8 - 0
Libraries/LibWeb/Layout/LayoutFrame.cpp

@@ -35,6 +35,8 @@
 #include <LibWeb/Page/Frame.h>
 #include <LibWeb/PageView.h>
 
+//#define DEBUG_HIGHLIGHT_FOCUSED_FRAME
+
 namespace Web {
 
 LayoutFrame::LayoutFrame(DOM::Document& document, DOM::Element& element, NonnullRefPtr<CSS::StyleProperties> style)
@@ -82,6 +84,12 @@ void LayoutFrame::paint(PaintContext& context, PaintPhase phase)
 
         context.set_viewport_rect(old_viewport_rect);
         context.painter().restore();
+
+#ifdef DEBUG_HIGHLIGHT_FOCUSED_FRAME
+        if (node().hosted_frame()->is_focused_frame()) {
+            context.painter().draw_rect(absolute_rect().to<int>(), Color::Cyan);
+        }
+#endif
     }
 }
 

+ 2 - 0
Libraries/LibWeb/Page/EventHandler.cpp

@@ -119,6 +119,8 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
         return false;
     }
 
+    m_frame.page().set_focused_frame({}, m_frame);
+
     auto offset = compute_mouse_event_offset(position, *result.layout_node);
     node->dispatch_event(UIEvents::MouseEvent::create("mousedown", offset.x(), offset.y()));
     if (!layout_root())

+ 5 - 0
Libraries/LibWeb/Page/Frame.cpp

@@ -68,6 +68,11 @@ void Frame::setup()
     });
 }
 
+bool Frame::is_focused_frame() const
+{
+    return this == &page().focused_frame();
+}
+
 void Frame::set_document(DOM::Document* document)
 {
     if (m_document == document)

+ 1 - 0
Libraries/LibWeb/Page/Frame.h

@@ -48,6 +48,7 @@ public:
     ~Frame();
 
     bool is_main_frame() const { return this == &m_main_frame; }
+    bool is_focused_frame() const;
 
     const DOM::Document* document() const { return m_document; }
     DOM::Document* document() { return m_document; }

+ 12 - 0
Libraries/LibWeb/Page/Page.cpp

@@ -40,6 +40,18 @@ Page::~Page()
 {
 }
 
+Frame& Page::focused_frame()
+{
+    if (m_focused_frame)
+        return *m_focused_frame;
+    return main_frame();
+}
+
+void Page::set_focused_frame(Badge<EventHandler>, Frame& frame)
+{
+    m_focused_frame = frame.make_weak_ptr();
+}
+
 void Page::load(const URL& url)
 {
     main_frame().loader().load(url, FrameLoader::Type::Navigation);

+ 6 - 0
Libraries/LibWeb/Page/Page.h

@@ -53,6 +53,11 @@ public:
     Web::Frame& main_frame() { return *m_main_frame; }
     const Web::Frame& main_frame() const { return *m_main_frame; }
 
+    Web::Frame& focused_frame();
+    const Web::Frame& focused_frame() const { return const_cast<Page*>(this)->focused_frame(); }
+
+    void set_focused_frame(Badge<EventHandler>, Frame&);
+
     void load(const URL&);
 
     bool handle_mouseup(const Gfx::IntPoint&, unsigned button, unsigned modifiers);
@@ -67,6 +72,7 @@ private:
     PageClient& m_client;
 
     RefPtr<Frame> m_main_frame;
+    WeakPtr<Frame> m_focused_frame;
 };
 
 class PageClient {