Skip to content

Commit

Permalink
dispatchers: add setsize, setwidth, setheight
Browse files Browse the repository at this point in the history
  • Loading branch information
dawsers committed Dec 20, 2024
1 parent 9262b81 commit 9a4c688
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 4 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ The plugin adds the following dispatchers:
| `scroller:cyclesize` | Resize the focused column width (*row* mode), or the active window height (*column* mode). |
| `scroller:cyclewidth` | Resize the focused column width. |
| `scroller:cycleheight` | Resize the active window height. |
| `scroller:setsize` | Set the focused column width (*row* mode), or the active window height (*column* mode) to one of the standard sizes. |
| `scroller:setwidth` | Set the focused column width to one of `column_widths`. Takes an int value (0-based idx of the desired size in `column_widths`) |
| `scroller:setheight` | Set the active window height to one of `window_heights`. Parameter similar to `setwidth` |
| `scroller:alignwindow` | Align window on the screen, `l/left`, `c/center`, `r/right` (*row* mode), `c/center`, `u/up`, `d/down` (*col* mode), `m/middle` |
| `scroller:admitwindow` | Push the current window below the active one of the column to its left. |
| `scroller:expelwindow` | Pop the current window out of its column and place it on a new column to the right. |
Expand Down Expand Up @@ -160,12 +163,12 @@ The plugin adds the following dispatchers:
*Hyprscroller* works in any of two modes that can be changed at any moment.

1. *row* mode: it is the default. It creates new windows in a new column.
`cyclesize` affects the width of the active column. `alignwindow` aligns
`cyclesize` and `setsize` affect the width of the active column. `alignwindow` aligns
the active column according to the argument received. `fitsize` fits the
selected columns to the width of the monitor.

2. *column* mode: It creates new windows in the current column, right below the
active window. `cyclesize` affects the height of the active window.
active window. `cyclesize` and `setsize` affect the height of the active window.
`alignwindow` aligns the active window within the column, according to the
argument received. `fitsize` fits the selected windows in the column to the
height of the monitor.
Expand Down Expand Up @@ -200,6 +203,32 @@ freely.
regardless of which mode you are in. `cycleheight` works similarly, but
resizing the active window's height.

`setsize`, `setwidth` and `setheight` are similar to their `cycle...`
counterparts, but instead of cycling, they set the width or height directly.
These dispatchers accept an integer parameter *index* which is the zero-based
index in the `column_widths` array for `setwidth`, or `setsize` in row mode, or
the index in `window_heights` for `setheight`, or `setsize` in column mode.

```
plugin {
scroller {
column_widths = onethird onehalf twothirds one
window_heights = onethird onehalf twothirds one
}
}
bind = $mainMod, 1, scroller:setwidth, 0 # sets width to onethird
bind = $mainMod, 2, scroller:setwidth, 1 # sets width to onehalf
bind = $mainMod, 3, scroller:setwidth, 2 # ...
bind = $mainMod, 4, scroller:setwidth, 3
...
bind = $mainMod SHIFT, 1, scroller:setheight, 0 # sets height to onethird
bind = $mainMod SHIFT, 2, scroller:setheight, 1 # sets height to onehalf
bind = $mainMod SHIFT, 3, scroller:setheight, 2 # ...
bind = $mainMod SHIFT, 4, scroller:setheight, 3
...
```


## Aligning

Columns are generally aligned in automatic mode, always making the active one
Expand Down
11 changes: 11 additions & 0 deletions src/column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,17 @@ void Column::cycle_size_active_window(int step, const Vector2D &gap_x, double ga
recalculate_col_geometry(gap_x, gap);
}

void Column::size_active_window(int index, const Vector2D &gap_x, double gap)
{
static auto const *window_heights_str = (Hyprlang::STRING const *)HyprlandAPI::getConfigValue(PHANDLE, "plugin:scroller:window_heights")->getDataStaticPtr();
window_heights.update(*window_heights_str);

reorder = Reorder::Auto;
StandardSize height = window_heights.get_size(index);
active->data()->update_height(height, row->get_max().h);
recalculate_col_geometry(gap_x, gap);
}

