Skip to content

Commit

Permalink
bugfix: minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkae committed Dec 29, 2023
1 parent abcb130 commit cf336c0
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main(int argc, char const* argv[])
{
window_managers,
WaylandExtensions{},
X11Support{},
X11Support{}.default_to_enabled(),
config_keymap,
external_client_launcher,
internal_client_launcher,
Expand Down
69 changes: 19 additions & 50 deletions src/miracle_window_management_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ const std::string TERMINAL = "konsole";
template <typename T>
bool is_tileable(T& requested_specification)
{
return (requested_specification.type() == mir_window_type_normal || requested_specification.type() == mir_window_type_freestyle)
&& !requested_specification.parent()
&& (requested_specification.state() == mir_window_state_restored || requested_specification.state() == mir_window_state_maximized)
&& !requested_specification.exclusive_rect().is_set();
auto t = requested_specification.type();
auto parent = requested_specification.parent();
auto state = requested_specification.state();
auto has_exclusive_rect = requested_specification.exclusive_rect().is_set();
return (t == mir_window_type_normal || t == mir_window_type_freestyle)
&& !parent
&& (state == mir_window_state_restored || state == mir_window_state_maximized)
&& !has_exclusive_rect;
}
}

Expand Down Expand Up @@ -158,14 +162,11 @@ auto MiracleWindowManagementPolicy::place_new_window(
return requested_specification;
}

void MiracleWindowManagementPolicy::advise_new_window(const miral::WindowInfo &window_info)
void MiracleWindowManagementPolicy::advise_new_window(miral::WindowInfo const& window_info)
{
miral::WindowManagementPolicy::advise_new_window(window_info);
if (is_tileable(window_info))
{
miral::WindowSpecification mods;
constrain_window(mods, window_info);
window_manager_tools.modify_window(window_info.window(), mods);
}
active_tree->tree.advise_new_window(window_info);
}

void MiracleWindowManagementPolicy::handle_window_ready(miral::WindowInfo &window_info)
Expand All @@ -175,10 +176,8 @@ void MiracleWindowManagementPolicy::handle_window_ready(miral::WindowInfo &windo
return;
}

miral::WindowSpecification mods;
constrain_window(mods, window_info);
window_manager_tools.modify_window(window_info.window(), mods);
active_tree->tree.confirm_new_window(window_info);
if (window_info.can_be_active())
window_manager_tools.select_active_window(window_info.window());
}

void MiracleWindowManagementPolicy::advise_focus_gained(const miral::WindowInfo &window_info)
Expand Down Expand Up @@ -206,6 +205,11 @@ void MiracleWindowManagementPolicy::advise_resize(miral::WindowInfo const& windo
tree->tree.advise_resize(window_info, new_size);
}

void MiracleWindowManagementPolicy::advise_move_to(miral::WindowInfo const& window_info, geom::Point top_left)
{
miral::WindowManagementPolicy::advise_move_to(window_info, top_left);
}

void MiracleWindowManagementPolicy::advise_output_create(miral::Output const& output)
{
WindowTreeOptions options = { 10, 10 };
Expand Down Expand Up @@ -253,9 +257,7 @@ void MiracleWindowManagementPolicy::handle_modify_window(
miral::WindowInfo &window_info,
const miral::WindowSpecification &modifications)
{
auto mods = modifications;
constrain_window(mods, window_info);
window_manager_tools.modify_window(window_info.window(), mods);
window_manager_tools.modify_window(window_info.window(), modifications);
}

void MiracleWindowManagementPolicy::handle_raise_window(miral::WindowInfo &window_info)
Expand Down Expand Up @@ -297,39 +299,6 @@ mir::geometry::Rectangle MiracleWindowManagementPolicy::confirm_inherited_move(
return {window_info.window().top_left()+movement, window_info.window().size()};
}

namespace
{
// Taken from miral
template<typename ValueType>
void reset(mir::optional_value<ValueType>& option)
{
if (option.is_set()) option.consume();
}

// Taken from miral
template<typename ValueType>
void set_if_needed(mir::optional_value<ValueType>& pending, ValueType const& current, ValueType const& correct)
{
if (current == correct)
{
reset(pending);
}
else
{
pending = correct;
}
}
}


void MiracleWindowManagementPolicy::constrain_window(miral::WindowSpecification& mods, miral::WindowInfo const& window_info)
{
set_if_needed(mods.min_width(), window_info.min_width(), geom::Width{0});
set_if_needed(mods.min_height(), window_info.min_height(), geom::Height{0});
set_if_needed(mods.max_width(), window_info.max_width(), geom::Width{std::numeric_limits<int>::max()});
set_if_needed(mods.max_height(), window_info.max_height(), geom::Height{std::numeric_limits<int>::max()});
}

void MiracleWindowManagementPolicy::advise_application_zone_create(miral::Zone const& application_zone)
{
for (auto tree : tree_list)
Expand Down
3 changes: 1 addition & 2 deletions src/miracle_window_management_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class MiracleWindowManagementPolicy : public miral::WindowManagementPolicy
void advise_focus_lost(miral::WindowInfo const& window_info) override;
void advise_delete_window(miral::WindowInfo const& window_info) override;
void advise_resize(miral::WindowInfo const& window_info, geom::Size const& new_size) override;
void advise_move_to(miral::WindowInfo const& window_info, geom::Point top_left) override;
void advise_output_create(miral::Output const& output);
void advise_output_update(miral::Output const& updated, miral::Output const& original);
void advise_output_delete(miral::Output const& output);
Expand Down Expand Up @@ -78,8 +79,6 @@ class MiracleWindowManagementPolicy : public miral::WindowManagementPolicy
miral::WindowManagerTools window_manager_tools;
miral::ExternalClientLauncher const external_client_launcher;
miral::InternalClientLauncher const internal_client_launcher;

void constrain_window(miral::WindowSpecification&, miral::WindowInfo const&);
};
}

