sliceEngines.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * Registry of the available slicing engines.
  3. *
  4. * "Engine" here means *where slicing runs*, which is a separate axis from the
  5. * `preferred_slicer` setting (that one only selects which slicer binary the
  6. * server-side sidecar drives). Keeping them apart avoids having to represent
  7. * combinations that don't exist — there is no browser build of BambuStudio, so
  8. * a single dropdown mixing the two would offer choices that cannot work.
  9. *
  10. * Today exactly one engine is registered. The registry exists so that adding a
  11. * browser/WASM engine is a matter of pushing a second entry: the settings card
  12. * and the slice modal both derive their UI from `availableEngines()`, so a
  13. * second engine makes the pickers appear without either of them changing.
  14. *
  15. * Deliberately *not* shipping a disabled "In browser" option in the meantime.
  16. * An option a user can see but never pick reads as a broken feature, and there
  17. * is nothing behind it yet: the WASM engine returns raw G-code, while dispatch
  18. * needs a `.gcode.3mf` container, so browser slicing cannot reach a printer
  19. * until that packaging exists.
  20. */
  21. export type SliceEngineId = 'sidecar' | 'browser';
  22. export interface SliceEngine {
  23. id: SliceEngineId;
  24. /** i18n key for the human-readable name. */
  25. labelKey: string;
  26. /** i18n key for the one-line explanation shown under the picker. */
  27. descriptionKey: string;
  28. /**
  29. * False while an engine is defined but not yet usable. Unavailable engines
  30. * are never offered; they exist here so the surrounding code can be written
  31. * against the full set rather than special-cased later.
  32. */
  33. available: boolean;
  34. }
  35. const ENGINES: SliceEngine[] = [
  36. {
  37. id: 'sidecar',
  38. labelKey: 'settings.sliceEngineSidecar',
  39. descriptionKey: 'settings.sliceEngineSidecarHint',
  40. available: true,
  41. },
  42. {
  43. id: 'browser',
  44. labelKey: 'settings.sliceEngineBrowser',
  45. descriptionKey: 'settings.sliceEngineBrowserHint',
  46. available: false,
  47. },
  48. ];
  49. export const DEFAULT_SLICE_ENGINE: SliceEngineId = 'sidecar';
  50. /** Engines a user can actually pick right now. */
  51. export function availableEngines(): SliceEngine[] {
  52. return ENGINES.filter((e) => e.available);
  53. }
  54. /** True when there is a real choice to present. */
  55. export function hasEngineChoice(): boolean {
  56. return availableEngines().length > 1;
  57. }
  58. /**
  59. * Resolves a stored or per-job engine id to one that can actually run.
  60. *
  61. * A setting can outlive the engine it names — an install that had browser
  62. * slicing enabled and then loaded a build without it must still be able to
  63. * slice, so an unavailable id falls back rather than failing.
  64. */
  65. export function resolveEngine(id: string | null | undefined): SliceEngineId {
  66. const match = availableEngines().find((e) => e.id === id);
  67. return match?.id ?? DEFAULT_SLICE_ENGINE;
  68. }