Skip to content

Commit

Permalink
options: add cyclesize_closest
Browse files Browse the repository at this point in the history
  • Loading branch information
dawsers committed Dec 23, 2024
1 parent 0340f47 commit f477e65
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 12 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,15 @@ in a cycle. This is the default behavior. If you prefer cycling not to *wrap*
first size if you call `prev`, and at the last size if you call `next`.
Possible arguments are: `true`|`1` (default), or `false`|`0`.

### `cyclesize_closest`

If `true`, when a window/column has been manually resized or is not of one
of the standard sizes in `window_heights`/`column_widths`, calling one of the
`cyclesize` dispatchers will resize it to the closest available size in
`window_heights`/`column_widths`. If `false`, the window/column will adopt its
default size instead (`window_default_height`/`column_default_width`).
Possible arguments are: `true`|`1` (default), or `false`|`0`.

### `center_row_if_space_available`

If there is empty space in the viewport, the row will be centered, leaving the
Expand Down Expand Up @@ -752,7 +761,7 @@ Determines the set of column widths *hyprscroller* will
cycle through when resizing the width of a column in *row* mode. It is a string
of any number of values chosen among: *oneeighth, onesixth, onefourth, onethird,
threeeighths, onehalf, fiveeighths, twothirds, threequarters, fivesixths,
seveneighths, one*. The default value is: *onehalf twothirds onethird*.
seveneighths, one*. The default value is: *onethird onehalf twothirds one*.

### `window_heights`

Expand All @@ -761,7 +770,7 @@ cycle through when resizing the height of a window in *column* mode. It is a
string of any number of values chosen among: *oneeighth, onesixth, onefourth,
onethird, threeeighths, onehalf, fiveeighths, twothirds, threequarters,
fivesixths, seveneighths, one*. The default value is:
*one onethird onehalf twothirds*.
*onethird onehalf twothirds one*.

### `monitor_options`

Expand Down
11 changes: 9 additions & 2 deletions src/column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,15 @@ void Column::cycle_size_active_window(int step, const Vector2D &gap_x, double ga
reorder = Reorder::Auto;
StandardSize height = active->data()->get_height();
if (height == StandardSize::Free) {
// When cycle-resizing from Free mode, always move back to default
height = scroller_sizes.get_window_default_height(active->data()->get_window());

// When cycle-resizing from Free mode, move back to closest or default
static auto* const *CYCLESIZE_CLOSEST = (Hyprlang::INT* const *)HyprlandAPI::getConfigValue(PHANDLE, "plugin:scroller:cyclesize_closest")->getDataStaticPtr();
if (**CYCLESIZE_CLOSEST) {
double fraction = active->data()->get_geom_h() / row->get_max().h;
height = scroller_sizes.get_window_closest_height(g_pCompositor->m_pLastMonitor, fraction, step);
} else {
height = scroller_sizes.get_window_default_height(active->data()->get_window());
}
} else {
height = scroller_sizes.get_next_window_height(height, step);
}
Expand Down
16 changes: 10 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,21 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
// 0, 1
HyprlandAPI::addConfigValue(PHANDLE, "plugin:scroller:cyclesize_wrap", Hyprlang::INT{1});
// 0, 1
HyprlandAPI::addConfigValue(PHANDLE, "plugin:scroller:cyclesize_closest", Hyprlang::INT{1});
// 0, 1
HyprlandAPI::addConfigValue(PHANDLE, "plugin:scroller:center_row_if_space_available", Hyprlang::INT{0});
// 0, 1
HyprlandAPI::addConfigValue(PHANDLE, "plugin:scroller:overview_scale_content", Hyprlang::INT{1});
// a list of values used as standard widths for cyclesize in row mode, and in the cycling order
// available options: onesixth, onefourth, onethird, onehalf, twothirds, one
// default: onehalf twothirds onethird
HyprlandAPI::addConfigValue(PHANDLE, "plugin:scroller:column_widths", Hyprlang::STRING{"onehalf twothirds onethird"});
// available options: oneeighth, onesixth, onefourth, onethird, threeeighths, onehalf,
// fiveeighths, twothirds, threequarters, fivesixths, seveneighths, one
// default: onethird onehalf twothirds one
HyprlandAPI::addConfigValue(PHANDLE, "plugin:scroller:column_widths", Hyprlang::STRING{"onethird onehalf twothirds one"});
// a list of values used as standard heights for cyclesize in column mode, and in the cycling order
// available options: onesixth, onefourth, onethird, onehalf, twothirds, one
// default: one onethird onehalf twothirds
HyprlandAPI::addConfigValue(PHANDLE, "plugin:scroller:window_heights", Hyprlang::STRING{"one onethird onehalf twothirds"});
// available options: oneeighth, onesixth, onefourth, onethird, threeeighths, onehalf,
// fiveeighths, twothirds, threequarters, fivesixths, seveneighths, one
// default: onethird onehalf twothirds one
HyprlandAPI::addConfigValue(PHANDLE, "plugin:scroller:window_heights", Hyprlang::STRING{"onethird onehalf twothirds one"});
// a list of values separated by ','. Each value is of the type MONITORID = ( options ),
// where MONITORID is the name of a monitor;
// options is a list of key = value separated by ';'.
Expand Down
10 changes: 8 additions & 2 deletions src/row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,14 @@ void Row::resize_active_column(int step)
} else {
StandardSize width = active->data()->get_width();
if (width == StandardSize::Free) {
// When cycle-resizing from Free mode, always move back to default
width = scroller_sizes.get_column_default_width(get_active_window());
// When cycle-resizing from Free mode, move back to closest or default
static auto* const *CYCLESIZE_CLOSEST = (Hyprlang::INT* const *)HyprlandAPI::getConfigValue(PHANDLE, "plugin:scroller:cyclesize_closest")->getDataStaticPtr();
if (**CYCLESIZE_CLOSEST) {
double fraction = active->data()->get_geom_w() / max.w;
width = scroller_sizes.get_column_closest_width(g_pCompositor->m_pLastMonitor, fraction, step);
} else {
width = scroller_sizes.get_column_default_width(get_active_window());
}
} else {
width = scroller_sizes.get_next_column_width(width, step);
}
Expand Down
79 changes: 79 additions & 0 deletions src/sizes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,85 @@ StandardSize ScrollerSizes::get_column_default_width(PHLWINDOW window)
return column_default_width;
}

