BREP_ecad_egui 0.3.0

Embeddable egui editors for schematics, wiring diagrams, boards, symbols, and pads.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Every toolbar command and keyboard shortcut, as data. Each editor's own toolbar draws
//! these and its keyboard handling runs them, so a host that lists an editor's actions
//! and runs them by id offers exactly what that editor's toolbar does.
use super::*;
use egui::{KeyboardShortcut, Modifiers};

/// Where an action sits in the editor's own toolbar.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ActionGroup {
    /// Switching between the schematic and the board.
    View,
    /// Choosing the pointer tool.
    Tool,
    /// Undo and redo.
    History,
    /// Zooming and fitting.
    Zoom,
    /// The copper layer to route on.
    Layer,
    /// The snapping grid: one action per step, of which one is pressed.
    Grid,
    /// Commands for the tool in use, on the toolbar's second row.
    Context,
    /// Commands on the selection or the gesture in progress, run from the keyboard.
    Selection,
}

/// One command of an editor `T`. Its predicates read the editor, so they answer for the
/// current state.
pub struct Action<T> {
    /// Stable identifier, such as `sheet.tool.wire`.
    pub id: &'static str,
    /// Name for menus and tooltips.
    pub label: &'static str,
    pub group: ActionGroup,
    /// Keys that run it while the pointer is over the sheet or board. Shift and Alt
    /// are ignored unless listed.
    pub shortcuts: &'static [KeyboardShortcut],
    offered: fn(&T) -> bool,
    enabled: fn(&T) -> bool,
    pressed: Option<fn(&T) -> bool>,
    caption: Option<fn(&T) -> String>,
    run: fn(&mut T),
}
impl<T> Action<T> {
    /// Whether the editor offers this action now: its view and tool are showing.
    pub fn offered(&self, editor: &T) -> bool {
        (self.offered)(editor)
    }
    /// Whether an offered action would do something now.
    pub fn enabled(&self, editor: &T) -> bool {
        (self.enabled)(editor)
    }
    /// For a toggle or choice, whether it is on; `None` for a plain command.
    pub fn pressed(&self, editor: &T) -> Option<bool> {
        self.pressed.map(|pressed| pressed(editor))
    }
    /// Button text, which for some actions describes the current state, such as
    /// "Bend: vertical first".
    pub fn caption(&self, editor: &T) -> String {
        self.caption
            .map_or_else(|| self.label.to_owned(), |caption| caption(editor))
    }
}

/// Every action the sheet and board editor has, in toolbar order.
pub fn actions() -> impl Iterator<Item = &'static Action<Editor>> {
    SHARED.iter().chain(SHEET).chain(board_view::BOARD_ACTIONS)
}

pub(crate) const fn action<T>(
    id: &'static str,
    label: &'static str,
    group: ActionGroup,
    run: fn(&mut T),
) -> Action<T> {
    Action {
        id,
        label,
        group,
        shortcuts: &[],
        offered: |_| true,
        enabled: |_| true,
        pressed: None,
        caption: None,
        run,
    }
}
impl<T> Action<T> {
    pub(crate) const fn offered_when(mut self, offered: fn(&T) -> bool) -> Self {
        self.offered = offered;
        self
    }
    pub(crate) const fn enabled_when(mut self, enabled: fn(&T) -> bool) -> Self {
        self.enabled = enabled;
        self
    }
    pub(crate) const fn pressed_when(mut self, pressed: fn(&T) -> bool) -> Self {
        self.pressed = Some(pressed);
        self
    }
    pub(crate) const fn captioned(mut self, caption: fn(&T) -> String) -> Self {
        self.caption = Some(caption);
        self
    }
    pub(crate) const fn keys(mut self, shortcuts: &'static [KeyboardShortcut]) -> Self {
        self.shortcuts = shortcuts;
        self
    }
}
/// [`action`] for the sheet and board editor, so a table's closures need no type.
pub(crate) const fn sheet_action(
    id: &'static str,
    label: &'static str,
    group: ActionGroup,
    run: fn(&mut Editor),
) -> Action<Editor> {
    action(id, label, group, run)
}
pub(crate) const fn key(key: Key) -> KeyboardShortcut {
    KeyboardShortcut::new(Modifiers::NONE, key)
}
pub(crate) const fn command(key: Key) -> KeyboardShortcut {
    KeyboardShortcut::new(Modifiers::COMMAND, key)
}

