// The Machines pane: the four matrix slots, their arrangement, and this Mac's // own name. // // The Windows equivalents are the drag-and-drop device layout on the modern page // ("Device layout", "Drag and drop a machine to rearrange the order", // `MouseWithoutBordersPage.xaml:102-181`) and the four name boxes on // `Form/frmMatrix.cs`. The matrix is what the whole protocol hangs off: the // names decide who is dialled (`ChannelConfig.matrixNames`) and the order decides // which machine is to the left or right of which (docs/protocol.md §4.1). import Foundation import MWBCore import SwiftUI /// The device-layout editor. struct MachinesPane: View { @ObservedObject var model: SettingsViewModel var body: some View { SettingsPaneLayout { SettingsSection( "Device layout", note: "Type each machine's host name into the slot that matches where it sits on " + "your desk. Use the arrows to rearrange them." ) { if model.settings.matrixOneRow { HStack(spacing: 8) { ForEach(0..<4, id: \.self) { slot(at: $0) } } } else { VStack(spacing: 8) { HStack(spacing: 8) { slot(at: 0); slot(at: 1) } HStack(spacing: 8) { slot(at: 2); slot(at: 3) } } } HStack { Button("Save layout") { model.saveMatrix() } .disabled(!model.matrixHasEdits) Button("Revert") { model.refreshDrafts() } .disabled(!model.matrixHasEdits) Spacer() } } SettingsSection("Arrangement") { SettingsToggle( "Devices in a single row", help: "Sets whether the devices are aligned on a single row. A two by two " + "matrix is considered otherwise.", isOn: model.binding(\.matrixOneRow)) SettingsToggle( "Move the mouse back to the first machine when it passes the last one", isOn: model.binding(\.wrapMouse)) } SettingsSection( "This machine", note: "The other machines have to list this exact name in their own layout, and " + "have to be able to resolve it on the network." ) { SettingsRow(model.localName.isEmpty ? "(no host name)" : model.localName) { HStack(spacing: 8) { Button("Copy") { model.copyLocalName() } Menu("Put in slot") { ForEach(0..<4, id: \.self) { index in Button("Slot \(index + 1)") { model.useLocalName(inSlot: index) } } } .frame(width: 110) } } } } } /// One slot: its number, its name, and the two buttons that move it. private func slot(at index: Int) -> some View { VStack(alignment: .leading, spacing: 4) { Text("Slot \(index + 1)").font(.caption).foregroundStyle(.secondary) TextField("Machine name", text: binding(for: index)) .textFieldStyle(.roundedBorder) .onSubmit { model.saveMatrix() } HStack(spacing: 4) { Button(model.settings.matrixOneRow ? "◀" : "▲") { model.swapSlots(index, previous(of: index)) } .disabled(index == 0) Button(model.settings.matrixOneRow ? "▶" : "▼") { model.swapSlots(index, next(of: index)) } .disabled(index == 3) Spacer() } .buttonStyle(.bordered) } } private func binding(for index: Int) -> Binding { Binding( get: { model.slotDrafts.indices.contains(index) ? model.slotDrafts[index] : "" }, set: { value in guard model.slotDrafts.indices.contains(index) else { return } model.slotDrafts[index] = value }) } /// In one row the neighbours are the slots either side; in two rows the /// buttons walk the same 0,1,2,3 order, which is the order the matrix packets /// use (`MachineStuff.cs:969`). private func previous(of index: Int) -> Int { max(0, index - 1) } private func next(of index: Int) -> Int { min(3, index + 1) } }