Skip to content

Commit fa2811d

Browse files
Allow decoration authors to pass custom geometry
1 parent 550966a commit fa2811d

File tree

6 files changed

+53
-24
lines changed

6 files changed

+53
-24
lines changed

examples/miral-shell/decoration/decoration.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,13 @@ auto resize_edge_rect(
291291
case mir_resize_edge_north:
292292
{
293293
auto rect = window_state.titlebar_rect();
294-
rect.size.height = window_state.bottom_border_height();
294+
295+
// Cap the resize edge to half of the titlebar height
296+
auto bottom_height = window_state.bottom_border_height();
297+
auto top_height = window_state.titlebar_height();
298+
auto const min_titlebar_drag_height = top_height / 2;
299+
300+
rect.size.height = std::min(min_titlebar_drag_height, bottom_height);
295301
return rect;
296302
}
297303

@@ -369,10 +375,22 @@ void UserDecoration::focused()
369375
auto UserDecoration::create_manager(mir::Server& server)
370376
-> std::shared_ptr<miral::DecorationManagerAdapter>
371377
{
378+
using namespace mir::geometry;
379+
// Shared between all decorations.
380+
auto const custom_geometry = std::make_shared<StaticGeometry>(StaticGeometry{
381+
.titlebar_height = Height{24},
382+
.side_border_width = geom::Width{6},
383+
.bottom_border_height = geom::Height{6},
384+
.resize_corner_input_size = geom::Size{16, 16},
385+
.button_width = geom::Width{24},
386+
.padding_between_buttons = geom::Width{6},
387+
});
388+
372389
return miral::DecorationBasicManager(
373390
server,
374-
[](auto, auto)
391+
[custom_geometry](auto, auto)
375392
{
393+
376394
auto decoration = std::make_shared<UserDecoration>();
377395
auto decoration_adapter = std::make_shared<miral::DecorationAdapter>(
378396
decoration->redraw_notifier(),
@@ -456,9 +474,9 @@ auto UserDecoration::create_manager(mir::Server& server)
456474
decoration->input_manager.update_window_state(*window_state);
457475
});
458476

477+
decoration_adapter->set_custom_geometry(custom_geometry);
478+
459479
return decoration_adapter;
460480
})
461481
.to_adapter();
462482
}
463-
464-
// TODO User defined static_geometry?

include/miral/miral/decoration_adapter.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
// TODO one more layer of indirection
2323
#include "mir/geometry/forward.h"
2424
#include "mir/geometry/size.h"
25-
#include "mir/geometry/point.h"
2625
#include "mir_toolkit/common.h"
2726

2827
#include "miral/decoration_basic_manager.h"
2928
#include "miral/decoration_manager_builder.h"
29+
#include "miral/decoration_window_state.h"
3030

3131
#include <functional>
3232
#include <glm/gtc/constants.hpp>
@@ -260,6 +260,8 @@ struct DecorationAdapter
260260
std::function<void(ms::Surface const* window_surface, std::string const& /*name*/)> window_renamed,
261261
std::function<void(std::shared_ptr<WindowState>)> update_decoration_window_state);
262262

263+
void set_custom_geometry(std::shared_ptr<StaticGeometry> geometry);
264+
263265
std::shared_ptr<msd::Decoration> to_decoration() const;
264266

265267
void init(

include/miral/miral/decoration_window_state.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "mir/geometry/point.h"
2424
#include "mir/geometry/size.h"
2525
#include "mir_toolkit/common.h"
26+
#include <memory>
2627

2728
namespace mir::scene { class Surface; }
2829

@@ -48,16 +49,12 @@ struct StaticGeometry
4849
///< surface is considered a resize corner)
4950
geometry::Width const button_width; ///< The width of window control buttons
5051
geometry::Width const padding_between_buttons; ///< The gep between titlebar buttons
51-
geometry::Height const title_font_height; ///< Height of the text used to render the window title
52-
geometry::Point const title_font_top_left; ///< Where to render the window title
53-
geometry::Displacement const icon_padding; ///< Padding inside buttons around icons
54-
geometry::Width const icon_line_width; ///< Width for lines in button icons
5552
};
5653

5754
class WindowState
5855
{
5956
public:
60-
WindowState(StaticGeometry const& static_geometry, ms::Surface const* surface);
57+
WindowState(std::shared_ptr<StaticGeometry> const& static_geometry, ms::Surface const* surface);
6158

6259
auto window_size() const -> geometry::Size;
6360
auto focused_state() const -> MirWindowFocusState;
@@ -85,7 +82,7 @@ class WindowState
8582
WindowState(WindowState const&) = delete;
8683
WindowState& operator=(WindowState const&) = delete;
8784

88-
StaticGeometry const static_geometry; // TODO: this can be shared between all instance of a decoration
85+
std::shared_ptr<StaticGeometry> const static_geometry; // TODO: this can be shared between all instance of a decoration
8986
geometry::Size const window_size_;
9087
BorderType const border_type_;
9188
MirWindowFocusState const focus_state_;

src/miral/decoration_adapter.cpp

+18-7
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ StaticGeometry const default_geometry{
196196
geom::Size{16, 16}, // resize_corner_input_size
197197
geom::Width{24}, // button_width
198198
geom::Width{6}, // padding_between_buttons
199-
geom::Height{14}, // title_font_height
200-
geom::Point{8, 2}, // title_font_top_left
201-
geom::Displacement{5, 5}, // icon_padding
202-
geom::Width{1}, // detail_line_width
203199
};
204200

205201
miral::Renderer::Renderer(
@@ -445,10 +441,13 @@ struct miral::DecorationAdapter::Impl : public msd::Decoration
445441
window_state_updated(window_surface);
446442
window_renamed(args...);
447443
on_update_decoration_window_state(window_state);
448-
}}
444+
}},
445+
geometry{std::make_shared<StaticGeometry>(default_geometry)} // I could use a default parameter, but I don't like those...
449446
{
450447
}
451448