fn sheet(e: &Editor) -> bool {
    e.view == View::Schematic
}
fn sheet_tool(e: &Editor, tool: Tool) -> bool {
    sheet(e) && e.tool == tool
}
/// Tools that draw nets, which a wiring diagram does not have.
fn net_tools(e: &Editor) -> bool {
    sheet(e) && !e.wiring()
}

static SHARED: &[Action<Editor>] = &[
    sheet_action("view.schematic", "Schematic", ActionGroup::View, |e| {
        e.set_view(View::Schematic)
    })
    .offered_when(|e| !e.wiring())
    .enabled_when(|e| !e.routing())
    .pressed_when(|e| e.view == View::Schematic),
    sheet_action("view.board", "PCB", ActionGroup::View, |e| {
        e.set_view(View::Board)
    })
    .offered_when(|e| !e.wiring())
    .enabled_when(|e| !e.routing())
    .pressed_when(|e| e.view == View::Board),
    sheet_action("edit.undo", "Undo", ActionGroup::History, |e| {
        e.step_history(HistoryKey::Undo)
    })
    .keys(&[command(Key::Z)])
    .enabled_when(|e| e.can_step_history(HistoryKey::Undo)),
    sheet_action("edit.redo", "Redo", ActionGroup::History, |e| {
        e.step_history(HistoryKey::Redo)
    })
    .keys(&[
        KeyboardShortcut::new(Modifiers::COMMAND.plus(Modifiers::SHIFT), Key::Z),
        command(Key::Y),
    ])
    .enabled_when(|e| e.can_step_history(HistoryKey::Redo)),
    sheet_action("view.zoom_out", "Zoom −", ActionGroup::Zoom, |e| {
        e.zoom_view(1. / 1.25)
    }),
    sheet_action("view.zoom_in", "Zoom +", ActionGroup::Zoom, |e| {
        e.zoom_view(1.25)
    }),
    sheet_action("view.fit", "Fit", ActionGroup::Zoom, Editor::fit_view)
        .keys(&[key(Key::F)])
        .captioned(|e| if sheet(e) { "Fit sheet" } else { "Fit board" }.into()),
];

