From 943e867355de6cb169bd897573484ba721b2d68a Mon Sep 17 00:00:00 2001 From: Campbell Jones Date: Thu, 26 Oct 2023 19:24:38 -0400 Subject: [PATCH] Figured out a better way --- src/foreign_toplevel.cpp | 4 ++-- src/foreign_toplevel.hpp | 3 ++- src/surface/view.cpp | 13 ++++++++++--- src/surface/view.hpp | 5 +++-- src/surface/xdg_view.cpp | 7 +++---- src/surface/xwayland_view.cpp | 21 ++++++++++----------- 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/foreign_toplevel.cpp b/src/foreign_toplevel.cpp index 33509aaf0..a51b7743c 100644 --- a/src/foreign_toplevel.cpp +++ b/src/foreign_toplevel.cpp @@ -85,8 +85,8 @@ void ForeignToplevelHandle::set_app_id(const char* app_id) { } } -void ForeignToplevelHandle::set_parent(const ForeignToplevelHandle* parent) { - wlr_foreign_toplevel_handle_v1_set_parent(&handle, (parent == nullptr) ? nullptr : &parent->handle); +void ForeignToplevelHandle::set_parent(std::optional> parent) { + wlr_foreign_toplevel_handle_v1_set_parent(&handle, (parent.has_value()) ? nullptr : &parent->get().handle); } void ForeignToplevelHandle::set_maximized(const bool maximized) { diff --git a/src/foreign_toplevel.hpp b/src/foreign_toplevel.hpp index a24c0e29a..b8805b36c 100644 --- a/src/foreign_toplevel.hpp +++ b/src/foreign_toplevel.hpp @@ -4,6 +4,7 @@ #include "types.hpp" #include +#include #include "wlr-wrap-start.hpp" #include @@ -34,7 +35,7 @@ class ForeignToplevelHandle { void set_title(const char* title); void set_app_id(const char* app_id); - void set_parent(const ForeignToplevelHandle* parent); + void set_parent(std::optional> parent); void set_maximized(bool maximized); void set_minimized(bool minimized); void set_activated(bool activated); diff --git a/src/surface/view.cpp b/src/surface/view.cpp index 0c6262eeb..8982b26ec 100644 --- a/src/surface/view.cpp +++ b/src/surface/view.cpp @@ -112,7 +112,10 @@ void View::set_size(const int new_width, const int new_height) { void View::set_activated(const bool activated) { impl_set_activated(activated); - toplevel_handle->set_activated(activated); + + if (toplevel_handle.has_value()) { + toplevel_handle->set_activated(activated); + } } void View::set_maximized(const bool maximized) { @@ -153,7 +156,9 @@ void View::set_maximized(const bool maximized) { } this->is_maximized = maximized; - toplevel_handle->set_maximized(maximized); + if (toplevel_handle.has_value()) { + toplevel_handle->set_maximized(maximized); + } } void View::set_minimized(const bool minimized) { @@ -161,7 +166,9 @@ void View::set_minimized(const bool minimized) { return; } - toplevel_handle->set_minimized(minimized); + if (toplevel_handle.has_value()) { + toplevel_handle->set_minimized(minimized); + } impl_set_minimized(minimized); this->is_minimized = minimized; diff --git a/src/surface/view.hpp b/src/surface/view.hpp index fececd577..a6af7246a 100644 --- a/src/surface/view.hpp +++ b/src/surface/view.hpp @@ -1,6 +1,7 @@ #ifndef MAGPIE_VIEW_HPP #define MAGPIE_VIEW_HPP +#include "foreign_toplevel.hpp" #include "input/cursor.hpp" #include "surface.hpp" #include "types.hpp" @@ -19,8 +20,8 @@ struct View : public Surface { wlr_box current; wlr_box pending; wlr_box previous; - wlr_surface* surface; - ForeignToplevelHandle* toplevel_handle; + wlr_surface* surface = nullptr; + std::optional toplevel_handle = {}; virtual ~View() noexcept {}; diff --git a/src/surface/xdg_view.cpp b/src/surface/xdg_view.cpp index 1ac84f764..08799adfb 100644 --- a/src/surface/xdg_view.cpp +++ b/src/surface/xdg_view.cpp @@ -36,7 +36,6 @@ static void xdg_toplevel_destroy_notify(wl_listener* listener, void* data) { (void) data; view.server.views.remove(&view); - delete view.toplevel_handle; delete &view; } @@ -119,7 +118,7 @@ static void xdg_toplevel_set_parent_notify(wl_listener* listener, void* data) { } } - view.toplevel_handle->set_parent(nullptr); + view.toplevel_handle->set_parent({}); } XdgView::XdgView(Server& server, wlr_xdg_toplevel& toplevel) noexcept @@ -137,14 +136,14 @@ XdgView::XdgView(Server& server, wlr_xdg_toplevel& toplevel) noexcept toplevel.base->surface->data = this; xdg_toplevel = toplevel; - toplevel_handle = new ForeignToplevelHandle(*this); + toplevel_handle.emplace(*this); toplevel_handle->set_title(xdg_toplevel.title); toplevel_handle->set_app_id(xdg_toplevel.app_id); if (xdg_toplevel.parent != nullptr) { auto* m_view = dynamic_cast(static_cast(xdg_toplevel.parent->base->data)); if (m_view != nullptr) { - toplevel_handle->set_parent(m_view->toplevel_handle); + toplevel_handle->set_parent(m_view->toplevel_handle.value()); } } diff --git a/src/surface/xwayland_view.cpp b/src/surface/xwayland_view.cpp index adf615404..dcef6c6a3 100644 --- a/src/surface/xwayland_view.cpp +++ b/src/surface/xwayland_view.cpp @@ -97,7 +97,7 @@ static void xwayland_surface_set_title_notify(wl_listener* listener, void* data) XWaylandView& view = magpie_container_of(listener, view, set_title); (void) data; - if (view.toplevel_handle != nullptr) { + if (view.toplevel_handle.has_value()) { view.toplevel_handle->set_title(view.xwayland_surface.title); } } @@ -106,7 +106,7 @@ static void xwayland_surface_set_class_notify(wl_listener* listener, void* data) XWaylandView& view = magpie_container_of(listener, view, set_class); (void) data; - if (view.toplevel_handle != nullptr) { + if (view.toplevel_handle.has_value()) { view.toplevel_handle->set_app_id(view.xwayland_surface._class); } } @@ -115,25 +115,25 @@ static void xwayland_surface_set_parent_notify(wl_listener* listener, void* data XWaylandView& view = magpie_container_of(listener, view, set_parent); (void) data; - if (view.toplevel_handle == nullptr) - return; - if (view.xwayland_surface.parent != nullptr) { auto* m_view = dynamic_cast(static_cast(view.xwayland_surface.parent->data)); if (m_view != nullptr) { wlr_scene_node_reparent(view.scene_node, m_view->scene_node->parent); - view.toplevel_handle->set_parent(m_view->toplevel_handle); + if (view.toplevel_handle.has_value() && m_view->toplevel_handle.has_value()) { + view.toplevel_handle->set_parent(m_view->toplevel_handle.value()); + } return; } } - view.toplevel_handle->set_parent(nullptr); + if (view.toplevel_handle.has_value()) { + view.toplevel_handle->set_parent({}); + } } XWaylandView::XWaylandView(Server& server, wlr_xwayland_surface& xwayland_surface) noexcept : listeners(*this), server(server), xwayland_surface(xwayland_surface) { this->xwayland_surface = xwayland_surface; - toplevel_handle = nullptr; /* Listen to the various events it can emit */ listeners.map.notify = xwayland_surface_map_notify; @@ -190,7 +190,7 @@ void XWaylandView::map() { this->surface = xwayland_surface.surface; - toplevel_handle = new ForeignToplevelHandle(*this); + toplevel_handle.emplace(*this); toplevel_handle->set_title(xwayland_surface.title); toplevel_handle->set_app_id(xwayland_surface._class); @@ -228,8 +228,7 @@ void XWaylandView::unmap() { wlr_scene_node_destroy(scene_node); server.views.remove(this); - delete toplevel_handle; - toplevel_handle = nullptr; + toplevel_handle.reset(); } void XWaylandView::impl_set_position(const int new_x, const int new_y) {