mirror of
https://github.com/hyprwm/Hyprland
synced 2024-11-15 08:05:57 +01:00
snap: add option border_overlap
and other improvements (#8289)
* snap: add option `border_overlap` and other improvements I really liked the way borders used to overlap when snapping and how only the window's main surface would snap to the monitor, so I would like to bring that behavior back, but in the form of a config option. Other improvements include: - reduced the number of snap functions from 4 down to 2, and only one ever gets called at any given time. - border size should not be added to gap size. It seemed like the right thing to do at the time, but it makes snapping feel way stronger than it actually should. - all const variables have been given the all-caps naming convention. - to avoid excessive casting, border size is declared as a double. - to avoid excessive x + w, y + h calculations. I'm using a struct called Range and working only with start and end values until the very end of the function. - check for both monitor snapping as well as reserved monitor space snapping in a relatively efficient way. * snap: always border-align for corners and reserved monitor space We probably don't want to treat reserved monitor space as if it were just a smaller monitor. Instead, it should be treated more like a borderless window, which means our window's border should never encroach upon it.
This commit is contained in:
parent
7c7a84ff60
commit
93b4478e70
3 changed files with 89 additions and 79 deletions
|
@ -122,6 +122,12 @@ inline static const std::vector<SConfigOptionDescription> CONFIG_OPTIONS = {
|
||||||
.type = CONFIG_OPTION_INT,
|
.type = CONFIG_OPTION_INT,
|
||||||
.data = SConfigOptionDescription::SRangeData{10, 0, 100},
|
.data = SConfigOptionDescription::SRangeData{10, 0, 100},
|
||||||
},
|
},
|
||||||
|
SConfigOptionDescription{
|
||||||
|
.value = "general:snap:border_overlap",
|
||||||
|
.description = "if true, windows snap such that only one border's worth of space is between them",
|
||||||
|
.type = CONFIG_OPTION_BOOL,
|
||||||
|
.data = SConfigOptionDescription::SBoolData{false},
|
||||||
|
},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* decoration:
|
* decoration:
|
||||||
|
|
|
@ -344,6 +344,7 @@ CConfigManager::CConfigManager() {
|
||||||
m_pConfig->addConfigValue("general:snap:enabled", Hyprlang::INT{0});
|
m_pConfig->addConfigValue("general:snap:enabled", Hyprlang::INT{0});
|
||||||
m_pConfig->addConfigValue("general:snap:window_gap", Hyprlang::INT{10});
|
m_pConfig->addConfigValue("general:snap:window_gap", Hyprlang::INT{10});
|
||||||
m_pConfig->addConfigValue("general:snap:monitor_gap", Hyprlang::INT{10});
|
m_pConfig->addConfigValue("general:snap:monitor_gap", Hyprlang::INT{10});
|
||||||
|
m_pConfig->addConfigValue("general:snap:border_overlap", Hyprlang::INT{0});
|
||||||
|
|
||||||
m_pConfig->addConfigValue("misc:disable_hyprland_logo", Hyprlang::INT{0});
|
m_pConfig->addConfigValue("misc:disable_hyprland_logo", Hyprlang::INT{0});
|
||||||
m_pConfig->addConfigValue("misc:disable_splash_rendering", Hyprlang::INT{0});
|
m_pConfig->addConfigValue("misc:disable_splash_rendering", Hyprlang::INT{0});
|
||||||
|
|
|
@ -389,91 +389,93 @@ void IHyprLayout::onEndDragWindow() {
|
||||||
g_pInputManager->m_bWasDraggingWindow = false;
|
g_pInputManager->m_bWasDraggingWindow = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool canSnap(const double sideA, const double sideB, const double gap) {
|
static inline bool canSnap(const double SIDEA, const double SIDEB, const double GAP) {
|
||||||
return std::abs(sideA - sideB) < gap;
|
return std::abs(SIDEA - SIDEB) < GAP;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void snapMoveLeft(double& pos, double& len, const double p) {
|
static void snapMove(double& start, double& end, const double P) {
|
||||||
pos = p;
|
end = P + (end - start);
|
||||||
|
start = P;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void snapMoveRight(double& pos, double& len, const double p) {
|
static void snapResize(double& start, double& end, const double P) {
|
||||||
pos = p - len;
|
start = P;
|
||||||
}
|
|
||||||
|
|
||||||
static void snapResizeLeft(double& pos, double& len, const double p) {
|
|
||||||
len += pos - p;
|
|
||||||
pos = p;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void snapResizeRight(double& pos, double& len, const double p) {
|
|
||||||
len = p - pos;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef std::function<void(double&, double&, const double)> SnapFn;
|
typedef std::function<void(double&, double&, const double)> SnapFn;
|
||||||
|
|
||||||
static void performSnap(Vector2D& sourcePos, Vector2D& sourceSize, PHLWINDOW DRAGGINGWINDOW, const eMouseBindMode mode, const int corner, const Vector2D& beginSize) {
|
static void performSnap(Vector2D& sourcePos, Vector2D& sourceSize, PHLWINDOW DRAGGINGWINDOW, const eMouseBindMode MODE, const int CORNER, const Vector2D& BEGINSIZE) {
|
||||||
static auto SNAPWINDOWGAP = CConfigValue<Hyprlang::INT>("general:snap:window_gap");
|
static auto SNAPWINDOWGAP = CConfigValue<Hyprlang::INT>("general:snap:window_gap");
|
||||||
static auto SNAPMONITORGAP = CConfigValue<Hyprlang::INT>("general:snap:monitor_gap");
|
static auto SNAPMONITORGAP = CConfigValue<Hyprlang::INT>("general:snap:monitor_gap");
|
||||||
|
static auto SNAPBORDEROVERLAP = CConfigValue<Hyprlang::INT>("general:snap:border_overlap");
|
||||||
|
|
||||||
const SnapFn snapRight = (mode == MBIND_MOVE) ? snapMoveRight : snapResizeRight;
|
const SnapFn SNAP = (MODE == MBIND_MOVE) ? snapMove : snapResize;
|
||||||
const SnapFn snapLeft = (mode == MBIND_MOVE) ? snapMoveLeft : snapResizeLeft;
|
int snaps = 0;
|
||||||
const SnapFn snapDown = snapRight;
|
|
||||||
const SnapFn snapUp = snapLeft;
|
|
||||||
int snaps = 0;
|
|
||||||
|
|
||||||
|
const bool OVERLAP = *SNAPBORDEROVERLAP;
|
||||||
const int DRAGGINGBORDERSIZE = DRAGGINGWINDOW->getRealBorderSize();
|
const int DRAGGINGBORDERSIZE = DRAGGINGWINDOW->getRealBorderSize();
|
||||||
CBox sourceBox = CBox{sourcePos, sourceSize}.expand(DRAGGINGBORDERSIZE);
|
|
||||||
|
struct SRange {
|
||||||
|
double start = 0;
|
||||||
|
double end = 0;
|
||||||
|
};
|
||||||
|
SRange sourceX = {sourcePos.x, sourcePos.x + sourceSize.x};
|
||||||
|
SRange sourceY = {sourcePos.y, sourcePos.y + sourceSize.y};
|
||||||
|
|
||||||
if (*SNAPWINDOWGAP) {
|
if (*SNAPWINDOWGAP) {
|
||||||
const auto WSID = DRAGGINGWINDOW->workspaceID();
|
const double GAPSIZE = *SNAPWINDOWGAP;
|
||||||
|
const auto WSID = DRAGGINGWINDOW->workspaceID();
|
||||||
|
|
||||||
for (auto& other : g_pCompositor->m_vWindows) {
|
for (auto& other : g_pCompositor->m_vWindows) {
|
||||||
if (other == DRAGGINGWINDOW || other->workspaceID() != WSID || !other->m_bIsMapped || other->m_bFadingOut || other->isX11OverrideRedirect())
|
if (other == DRAGGINGWINDOW || other->workspaceID() != WSID || !other->m_bIsMapped || other->m_bFadingOut || other->isX11OverrideRedirect())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const int BORDERSIZE = other->getRealBorderSize();
|
const int OTHERBORDERSIZE = other->getRealBorderSize();
|
||||||
const double GAPSIZE = *SNAPWINDOWGAP + BORDERSIZE;
|
const double BORDERSIZE = OVERLAP ? std::max(DRAGGINGBORDERSIZE, OTHERBORDERSIZE) : (DRAGGINGBORDERSIZE + OTHERBORDERSIZE);
|
||||||
|
|
||||||
const CBox SURFBB = other->getWindowMainSurfaceBox().expand(BORDERSIZE);
|
const CBox SURF = other->getWindowMainSurfaceBox();
|
||||||
const Vector2D end = sourceBox.pos() + sourceBox.size();
|
const SRange SURFBX = {SURF.x - BORDERSIZE, SURF.x + SURF.w + BORDERSIZE};
|
||||||
|
const SRange SURFBY = {SURF.y - BORDERSIZE, SURF.y + SURF.h + BORDERSIZE};
|
||||||
|
|
||||||
// only snap windows if their ranges intersect in the opposite axis
|
// only snap windows if their ranges overlap in the opposite axis
|
||||||
if (sourceBox.y <= SURFBB.y + SURFBB.h && SURFBB.y <= end.y) {
|
if (sourceY.start <= SURFBY.end && SURFBY.start <= sourceY.end) {
|
||||||
if (corner & (CORNER_TOPLEFT | CORNER_BOTTOMLEFT) && canSnap(sourceBox.x, SURFBB.x + SURFBB.w, GAPSIZE)) {
|
if (CORNER & (CORNER_TOPLEFT | CORNER_BOTTOMLEFT) && canSnap(sourceX.start, SURFBX.end, GAPSIZE)) {
|
||||||
snapLeft(sourceBox.x, sourceBox.w, SURFBB.x + SURFBB.w);
|
SNAP(sourceX.start, sourceX.end, SURFBX.end);
|
||||||
snaps |= SNAP_LEFT;
|
snaps |= SNAP_LEFT;
|
||||||
} else if (corner & (CORNER_TOPRIGHT | CORNER_BOTTOMRIGHT) && canSnap(end.x, SURFBB.x, GAPSIZE)) {
|
} else if (CORNER & (CORNER_TOPRIGHT | CORNER_BOTTOMRIGHT) && canSnap(sourceX.end, SURFBX.start, GAPSIZE)) {
|
||||||
snapRight(sourceBox.x, sourceBox.w, SURFBB.x);
|
SNAP(sourceX.end, sourceX.start, SURFBX.start);
|
||||||
snaps |= SNAP_RIGHT;
|
snaps |= SNAP_RIGHT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sourceBox.x <= SURFBB.x + SURFBB.w && SURFBB.x <= end.x) {
|
if (sourceX.start <= SURFBX.end && SURFBX.start <= sourceX.end) {
|
||||||
if (corner & (CORNER_TOPLEFT | CORNER_TOPRIGHT) && canSnap(sourceBox.y, SURFBB.y + SURFBB.h, GAPSIZE)) {
|
if (CORNER & (CORNER_TOPLEFT | CORNER_TOPRIGHT) && canSnap(sourceY.start, SURFBY.end, GAPSIZE)) {
|
||||||
snapUp(sourceBox.y, sourceBox.h, SURFBB.y + SURFBB.h);
|
SNAP(sourceY.start, sourceY.end, SURFBY.end);
|
||||||
snaps |= SNAP_UP;
|
snaps |= SNAP_UP;
|
||||||
} else if (corner & (CORNER_BOTTOMLEFT | CORNER_BOTTOMRIGHT) && canSnap(end.y, SURFBB.y, GAPSIZE)) {
|
} else if (CORNER & (CORNER_BOTTOMLEFT | CORNER_BOTTOMRIGHT) && canSnap(sourceY.end, SURFBY.start, GAPSIZE)) {
|
||||||
snapDown(sourceBox.y, sourceBox.h, SURFBB.y);
|
SNAP(sourceY.end, sourceY.start, SURFBY.start);
|
||||||
snaps |= SNAP_DOWN;
|
snaps |= SNAP_DOWN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// corner snapping
|
// corner snapping
|
||||||
if (sourceBox.x == SURFBB.x + SURFBB.w || SURFBB.x == sourceBox.x + sourceBox.w) {
|
const double BORDERDIFF = OTHERBORDERSIZE - DRAGGINGBORDERSIZE;
|
||||||
if (corner & (CORNER_TOPLEFT | CORNER_TOPRIGHT) && canSnap(sourceBox.y, SURFBB.y, GAPSIZE)) {
|
if (sourceX.start == SURFBX.end || SURFBX.start == sourceX.end) {
|
||||||
snapUp(sourceBox.y, sourceBox.h, SURFBB.y);
|
const SRange SURFY = {SURF.y - BORDERDIFF, SURF.y + SURF.h + BORDERDIFF};
|
||||||
|
if (CORNER & (CORNER_TOPLEFT | CORNER_TOPRIGHT) && canSnap(sourceY.start, SURFY.start, GAPSIZE)) {
|
||||||
|
SNAP(sourceY.start, sourceY.end, SURFY.start);
|
||||||
snaps |= SNAP_UP;
|
snaps |= SNAP_UP;
|
||||||
} else if (corner & (CORNER_BOTTOMLEFT | CORNER_BOTTOMRIGHT) && canSnap(end.y, SURFBB.y + SURFBB.h, GAPSIZE)) {
|
} else if (CORNER & (CORNER_BOTTOMLEFT | CORNER_BOTTOMRIGHT) && canSnap(sourceY.end, SURFY.end, GAPSIZE)) {
|
||||||
snapDown(sourceBox.y, sourceBox.h, SURFBB.y + SURFBB.h);
|
SNAP(sourceY.end, sourceY.start, SURFY.end);
|
||||||
snaps |= SNAP_DOWN;
|
snaps |= SNAP_DOWN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sourceBox.y == SURFBB.y + SURFBB.h || SURFBB.y == sourceBox.y + sourceBox.h) {
|
if (sourceY.start == SURFBY.end || SURFBY.start == sourceY.end) {
|
||||||
if (corner & (CORNER_TOPLEFT | CORNER_BOTTOMLEFT) && canSnap(sourceBox.x, SURFBB.x, GAPSIZE)) {
|
const SRange SURFX = {SURF.x - BORDERDIFF, SURF.x + SURF.w + BORDERDIFF};
|
||||||
snapLeft(sourceBox.x, sourceBox.w, SURFBB.x);
|
if (CORNER & (CORNER_TOPLEFT | CORNER_BOTTOMLEFT) && canSnap(sourceX.start, SURFX.start, GAPSIZE)) {
|
||||||
|
SNAP(sourceX.start, sourceX.end, SURFX.start);
|
||||||
snaps |= SNAP_LEFT;
|
snaps |= SNAP_LEFT;
|
||||||
} else if (corner & (CORNER_TOPRIGHT | CORNER_BOTTOMRIGHT) && canSnap(end.x, SURFBB.x + SURFBB.w, GAPSIZE)) {
|
} else if (CORNER & (CORNER_TOPRIGHT | CORNER_BOTTOMRIGHT) && canSnap(sourceX.end, SURFX.end, GAPSIZE)) {
|
||||||
snapRight(sourceBox.x, sourceBox.w, SURFBB.x + SURFBB.w);
|
SNAP(sourceX.end, sourceX.start, SURFX.end);
|
||||||
snaps |= SNAP_RIGHT;
|
snaps |= SNAP_RIGHT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -481,49 +483,50 @@ static void performSnap(Vector2D& sourcePos, Vector2D& sourceSize, PHLWINDOW DRA
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*SNAPMONITORGAP) {
|
if (*SNAPMONITORGAP) {
|
||||||
const auto MON = DRAGGINGWINDOW->m_pMonitor.lock();
|
const double GAPSIZE = *SNAPMONITORGAP;
|
||||||
const CBox mon =
|
const double BORDERSIZE = OVERLAP ? 0 : DRAGGINGBORDERSIZE;
|
||||||
CBox{MON->vecPosition.x + MON->vecReservedTopLeft.x, MON->vecPosition.y + MON->vecReservedTopLeft.y,
|
const double BORDERDIFF = DRAGGINGBORDERSIZE - BORDERSIZE;
|
||||||
MON->vecSize.x - MON->vecReservedTopLeft.x - MON->vecReservedBottomRight.x, MON->vecSize.y - MON->vecReservedBottomRight.y - MON->vecReservedTopLeft.y};
|
const auto MON = DRAGGINGWINDOW->m_pMonitor.lock();
|
||||||
const double gap = *SNAPMONITORGAP;
|
|
||||||
|
|
||||||
if (canSnap(sourceBox.x, mon.x, gap)) {
|
SRange monX = {MON->vecPosition.x + BORDERSIZE, MON->vecSize.x - BORDERSIZE};
|
||||||
snapLeft(sourceBox.x, sourceBox.w, mon.x);
|
SRange monY = {MON->vecPosition.y + BORDERSIZE, MON->vecSize.y - BORDERSIZE};
|
||||||
|
|
||||||
|
if (canSnap(sourceX.start, monX.start, GAPSIZE) || canSnap(sourceX.start, (monX.start += MON->vecReservedTopLeft.x + BORDERDIFF), GAPSIZE)) {
|
||||||
|
SNAP(sourceX.start, sourceX.end, monX.start);
|
||||||
snaps |= SNAP_LEFT;
|
snaps |= SNAP_LEFT;
|
||||||
}
|
}
|
||||||
if (canSnap(sourceBox.x + sourceBox.w, mon.x + mon.w, gap)) {
|
if (canSnap(sourceX.end, monX.end, GAPSIZE) || canSnap(sourceX.end, (monX.end -= MON->vecReservedBottomRight.x + BORDERDIFF), GAPSIZE)) {
|
||||||
snapRight(sourceBox.x, sourceBox.w, mon.w + mon.x);
|
SNAP(sourceX.end, sourceX.start, monX.end);
|
||||||
snaps |= SNAP_RIGHT;
|
snaps |= SNAP_RIGHT;
|
||||||
}
|
}
|
||||||
if (canSnap(sourceBox.y, mon.y, gap)) {
|
if (canSnap(sourceY.start, monY.start, GAPSIZE) || canSnap(sourceY.start, (monY.start += MON->vecReservedTopLeft.y + BORDERDIFF), GAPSIZE)) {
|
||||||
snapUp(sourceBox.y, sourceBox.h, mon.y);
|
SNAP(sourceY.start, sourceY.end, monY.start);
|
||||||
snaps |= SNAP_UP;
|
snaps |= SNAP_UP;
|
||||||
}
|
}
|
||||||
if (canSnap(sourceBox.y + sourceBox.h, mon.y + mon.h, gap)) {
|
if (canSnap(sourceY.end, monY.end, GAPSIZE) || canSnap(sourceY.end, (monY.end -= MON->vecReservedBottomRight.y + BORDERDIFF), GAPSIZE)) {
|
||||||
snapDown(sourceBox.y, sourceBox.h, mon.h + mon.y);
|
SNAP(sourceY.end, sourceY.start, monY.end);
|
||||||
snaps |= SNAP_DOWN;
|
snaps |= SNAP_DOWN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == MBIND_RESIZE_FORCE_RATIO) {
|
if (MODE == MBIND_RESIZE_FORCE_RATIO) {
|
||||||
const double RATIO = beginSize.y / beginSize.x;
|
if ((CORNER & (CORNER_TOPLEFT | CORNER_BOTTOMLEFT) && snaps & SNAP_LEFT) || (CORNER & (CORNER_TOPRIGHT | CORNER_BOTTOMRIGHT) && snaps & SNAP_RIGHT)) {
|
||||||
|
const double SIZEY = (sourceX.end - sourceX.start) * (BEGINSIZE.y / BEGINSIZE.x);
|
||||||
if ((corner & (CORNER_TOPLEFT | CORNER_BOTTOMLEFT) && snaps & SNAP_LEFT) || (corner & (CORNER_TOPRIGHT | CORNER_BOTTOMRIGHT) && snaps & SNAP_RIGHT)) {
|
if (CORNER & (CORNER_TOPLEFT | CORNER_TOPRIGHT))
|
||||||
const double sizeY = sourceBox.w * RATIO;
|
sourceY.start = sourceY.end - SIZEY;
|
||||||
if (corner & (CORNER_TOPLEFT | CORNER_TOPRIGHT))
|
else
|
||||||
sourceBox.y += sourceBox.h - sizeY;
|
sourceY.end = sourceY.start + SIZEY;
|
||||||
sourceBox.h = sizeY;
|
} else if ((CORNER & (CORNER_TOPLEFT | CORNER_TOPRIGHT) && snaps & SNAP_UP) || (CORNER & (CORNER_BOTTOMLEFT | CORNER_BOTTOMRIGHT) && snaps & SNAP_DOWN)) {
|
||||||
} else if ((corner & (CORNER_TOPLEFT | CORNER_TOPRIGHT) && snaps & SNAP_UP) || (corner & (CORNER_BOTTOMLEFT | CORNER_BOTTOMRIGHT) && snaps & SNAP_DOWN)) {
|
const double SIZEX = (sourceY.end - sourceY.start) * (BEGINSIZE.x / BEGINSIZE.y);
|
||||||
const double sizeX = sourceBox.h / RATIO;
|
if (CORNER & (CORNER_TOPLEFT | CORNER_BOTTOMLEFT))
|
||||||
if (corner & (CORNER_TOPLEFT | CORNER_BOTTOMLEFT))
|
sourceX.start = sourceX.end - SIZEX;
|
||||||
sourceBox.x += sourceBox.w - sizeX;
|
else
|
||||||
sourceBox.w = sizeX;
|
sourceX.end = sourceX.start + SIZEX;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceBox.expand(-DRAGGINGBORDERSIZE).round();
|
sourcePos = {sourceX.start, sourceY.start};
|
||||||
sourcePos = sourceBox.pos();
|
sourceSize = {sourceX.end - sourceX.start, sourceY.end - sourceY.start};
|
||||||
sourceSize = sourceBox.size();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IHyprLayout::onMouseMove(const Vector2D& mousePos) {
|
void IHyprLayout::onMouseMove(const Vector2D& mousePos) {
|
||||||
|
|
Loading…
Reference in a new issue