StandardSize ScrollerSizes::get_closest_size(const std::vector<StandardSize> &sizes, double fraction, int step) const
{
size_t closest = step >= 0 ? sizes.size() - 1 : 0;
double closest_distance = 2.0;
for (size_t i = 0; i < sizes.size(); ++i) {
double s;
switch (sizes[i]) {
case StandardSize::OneEighth:
s = 1.0 / 8.0;
break;
case StandardSize::OneSixth:
s = 1.0 / 6.0;
break;
case StandardSize::OneFourth:
s = 1.0 / 4.0;
break;
case StandardSize::OneThird:
s = 1.0 / 3.0;
break;
case StandardSize::ThreeEighths:
s = 3.0 / 8.0;
break;
case StandardSize::OneHalf:
s = 1.0 / 2.0;
break;
case StandardSize::FiveEighths:
s = 5.0 / 8.0;
break;
case StandardSize::TwoThirds:
s = 2.0 / 3.0;
break;
case StandardSize::ThreeQuarters:
s = 3.0 / 4.0;
break;
case StandardSize::FiveSixths:
s = 5.0 / 6.0;
break;
case StandardSize::SevenEighths:
s = 7.0 / 8.0;
break;
case StandardSize::One:
s = 1.0;
break;
default:
// Shouldn't be here
s = 2.0;
break;
}
double distance = step * (s - fraction);
if (distance >= 0.0 && distance < closest_distance) {
closest = i;
closest_distance = distance;
}
}
return sizes[closest];
}

StandardSize ScrollerSizes::get_window_closest_height(PHLMONITORREF monitor, double fraction, int step)
{
update();
for (const auto monitor_data : monitors) {
if (monitor_data.name == monitor->szName) {
return get_closest_size(monitor_data.window_heights, fraction, step);
}
}
return get_closest_size(window_heights, fraction, step);
}

StandardSize ScrollerSizes::get_column_closest_width(PHLMONITORREF monitor, double fraction, int step)
{
update();
for (const auto monitor_data : monitors) {
if (monitor_data.name == monitor->szName) {
return get_closest_size(monitor_data.column_widths, fraction, step);
}
}
return get_closest_size(column_widths, fraction, step);
}

void ScrollerSizes::update_sizes(std::vector<StandardSize> &sizes, const std::string &option, StandardSize default_size)
{
sizes.clear();
Expand Down
3 changes: 3 additions & 0 deletions src/sizes.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class ScrollerSizes {
Mode get_mode(PHLMONITOR monitor);
StandardSize get_window_default_height(PHLWINDOW window);
StandardSize get_column_default_width(PHLWINDOW window);
StandardSize get_window_closest_height(PHLMONITORREF monitor, double fraction, int step);
StandardSize get_column_closest_width(PHLMONITORREF monitor, double fraction, int step);
StandardSize get_next_window_height(StandardSize size, int step);
StandardSize get_next_column_width(StandardSize size, int step);
StandardSize get_window_height(int index);
Expand All @@ -45,6 +47,7 @@ class ScrollerSizes {
private:
StandardSize get_next(const std::vector<StandardSize> &sizes, StandardSize size, int step) const;
StandardSize get_size(const std::vector<StandardSize> &sizes, int index) const;
StandardSize get_closest_size(const std::vector<StandardSize> &sizes, double fraction, int step) const;
void update_sizes(std::vector<StandardSize> &sizes, const std::string &option, StandardSize default_size);
StandardSize get_size_from_string(const std::string &size, StandardSize default_size);
void trim(std::string &str);
Expand Down

0 comments on commit f477e65

Please sign in to comment.