created abstract class TilingWindowManager

`TilingWindowManager` was created from `VerticalTilingWindowManager` and
separated its functionality so that there can be a
`HorizontalTilingWindowManager` next.
This commit is contained in:
Christian Burger 2022-05-08 20:57:48 +02:00
parent 1357a7f6bf
commit 792f12c96c
7 changed files with 126 additions and 81 deletions

View file

@ -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,7 +44,7 @@ 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;"
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

72
TilingWindowManager.cpp Normal file
View file

@ -0,0 +1,72 @@
/**
* @author Christian Burger (christian@krikkel.de)
*/
#include <kNCurses/TilingWindowManager.hpp>
#include <kNCurses/Window.hpp>
#include <ncursesw/ncurses.h>
#include <algorithm>
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<Window *>::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);
}
}

View file

@ -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<Window *>::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);
}
}

View file

@ -7,7 +7,7 @@
namespace krikkel::NCurses
{
Window::Window(VerticalTilingWindowManager *windowManager)
Window::Window(TilingWindowManager *windowManager)
: NCursesWindow(0, 0, 0, 0)
{
windowManager->addWindow(this);

View file

@ -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 <ncursesw/cursesw.h>
#include <list>
#include <mutex>
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<Window *> stack, visibleStack;
void resizeWindow(Window *window, uint16_t height, uint16_t width, uint16_t y, uint16_t x);
};
}
#endif /* C51BA18F_0915_43B9_BD5D_129F0CDBC1CD */

View file

@ -6,33 +6,19 @@
#ifndef C10F5DF3_1DB4_4714_A84D_115F492F5CDC
#define C10F5DF3_1DB4_4714_A84D_115F492F5CDC
#include <ncursesw/cursesw.h>
#include <list>
#include <mutex>
#include <kNCurses/TilingWindowManager.hpp>
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<Window *> stack, visibleStack;
void resizeWindowInStack(Window *window, uint16_t y, uint16_t height, uint16_t width);
void updateLayout() override;
};
}

View file

@ -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);