refactor(style): dedup layers/xref row-button style into ui::style::row_button

Moves the byte-identical zebra/selected cell-button bodies from layers.rs and xref_manager.rs plus the layers header-button body into one shared module. All six call sites now use the shared helpers; toolbar_btn helpers untouched (different semantics). No new tests per owner request (pure move, verified by build plus full suite before test removal).
This commit is contained in:
Karim Jerbi committed 2026-10-05 22:33:24 +01:00
1 parent 7c76d2c27f
commit c5f8eb2921
4 files changed
+66 -75

No files matched your search

+1
View File
@@ -10,6 +10,7 @@ pub mod scale_manager;
pub mod anno_object_scale;
pub mod style_manager;
pub mod common;
pub mod row_button;
pub mod form;
pub mod fusion_theme;
+57
View File
@@ -0,0 +1,57 @@
//! Row/header button styles shared by the layers and xref list panels.
//!
//! Centralizes the zebra/selected row-button coloring previously duplicated
//! in `src/ui/window/layers.rs` (`layer_cell_button_style` +
//! `layer_header_button_style`) and `src/ui/window/xref_manager.rs`
//! (`row_button_style`). The two cell copies were functionally byte-identical
//! (Hovered → `background.strong`, selected → `primary.weak`, even →
//! `background.base`, else → `background.weak`); a single source keeps hover,
//! selection, and zebra colors consistent across both panels.
use iced::widget::button;
use iced::{Background, Theme};
/// Cell (row) button style — closure form so both `.style(...)` call sites
/// (`layers.rs`, `xref_manager.rs`) keep their existing shape.
pub(crate) fn cell_button_style(
selected: bool,
index: usize,
) -> impl Fn(&Theme, button::Status) -> button::Style {
move |theme: &Theme, status: button::Status| {
let palette = theme.palette();
let highlighted = matches!(status, button::Status::Hovered);
let pair = if highlighted {
palette.background.strong
} else if selected {
palette.primary.weak
} else if index % 2 == 0 {
palette.background.base
} else {
palette.background.weak
};
button::Style {
background: highlighted.then_some(Background::Color(pair.color)),
text_color: pair.text,
..Default::default()
}
}
}
/// Column-header button style (layers panel).
pub(crate) fn header_button_style(theme: &Theme, status: button::Status) -> button::Style {
let palette = theme.palette();
let highlighted = matches!(
status,
button::Status::Hovered | button::Status::Pressed
);
let pair = if highlighted {
palette.background.strong
} else {
palette.background.weak
};
button::Style {
background: highlighted.then_some(Background::Color(pair.color)),
text_color: pair.text,
..Default::default()
}
}
+5 -52
View File
@@ -3,6 +3,7 @@
use crate::app::Message;
use crate::ui::properties::{lw_options, LinetypeItem, LwItem};
use crate::ui::style::common::muted_style;
use crate::ui::style::row_button::{cell_button_style, header_button_style};
use crate::ui::ROW_H;
use codec::tables::layer::Layer as DocLayer;
use codec::tables::Table;
@@ -503,48 +504,6 @@ impl LayerPanel {
// ── Sorting helpers ─────────────────────────────────────────────────────────
fn layer_cell_button_style(
theme: &Theme,
status: button::Status,
is_selected: bool,
index: usize,
) -> button::Style {
let palette = theme.palette();
let highlighted = matches!(status, button::Status::Hovered);
let pair = if highlighted {
palette.background.strong
} else if is_selected {
palette.primary.weak
} else if index % 2 == 0 {
palette.background.base
} else {
palette.background.weak
};
button::Style {
background: highlighted.then_some(Background::Color(pair.color)),
text_color: pair.text,
..Default::default()
}
}
fn layer_header_button_style(theme: &Theme, status: button::Status) -> button::Style {
let palette = theme.palette();
let highlighted = matches!(
status,
button::Status::Hovered | button::Status::Pressed
);
let pair = if highlighted {
palette.background.strong
} else {
palette.background.weak
};
button::Style {
background: highlighted.then_some(Background::Color(pair.color)),
text_color: pair.text,
..Default::default()
}
}
/// Packed RGB key for ordering colours deterministically by hue-ish bytes.
fn color_sort_key(c: AcadColor) -> u32 {
let c = iced_color_from_acad(&c);
@@ -573,7 +532,7 @@ fn sortable_header<'a>(
}
button(content)
.on_press(Message::LayerSort(col))
.style(layer_header_button_style)
.style(header_button_style)
.padding(Padding {
top: 0.0,
bottom: 0.0,
@@ -717,9 +676,7 @@ fn layer_row<'a>(
let svg_btn = |bytes: &'static [u8], on_press: Message| -> Element<'a, Message> {
button(crate::ui::icons::semantic(bytes, ICON_SZ))
.on_press(on_press)
.style(move |theme: &Theme, status| {
layer_cell_button_style(theme, status, is_selected, index)
})
.style(cell_button_style(is_selected, index))
.padding(Padding {
top: COMBO_PAD_V,
bottom: COMBO_PAD_V,
@@ -747,9 +704,7 @@ fn layer_row<'a>(
let plot_btn: Element<'_, Message> = button(plot_icon)
.on_press(Message::LayerTogglePlot(index))
.style(move |theme: &Theme, status| {
layer_cell_button_style(theme, status, is_selected, index)
})
.style(cell_button_style(is_selected, index))
.padding(Padding {
top: COMBO_PAD_V,
bottom: COMBO_PAD_V,
@@ -788,9 +743,7 @@ fn layer_row<'a>(
.size(FONT_SZ),
)
.on_press(Message::LayerRenameStart(index))
.style(move |theme: &Theme, status| {
layer_cell_button_style(theme, status, is_selected, index)
})
.style(cell_button_style(is_selected, index))
.padding(Padding {
top: COMBO_PAD_V,
bottom: COMBO_PAD_V,
+3 -23
View File
@@ -10,6 +10,7 @@ use crate::io::xref::collect_entries_with_prev;
use crate::io::xref_model::{normalize_lexical, Pathtype, RefKind, RefStatus, RefType, ReferenceEntry};
use crate::ui::ROW_H;
use crate::ui::style::common::muted_style;
use crate::ui::style::row_button::cell_button_style;
use codec::CadDocument;
use iced::widget::{button, column, container, mouse_area, row, scrollable, text, tooltip};
use iced::Padding;
@@ -1259,27 +1260,6 @@ fn format_date(modified: Option<SystemTime>) -> String {
// ── Widget helpers (layers.rs conventions) ────────────────────────────────
fn row_button_style(selected: bool, index: usize) -> impl Fn(&Theme, button::Status) -> button::Style {
move |theme: &Theme, status: button::Status| {
let palette = theme.palette();
let highlighted = matches!(status, button::Status::Hovered);
let pair = if highlighted {
palette.background.strong
} else if selected {
palette.primary.weak
} else if index % 2 == 0 {
palette.background.base
} else {
palette.background.weak
};
button::Style {
background: highlighted.then_some(Background::Color(pair.color)),
text_color: pair.text,
..Default::default()
}
}
}
fn toolbar_btn(label: String, msg: Option<Message>, fill: bool) -> Element<'static, Message> {
let mut b = button(text(label).size(11))
.style(|theme: &Theme, status| {
@@ -1808,7 +1788,7 @@ fn xref_row<'a>(
let key = entry.key;
let arrow = button(crate::ui::icons::themed_arrow_toggle(is_expanded, 10.0))
.on_press(Message::XrefManagerToggleExpand(key))
.style(row_button_style(is_selected, display.index))
.style(cell_button_style(is_selected, display.index))
.padding(Padding {
top: 6.0,
bottom: 6.0,
@@ -1893,7 +1873,7 @@ fn tree_row(
let key = entry.key;
let arrow = button(crate::ui::icons::themed_arrow_toggle(is_expanded, 10.0))
.on_press(Message::XrefManagerToggleExpand(key))
.style(row_button_style(is_selected, display.index))
.style(cell_button_style(is_selected, display.index))
.padding(Padding {
top: 6.0,
bottom: 6.0,