brep_app/panels/
workbench_toolbar.rs1use crate::automation::hit_keys::HitKeyDoc;
7use crate::panels::toolbar_button;
8use crate::workbench;
9use brep_render::engine_state::EngineState;
10use eframe::egui;
11use std::collections::HashMap;
12
13#[derive(Default)]
15pub struct WorkbenchToolbarOutcome {
16 pub feature: Option<String>,
18 pub constraint: Option<String>,
20 pub annotation: Option<String>,
22}
23
24#[derive(Default)]
28pub struct WorkbenchToolbarPanel {
29 hits: HashMap<String, egui::Rect>,
30 pub read_only: bool,
34}
35
36impl WorkbenchToolbarPanel {
37 pub fn new() -> Self {
38 Self::default()
39 }
40
41 pub fn visible(state: &EngineState) -> bool {
47 state.settings.show_workbench_toolbar && !state.any_sketch_mode() && !state.ref_select_modal()
48 }
49
50 pub fn show(&mut self, ui: &mut egui::Ui, state: &EngineState) -> WorkbenchToolbarOutcome {
55 self.hits.clear();
56 let mut outcome = WorkbenchToolbarOutcome::default();
57 if !Self::visible(state) {
58 return outcome;
59 }
60 let commands = workbench::offered_home_commands(state);
61 let groups = workbench::creation_groups(&commands);
62 if groups.is_empty() {
63 return outcome;
64 }
65 egui::containers::panel::Panel::top("brep-workbench-toolbar")
66 .resizable(false)
67 .show(ui, |ui| {
68 ui.add_space(2.0);
69 toolbar_button::wrapped(ui, |ui| {
70 for (index, (name, members)) in groups.iter().enumerate() {
71 let starts_line = index > 0 && group_does_not_fit(ui, name, members.len());
78 if starts_line {
79 ui.end_row();
80 } else if index > 0 {
81 ui.separator();
82 }
83 caption(ui, name);
84 for command in members {
85 let mut command = (*command).clone();
86 if self.read_only {
87 command.disabled_reason = Some("Document is read-only".into());
88 }
89 if let Some(target) = super::ribbon::draw_command(
90 ui,
91 &command,
92 true,
93 false,
94 &mut self.hits,
95 ) {
96 match target {
97 workbench::CommandTarget::Feature(id) => {
98 outcome.feature = Some(id)
99 }
100 workbench::CommandTarget::Constraint(id) => {
101 outcome.constraint = Some(id)
102 }
103 workbench::CommandTarget::Annotation(id) => {
104 outcome.annotation = Some(id)
105 }
106 _ => {}
107 }
108 }
109 }
110 }
111 });
112 ui.add_space(2.0);
113 });
114 outcome
115 }
116
117 pub fn set_ribbon_hits(&mut self, hits: HashMap<String, egui::Rect>) {
118 self.hits = hits;
119 }
120
121 pub fn hits_json(&self) -> String {
125 crate::automation::hit_rects::hits_json(&self.hits)
126 }
127}
128
129fn group_does_not_fit(ui: &egui::Ui, name: &str, buttons: usize) -> bool {
133 let font = egui::FontId::proportional(super::ribbon::CHROME_CAPTION_SIZE);
134 let caption_w = ui.ctx().fonts_mut(|f| f.layout_no_wrap(name.to_owned(), font, egui::Color32::PLACEHOLDER).size().x);
135 let gap = ui.spacing().item_spacing.x;
136 let button = crate::viewport::touch::target_size(
137 ui.ctx(),
138 egui::vec2(super::toolbar_button::TOOLBAR_BTN, super::toolbar_button::TOOLBAR_BTN),
139 )
140 .x;
141 let separator = ui.spacing().item_spacing.x * 2.0 + 1.0;
142 let needed = separator + caption_w + gap + buttons as f32 * (button + gap);
143 let remaining = ui.max_rect().right().min(ui.clip_rect().right()) - ui.cursor().left();
144 needed > remaining && needed <= ui.max_rect().width() && ui.cursor().left() > ui.max_rect().left() + 0.5
145}
146
147fn caption(ui: &mut egui::Ui, text: &str) {
149 ui.label(egui::RichText::new(text).weak().size(super::ribbon::CHROME_CAPTION_SIZE));
150}
151
152
153pub static HIT_KEYS: &[HitKeyDoc] = &[
155 HitKeyDoc { panel: "wbtoolbar", prefix: "wbtb:feature:", meaning: "add a feature of that type (one button per feature the active workbench offers)", command: Some("feature_add") },
156 HitKeyDoc { panel: "wbtoolbar", prefix: "wbtb:constraint:", meaning: "add an assembly constraint of that type, seeded from the current selection", command: Some("assembly_add_constraint") },
157 HitKeyDoc { panel: "wbtoolbar", prefix: "wbtb:annotation:", meaning: "add a PMI annotation of that type to the active view, its references seeded from the current selection; its dialog opens (wbtb:annotation:<type>)", command: Some("pmi_add_annotation") },
158];