use std::sync::Arc;
use egui::{vec2, Color32, FontSelection, Vec2};
use crate::{misc::layout, NodeSide, Socket, SocketShape};
use super::{SOCKET_NAME_GAP, SOCKET_WIDTH};
pub(crate) struct PreparedSocket<SocketId> {
pub(super) id: SocketId,
pub(super) side: NodeSide,
pub(super) text: Arc<egui::Galley>,
pub(super) filled: bool,
pub(super) color: Color32,
pub(super) shape: SocketShape,
}
impl<S> PreparedSocket<S> {
pub(super) fn compute_size(&self) -> Vec2 {
let socket_size = Vec2::splat(SOCKET_WIDTH);
let socket_text_gap = vec2(SOCKET_NAME_GAP, 0.0);
let text_size = self.text.size();
layout::stack_horizontally([socket_size, socket_text_gap, text_size])
}
}
pub(crate) fn prepare<S>(ui: &egui::Ui, socket: Socket<S>) -> PreparedSocket<S> {
let Socket {
id,
side,
text,
filled,
shape,
mut color,
} = socket;
if color == Color32::PLACEHOLDER {
color = ui.visuals().strong_text_color();
}
let layout_job = text.into_layout_job(
ui.style(),
FontSelection::Style(egui::TextStyle::Monospace),
egui::Align::BOTTOM,
);
let text = ui.fonts(|fonts| {
fonts.layout_job(egui::text::LayoutJob {
halign: match side {
NodeSide::Left => egui::Align::LEFT,
NodeSide::Right => egui::Align::RIGHT,
},
..(*layout_job).clone()
})
});
PreparedSocket {
id,
side,
text,
filled,
color,
shape,
}
}