static SHEET: &[Action<Editor>] = &[
    sheet_action("sheet.tool.select", "Select", ActionGroup::Tool, |e| {
        e.set_tool(Tool::Select)
    })
    .keys(&[key(Key::V)])
    .offered_when(sheet)
    .pressed_when(|e| e.tool == Tool::Select),
    sheet_action("sheet.tool.wire", "Draw wire", ActionGroup::Tool, |e| {
        e.set_tool(Tool::Wire)
    })
    .keys(&[key(Key::W)])
    .offered_when(net_tools)
    .pressed_when(|e| e.tool == Tool::Wire),
    sheet_action("sheet.tool.junction", "Junction", ActionGroup::Tool, |e| {
        e.set_tool(Tool::Junction)
    })
    .keys(&[key(Key::J)])
    .offered_when(net_tools)
    .pressed_when(|e| e.tool == Tool::Junction),
    sheet_action("sheet.tool.label", "Net label", ActionGroup::Tool, |e| {
        e.set_tool(Tool::Label)
    })
    .keys(&[key(Key::L)])
    .offered_when(net_tools)
    .pressed_when(|e| e.tool == Tool::Label),
    sheet_action("sheet.tool.no_connect", "No-connect", ActionGroup::Tool, |e| {
        e.set_tool(Tool::NoConnect)
    })
    .keys(&[key(Key::Q)])
    .offered_when(net_tools)
    .pressed_when(|e| e.tool == Tool::NoConnect),
    sheet_action("sheet.tool.power_flag", "Power flag", ActionGroup::Tool, |e| {
        e.set_tool(Tool::PowerFlag)
    })
    .offered_when(net_tools)
    .pressed_when(|e| e.tool == Tool::PowerFlag),
    sheet_action("sheet.tool.power", "Power symbol", ActionGroup::Tool, |e| {
        e.set_tool(Tool::Power)
    })
    .keys(&[key(Key::P)])
    .offered_when(net_tools)
    .pressed_when(|e| e.tool == Tool::Power),
    sheet_action(
        "sheet.place.rotate",
        "Rotate 90°",
        ActionGroup::Context,
        Editor::rotate,
    )
    .offered_when(|e| sheet_tool(e, Tool::Place)),
    sheet_action(
        "sheet.place.done",
        "Done placing",
        ActionGroup::Context,
        Editor::cancel,
    )
    .offered_when(|e| sheet_tool(e, Tool::Place)),
    sheet_action(
        "sheet.wire.bend",
        "Bend direction",
        ActionGroup::Context,
        |e| e.vertical_first = !e.vertical_first,
    )
    .keys(&[key(Key::Tab)])
    .offered_when(|e| sheet_tool(e, Tool::Wire))
    .captioned(|e| {
        if e.vertical_first {
            "Bend: vertical first"
        } else {
            "Bend: horizontal first"
        }
        .into()
    }),
    sheet_action(
        "sheet.wire.finish",
        "Finish wire",
        ActionGroup::Context,
        |e| e.wire_start = None,
    )
    .keys(&[key(Key::Enter)])
    .offered_when(|e| sheet_tool(e, Tool::Wire))
    .enabled_when(|e| e.wire_start.is_some()),
    sheet_action(
        "sheet.wire.done",
        "Done wiring",
        ActionGroup::Context,
        Editor::cancel,
    )
    .offered_when(|e| sheet_tool(e, Tool::Wire)),
    sheet_action(
        "sheet.label.done",
        "Done labeling",
        ActionGroup::Context,
        Editor::cancel,
    )
    .offered_when(|e| sheet_tool(e, Tool::Label)),
    sheet_action(
        "sheet.move.cancel",
        "Cancel move",
        ActionGroup::Context,
        Editor::cancel,
    )
    .offered_when(|e| sheet_tool(e, Tool::Move)),
    sheet_action(
        "sheet.junction.done",
        "Done",
        ActionGroup::Context,
        Editor::cancel,
    )
    .offered_when(|e| sheet_tool(e, Tool::Junction)),
    sheet_action(
        "sheet.rotate",
        "Rotate 90°",
        ActionGroup::Selection,
        Editor::rotate,
    )
    .keys(&[key(Key::R)])
    .offered_when(sheet)
    .enabled_when(|e| {
        e.tool == Tool::Place
            || matches!(
                e.selected,
                Some(Selection::Component(_) | Selection::Label(_))
            )
    }),
    sheet_action(
        "sheet.duplicate",
        "Duplicate",
        ActionGroup::Selection,
        Editor::duplicate,
    )
    .keys(&[command(Key::D)])
    .offered_when(sheet)
    .enabled_when(|e| matches!(e.selected, Some(Selection::Component(_)))),
    sheet_action(
        "sheet.delete",
        "Delete selection",
        ActionGroup::Selection,
        Editor::delete,
    )
    .keys(&[key(Key::Delete), key(Key::Backspace)])
    .offered_when(sheet)
    .enabled_when(|e| e.selected.is_some()),
    sheet_action(
        "sheet.cancel",
        "Cancel",
        ActionGroup::Selection,
        Editor::cancel,
    )
    .keys(&[key(Key::Escape)])
    .offered_when(sheet),
];

/// Modifier keys a shortcut asks for; a press matching several shortcuts runs the one
/// asking for the most, so Ctrl+Shift+Z redoes rather than undoes.
fn specificity(modifiers: Modifiers) -> u8 {
    u8::from(modifiers.alt) + u8::from(modifiers.shift) + u8::from(modifiers.command)
}

