Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Sources/StableBar/Configuration/LayoutConfiguration.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import Foundation

struct WidgetGroupConfiguration: Decodable, Sendable {
/// The group's name in Settings; the bar doesn't draw it.
let name: String?
var style: WidgetStyle
let members: [WidgetGroupMember]

enum CodingKeys: String, CodingKey, CaseIterable { case style, members }
enum CodingKeys: String, CodingKey, CaseIterable { case name, style, members }

init(from decoder: Decoder) throws {
try decoder.rejectUnknownKeys(CodingKeys.self)
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decodeIfPresent(String.self, forKey: .name)
style = try values.decodeIfPresent(WidgetStyle.self, forKey: .style) ?? WidgetStyle()
members = try values.decodeIfPresent([WidgetGroupMember].self, forKey: .members) ?? []
}

var takesOthers: Bool { members.contains(where: \.others) }

/// What Settings calls `group`: its name, or its ID made readable when it has none.
static func title(of group: String, in groups: [String: WidgetGroupConfiguration]) -> String {
let name = groups[group]?.name?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
return name.isEmpty ? friendly(group) : name
}

/// `widgets`, each enabled one without a `group` in the group whose `members` match it best, unless that group's
/// widgets sit in another position and it doesn't take others. Widgets with their own `group` settle a group's
/// position first. `ownGroup` can stand in for a widget's own `group`, to see where a widget would go without it.
Expand Down
7 changes: 4 additions & 3 deletions Sources/StableBar/Editing/BarEditView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -455,17 +455,18 @@ struct BarEditTile: View {

@ViewBuilder private func groupCommands(on screen: BarDisplay) -> some View {
let grouping = WidgetGrouping(store: store)
let groups = store.configuration?.onScreen(kind).groups ?? [:]
if let group = entry.widget.group {
Divider()
Button("Edit Group “\(friendly(group))”…") { editing.editGroup(group) }
Button("Edit Group “\(WidgetGroupConfiguration.title(of: group, in: groups))”…") { editing.editGroup(group) }
.disabled(grouping.groups[group] == nil)
Button("Leave Group") { editing.leaveGroup(entry.id, on: display) }
.disabled(!grouping.canLeave(entry.id))
} else {
let choices = grouping.choices(for: entry.id, among: editing.widgets(on: screen), groups: store.configuration?.onScreen(kind).groups ?? [:])
let choices = grouping.choices(for: entry.id, among: editing.widgets(on: screen), groups: groups)
if !choices.isEmpty { Divider() }
ForEach(choices, id: \.self) { choice in
Button(choice.title(names: editing.names)) { editing.group(entry.id, choice, on: display) }
Button(choice.title(names: editing.names, groups: groups)) { editing.group(entry.id, choice, on: display) }
}
}
Divider()
Expand Down
4 changes: 2 additions & 2 deletions Sources/StableBar/Editing/WidgetGrouping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ struct WidgetGrouping {
case join(String)
case groupWith(String)

func title(names: [String: String]) -> String {
func title(names: [String: String], groups: [String: WidgetGroupConfiguration]) -> String {
switch self {
case let .join(group): "Add to Group “\(friendly(group))”"
case let .join(group): "Add to Group “\(WidgetGroupConfiguration.title(of: group, in: groups))”"
case let .groupWith(id): "Group With “\(names[id] ?? id)”"
}
}
Expand Down
9 changes: 8 additions & 1 deletion Sources/StableBar/Settings/BarInspector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ struct BarInspector: View {
if let widget {
widgetHeader(widget)
} else if let group {
header(Image(systemName: WidgetGrouping.symbol).font(.system(size: 18)), title: friendly(group)) {
header(Image(systemName: WidgetGrouping.symbol).font(.system(size: 18)),
title: WidgetGroupConfiguration.title(of: group, in: store.configuration?.onScreen(screen).groups ?? [:])) {
Text(groupMembers(group).isEmpty ? "Group"
: "Group of \(groupMembers(group).map { names[$0.id] ?? $0.id }.formatted(.list(type: .and)))")
}
Expand Down Expand Up @@ -248,6 +249,12 @@ struct BarInspector: View {
set: { editor.changeLook(ConfigurationEdit(action: .groups, properties: [group: .object(["style": .object($0)])])) },
unset: { unset(["groups": .object([group: .object(["style": .object($0)])])]) })
let grouping = WidgetGrouping(store: store)
SettingsGroup("Group") {
SettingRow(title: "Name", hint: "Shown in Settings only, under every theme.") {
GroupNameField(placeholder: friendly(group), name: store.configuration?.groups[group]?.name ?? "",
focusRequested: $editor.renameGroupRequested) { editor.renameGroup(group, to: $0) }
}
}
GroupsLookSection(editor: editor, style: style, flared: store.configuration?.groups[group]?.style.flare != nil,
spacing: store.configuration?.onScreen(screen).bar.spacing ?? 0, title: "Style")
SettingsGroup("Widgets") {
Expand Down
28 changes: 28 additions & 0 deletions Sources/StableBar/Settings/ConfigurationEditor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ final class ConfigurationEditor {
var barItem = BarItem.widget
/// The group Settings shows while `barItem` is `group`.
var selectedGroup: String?
/// Set by Rename…: the group's Name field takes focus once it shows, and clears it.
var renameGroupRequested = false
var appearanceTab = AppearanceTab.themes
var widgetTab = WidgetTab.content
var barScreen = ScreenKind.laptop
Expand Down Expand Up @@ -110,6 +112,32 @@ final class ConfigurationEditor {
catch { self.error = errorMessage(error) }
}

func startRenaming(_ group: String) {
showGroup(group)
renameGroupRequested = barItem == .group && selectedGroup == group
}

/// Names `group`, or removes its name when `name` is blank. The name isn't part of the look, so it's saved for every
/// theme rather than as an edit of the selected one.
func renameGroup(_ group: String, to name: String) {
// The field saves when it loses focus, which can come after Ungroup or Undo took the group away.
guard let current = store.configuration?.groups[group],
store.configuration?.widgets.contains(where: { $0.group == group }) == true else { return }
let name = name.trimmingCharacters(in: .whitespacesAndNewlines)
if !name.isEmpty {
change([ConfigurationEdit(action: .groups, properties: [group: .object(["name": .string(name)])])])
return
}
guard current.name != nil else { return }
// A name another layer gives the group, such as an included file or the bar under a profile, needs a `null` here.
let mainFile = store.configurationStore.configurationURL.standardizedFileURL.path
let elsewhere = store.contributions["groups.\(group).name"]?.contains {
$0.value != .null && ($0.origin.file != mainFile || $0.origin.profile != editingProfile)
} == true
change([elsewhere ? ConfigurationEdit(action: .groups, properties: [group: .object(["name": .null])])
: ConfigurationEdit(action: .unset, properties: ["groups": .object([group: .object(["name": .null])])])])
}

func showPackage(_ id: String) {
finishEditing()
searchTarget = nil
Expand Down
35 changes: 35 additions & 0 deletions Sources/StableBar/Settings/GroupNameField.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import SwiftUI

/// A group's name, saved on Return or when the field loses focus. A blank name goes back to `placeholder`.
struct GroupNameField: View {
let placeholder: String
let name: String
@Binding var focusRequested: Bool
let save: (String) -> Void
@State private var text = ""
@FocusState private var focused: Bool

var body: some View {
TextField("Name", text: $text, prompt: Text(placeholder))
.labelsHidden()
.frame(width: 220)
.focused($focused)
.onSubmit(commit)
.onAppear { text = name }
// A draft the user hasn't touched follows a rename made elsewhere, so leaving the field can't undo it.
.onChange(of: name) { oldName, newName in
if !focused || text == oldName { text = newName }
}
.onChange(of: focused) { if !focused { commit() } }
.onChange(of: focusRequested, initial: true) {
guard focusRequested else { return }
focusRequested = false
focused = true
}
}

private func commit() {
guard text.trimmingCharacters(in: .whitespacesAndNewlines) != name else { return }
save(text)
}
}
8 changes: 5 additions & 3 deletions Sources/StableBar/Settings/WidgetsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ struct WidgetsView: View {
grouping: WidgetGrouping) -> some View {
let selected = selectedGroup == group
let first = members[0]
let title = friendly(group)
let title = WidgetGroupConfiguration.title(of: group, in: groups)
let subtitle = members.count == 1 ? "Group · 1 widget" : "Group · \(members.count) widgets"
let collapsed = collapsedGroups.contains(group)
return ListRow(selected: selected) { _ in
Expand Down Expand Up @@ -264,6 +264,8 @@ struct WidgetsView: View {
}
}
Divider()
Button("Rename…") { editor.startRenaming(group) }
.disabled(grouping.groups[group] == nil)
Button("Ungroup") { editor.ungroup(group) }
.disabled(grouping.ungroupBlocker(group) != nil)
}
Expand Down Expand Up @@ -310,15 +312,15 @@ struct WidgetsView: View {
}
Divider()
if let group = widget.group {
Button("Edit Group “\(friendly(group))”") { editor.showGroup(group) }
Button("Edit Group “\(WidgetGroupConfiguration.title(of: group, in: groups))”") { editor.showGroup(group) }
.disabled(grouping.groups[group] == nil)
Button("Leave Group") { editor.leaveGroup(widget.id) }
.disabled(!grouping.canLeave(widget.id))
Divider()
} else {
let choices = grouping.choices(for: widget.id, among: listed, groups: groups)
ForEach(choices, id: \.self) { choice in
Button(choice.title(names: names)) { editor.group(widget.id, choice) }
Button(choice.title(names: names, groups: groups)) { editor.group(widget.id, choice) }
}
if !choices.isEmpty { Divider() }
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/StableBar/Themes/ThemeEdits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ private func lookValues(in document: [String: ConfigurationValue], tokens: (Stri
case "defaults": add(path, leaf, section: "Widgets", title: styleTitle(Array(path.dropFirst())))
case "popupDefaults": add(path, leaf, section: "Popups", title: styleTitle(Array(path.dropFirst())))
case "groupDefaults": add(path, leaf, section: "Groups", title: "Every group: \(detailTitle(spacing(Array(path.dropFirst()))))")
// A group's name belongs to the bar under every theme, not to its look.
case "groups" where path == ["groups", name, "name"]: continue
case "groups":
add(path, leaf, section: "Groups", title: "\(friendly(name)) group: \(detailTitle(spacing(path.dropFirst(2).filter { $0 != "style" })))")
case "roles": add(path, leaf, section: "Roles", title: "\(friendly(name)): \(detailTitle(Array(path.dropFirst(2))))")
Expand Down
Loading
Loading