From 792f12c96c433cf17517d14325e38b87fe08cdfb Mon Sep 17 00:00:00 2001 From: Christian Burger Date: Sun, 8 May 2022 20:57:48 +0200 Subject: [PATCH] created abstract class `TilingWindowManager` `TilingWindowManager` was created from `VerticalTilingWindowManager` and separated its functionality so that there can be a `HorizontalTilingWindowManager` next. --- CMakeLists.txt | 6 +- TilingWindowManager.cpp | 72 +++++++++++++++++++ VerticalTilingWindowManager.cpp | 60 ++-------------- Window.cpp | 2 +- include/kNCurses/TilingWindowManager.hpp | 39 ++++++++++ .../kNCurses/VerticalTilingWindowManager.hpp | 22 ++---- include/kNCurses/Window.hpp | 6 +- 7 files changed, 126 insertions(+), 81 deletions(-) create mode 100644 TilingWindowManager.cpp create mode 100644 include/kNCurses/TilingWindowManager.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 75cc7e7..aa1edee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ set(CMAKE_CXX_STANDARD 17) include(CTest) enable_testing() -add_library(kNCurses Window.cpp PtyWindow.cpp SingleUserInput.cpp Debug.cpp VerticalTilingWindowManager.cpp) +add_library(kNCurses Window.cpp PtyWindow.cpp SingleUserInput.cpp Debug.cpp TilingWindowManager.cpp VerticalTilingWindowManager.cpp) ### path to own system includes include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/include") @@ -44,8 +44,8 @@ target_link_libraries(kNCursesDemoApp kNCurses) ### installation and packaging set(NCURSES_SYSTEM_INCLUDE "include/kNCurses") -set_target_properties(kNCurses PROPERTIES PUBLIC_HEADER "${NCURSES_SYSTEM_INCLUDE}/Window.hpp;${NCURSES_SYSTEM_INCLUDE}/SingleUserInput.hpp;${NCURSES_SYSTEM_INCLUDE}/PtyWindow.hpp;${NCURSES_SYSTEM_INCLUDE}/VerticalTilingWindowManager.hpp;" - VERSION "${CMAKE_PROJECT_VERSION}") +set_target_properties(kNCurses PROPERTIES PUBLIC_HEADER "${NCURSES_SYSTEM_INCLUDE}/Window.hpp;${NCURSES_SYSTEM_INCLUDE}/SingleUserInput.hpp;${NCURSES_SYSTEM_INCLUDE}/PtyWindow.hpp;${NCURSES_SYSTEM_INCLUDE}/VerticalTilingWindowManager.hpp;${NCURSES_SYSTEM_INCLUDE}/TilingWindowManager.hpp;" + VERSION "${CMAKE_PROJECT_VERSION}") include(GNUInstallDirs) install(TARGETS kNCurses ARCHIVE PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/kNCurses/") diff --git a/TilingWindowManager.cpp b/TilingWindowManager.cpp new file mode 100644 index 0000000..1e0abf0 --- /dev/null +++ b/TilingWindowManager.cpp @@ -0,0 +1,72 @@ +/** + * @author Christian Burger (christian@krikkel.de) + */ + +#include +#include +#include +#include + +namespace krikkel::NCurses +{ + using std::list; + using std::recursive_mutex; + using std::scoped_lock; + using std::find; + + TilingWindowManager::TilingWindowManager(NCursesWindow *rootWindow, recursive_mutex *ncursesMutex) + : rootWindow(new Window(*rootWindow)), ncursesMutex(ncursesMutex) + {} + + void TilingWindowManager::addWindow(Window *window) + { + stack.push_back(window); + visibleStack.push_back(window); + } + + void TilingWindowManager::hideWindow(Window *window) + { + visibleStack.remove(window); + window->hidden = true; + } + + void TilingWindowManager::showWindow(Window *window) + { + if(!window->hidden) + return; + + list::iterator currentVisibleWindow = visibleStack.begin(); + for(Window *currentWindow : stack) + { + if(currentWindow == window) + { + visibleStack.insert(currentVisibleWindow, window); + window->hidden = false; + break; + } + if(currentWindow != (*currentVisibleWindow)) + continue; + ++currentVisibleWindow; + } + } + + void TilingWindowManager::refresh() + { + scoped_lock lock(*ncursesMutex); + + rootWindow->refresh(); + for(Window *window : visibleStack) + window->refresh(); + } + + SingleUserInput TilingWindowManager::readSingleUserInput() + { + return rootWindow->readSingleUserInput(); + } + + void TilingWindowManager::resizeWindow(Window *window, uint16_t height, uint16_t width, uint16_t y, uint16_t x) + { + window->resize(height, width); + window->mvwin(y, x); + } +} \ No newline at end of file diff --git a/VerticalTilingWindowManager.cpp b/VerticalTilingWindowManager.cpp index cbae701..abbd6fd 100644 --- a/VerticalTilingWindowManager.cpp +++ b/VerticalTilingWindowManager.cpp @@ -14,56 +14,10 @@ namespace krikkel::NCurses using std::scoped_lock; using std::find; - VerticalTilingWindowManager::VerticalTilingWindowManager(NCursesWindow *rootWindow, recursive_mutex *ncursesMutex) - : rootWindow(new Window(*rootWindow)), ncursesMutex(ncursesMutex) + VerticalTilingWindowManager::VerticalTilingWindowManager(NCursesWindow *rootWindow, std::recursive_mutex *ncursesMutex) + : TilingWindowManager(rootWindow, ncursesMutex) {} - void VerticalTilingWindowManager::addWindow(Window *window) - { - stack.push_back(window); - visibleStack.push_back(window); - } - - void VerticalTilingWindowManager::hideWindow(Window *window) - { - visibleStack.remove(window); - window->hidden = true; - } - - void VerticalTilingWindowManager::showWindow(Window *window) - { - if(!window->hidden) - return; - - list::iterator currentVisibleWindow = visibleStack.begin(); - for(Window *currentWindow : stack) - { - if(currentWindow == window) - { - visibleStack.insert(currentVisibleWindow, window); - window->hidden = false; - break; - } - if(currentWindow != (*currentVisibleWindow)) - continue; - ++currentVisibleWindow; - } - } - - void VerticalTilingWindowManager::refresh() - { - scoped_lock lock(*ncursesMutex); - - rootWindow->refresh(); - for(Window *window : visibleStack) - window->refresh(); - } - - SingleUserInput VerticalTilingWindowManager::readSingleUserInput() - { - return rootWindow->readSingleUserInput(); - } - void VerticalTilingWindowManager::updateLayout() { size_t stackSize = visibleStack.size(); @@ -87,19 +41,13 @@ namespace krikkel::NCurses ; index < visibleStack.size() - 1 ; ++index) { - resizeWindowInStack(*it++, y, windowHeight, availableWidth); + resizeWindow(*it++, windowHeight, availableWidth, y, 0); y += windowHeight; { rootWindow->move(y++, 0); rootWindow->hline(availableWidth); } } - resizeWindowInStack(*it, y, availableHeight - y, availableWidth); - } - - void VerticalTilingWindowManager::resizeWindowInStack(Window *window, uint16_t y, uint16_t height, uint16_t width) - { - window->resize(height, width); - window->mvwin(y, 0); + resizeWindow(*it, availableHeight - y, availableWidth, y, 0); } } \ No newline at end of file diff --git a/Window.cpp b/Window.cpp index 89a127a..1f721b2 100644 --- a/Window.cpp +++ b/Window.cpp @@ -7,7 +7,7 @@ namespace krikkel::NCurses { - Window::Window(VerticalTilingWindowManager *windowManager) + Window::Window(TilingWindowManager *windowManager) : NCursesWindow(0, 0, 0, 0) { windowManager->addWindow(this); diff --git a/include/kNCurses/TilingWindowManager.hpp b/include/kNCurses/TilingWindowManager.hpp new file mode 100644 index 0000000..7c336ae --- /dev/null +++ b/include/kNCurses/TilingWindowManager.hpp @@ -0,0 +1,39 @@ +/** + * @brief Window manager + * @author Christian Burger (christian@krikkel.de) + */ + +#ifndef C51BA18F_0915_43B9_BD5D_129F0CDBC1CD +#define C51BA18F_0915_43B9_BD5D_129F0CDBC1CD + +#include +#include +#include + +namespace krikkel::NCurses +{ + class Window; + class SingleUserInput; + + class TilingWindowManager + { + public: + TilingWindowManager(NCursesWindow *rootWindow, std::recursive_mutex *ncursesMutex); + void addWindow(Window *window); + void refresh(); + virtual void updateLayout() = 0; + + void hideWindow(Window *window); + void showWindow(Window *window); + SingleUserInput readSingleUserInput(); + + protected: + Window *rootWindow; + std::recursive_mutex *ncursesMutex; + std::list stack, visibleStack; + + void resizeWindow(Window *window, uint16_t height, uint16_t width, uint16_t y, uint16_t x); + }; +} + +#endif /* C51BA18F_0915_43B9_BD5D_129F0CDBC1CD */ diff --git a/include/kNCurses/VerticalTilingWindowManager.hpp b/include/kNCurses/VerticalTilingWindowManager.hpp index 0d16f53..f101b71 100644 --- a/include/kNCurses/VerticalTilingWindowManager.hpp +++ b/include/kNCurses/VerticalTilingWindowManager.hpp @@ -6,33 +6,19 @@ #ifndef C10F5DF3_1DB4_4714_A84D_115F492F5CDC #define C10F5DF3_1DB4_4714_A84D_115F492F5CDC -#include -#include -#include +#include namespace krikkel::NCurses { class Window; class SingleUserInput; - class VerticalTilingWindowManager + + class VerticalTilingWindowManager : public TilingWindowManager { public: VerticalTilingWindowManager(NCursesWindow *rootWindow, std::recursive_mutex *ncursesMutex); - void addWindow(Window *window); - void refresh(); - void updateLayout(); - - void hideWindow(Window *window); - void showWindow(Window *window); - SingleUserInput readSingleUserInput(); - - private: - Window *rootWindow; - std::recursive_mutex *ncursesMutex; - std::list stack, visibleStack; - - void resizeWindowInStack(Window *window, uint16_t y, uint16_t height, uint16_t width); + void updateLayout() override; }; } diff --git a/include/kNCurses/Window.hpp b/include/kNCurses/Window.hpp index 26e8bc3..a8ae8b2 100644 --- a/include/kNCurses/Window.hpp +++ b/include/kNCurses/Window.hpp @@ -36,14 +36,14 @@ inline int UNDEF(get_wch)(wint_t *character) { get_wch(character); } namespace krikkel::NCurses { - class VerticalTilingWindowManager; + class TilingWindowManager; class Window : public NCursesWindow { - friend class VerticalTilingWindowManager; + friend class TilingWindowManager; public: - Window(VerticalTilingWindowManager *windowManager); + Window(TilingWindowManager *windowManager); Window(const NCursesWindow &window); Window(int lines, int columns, int y, int x); int addnwstr(const wchar_t *wstr, int n);