Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkae committed Feb 25, 2024
1 parent d59b0d3 commit 452ec12
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 45 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ add_executable(miracle-wm
src/ipc.cpp
src/auto_restarting_launcher.cpp
src/workspace_observer.cpp
src/window_metadata.cpp
)

target_include_directories(miracle-wm PUBLIC SYSTEM ${MIRAL_INCLUDE_DIRS})
Expand Down
78 changes: 36 additions & 42 deletions src/node.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
#define MIR_LOG_COMPONENT "node"

#include "node.h"
#include "window_helpers.h"
#include "miracle_config.h"
#include <cmath>
#include <iostream>
#include <mir/log.h>

using namespace miracle;

Node::Node(miral::WindowManagerTools const& tools, geom::Rectangle const& area, std::shared_ptr<MiracleConfig> const& config)
: tools{tools},
Node::Node(miral::WindowManagerTools const& tools_, geom::Rectangle const& area, std::shared_ptr<MiracleConfig> const& config)
: tools{tools_},
state{NodeState::lane},
logical_area{area},
config{config}
{
}

Node::Node(miral::WindowManagerTools const& tools, geom::Rectangle const& area, std::shared_ptr<Node> parent, miral::Window &window, std::shared_ptr<MiracleConfig> const& config)
: tools{tools},
Node::Node(
miral::WindowManagerTools const& tools_,
geom::Rectangle const& area,
std::shared_ptr<Node> parent,
std::shared_ptr<WindowMetadata> const& metadata,
std::shared_ptr<MiracleConfig> const& config)
: tools{tools_},
parent{std::move(parent)},
window{window},
metadata{metadata},
state{NodeState::window},
logical_area{area},
config{config}
{
miral::WindowSpecification spec;
spec.userdata() = metadata;
tools.modify_window(metadata->get_window(), spec);
}

geom::Rectangle Node::get_logical_area_internal(geom::Rectangle const& rectangle)
Expand Down Expand Up @@ -204,12 +215,14 @@ void Node::add_window(miral::Window& new_window)
if (pending_index < 0)
pending_index = (int)sub_nodes.size();

auto node_metadata = std::make_shared<WindowMetadata>(WindowType::tiled, new_window);
auto node = std::make_shared<Node>(
tools,
pending_logical_rect,
shared_from_this(),
new_window,
node_metadata,
config);
node_metadata->associate_to_node(node);

