Skip to content

Commit

Permalink
Figured out a better way
Browse files Browse the repository at this point in the history
  • Loading branch information
serebit committed Oct 26, 2023
1 parent 0963ffc commit 943e867
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/foreign_toplevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::reference_wrapper<const ForeignToplevelHandle>> parent) {
wlr_foreign_toplevel_handle_v1_set_parent(&handle, (parent.has_value()) ? nullptr : &parent->get().handle);
}

void ForeignToplevelHandle::set_maximized(const bool maximized) {
Expand Down
3 changes: 2 additions & 1 deletion src/foreign_toplevel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "types.hpp"

#include <functional>
#include <optional>

#include "wlr-wrap-start.hpp"
#include <wlr/types/wlr_foreign_toplevel_management_v1.h>
Expand Down Expand Up @@ -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<std::reference_wrapper<const ForeignToplevelHandle>> parent);
void set_maximized(bool maximized);
void set_minimized(bool minimized);
void set_activated(bool activated);
Expand Down
13 changes: 10 additions & 3 deletions src/surface/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -153,15 +156,19 @@ 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) {
if (minimized == is_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;

Expand Down
5 changes: 3 additions & 2 deletions src/surface/view.hpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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<ForeignToplevelHandle> toplevel_handle = {};

virtual ~View() noexcept {};

Expand Down
7 changes: 3 additions & 4 deletions src/surface/xdg_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand All @@ -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<View*>(static_cast<Surface*>(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());
}
}

Expand Down
21 changes: 10 additions & 11 deletions src/surface/xwayland_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand All @@ -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<View*>(static_cast<Surface*>(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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 943e867

Please sign in to comment.