Skip to content

Commit 9bf1b49

Browse files
snap: add option to respect gaps (#10524)
1 parent 5cc6cb4 commit 9bf1b49

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/config/ConfigDescriptions.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ inline static const std::vector<SConfigOptionDescription> CONFIG_OPTIONS = {
128128
.type = CONFIG_OPTION_BOOL,
129129
.data = SConfigOptionDescription::SBoolData{false},
130130
},
131+
SConfigOptionDescription{
132+
.value = "general:snap:respect_gaps",
133+
.description = "if true, snapping will respect gaps between windows",
134+
.type = CONFIG_OPTION_BOOL,
135+
.data = SConfigOptionDescription::SBoolData{false},
136+
},
131137

132138
/*
133139
* decoration:

src/config/ConfigManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ CConfigManager::CConfigManager() {
456456
registerConfigVar("general:snap:window_gap", Hyprlang::INT{10});
457457
registerConfigVar("general:snap:monitor_gap", Hyprlang::INT{10});
458458
registerConfigVar("general:snap:border_overlap", Hyprlang::INT{0});
459+
registerConfigVar("general:snap:respect_gaps", Hyprlang::INT{0});
459460
registerConfigVar("general:col.active_border", Hyprlang::CConfigCustomValueType{&configHandleGradientSet, configHandleGradientDestroy, "0xffffffff"});
460461
registerConfigVar("general:col.inactive_border", Hyprlang::CConfigCustomValueType{&configHandleGradientSet, configHandleGradientDestroy, "0xff444444"});
461462
registerConfigVar("general:col.nogroup_border", Hyprlang::CConfigCustomValueType{&configHandleGradientSet, configHandleGradientDestroy, "0xffffaaff"});

src/layout/IHyprLayout.cpp

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ static void performSnap(Vector2D& sourcePos, Vector2D& sourceSize, PHLWINDOW DRA
417417
static auto SNAPWINDOWGAP = CConfigValue<Hyprlang::INT>("general:snap:window_gap");
418418
static auto SNAPMONITORGAP = CConfigValue<Hyprlang::INT>("general:snap:monitor_gap");
419419
static auto SNAPBORDEROVERLAP = CConfigValue<Hyprlang::INT>("general:snap:border_overlap");
420+
static auto SNAPRESPECTGAPS = CConfigValue<Hyprlang::INT>("general:snap:respect_gaps");
420421

421422
const SnapFn SNAP = (MODE == MBIND_MOVE) ? snapMove : snapResize;
422423
int snaps = 0;
@@ -444,9 +445,15 @@ static void performSnap(Vector2D& sourcePos, Vector2D& sourceSize, PHLWINDOW DRA
444445
const int OTHERBORDERSIZE = other->getRealBorderSize();
445446
const double BORDERSIZE = OVERLAP ? std::max(DRAGGINGBORDERSIZE, OTHERBORDERSIZE) : (DRAGGINGBORDERSIZE + OTHERBORDERSIZE);
446447

447-
const CBox SURF = other->getWindowMainSurfaceBox();
448-
const SRange SURFBX = {SURF.x - BORDERSIZE, SURF.x + SURF.w + BORDERSIZE};
449-
const SRange SURFBY = {SURF.y - BORDERSIZE, SURF.y + SURF.h + BORDERSIZE};
448+
const CBox SURF = other->getWindowMainSurfaceBox();
449+
double gapOffset = 0;
450+
if (*SNAPRESPECTGAPS) {
451+
static auto PGAPSINDATA = CConfigValue<Hyprlang::CUSTOMTYPE>("general:gaps_in");
452+
auto* PGAPSINPTR = (CCssGapData*)(PGAPSINDATA.ptr())->getData();
453+
gapOffset = std::max({PGAPSINPTR->m_left, PGAPSINPTR->m_right, PGAPSINPTR->m_top, PGAPSINPTR->m_bottom});
454+
}
455+
const SRange SURFBX = {SURF.x - BORDERSIZE - gapOffset, SURF.x + SURF.w + BORDERSIZE + gapOffset};
456+
const SRange SURFBY = {SURF.y - BORDERSIZE - gapOffset, SURF.y + SURF.h + BORDERSIZE + gapOffset};
450457

451458
// only snap windows if their ranges overlap in the opposite axis
452459
if (sourceY.start <= SURFBY.end && SURFBY.start <= sourceY.end) {
@@ -497,32 +504,37 @@ static void performSnap(Vector2D& sourcePos, Vector2D& sourceSize, PHLWINDOW DRA
497504
const double GAPSIZE = *SNAPMONITORGAP;
498505
const double BORDERDIFF = OVERLAP ? DRAGGINGBORDERSIZE : 0;
499506
const auto MON = DRAGGINGWINDOW->m_monitor.lock();
500-
501-
SRange monX = {MON->m_position.x + MON->m_reservedTopLeft.x + DRAGGINGBORDERSIZE, MON->m_position.x + MON->m_size.x - MON->m_reservedBottomRight.x - DRAGGINGBORDERSIZE};
502-
SRange monY = {MON->m_position.y + MON->m_reservedTopLeft.y + DRAGGINGBORDERSIZE, MON->m_position.y + MON->m_size.y - MON->m_reservedBottomRight.y - DRAGGINGBORDERSIZE};
507+
double gapOffset = 0;
508+
if (*SNAPRESPECTGAPS) {
509+
static auto PGAPSOUTDATA = CConfigValue<Hyprlang::CUSTOMTYPE>("general:gaps_out");
510+
auto* PGAPSOUTPTR = (CCssGapData*)(PGAPSOUTDATA.ptr())->getData();
511+
gapOffset = std::max({PGAPSOUTPTR->m_left, PGAPSOUTPTR->m_right, PGAPSOUTPTR->m_top, PGAPSOUTPTR->m_bottom});
512+
}
503513

504514
if (CORNER & (CORNER_TOPLEFT | CORNER_BOTTOMLEFT) &&
505-
((MON->m_reservedTopLeft.x > 0 && canSnap(sourceX.start, monX.start, GAPSIZE)) ||
506-
canSnap(sourceX.start, (monX.start -= MON->m_reservedTopLeft.x + BORDERDIFF), GAPSIZE))) {
507-
SNAP(sourceX.start, sourceX.end, monX.start);
515+
((MON->m_reservedTopLeft.x > 0 && canSnap(sourceX.start, MON->m_position.x + MON->m_reservedTopLeft.x + DRAGGINGBORDERSIZE + gapOffset, GAPSIZE)) ||
516+
canSnap(sourceX.start, MON->m_position.x + MON->m_reservedTopLeft.x - BORDERDIFF + gapOffset, GAPSIZE))) {
517+
SNAP(sourceX.start, sourceX.end, MON->m_position.x + MON->m_reservedTopLeft.x + DRAGGINGBORDERSIZE + gapOffset);
508518
snaps |= SNAP_LEFT;
509519
}
510520
if (CORNER & (CORNER_TOPRIGHT | CORNER_BOTTOMRIGHT) &&
511-
((MON->m_reservedBottomRight.x > 0 && canSnap(sourceX.end, monX.end, GAPSIZE)) ||
512-
canSnap(sourceX.end, (monX.end += MON->m_reservedBottomRight.x + BORDERDIFF), GAPSIZE))) {
513-
SNAP(sourceX.end, sourceX.start, monX.end);
521+
((MON->m_reservedBottomRight.x > 0 &&
522+
canSnap(sourceX.end, MON->m_position.x + MON->m_size.x - MON->m_reservedBottomRight.x - DRAGGINGBORDERSIZE - gapOffset, GAPSIZE)) ||
523+
canSnap(sourceX.end, MON->m_position.x + MON->m_size.x - MON->m_reservedBottomRight.x + BORDERDIFF - gapOffset, GAPSIZE))) {
524+
SNAP(sourceX.end, sourceX.start, MON->m_position.x + MON->m_size.x - MON->m_reservedBottomRight.x - DRAGGINGBORDERSIZE - gapOffset);
514525
snaps |= SNAP_RIGHT;
515526
}
516527
if (CORNER & (CORNER_TOPLEFT | CORNER_TOPRIGHT) &&
517-
((MON->m_reservedTopLeft.y > 0 && canSnap(sourceY.start, monY.start, GAPSIZE)) ||
518-
canSnap(sourceY.start, (monY.start -= MON->m_reservedTopLeft.y + BORDERDIFF), GAPSIZE))) {
519-
SNAP(sourceY.start, sourceY.end, monY.start);
528+
((MON->m_reservedTopLeft.y > 0 && canSnap(sourceY.start, MON->m_position.y + MON->m_reservedTopLeft.y + DRAGGINGBORDERSIZE + gapOffset, GAPSIZE)) ||
529+
canSnap(sourceY.start, MON->m_position.y + MON->m_reservedTopLeft.y - BORDERDIFF + gapOffset, GAPSIZE))) {
530+
SNAP(sourceY.start, sourceY.end, MON->m_position.y + MON->m_reservedTopLeft.y + DRAGGINGBORDERSIZE + gapOffset);
520531
snaps |= SNAP_UP;
521532
}
522533
if (CORNER & (CORNER_BOTTOMLEFT | CORNER_BOTTOMRIGHT) &&
523-
((MON->m_reservedBottomRight.y > 0 && canSnap(sourceY.end, monY.end, GAPSIZE)) ||
524-
canSnap(sourceY.end, (monY.end += MON->m_reservedBottomRight.y + BORDERDIFF), GAPSIZE))) {
525-
SNAP(sourceY.end, sourceY.start, monY.end);
534+
((MON->m_reservedBottomRight.y > 0 &&
535+
canSnap(sourceY.end, MON->m_position.y + MON->m_size.y - MON->m_reservedBottomRight.y - DRAGGINGBORDERSIZE - gapOffset, GAPSIZE)) ||
536+
canSnap(sourceY.end, MON->m_position.y + MON->m_size.y - MON->m_reservedBottomRight.y + BORDERDIFF - gapOffset, GAPSIZE))) {
537+
SNAP(sourceY.end, sourceY.start, MON->m_position.y + MON->m_size.y - MON->m_reservedBottomRight.y - DRAGGINGBORDERSIZE - gapOffset);
526538
snaps |= SNAP_DOWN;
527539
}
528540
}

0 commit comments

Comments
 (0)