449+
void set_custom_geometry(std::shared_ptr<StaticGeometry> geometry);
450+
452451
void init(
453452
std::shared_ptr<ms::Surface> window_surface,
454453
std::shared_ptr<ms::Surface> decoration_surface,
@@ -495,6 +494,8 @@ struct miral::DecorationAdapter::Impl : public msd::Decoration
495494
std::function<void(ms::Surface const* window_surface, mir::geometry::Size const& /*window_size*/)>
496495
on_window_resized_to;
497496
std::function<void(ms::Surface const* window_surface, std::string const& /*name*/)> on_window_renamed;
497+
498+
std::shared_ptr<StaticGeometry> geometry;
498499
};
499500

500501
miral::DecorationAdapter::DecorationAdapter(
@@ -602,6 +603,16 @@ void miral::DecorationAdapter::Impl::update()
602603
renderer->update_render_submit(window_state);
603604
}
604605

606+
void miral::DecorationAdapter::set_custom_geometry(std::shared_ptr<StaticGeometry> geometry)
607+
{
608+
impl->set_custom_geometry(geometry);
609+
}
610+
611+
void miral::DecorationAdapter::Impl::set_custom_geometry(std::shared_ptr<StaticGeometry> geometry)
612+
{
613+
this->geometry = geometry;
614+
}
615+
605616
void miral::DecorationAdapter::Impl::init(
606617
std::shared_ptr<ms::Surface> window_surface,
607618
std::shared_ptr<ms::Surface> decoration_surface,
@@ -613,7 +624,7 @@ void miral::DecorationAdapter::Impl::init(
613624
this->shell = shell;
614625
this->session = window_surface->session().lock();
615626
this->decoration_surface = decoration_surface;
616-
this->window_state = std::make_shared<WindowState>(default_geometry, window_surface.get());
627+
this->window_state = std::make_shared<WindowState>(geometry, window_surface.get());
617628

618629
renderer = std::make_unique<Renderer>(window_surface, buffer_allocator, render_titlebar, render_left_border, render_right_border, render_bottom_border);
619630
input_adapter = std::make_unique<InputResolverAdapter>(
@@ -640,7 +651,7 @@ void miral::DecorationAdapter::Impl::init(
640651

641652
void miral::DecorationAdapter::Impl::window_state_updated(std::shared_ptr<ms::Surface> const window_surface)
642653
{
643-
window_state = std::make_shared<WindowState>(default_geometry, window_surface.get());
654+
window_state = std::make_shared<WindowState>(geometry, window_surface.get());
644655
}
645656

646657
miral::DecorationAdapter::DecorationAdapter::Impl::~Impl()

src/miral/decoration_window_state.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ auto border_type_for(MirWindowType type, MirWindowState state) -> BorderType
6464
return {};
6565
}
6666

67-
WindowState::WindowState(StaticGeometry const& static_geometry, ms::Surface const* surface) :
67+
WindowState::WindowState(std::shared_ptr<StaticGeometry> const& static_geometry, ms::Surface const* surface) :
6868
static_geometry{static_geometry},
6969
window_size_{surface->window_size()},
7070
border_type_{border_type_for(surface->type(), surface->state())},
@@ -84,7 +84,7 @@ auto WindowState::titlebar_height() const -> geometry::Height
8484
{
8585
case BorderType::Full:
8686
case BorderType::Titlebar:
87-
return static_geometry.titlebar_height;
87+
return static_geometry->titlebar_height;
8888
case BorderType::None:
8989
return geometry::Height{0};
9090
}
@@ -98,7 +98,7 @@ auto WindowState::side_border_width() const -> geometry::Width
9898
switch(border_type_)
9999
{
100100
case BorderType::Full:
101-
return static_geometry.side_border_width;
101+
return static_geometry->side_border_width;
102102
case BorderType::Titlebar:
103103
case BorderType::None:
104104
return geometry::Width{0};
@@ -117,7 +117,7 @@ auto WindowState::bottom_border_height() const -> geometry::Height
117117
switch(border_type_)
118118
{
119119
case BorderType::Full:
120-
return static_geometry.bottom_border_height;
120+
return static_geometry->bottom_border_height;
121121
case BorderType::Titlebar:
122122
case BorderType::None:
123123
return geometry::Height{0};
@@ -176,12 +176,12 @@ auto WindowState::window_name() const -> std::string
176176

177177
auto WindowState::geometry() const -> StaticGeometry
178178
{
179-
return static_geometry;
179+
return *static_geometry;
180180
}
181181

182182
auto WindowState::resize_corner_input_size() const -> geometry::Size
183183
{
184-
return static_geometry.resize_corner_input_size;
184+
return static_geometry->resize_corner_input_size;
185185
}
186186

187187
auto WindowState::border_type() const -> BorderType

src/miral/symbols.map

+1
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ global:
535535
miral::DecorationAdapter::DecorationAdapter*;
536536
miral::DecorationAdapter::init*;
537537
miral::DecorationAdapter::redraw_notifier*;
538+
miral::DecorationAdapter::set_custom_geometry*;
538539
miral::DecorationAdapter::to_decoration*;
539540
miral::DecorationAdapter::update*;
540541
miral::DecorationBasicManager::DecorationBasicManager*;

0 commit comments

Comments
 (0)