Expand Down
46 changes: 42 additions & 4 deletions src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

using namespace miracle;

Node::Node(geom::Rectangle area, int gap_x, int gap_y)
: state{NodeState::lane},
Node::Node(miral::WindowManagerTools const& tools, geom::Rectangle area, int gap_x, int gap_y)
: tools{tools},
state{NodeState::lane},
logical_area{area},
gap_x{gap_x},
gap_y{gap_y}
{}

Node::Node(geom::Rectangle area, std::shared_ptr<Node> parent, miral::Window &window, int gap_x, int gap_y)
: parent{parent},
Node::Node(miral::WindowManagerTools const& tools,geom::Rectangle area, std::shared_ptr<Node> parent, miral::Window &window, int gap_x, int gap_y)
: tools{tools},
parent{parent},
window{window},
state{NodeState::window},
logical_area{area},
Expand Down Expand Up @@ -141,6 +143,7 @@ void Node::add_window(miral::Window& new_window)
};

auto node = std::make_shared<Node>(
tools,
new_logical_area,
shared_from_this(),
new_window,
Expand Down Expand Up @@ -292,6 +295,7 @@ std::shared_ptr<Node> Node::to_lane()
return parent->sub_nodes[0];

auto seed_node = std::make_shared<Node>(
tools,
logical_area,
shared_from_this(),
window,
Expand Down Expand Up @@ -444,3 +448,37 @@ std::shared_ptr<Node> Node::find_where(std::function<bool(std::shared_ptr<Node>)

return nullptr;
}

int Node::get_min_width()
{
if (is_window())
{
miral::WindowInfo& info = tools.info_for(window);
return info.min_width().as_int();
}

int min_width = 50;
for (auto node : sub_nodes)
{
min_width = std::max(node->get_min_width(), min_width);
}

return min_width;
}

int Node::get_min_height()
{
if (is_window())
{
miral::WindowInfo& info = tools.info_for(window);
return info.min_height().as_int();
}

int min_height = 50;
for (auto node : sub_nodes)
{
min_height = std::max(node->get_min_height(), min_height);
}

return min_height;
}
9 changes: 7 additions & 2 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include <memory>
#include <miral/window.h>
#include <miral/window_manager_tools.h>
#include <functional>

namespace geom = mir::geometry;
Expand All @@ -28,8 +29,8 @@ enum class NodeLayoutDirection
class Node : public std::enable_shared_from_this<Node>
{
public:
Node(geom::Rectangle, int gap_x, int gap_y);
Node(geom::Rectangle, std::shared_ptr<Node> parent, miral::Window& window, int gap_x, int gap_y);
Node(miral::WindowManagerTools const& tools, geom::Rectangle, int gap_x, int gap_y);
Node(miral::WindowManagerTools const& tools,geom::Rectangle, std::shared_ptr<Node> parent, miral::Window& window, int gap_x, int gap_y);

/// The rectangle defined by the node can be retrieved dynamically
/// by calculating the dimensions of the content in this node
Expand Down Expand Up @@ -84,7 +85,11 @@ class Node : public std::enable_shared_from_this<Node>
int get_gap_x() { return gap_x; }
int get_gap_y() { return gap_y; }

int get_min_width();
int get_min_height();

private:
miral::WindowManagerTools tools;
miral::Window window;
std::vector<std::shared_ptr<Node>> sub_nodes;
NodeState state;
Expand Down
40 changes: 20 additions & 20 deletions src/window_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ WindowTree::WindowTree(
geom::Rectangle default_area,
miral::WindowManagerTools const& tools,
WindowTreeOptions const& options)
: root_lane{std::make_shared<Node>(geom::Rectangle{default_area.top_left, default_area.size}, options.gap_x, options.gap_y)},
: root_lane{std::make_shared<Node>(
tools,
geom::Rectangle{default_area.top_left, default_area.size},
options.gap_x, options.gap_y)},
tools{tools},
area{default_area},
options{options}
Expand All @@ -39,23 +42,14 @@ miral::WindowSpecification WindowTree::allocate_position(const miral::WindowSpec
return new_spec;
}

void WindowTree::confirm_new_window(miral::WindowInfo &window_info)
void WindowTree::advise_new_window(miral::WindowInfo const& window_info)
{
get_active_lane()->add_window(window_info.window());

if (window_info.can_be_active())
tools.select_active_window(window_info.window());
}

void WindowTree::toggle_resize_mode()
{
if (is_resizing)
{
is_resizing = false;
return;
}

is_resizing = true;
is_resizing = !is_resizing;
}

bool WindowTree::try_resize_active_window(miracle::Direction direction)
Expand All @@ -73,6 +67,9 @@ bool WindowTree::try_resize_active_window(miracle::Direction direction)

bool WindowTree::try_select_next(miracle::Direction direction)
{
if (is_resizing)
return false;

if (!active_window)
return false;

Expand Down Expand Up @@ -166,21 +163,25 @@ void WindowTree::handle_direction_request(NodeLayoutDirection direction)

void WindowTree::advise_focus_gained(miral::Window& window)
{
if (is_resizing)
is_resizing = false;
is_resizing = false;

// The node that we find will be the window, so its parent must be the lane
auto found_node = root_lane->find_node_for_window(window);
if (!found_node)
{
active_window = nullptr;
return;
}

active_window = found_node;
}

void WindowTree::advise_focus_lost(miral::Window&)
void WindowTree::advise_focus_lost(miral::Window& window)
{
if (is_resizing)
is_resizing = false;
is_resizing = false;

if (active_window != nullptr && active_window->get_window() == window)
active_window = nullptr;
}

void WindowTree::advise_delete_window(miral::Window& window)
Expand Down Expand Up @@ -378,7 +379,6 @@ void WindowTree::resize_node_in_direction(
return;
}

constexpr int MIN_SIZE = 50;
bool is_negative = direction == Direction::left || direction == Direction::up;
auto resize_amount = is_negative ? -amount : amount;
auto nodes = parent->get_sub_nodes();
Expand All @@ -401,7 +401,7 @@ void WindowTree::resize_node_in_direction(
other_rect.top_left.y = geom::Y{prev_rect.top_left.y.as_int() + prev_rect.size.height.as_int()};
}

if (other_rect.size.height.as_int() <= MIN_SIZE)
if (other_rect.size.height.as_int() <= other_node->get_min_height())
{
std::cerr << "Unable to resize a rectangle that would cause another to be negative\n";
return;
Expand All @@ -428,7 +428,7 @@ void WindowTree::resize_node_in_direction(
other_rect.top_left.x = geom::X{prev_rect.top_left.x.as_int() + prev_rect.size.width.as_int()};
}

if (other_rect.size.width.as_int() <= MIN_SIZE)
if (other_rect.size.width.as_int() <= other_node->get_min_width())
{
std::cerr << "Unable to resize a rectangle that would cause another to be negative\n";
return;
Expand Down
3 changes: 1 addition & 2 deletions src/window_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class WindowTree
/// Makes space for the new window and returns its specified spot in the world.
miral::WindowSpecification allocate_position(const miral::WindowSpecification &requested_specification);

/// Confirms the position of this window in the previously allocated position.
void confirm_new_window(miral::WindowInfo&);
void advise_new_window(miral::WindowInfo const&);

/// Places us into resize mode. Other operations are prohibited while we are in resize mode.
void toggle_resize_mode();
Expand Down

0 comments on commit cf336c0

Please sign in to comment.