Skip to content
Repos

rgui

C++ 6.5 KB90 lines Opens in site

Directory Raw Download

// test_window_manager.cpp - §13.3.8 window manager (§5)
#include "../tttest.hpp"
#include "../ttgui.hpp"
using namespace ttgui;
TEST_CASE("wm: open/close/z-order/raise-on-click", "[wm][integration]"){
  WindowManager wm; Rect wa{0,1,80,22}; wm.workArea=wa;
  auto* a=wm.createWindow("A","grey",{1,2,20,6}); auto* b=wm.createWindow("B","grey",{3,3,20,6});
  CHECK_EQ(wm.windows.size(),(size_t)2); CHECK(wm.focusedWin==b); // newest on top
  wm.focusWindow(a); CHECK(wm.windows.back().get()==a); CHECK(wm.focusedWin==a);
  CHECK(wm.windowAt({5,3})!=nullptr);
  wm.closeWindow(b); CHECK(b->closing); CHECK_EQ(wm.visibleWindows().size(),(size_t)1); // deferred: hidden at once
  wm.processPendingCloses(); CHECK_EQ(wm.windows.size(),(size_t)1);
}
TEST_CASE("wm: drag by caption, Esc-cancel, off-screen clamp", "[wm][integration]"){
  Config cfg=Config::defaults(); WindowManager wm; wm.cfg=&cfg; wm.workArea={0,1,80,22};
  auto* w=wm.createWindow("D","grey",{10,5,20,6});
  wm.startDrag(w,{12,5}); wm.updateDrag({20,8}); CHECK_EQ(w->bounds.x,18); CHECK_EQ(w->bounds.y,8);
  wm.cancelDrag(); CHECK_EQ(w->bounds.x,10); CHECK_EQ(w->bounds.y,5); // Esc-cancel restores
  wm.startDrag(w,{12,5}); wm.updateDrag({-100,-100}); Rect c=wm.constrain(w->bounds); CHECK_GE(c.y,wm.workArea.y); CHECK(c.x > -c.w); // caption reachable
  wm.endDrag();
}
TEST_CASE("wm: snap to edge and to another window, resize min clamp", "[wm][integration]"){
  Config cfg=Config::defaults(); cfg.snapDistance=2; WindowManager wm; wm.cfg=&cfg; wm.workArea={0,1,80,22};
  auto* a=wm.createWindow("A","grey",{0,1,20,6}); (void)a;
  Rect r{1,2,20,6}; wm.snapRect(r); CHECK_EQ(r.x,0); // within snap of left edge
  auto* w=wm.createWindow("R","grey",{10,5,20,6});
  wm.startResize(w,{29,10}); wm.updateDrag({15,6}); CHECK_GE(w->bounds.w,12); CHECK_GE(w->bounds.h,3); wm.endDrag();
  wm.startResize(w,{29,10}); wm.updateDrag({50,20}); CHECK_GT(w->bounds.w,20); wm.endDrag();
}
TEST_CASE("wm: shade/sticky/minimize/maximize round-trips, sticky survives close-all", "[wm][integration]"){
  WindowManager wm; wm.workArea={0,1,80,22};
  auto* w=wm.createWindow("S","grey",{10,5,30,10}); auto* st=wm.createWindow("Sticky","grey",{1,2,20,6}); st->sticky=true;
  w->shaded=true; CHECK_EQ(w->contentRect().h,0); w->shaded=false; CHECK_GT(w->contentRect().h,0);
  Rect orig=w->bounds; wm.maximize(w); CHECK(w->maximized); CHECK_EQ(w->bounds.w,80); wm.maximize(w); CHECK_FALSE(w->maximized); CHECK_EQ(w->bounds.w,orig.w);
  w->minimized=true; CHECK(wm.visibleWindows().size()>=1); bool stickyVisible=false; for(auto*v:wm.visibleWindows()) if(v==st) stickyVisible=true; CHECK(stickyVisible);
  w->minimized=false; wm.closeAllNonSticky(); bool stickyAlive=false; for(auto&v:wm.windows) if(v.get()==st) stickyAlive=true; CHECK(stickyAlive);
}
TEST_CASE("wm: workspace visibility, tasklist/pager agreement, serialization", "[wm][integration]"){
  Config cfg=Config::defaults(); WindowManager wm; wm.cfg=&cfg; wm.workArea={0,1,80,22};
  auto* w1=wm.createWindow("WS1","grey",{1,2,20,6}); w1->workspace=0;
  auto* w2=wm.createWindow("WS2","grey",{1,2,20,6}); w2->workspace=1;
  wm.currentWorkspace=0;
  auto vis=wm.visibleWindows(); CHECK_EQ(vis.size(),(size_t)1); CHECK(vis[0]==w1);
  auto tl=wm.tasklist(); CHECK_EQ(tl.size(),(size_t)1);
  wm.switchWorkspace(1); auto vis2=wm.visibleWindows(); CHECK_EQ(vis2.size(),(size_t)1); CHECK(vis2[0]==w2);
  // sticky visible on all workspaces
  w1->sticky=true; CHECK_EQ(wm.visibleWindows().size(),(size_t)2);
  auto ser=w2->serialize(); Window copy; copy.deserialize(ser); CHECK_EQ(copy.title,std::string("WS2")); CHECK_EQ(copy.workspace,1); CHECK_EQ(copy.bounds.w,w2->bounds.w);
  // modal flag is carried
  w2->modal=true; CHECK(w2->modal);
  // invariants: caption reachable
  for(auto& win: wm.windows){ Rect c=wm.constrain(win->bounds); CHECK_GE(c.y,wm.workArea.y); }
}