sub_nodes.insert(sub_nodes.begin() + pending_index, node);
pending_index = -1;
Expand Down Expand Up @@ -262,7 +275,7 @@ void Node::set_logical_area(geom::Rectangle const& target_rect)
{
if (is_window())
{
auto& info = tools.info_for(window);
auto& info = tools.info_for(metadata->get_window());
if (!window_helpers::is_window_fullscreen(info.state()))
{
_set_window_rectangle(target_rect);
Expand Down Expand Up @@ -355,54 +368,34 @@ std::shared_ptr<Node> Node::to_lane()
return nullptr;

state = NodeState::lane;

// If we want to make a new node, but our parent only has one window, and it's us...
// then we can just return the parent
if (parent != nullptr && parent->sub_nodes.size() == 1)
return parent->sub_nodes[0];

auto seed_node = std::make_shared<Node>(
auto window_node = std::make_shared<Node>(
tools,
logical_area,
shared_from_this(),
window,
metadata,
config);
sub_nodes.push_back(seed_node);
return seed_node;
metadata->associate_to_node(window_node);
sub_nodes.push_back(window_node);
metadata = nullptr;
return window_node;
}

std::shared_ptr<miracle::Node> Node::find_node_for_window(miral::Window &window)
std::shared_ptr<Node> Node::find_node_for_window(miral::Window &window)
{
for (auto item : sub_nodes)
{
if (item->is_window())
{
if (item->get_window() == window)
return item;
}
else
{
auto node = item->find_node_for_window(window);
if (node != nullptr)
return node;
}
}

for (auto hidden : hidden_nodes)
auto& info = tools.info_for(window);
if (info.userdata())
{
if (hidden->is_window())
{
if (hidden->get_window() == window)
return hidden;
}
else
{
auto node = hidden->find_node_for_window(window);
if (node != nullptr)
return node;
}
std::shared_ptr<WindowMetadata> data = static_pointer_cast<WindowMetadata>(info.userdata());
return data->get_tiling_node();
}

// TODO: Error
mir::log_error("Unable to find node for window");
return nullptr;
}

Expand Down Expand Up @@ -448,7 +441,7 @@ void Node::remove_node(std::shared_ptr<Node> const& node)
sub_nodes.clear();
for (auto sub_node : dying_lane->get_sub_nodes())
{
add_window(sub_node->window);
add_window(sub_node->get_window());
}
set_direction(dying_lane->get_direction());
}
Expand Down Expand Up @@ -592,6 +585,7 @@ int Node::get_min_height() const
void Node::_set_window_rectangle(geom::Rectangle area)
{
auto visible_rect = _get_visible_from_logical(area, config);
auto window = metadata->get_window();
window.move_to(visible_rect.top_left);
window.resize(visible_rect.size);
auto& window_info = tools.info_for(window);
Expand All @@ -606,7 +600,7 @@ void Node::constrain()
{
if (is_window())
{
auto& info = tools.info_for(window);
auto& info = tools.info_for(metadata->get_window());
if (window_helpers::is_window_fullscreen(info.state()))
info.clip_area(mir::optional_value<geom::Rectangle>());
else
Expand Down
11 changes: 8 additions & 3 deletions src/node.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef NODE_H
#define NODE_H

#include "window_metadata.h"
#include <mir/geometry/rectangle.h>
#include <vector>
#include <memory>
Expand Down Expand Up @@ -31,7 +32,11 @@ class Node : public std::enable_shared_from_this<Node>
{
public:
Node(miral::WindowManagerTools const& tools, geom::Rectangle const&, std::shared_ptr<MiracleConfig> const& config);
Node(miral::WindowManagerTools const& tools, geom::Rectangle const&, std::shared_ptr<Node> parent, miral::Window& window, std::shared_ptr<MiracleConfig> const& config);
Node(miral::WindowManagerTools const& tools,
geom::Rectangle const&,
std::shared_ptr<Node> parent,
std::shared_ptr<WindowMetadata> const& metadata,
std::shared_ptr<MiracleConfig> const& config);

/// Area taken up by the node including gaps.
geom::Rectangle get_logical_area();
Expand Down Expand Up @@ -90,14 +95,14 @@ class Node : public std::enable_shared_from_this<Node>
bool is_window() const { return state == NodeState::window; }
bool is_lane() const { return state == NodeState::lane; }
NodeLayoutDirection get_direction() const { return direction; }
miral::Window& get_window() { return window; }
miral::Window& get_window() { return metadata->get_window(); }
std::shared_ptr<Node> get_parent() const { return parent; }
std::vector<std::shared_ptr<Node>> const& get_sub_nodes() const { return sub_nodes; }

private:
std::shared_ptr<Node> parent;
miral::WindowManagerTools tools;
miral::Window window;
std::shared_ptr<WindowMetadata> metadata;
std::vector<std::shared_ptr<Node>> sub_nodes;
std::vector<std::shared_ptr<Node>> hidden_nodes;
NodeState state;
Expand Down
13 changes: 13 additions & 0 deletions src/window_metadata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "window_metadata.h"

using namespace miracle;

WindowMetadata::WindowMetadata(miracle::WindowType type, miral::Window const& window)
: type{type},
window{window}
{}

void WindowMetadata::associate_to_node(std::shared_ptr<Node> const& node)
{
tiling_node = node;
}
41 changes: 41 additions & 0 deletions src/window_metadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef MIRACLEWM_WINDOW_METADATA_H
#define MIRACLEWM_WINDOW_METADATA_H

#include <memory>
#include <miral/window.h>
#include <miral/window_manager_tools.h>

namespace miracle
{

class Node;

enum class WindowType
{
tiled,
floating
};

/// Applied to WindowInfo to enable
class WindowMetadata
{
public:
WindowMetadata(WindowType type, miral::Window const& window);
void associate_to_node(std::shared_ptr<Node> const&);
miral::Window& get_window() { return window; }
std::shared_ptr<Node> get_tiling_node() {
if (type == WindowType::tiled)
return tiling_node;
return nullptr;
}

private:

WindowType type;
miral::Window window;
std::shared_ptr<Node> tiling_node;
};

}

#endif //MIRACLEWM_WINDOW_METADATA_H

0 comments on commit 452ec12

Please sign in to comment.