void Column::resize_active_window(const Vector2D &gap_x, double gap, const Vector2D &delta)
{
const Box &max = row->get_max();
Expand Down
1 change: 1 addition & 0 deletions src/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Column {
void update_width(StandardSize cwidth, double maxw);
void fit_size(FitSize fitsize, const Vector2D &gap_x, double gap);
void cycle_size_active_window(int step, const Vector2D &gap_x, double gap);
void size_active_window(int index, const Vector2D &gap_x, double gap);
void resize_active_window(const Vector2D &gap_x, double gap, const Vector2D &delta);
void selection_toggle();
void selection_set(PHLWINDOWREF window);
Expand Down
45 changes: 45 additions & 0 deletions src/dispatchers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,48 @@ namespace dispatchers {
g_ScrollerLayout->cycle_window_height(workspace, step);
}

void dispatch_setsize(std::string arg) {
auto workspace = workspace_for_action();
if (workspace == -1)
return;

int index;
try {
index = std::stoi(arg);
} catch (const std::invalid_argument &ia) {
index = 0;
}
g_ScrollerLayout->set_window_size(workspace, index);
}

void dispatch_setwidth(std::string arg) {
auto workspace = workspace_for_action();
if (workspace == -1)
return;

int index;
try {
index = std::stoi(arg);
} catch (const std::invalid_argument &ia) {
index = 0;
}
g_ScrollerLayout->set_window_width(workspace, index);
}

void dispatch_setheight(std::string arg) {
auto workspace = workspace_for_action();
if (workspace == -1)
return;

int index;
try {
index = std::stoi(arg);
} catch (const std::invalid_argument &ia) {
index = 0;
}
g_ScrollerLayout->set_window_height(workspace, index);
}

void dispatch_movefocus(std::string arg) {
auto workspace = workspace_for_action();
if (workspace == -1)
Expand Down Expand Up @@ -313,6 +355,9 @@ namespace dispatchers {
HyprlandAPI::addDispatcher(PHANDLE, "scroller:cyclesize", dispatch_cyclesize);
HyprlandAPI::addDispatcher(PHANDLE, "scroller:cyclewidth", dispatch_cyclewidth);
HyprlandAPI::addDispatcher(PHANDLE, "scroller:cycleheight", dispatch_cycleheight);
HyprlandAPI::addDispatcher(PHANDLE, "scroller:setsize", dispatch_setsize);
HyprlandAPI::addDispatcher(PHANDLE, "scroller:setwidth", dispatch_setwidth);
HyprlandAPI::addDispatcher(PHANDLE, "scroller:setheight", dispatch_setheight);
HyprlandAPI::addDispatcher(PHANDLE, "scroller:movefocus", dispatch_movefocus);
HyprlandAPI::addDispatcher(PHANDLE, "scroller:movewindow", dispatch_movewindow);
HyprlandAPI::addDispatcher(PHANDLE, "scroller:alignwindow", dispatch_alignwindow);
Expand Down
24 changes: 24 additions & 0 deletions src/row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,30 @@ void Row::resize_active_column(int step)
toggle_overview();
}

void Row::size_active_column(int index)
{
if (active->data()->fullscreen())
return;

bool overview_on = overview;
if (overview)
toggle_overview();

if (mode == Mode::Column) {
active->data()->size_active_window(index, calculate_gap_x(active), gap);
} else {
static auto const *column_widths_str = (Hyprlang::STRING const *)HyprlandAPI::getConfigValue(PHANDLE, "plugin:scroller:column_widths")->getDataStaticPtr();
column_widths.update(*column_widths_str);

StandardSize width = column_widths.get_size(index);
active->data()->update_width(width, max.w);
reorder = Reorder::Auto;
recalculate_row_geometry();
}
if (overview_on)
toggle_overview();
}

void Row::resize_active_window(const Vector2D &delta)
{
// If the active window in the active column is fullscreen, ignore.
Expand Down
1 change: 1 addition & 0 deletions src/row.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Row {
bool move_focus(Direction dir, bool focus_wrap);

void resize_active_column(int step);
void size_active_column(int size);
void resize_active_window(const Vector2D &delta);
void set_mode(Mode m, bool silent = false);
Mode get_mode() const;
Expand Down
36 changes: 36 additions & 0 deletions src/scroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,42 @@ void ScrollerLayout::cycle_window_height(WORKSPACEID workspace, int step)
s->set_mode(mode, true);
}

void ScrollerLayout::set_window_size(WORKSPACEID workspace, int index)
{
auto s = getRowForWorkspace(workspace);
if (s == nullptr) {
return;
}

s->size_active_column(index);
}

void ScrollerLayout::set_window_width(WORKSPACEID workspace, int index)
{
auto s = getRowForWorkspace(workspace);
if (s == nullptr) {
return;
}

Mode mode = s->get_mode();
s->set_mode(Mode::Row, true);
s->size_active_column(index);
s->set_mode(mode, true);
}

void ScrollerLayout::set_window_height(WORKSPACEID workspace, int index)
{
auto s = getRowForWorkspace(workspace);
if (s == nullptr) {
return;
}

Mode mode = s->get_mode();
s->set_mode(Mode::Column, true);
s->size_active_column(index);
s->set_mode(mode, true);
}

void ScrollerLayout::move_focus(WORKSPACEID workspace, Direction direction)
{
static auto* const *focus_wrap = (Hyprlang::INT* const *)HyprlandAPI::getConfigValue(PHANDLE, "plugin:scroller:focus_wrap")->getDataStaticPtr();
Expand Down
3 changes: 3 additions & 0 deletions src/scroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class ScrollerLayout : public IHyprLayout {
void cycle_window_size(WORKSPACEID workspace, int step);
void cycle_window_width(WORKSPACEID workspace, int step);
void cycle_window_height(WORKSPACEID workspace, int step);
void set_window_size(WORKSPACEID workspace, int index);
void set_window_width(WORKSPACEID workspace, int index);
void set_window_height(WORKSPACEID workspace, int index);
void move_focus(WORKSPACEID workspace, Direction);
void move_window(WORKSPACEID workspace, Direction);
void align_window(WORKSPACEID workspace, Direction);
Expand Down
8 changes: 7 additions & 1 deletion src/sizes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CycleSizes window_heights;
CycleSizes column_widths;
ScrollerSizes scroller_sizes;

StandardSize CycleSizes::get_next(StandardSize size, int step)
StandardSize CycleSizes::get_next(StandardSize size, int step) const
{
int current = -1;
for (size_t i = 0; i < sizes.size(); ++i) {
Expand All @@ -34,6 +34,12 @@ StandardSize CycleSizes::get_next(StandardSize size, int step)
return sizes[current];
}

StandardSize CycleSizes::get_size(int index) const
{
int current = std::min(std::max(0, index), static_cast<int>(sizes.size()) - 1);
return sizes[current];
}

void CycleSizes::update(const std::string &option)
{
if (option == str)
Expand Down
3 changes: 2 additions & 1 deletion src/sizes.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class CycleSizes {
CycleSizes() {}
~CycleSizes() { reset(); }
StandardSize get_default() { return sizes[0]; }
StandardSize get_next(StandardSize size, int step);
StandardSize get_next(StandardSize size, int step) const;
StandardSize get_size(int index) const;
void update(const std::string &option);

private:
Expand Down

0 comments on commit 9a4c688

Please sign in to comment.