TEST_CASE("wm: window content is inside its window and follows drag/resize (screenshot regression)", "[wm][layout][integration]"){
  // Regression: children were assigned screen coordinates and then
  // absoluteBounds() added the window position a second time, so editor /
  // terminal content rendered offset outside the window (see screenshot)
  // and stayed behind when the window was dragged.
  Config cfg=Config::defaults(); WindowManager wm; wm.cfg=&cfg; wm.workArea={0,0,80,24};
  auto* win=wm.createWindow("Mousepad - Untitled","grey",{14,5,40,12});
  auto* ed=new TextEditor(); ed->cfg=&cfg; ed->setText("Welcome to Mousepad\nEdit here.");
  win->setContent(std::unique_ptr<Widget>(ed)); win->layout();
  // content must be parent-relative: inside the frame, not at 2x offset
  CHECK_EQ(ed->bounds.x,1); CHECK_EQ(ed->bounds.y,1);
  CHECK_EQ(ed->absoluteBounds().x,15); CHECK_EQ(ed->absoluteBounds().y,6);
  CHECK_EQ(ed->absoluteBounds().w,38); CHECK_EQ(ed->absoluteBounds().h,10);
  VirtualTerminal vt(80,24); win->drawTree(vt.canvas);
  // find() returns a byte offset in the UTF-8 line, so assert exact cells:
  CHECK_EQ(vt.cellAt(20,7).ch,std::string("W")); CHECK_EQ(vt.find("Welcome").second,7);
  CHECK(vt.find("Mousepad").second==5);
  // Terminal content: same convention (its grid draws from absoluteBounds)
  auto* twin=wm.createWindow("Terminal","green",{30,3,40,12});
  auto* term=new TerminalWidget(38,10); term->cfg=&cfg; term->vt.feed("hello-term");
  twin->setContent(std::unique_ptr<Widget>(term)); twin->layout();
  CHECK_EQ(term->absoluteBounds().x,31); CHECK_EQ(term->absoluteBounds().y,4);
  VirtualTerminal vt2(80,24); twin->drawTree(vt2.canvas);
  CHECK_EQ(vt2.cellAt(31,4).ch,std::string("h")); CHECK_EQ(vt2.find("hello-term").second,4);
  // Drag the editor window: content must move by exactly the same delta
  wm.startDrag(win,{20,5}); wm.updateDrag({35,9}); wm.endDrag(); // (bottom edge would snap at y=10: 12-tall window in 24-row area)
  CHECK_EQ(win->bounds.x,29); CHECK_EQ(win->bounds.y,9);
  win->layout(); CHECK_EQ(ed->absoluteBounds().x,30); CHECK_EQ(ed->absoluteBounds().y,10);
  VirtualTerminal vt3(80,24); win->drawTree(vt3.canvas);
  CHECK_EQ(vt3.find("Welcome").second,11); CHECK_EQ(vt3.cellAt(35,11).ch,std::string("W"));
  // Resize (responsive): content grows with the window, stays inside it
  wm.startResize(win,{win->bounds.right()-1,win->bounds.bottom()-1}); wm.updateDrag({win->bounds.x+59,win->bounds.y+17}); wm.endDrag(); win->layout();
  CHECK_EQ(ed->absoluteBounds().w,win->bounds.w-2); CHECK_EQ(ed->absoluteBounds().h,win->bounds.h-2);
  CHECK(ed->absoluteBounds().right()<=win->bounds.right()-1); CHECK(ed->absoluteBounds().bottom()<=win->bounds.bottom()-1);
  CHECK(tttest::checkTreeInvariants<Widget>(win));
}
30.2ms