143 lines
3.5 KiB
MoonBit
143 lines
3.5 KiB
MoonBit
///|
|
|
/// The icon size setting as plain functions: the allowed range, parsing a stored or
|
|
/// slider value, and how icon size, row height and column count follow from it.
|
|
/// The size is a whole percent of today's size (`scale_default` = 100: no change).
|
|
|
|
///|
|
|
pub let scale_min : Int32 = 70
|
|
|
|
///|
|
|
pub let scale_max : Int32 = 160
|
|
|
|
///|
|
|
pub let scale_default : Int32 = 100
|
|
|
|
///|
|
|
/// Slider step in percent.
|
|
pub let scale_step : Int32 = 5
|
|
|
|
///|
|
|
/// The presets: Small, Medium (today's size), Large.
|
|
pub let scale_small : Int32 = 80
|
|
|
|
///|
|
|
pub let scale_medium : Int32 = 100
|
|
|
|
///|
|
|
pub let scale_large : Int32 = 130
|
|
|
|
///|
|
|
/// The home icon at 100%, in px (`.slots .icon`).
|
|
pub let home_icon_px : Int32 = 40
|
|
|
|
///|
|
|
/// The drawer icon at 100%, in px (`.icon`).
|
|
pub let drawer_icon_px : Int32 = 52
|
|
|
|
///|
|
|
/// Fewest columns either grid is reduced to, however large the icons.
|
|
pub let min_cols : Int32 = 3
|
|
|
|
///|
|
|
/// A size is a valid setting: in range and on the step.
|
|
pub fn scale_valid(pct : Int32) -> Bool {
|
|
pct >= scale_min && pct <= scale_max && pct % scale_step == 0
|
|
}
|
|
|
|
///|
|
|
/// Parse a stored or slider value (decimal whole percent, nothing else). The error says
|
|
/// what was wrong and what is allowed; callers show and log it.
|
|
pub fn parse_scale(text : String) -> Result[Int32, String] {
|
|
let t = text.trim().to_owned()
|
|
let n = @string.parse_int(t) catch {
|
|
_ => return Err("icon size \"\{text}\" is not a whole number")
|
|
}
|
|
if scale_valid(n) {
|
|
Ok(n)
|
|
} else {
|
|
Err(
|
|
"icon size \{n} is outside \{scale_min}..\{scale_max} in steps of \{scale_step}",
|
|
)
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// `base` px at `pct` percent, rounded to a whole px, at least 1.
|
|
pub fn scaled_px(base : Int32, pct : Int32) -> Int32 {
|
|
@cmp.maximum(1, (base * pct + 50) / 100)
|
|
}
|
|
|
|
///|
|
|
/// Height of a home row at `pct`.
|
|
pub fn cell_height(pct : Int32) -> Int32 {
|
|
scaled_px(cell_px, pct)
|
|
}
|
|
|
|
///|
|
|
/// The percent as a CSS number (`1`, `1.25`, `0.8`), for the `--s` custom property.
|
|
pub fn css_factor(pct : Int32) -> String {
|
|
let whole = pct / 100
|
|
let frac = pct % 100
|
|
if frac == 0 {
|
|
whole.to_string()
|
|
} else if frac < 10 {
|
|
"\{whole}.0\{frac}"
|
|
} else if frac % 10 == 0 {
|
|
"\{whole}.\{frac / 10}"
|
|
} else {
|
|
"\{whole}.\{frac}"
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Columns of a grid `avail` px wide whose icons are `icon` px: `base` columns at 100%,
|
|
/// `base * 100 / pct` (rounded) at `pct`, no fewer than `min_cols` and then never so
|
|
/// many that a column (`gap` px apart) is narrower than its icon.
|
|
pub fn scaled_columns(
|
|
base : Int32,
|
|
pct : Int32,
|
|
avail : Double,
|
|
gap : Int32,
|
|
icon : Int32,
|
|
) -> Int32 {
|
|
let mut c = @cmp.maximum(min_cols, (base * 100 + pct / 2) / pct)
|
|
while c > 1 && column_width_in(avail, c, gap) < icon.to_double() {
|
|
c -= 1
|
|
}
|
|
c
|
|
}
|
|
|
|
///|
|
|
fn column_width_in(avail : Double, cols : Int32, gap : Int32) -> Double {
|
|
(avail - (cols - 1).to_double() * gap.to_double()) / cols.to_double()
|
|
}
|
|
|
|
///|
|
|
/// Home grid columns for a viewport width, the width of the grid and the size.
|
|
pub fn home_columns(vw : Int32, grid_w : Double, pct : Int32) -> Int32 {
|
|
scaled_columns(
|
|
columns_for(vw),
|
|
pct,
|
|
grid_w,
|
|
col_gap,
|
|
scaled_px(home_icon_px, pct),
|
|
)
|
|
}
|
|
|
|
///|
|
|
/// Drawer columns (4 below 480px, else 5, at 100%); its list is the viewport less 16px
|
|
/// padding each side, with 8px between columns.
|
|
pub fn drawer_columns(vw : Int32, pct : Int32) -> Int32 {
|
|
scaled_columns(
|
|
if vw >= 480 {
|
|
5
|
|
} else {
|
|
4
|
|
},
|
|
pct,
|
|
(vw - 32).to_double(),
|
|
8,
|
|
scaled_px(drawer_icon_px, pct),
|
|
)
|
|
}
|