diff options
Diffstat (limited to 'CruUI')
-rw-r--r-- | CruUI/base.h | 4 | ||||
-rw-r--r-- | CruUI/ui/control.cpp | 23 | ||||
-rw-r--r-- | CruUI/ui/control.h | 34 | ||||
-rw-r--r-- | CruUI/ui/window.cpp | 47 | ||||
-rw-r--r-- | CruUI/ui/window.h | 38 |
5 files changed, 68 insertions, 78 deletions
diff --git a/CruUI/base.h b/CruUI/base.h index 83d1968c..861bf677 100644 --- a/CruUI/base.h +++ b/CruUI/base.h @@ -5,6 +5,7 @@ #include <folly/String.h> +#include <folly/FBVector.h> #include <folly/Function.h> #include <stdexcept> @@ -29,6 +30,9 @@ namespace cru template<typename... Args> using FlowControlAction = Function<FlowControl(Args...)>; + template<typename T> + using Vector = folly::fbvector<T>; + class Object { public: diff --git a/CruUI/ui/control.cpp b/CruUI/ui/control.cpp index 68713841..e2e6f98b 100644 --- a/CruUI/ui/control.cpp +++ b/CruUI/ui/control.cpp @@ -24,13 +24,13 @@ namespace cru { } - void Control::ForeachChild(Action<Control*>&& predicate) + void Control::ForeachChild(Action<Control*>&& predicate) const { for (const auto child : children_) predicate(child); } - void Control::ForeachChild(FlowControlAction<Control*>&& predicate) + void Control::ForeachChild(FlowControlAction<Control*>&& predicate) const { for (const auto child : children_) { @@ -39,11 +39,6 @@ namespace cru { } } - std::vector<Control*> Control::GetChildren() - { - return this->children_; - } - void AddChildCheck(Control* control) { if (control->GetParent() != nullptr) @@ -91,7 +86,7 @@ namespace cru { this->OnRemoveChild(this); } - void Control::RemoveChild(int position) + void Control::RemoveChild(const int position) { if (position < 0 || static_cast<decltype(this->children_.size())>(position) >= this->children_.size()) throw std::invalid_argument("The position is out of range."); @@ -163,18 +158,18 @@ namespace cru { window->Repaint(); } - Point Control::GetPositionAbsolute() + Point Control::GetPositionAbsolute() const { return position_cache_.lefttop_position_absolute; } - Point Control::LocalToAbsolute(const Point& point) + Point Control::LocalToAbsolute(const Point& point) const { return Point(point.x + position_cache_.lefttop_position_absolute.x, point.y + position_cache_.lefttop_position_absolute.y); } - Point Control::AbsoluteToLocal(const Point & point) + Point Control::AbsoluteToLocal(const Point & point) const { return Point(point.x - position_cache_.lefttop_position_absolute.x, point.y - position_cache_.lefttop_position_absolute.y); @@ -182,7 +177,7 @@ namespace cru { bool Control::IsPointInside(const Point & point) { - auto size = GetSize(); + const auto size = GetSize(); return point.x >= 0.0f && point.x < size.width && point.y >= 0.0f && point.y < size.height; } @@ -191,7 +186,7 @@ namespace cru { D2D1::Matrix3x2F old_transform; device_context->GetTransform(&old_transform); - auto position = GetPositionRelative(); + const auto position = GetPositionRelative(); device_context->SetTransform(old_transform * D2D1::Matrix3x2F::Translation(position.x, position.y)); OnDraw(device_context); @@ -234,7 +229,7 @@ namespace cru { OnLayout(rect); } - Size Control::GetDesiredSize() + Size Control::GetDesiredSize() const { return desired_size_; } diff --git a/CruUI/ui/control.h b/CruUI/ui/control.h index 588eda17..a87e6ce4 100644 --- a/CruUI/ui/control.h +++ b/CruUI/ui/control.h @@ -1,7 +1,6 @@ #pragma once #include "system_headers.h" -#include <vector> #include <optional> #include "base.h" @@ -50,13 +49,16 @@ namespace cru } //Traverse the children - void ForeachChild(Action<Control*>&& predicate); - void ForeachChild(FlowControlAction<Control*>&& predicate); + void ForeachChild(Action<Control*>&& predicate) const; + void ForeachChild(FlowControlAction<Control*>&& predicate) const; //Return a vector of all children. This function will create a //temporary copy of vector of children. If you just want to //traverse all children, just call ForeachChild. - std::vector<Control*> GetChildren(); + Vector<Control*> GetChildren() const + { + return children_; + } //Add a child at tail. void AddChild(Control* control); @@ -96,19 +98,19 @@ namespace cru //Get the actual size. virtual Size GetSize(); - //Set the actual size directly without relayout. + //Set the actual size directly without re-layout. virtual void SetSize(const Size& size); //Get lefttop relative to ancestor. This is only valid when //attached to window. Notice that the value is cached. //You can invalidate and recalculate it by calling "InvalidatePositionCache". - Point GetPositionAbsolute(); + Point GetPositionAbsolute() const; //Local point to absolute point. - Point LocalToAbsolute(const Point& point); + Point LocalToAbsolute(const Point& point) const; //Absolute point to local point. - Point AbsoluteToLocal(const Point& point); + Point AbsoluteToLocal(const Point& point) const; bool IsPointInside(const Point& point); @@ -131,7 +133,7 @@ namespace cru void Layout(const Rect& rect); - Size GetDesiredSize(); + Size GetDesiredSize() const; void SetDesiredSize(const Size& desired_size); @@ -226,15 +228,23 @@ namespace cru private: // Only for layout manager to use. + // Check if the old position is updated to current position. + // If not, then a notify of position change and update will + // be done. void CheckAndNotifyPositionChanged(); private: Window * window_; Control * parent_; - std::vector<Control*> children_; - - Point old_position_; + Vector<Control*> children_; + + // When position is changed and notification hasn't been + // sent, it will be the old position. When position is changed + // more than once, it will be the oldest position since last + // notification. If notification has been sent, it will be updated + // to position_. + Point old_position_; Point position_; Size size_; diff --git a/CruUI/ui/window.cpp b/CruUI/ui/window.cpp index bc7798c3..4ab38cad 100644 --- a/CruUI/ui/window.cpp +++ b/CruUI/ui/window.cpp @@ -8,7 +8,7 @@ namespace cru { namespace ui { - WindowClass::WindowClass(const std::wstring& name, WNDPROC window_proc, HINSTANCE hinstance) + WindowClass::WindowClass(const String& name, WNDPROC window_proc, HINSTANCE h_instance) : name_(name) { WNDCLASSEX window_class; @@ -18,7 +18,7 @@ namespace cru window_class.lpfnWndProc = window_proc; window_class.cbClsExtra = 0; window_class.cbWndExtra = 0; - window_class.hInstance = hinstance; + window_class.hInstance = h_instance; window_class.hIcon = LoadIcon(NULL, IDI_APPLICATION); window_class.hCursor = LoadCursor(NULL, IDC_ARROW); window_class.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); @@ -31,19 +31,6 @@ namespace cru throw std::runtime_error("Failed to create window class."); } - WindowClass::~WindowClass() - { - - } - - const wchar_t * WindowClass::GetName() { - return name_.c_str(); - } - - ATOM WindowClass::GetAtom() { - return atom_; - } - LRESULT __stdcall GeneralWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) { auto window = Application::GetInstance()->GetWindowManager()->FromHandle(hWnd); @@ -88,14 +75,6 @@ namespace cru return find_result->second; } - WindowLayoutManager::WindowLayoutManager() - { - } - - WindowLayoutManager::~WindowLayoutManager() - { - } - void WindowLayoutManager::InvalidateControlPositionCache(Control * control) { if (cache_invalid_controls_.count(control) == 1) @@ -115,13 +94,13 @@ namespace cru if (cache_invalid_controls_.size() == 1) // when insert just now and not repeat to "InvokeLater". { InvokeLater([this] { - RefreshInvalidControlPositionCache(); - for (const auto i : cache_invalid_controls_) + RefreshInvalidControlPositionCache(); // first refresh position cache. + for (const auto i : cache_invalid_controls_) // traverse all descendants of position-invalid controls and notify position change event i->TraverseDescendants([](Control* control) { control->CheckAndNotifyPositionChanged(); }); - cache_invalid_controls_.clear(); + cache_invalid_controls_.clear(); // after update and notify, clear the set. }); } } @@ -134,7 +113,7 @@ namespace cru void WindowLayoutManager::RefreshControlPositionCache(Control * control) { - Point point = Point::zero; + auto point = Point::zero; auto parent = control; while ((parent = parent->GetParent())) { const auto p = parent->GetPositionRelative(); @@ -178,20 +157,6 @@ namespace cru Close(); } - WindowLayoutManager* Window::GetLayoutManager() - { - return layout_manager_.get(); - } - - HWND Window::GetWindowHandle() - { - return hwnd_; - } - - bool Window::IsWindowValid() { - return hwnd_ != nullptr; - } - void Window::Close() { if (IsWindowValid()) DestroyWindow(hwnd_); diff --git a/CruUI/ui/window.h b/CruUI/ui/window.h index 1cb6b5f7..16427a2c 100644 --- a/CruUI/ui/window.h +++ b/CruUI/ui/window.h @@ -6,7 +6,7 @@ #include <list> #include <memory> -#include "Control.h" +#include "control.h" namespace cru { namespace graph { @@ -17,19 +17,26 @@ namespace cru { class WindowClass : public Object { public: - WindowClass(const std::wstring& name, WNDPROC window_proc, HINSTANCE h_instance); + WindowClass(const String& name, WNDPROC window_proc, HINSTANCE h_instance); WindowClass(const WindowClass& other) = delete; WindowClass(WindowClass&& other) = delete; WindowClass& operator=(const WindowClass& other) = delete; WindowClass& operator=(WindowClass&& other) = delete; - ~WindowClass() override; + ~WindowClass() override = default; - const wchar_t* GetName(); - ATOM GetAtom(); + const wchar_t* GetName() const + { + return name_.c_str(); + } + + ATOM GetAtom() const + { + return atom_; + } private: - std::wstring name_; + String name_; ATOM atom_; }; @@ -72,12 +79,12 @@ namespace cru { class WindowLayoutManager : public Object { public: - WindowLayoutManager(); + WindowLayoutManager() = default; WindowLayoutManager(const WindowLayoutManager& other) = delete; WindowLayoutManager(WindowLayoutManager&& other) = delete; WindowLayoutManager& operator=(const WindowLayoutManager& other) = delete; WindowLayoutManager& operator=(WindowLayoutManager&& other) = delete; - ~WindowLayoutManager() override; + ~WindowLayoutManager() override = default; //Mark position cache of the control and its descendants invalid, //(which is saved as an auto-managed list internal) @@ -111,16 +118,25 @@ namespace cru { public: //*************** region: managers *************** - WindowLayoutManager* GetLayoutManager(); + WindowLayoutManager* GetLayoutManager() const + { + return layout_manager_.get(); + } //*************** region: handle *************** //Get the handle of the window. Return null if window is invalid. - HWND GetWindowHandle(); + HWND GetWindowHandle() const + { + return hwnd_; + } //Return if the window is still valid, that is, hasn't been closed or destroyed. - bool IsWindowValid(); + bool IsWindowValid() const + { + return hwnd_ != nullptr; + } //*************** region: window operations *************** |