mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
Start using WeakPtr for some of the WindowManager window pointers.
This commit is contained in:
parent
16fff6dff7
commit
560405667e
Notes:
sideshowbarker
2024-07-19 18:49:01 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/560405667e9
5 changed files with 29 additions and 8 deletions
19
AK/WeakPtr.h
19
AK/WeakPtr.h
|
@ -11,11 +11,28 @@ public:
|
|||
WeakPtr() { }
|
||||
WeakPtr(std::nullptr_t) { }
|
||||
|
||||
template<typename U>
|
||||
WeakPtr& operator=(WeakPtr<U>&& other)
|
||||
{
|
||||
m_link = reinterpret_cast<WeakLink<T>*>(other.leakLink());
|
||||
return *this;
|
||||
}
|
||||
|
||||
operator bool() const { return ptr(); }
|
||||
|
||||
T* ptr() { return m_link ? m_link->ptr() : nullptr; }
|
||||
const T* ptr() const { return m_link ? m_link->ptr() : nullptr; }
|
||||
|
||||
T* operator->() { return ptr(); }
|
||||
const T* operator->() const { return ptr(); }
|
||||
|
||||
T& operator*() { return *ptr(); }
|
||||
const T& operator*() const { return *ptr(); }
|
||||
|
||||
bool isNull() const { return !m_link || !m_link->ptr(); }
|
||||
void clear() { m_link = nullptr; }
|
||||
|
||||
WeakLink<T>* leakLink() { return m_link.leakRef(); }
|
||||
|
||||
private:
|
||||
WeakPtr(RetainPtr<WeakLink<T>>&& link) : m_link(std::move(link)) { }
|
||||
|
@ -27,7 +44,7 @@ template<typename T>
|
|||
inline WeakPtr<T> Weakable<T>::makeWeakPtr()
|
||||
{
|
||||
if (!m_link)
|
||||
m_link = adopt(*new WeakLink<T>(*this));
|
||||
m_link = adopt(*new WeakLink<T>(static_cast<T&>(*this)));
|
||||
return WeakPtr<T>(m_link.copyRef());
|
||||
}
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "Assertions.h"
|
||||
#include "Retainable.h"
|
||||
#include "RetainPtr.h"
|
||||
|
||||
namespace AK {
|
||||
|
||||
|
@ -16,8 +17,8 @@ public:
|
|||
const T* ptr() const { return static_cast<const T*>(m_ptr); }
|
||||
|
||||
private:
|
||||
explicit WeakLink(Weakable<T>& weakable) : m_ptr(&weakable) { }
|
||||
Weakable<T>* m_ptr;
|
||||
explicit WeakLink(T& weakable) : m_ptr(&weakable) { }
|
||||
T* m_ptr;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Vector.h>
|
||||
#include <AK/Weakable.h>
|
||||
|
||||
class Event;
|
||||
class TimerEvent;
|
||||
|
||||
class Object {
|
||||
class Object : public Weakable<Object> {
|
||||
public:
|
||||
Object(Object* parent = nullptr);
|
||||
virtual ~Object();
|
||||
|
|
|
@ -96,7 +96,7 @@ void WindowManager::paintWindowFrame(Window& window)
|
|||
m_lastDragRect = Rect();
|
||||
}
|
||||
|
||||
if (m_dragWindow == &window) {
|
||||
if (m_dragWindow.ptr() == &window) {
|
||||
p.xorRect(outerRect, Color::Red);
|
||||
m_lastDragRect = outerRect;
|
||||
return;
|
||||
|
@ -138,7 +138,7 @@ void WindowManager::handleTitleBarMouseEvent(Window& window, MouseEvent& event)
|
|||
{
|
||||
if (event.type() == Event::MouseDown) {
|
||||
printf("[WM] Begin dragging Window{%p}\n", &window);
|
||||
m_dragWindow = &window;
|
||||
m_dragWindow = window.makeWeakPtr();;
|
||||
m_dragOrigin = event.position();
|
||||
m_dragWindowOrigin = window.position();
|
||||
m_dragStartRect = outerRectForWindow(window);
|
||||
|
@ -180,7 +180,7 @@ void WindowManager::processMouseEvent(MouseEvent& event)
|
|||
{
|
||||
if (event.type() == Event::MouseUp) {
|
||||
if (m_dragWindow) {
|
||||
printf("[WM] Finish dragging Window{%p}\n", m_dragWindow);
|
||||
printf("[WM] Finish dragging Window{%p}\n", m_dragWindow.ptr());
|
||||
m_dragWindow->setIsBeingDragged(false);
|
||||
m_dragEndRect = outerRectForWindow(*m_dragWindow);
|
||||
m_dragWindow = nullptr;
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include "Rect.h"
|
||||
#include "Color.h"
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
|
||||
class MouseEvent;
|
||||
class PaintEvent;
|
||||
|
@ -41,7 +42,8 @@ private:
|
|||
HashTable<Window*> m_windows;
|
||||
Widget* m_rootWidget { nullptr };
|
||||
|
||||
Window* m_dragWindow { nullptr };
|
||||
WeakPtr<Window> m_dragWindow;
|
||||
|
||||
Point m_dragOrigin;
|
||||
Point m_dragWindowOrigin;
|
||||
Rect m_lastDragRect;
|
||||
|
|
Loading…
Reference in a new issue