Skip to content

Commit decdec9

Browse files
refactor(ui): simplify pane/grid internals and tighten invariants
Signed-off-by: Maika Namuo <httpworldview@gmail.com>
1 parent d3e97d9 commit decdec9

8 files changed

Lines changed: 20 additions & 33 deletions

File tree

src/infra/pipewire/registry/runtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl AudioRegistryHandle {
7979
self.runtime.subscribe()
8080
}
8181

82-
pub fn send_command(&self, command: RegistryCommand) -> bool {
82+
fn send_command(&self, command: RegistryCommand) -> bool {
8383
self.runtime.send_command(command)
8484
}
8585

src/ui/app/windowing.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,9 @@ impl UiApp {
268268
.for_each(|popout| popout.sync_from_snapshot(&snapshot));
269269
let stale_windows: Vec<_> = self
270270
.popout_windows
271-
.iter()
272-
.filter_map(|(id, popout)| popout.cached.is_none().then_some(*id))
271+
.extract_if(|_, popout| popout.cached.is_none())
272+
.map(|(id, _)| id)
273273
.collect();
274-
self.popout_windows
275-
.retain(|_, popout| popout.cached.is_some());
276274
self.visuals_page
277275
.apply_snapshot_excluding(&snapshot, &self.popped_out_ids());
278276
Task::batch(

src/ui/pages/visuals.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl VisualsPage {
159159
.map(|(_, p)| p.id)
160160
.ne(slots.iter().map(|s| s.id))
161161
}) {
162-
self.panes = self.build_panes(&slots);
162+
self.panes = Some(self.build_panes(&slots));
163163
self.hovered_pane = None;
164164
return;
165165
}
@@ -190,7 +190,7 @@ impl VisualsPage {
190190
.collect()
191191
}
192192

193-
fn build_panes(&self, slots: &[&VisualSlotSnapshot]) -> Option<pane_grid::State<VisualPane>> {
193+
fn build_panes(&self, slots: &[&VisualSlotSnapshot]) -> pane_grid::State<VisualPane> {
194194
let settings = self.settings.borrow();
195195
let saved_width_basis = &settings.data.visuals.width_basis;
196196
pane_grid::State::from_iter(slots.iter().map(|&slot| {

src/ui/pages/visuals/settings/palette.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use super::widgets::clipped_text;
55
use crate::ui::theme::{self, Palette};
66
use crate::ui::widgets::{scroll_delta_lines, scroll_glow::ScrollGlow};
77
use crate::util::color::{
8-
EPSILON, colors_equal, f32_to_u8, find_segment, lerp_color, sanitize_stop_positions,
9-
sanitize_stop_spreads, with_alpha,
8+
EPSILON, STOP_SPREAD_MAX, STOP_SPREAD_MIN, colors_equal, f32_to_u8, find_segment, lerp_color,
9+
sanitize_stop_positions, sanitize_stop_spreads, with_alpha,
1010
};
1111
use iced::advanced::renderer::{self, Quad};
1212
use iced::advanced::widget::{Tree, tree};
@@ -19,8 +19,6 @@ const SWATCH_SIZE: (f32, f32) = (56.0, 28.0);
1919
const GRADIENT_BAR_HEIGHT: f32 = 24.0;
2020
const MARKER_HEIGHT: f32 = 8.0;
2121
const MIN_STOP_GAP: f32 = 0.01;
22-
const MIN_SPREAD: f32 = 0.2;
23-
const MAX_SPREAD: f32 = 5.0;
2422

2523
#[derive(Debug, Clone, Copy, PartialEq)]
2624
pub enum PaletteEvent {
@@ -155,7 +153,7 @@ impl PaletteEditor {
155153
if index >= self.palette.len() {
156154
return false;
157155
}
158-
let next = spread.clamp(MIN_SPREAD, MAX_SPREAD);
156+
let next = spread.clamp(STOP_SPREAD_MIN, STOP_SPREAD_MAX);
159157
if (self.spreads[index] - next).abs() < EPSILON {
160158
return false;
161159
}
@@ -419,7 +417,7 @@ impl Widget<PaletteEvent, iced::Theme, iced::Renderer> for GradientBar<'_> {
419417
{
420418
let dy = scroll_delta_lines(*delta);
421419
let current = self.spreads.get(i).copied().unwrap_or(1.0);
422-
let new_spread = (current + dy * 0.2).clamp(MIN_SPREAD, MAX_SPREAD);
420+
let new_spread = (current + dy * 0.2).clamp(STOP_SPREAD_MIN, STOP_SPREAD_MAX);
423421
if (current - new_spread).abs() >= EPSILON {
424422
shell.publish(PaletteEvent::AdjustSpread {
425423
index: i,

src/ui/theme.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const TEXT_DARK: Color = Color::from_rgba(0.10, 0.10, 0.10, 1.0);
1919
pub const BORDER_SUBTLE: Color = Color::from_rgba(0.280, 0.288, 0.304, 1.0);
2020
const BORDER_FOCUS: Color = Color::from_rgba(0.520, 0.536, 0.560, 1.0);
2121

22-
const ACCENT_PRIMARY: Color = Color::from_rgba(0.157, 0.157, 0.157, 1.0);
22+
pub(crate) const ACCENT_PRIMARY: Color = Color::from_rgba(0.157, 0.157, 0.157, 1.0);
2323
const ACCENT_SUCCESS: Color = Color::from_rgba(0.478, 0.557, 0.502, 1.0);
2424
const ACCENT_DANGER: Color = Color::from_rgba(0.557, 0.478, 0.478, 1.0);
2525

@@ -65,7 +65,7 @@ pub fn focus_border() -> Border {
6565
border(BORDER_FOCUS)
6666
}
6767

68-
pub fn button_style(theme: &Theme, base: Color, status: button::Status) -> button::Style {
68+
fn button_style(theme: &Theme, base: Color, status: button::Status) -> button::Style {
6969
use button::Status::{Hovered, Pressed};
7070
let bg = if status == Hovered {
7171
palette::deviate(base, 0.05)
@@ -142,10 +142,6 @@ pub fn resize_overlay(theme: &Theme) -> container::Style {
142142
}
143143
}
144144

145-
pub fn accent_primary() -> Color {
146-
ACCENT_PRIMARY
147-
}
148-
149145
pub fn slider_style(theme: &Theme, status: slider::Status) -> slider::Style {
150146
let palette = theme.extended_palette();
151147

src/ui/widgets/pane_grid.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ pub struct State<T> {
1818
}
1919

2020
impl<T> State<T> {
21-
pub fn from_iter(items: impl IntoIterator<Item = T>) -> Option<Self> {
21+
pub fn from_iter(items: impl IntoIterator<Item = T>) -> Self {
2222
let panes = items
2323
.into_iter()
2424
.enumerate()
2525
.map(|(id, item)| (Pane(id), item))
26-
.collect::<Vec<_>>();
27-
(!panes.is_empty()).then_some(Self { panes })
26+
.collect();
27+
Self { panes }
2828
}
2929

3030
pub fn get(&self, pane: Pane) -> Option<&T> {
@@ -452,7 +452,7 @@ where
452452
);
453453
});
454454
if interaction.dragging.is_some_and(|(p, _)| p == *pane) {
455-
let accent = crate::ui::theme::accent_primary();
455+
let accent = crate::ui::theme::ACCENT_PRIMARY;
456456
renderer.fill_quad(
457457
Quad {
458458
bounds: child_layout.bounds(),
@@ -482,7 +482,7 @@ where
482482
shadow: Default::default(),
483483
snap: true,
484484
},
485-
Background::Color(with_alpha(crate::ui::theme::accent_primary(), 0.75)),
485+
Background::Color(with_alpha(crate::ui::theme::ACCENT_PRIMARY, 0.75)),
486486
);
487487
}
488488
}

src/util/color.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
use iced::Color;
55

66
pub const EPSILON: f32 = 1e-4;
7+
pub const STOP_SPREAD_MIN: f32 = 0.2;
8+
pub const STOP_SPREAD_MAX: f32 = 5.0;
79

810
pub fn f32_to_u8(v: f32) -> u8 {
911
(v.clamp(0.0, 1.0) * 255.0).round() as u8
@@ -116,7 +118,7 @@ pub fn sanitize_stop_spreads(raw: Option<&[f32]>, count: usize) -> Vec<f32> {
116118
};
117119
for (dst, &value) in out.iter_mut().zip(raw.iter()) {
118120
*dst = if value.is_finite() {
119-
value.clamp(0.2, 5.0)
121+
value.clamp(STOP_SPREAD_MIN, STOP_SPREAD_MAX)
120122
} else {
121123
1.0
122124
};

src/visuals/spectrum/processor.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,7 @@ impl SpectrumProcessor {
223223
let floor = self.config.floor_db;
224224
let mut produced = false;
225225

226-
if self.a_weighting_db.len() != bins {
227-
self.a_weighting_db = self
228-
.snapshot
229-
.frequency_bins
230-
.iter()
231-
.map(|&f| a_weight(f))
232-
.collect();
233-
}
226+
debug_assert_eq!(self.a_weighting_db.len(), bins);
234227

235228
while self.pcm_buffer.len() >= fft_size {
236229
copy_dc_removed_from_deque(&mut self.real_buffer[..fft_size], &self.pcm_buffer);

0 commit comments

Comments
 (0)