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