From f24f535f31e66b2321995cbb4a5c98d96e6b6afb Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Sun, 19 Jul 2015 13:26:07 -0700 Subject: [PATCH] Adding utility to center windows in their parent container. --- src/el/window.cc | 8 ++++++++ src/el/window.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/src/el/window.cc b/src/el/window.cc index 630c6a5..1c5e80f 100644 --- a/src/el/window.cc +++ b/src/el/window.cc @@ -180,6 +180,14 @@ void Window::set_settings(WindowSettings settings) { Invalidate(); } +void Window::CenterInParent() { + if (!parent()) { + return; + } + Rect bounds(0, 0, parent()->rect().w, parent()->rect().h); + set_rect(rect().CenterIn(bounds).MoveIn(bounds).Clip(bounds)); +} + int Window::title_bar_height() { if (any(window_settings_ & WindowSettings::kTitleBar)) { return title_mover_.GetPreferredSize().pref_h; diff --git a/src/el/window.h b/src/el/window.h index 8532c18..a2ed3d5 100644 --- a/src/el/window.h +++ b/src/el/window.h @@ -77,6 +77,8 @@ class Window : public Element { // Sets settings for how this window should look and behave. void set_settings(WindowSettings settings); + void CenterInParent(); + // ResizeFit specifies how ResizeToFitContent should resize the window. enum class ResizeFit { kPreferred, // Fit the preferred size of all content.