/// Run `id` on `editor`, as a click on its button does. False when there is no such
/// action, or it is not offered or not enabled now.
pub(crate) fn run_action<T: 'static>(
    editor: &mut T,
    actions: impl Iterator<Item = &'static Action<T>>,
    id: &str,
) -> bool {
    match actions.into_iter().find(|a| a.id == id) {
        Some(a) if a.offered(editor) && a.enabled(editor) => {
            (a.run)(editor);
            true
        }
        _ => false,
    }
}

/// Run the actions whose shortcuts were pressed this frame, most specific first.
pub(crate) fn run_shortcuts<T: 'static>(
    editor: &mut T,
    actions: &[&'static Action<T>],
    ctx: &egui::Context,
) {
    let presses: Vec<(Key, Modifiers)> = ctx.input(|i| {
        i.events
            .iter()
            .filter_map(|event| match event {
                egui::Event::Key {
                    key,
                    pressed: true,
                    modifiers,
                    ..
                } => Some((*key, *modifiers)),
                _ => None,
            })
            .collect()
    });
    for (key, modifiers) in presses {
        let bound = actions
            .iter()
            .copied()
            .filter(|a| a.offered(editor))
            .filter_map(|a| {
                a.shortcuts
                    .iter()
                    .find(|s| s.logical_key == key && modifiers.matches_logically(s.modifiers))
                    .map(|s| (a, specificity(s.modifiers)))
            })
            .max_by_key(|(_, specificity)| *specificity);
        if let Some((action, _)) = bound
            && action.enabled(editor)
        {
            // Handled here, so an open editor window's own history keys do not
            // run it a second time this frame.
            ctx.input_mut(|i| i.consume_key(modifiers, key));
            (action.run)(editor);
        }
    }
}

/// Draw the offered actions of one group as a toolbar's buttons.
pub(crate) fn action_buttons<T: 'static>(
    editor: &mut T,
    actions: impl Iterator<Item = &'static Action<T>>,
    ui: &mut egui::Ui,
    group: ActionGroup,
) {
    let offered: Vec<&'static Action<T>> = actions
        .into_iter()
        .filter(|a| a.group == group && a.offered(editor))
        .collect();
    for action in offered {
        let caption = action.caption(editor);
        let button = match action.pressed(editor) {
            Some(pressed) => egui::Button::selectable(pressed, caption),
            None => egui::Button::new(caption),
        };
        if ui.add_enabled(action.enabled(editor), button).clicked() {
            (action.run)(editor);
        }
    }
}

impl Editor {
    /// Run an action by id, as a click on its button does. Returns false when there is
    /// no such action, or it is not offered or not enabled now.
    pub fn run_action(&mut self, id: &str) -> bool {
        run_action(self, actions(), id)
    }
    pub(crate) fn run_shortcuts(&mut self, ctx: &egui::Context) {
        run_shortcuts(self, &actions().collect::<Vec<_>>(), ctx);
    }
    /// Draw the offered actions of one group as the toolbar's buttons.
    pub(crate) fn action_buttons(&mut self, ui: &mut egui::Ui, group: ActionGroup) {
        action_buttons(self, actions(), ui, group);
    }
    fn step_history(&mut self, key: HistoryKey) {
        match key {
            HistoryKey::Undo => self.undo(),
            HistoryKey::Redo => self.redo(),
        }
    }
    fn can_step_history(&self, key: HistoryKey) -> bool {
        !self.routing()
            && match key {
                HistoryKey::Undo => self.history.can_undo(),
                HistoryKey::Redo => self.history.can_redo(),
            }
    }
    fn zoom_view(&mut self, factor: f32) {
        match self.view {
            View::Schematic => self.zoom_by(factor),
            View::Board => self.zoom_board(factor),
        }
    }
    fn fit_view(&mut self) {
        match self.view {
            View::Schematic => self.fit(self.canvas_size),
            View::Board => self.fit_board(),
        }
    }
}