added renaming themes

gonna add delete themes next
This commit is contained in:
neon443
2025-06-27 21:45:09 +01:00
parent a607996ca5
commit d0c84b19f4
3 changed files with 73 additions and 10 deletions

View File

@@ -48,6 +48,16 @@ class HostsManager: ObservableObject, @unchecked Sendable {
task.resume()
}
func renameTheme(_ theme: Theme?, to newName: String) {
guard let theme else { return }
guard let index = themes.firstIndex(where: {$0.id == theme.id}) else { return }
var newTheme = themes[index]
newTheme.name = newName
newTheme.id = UUID()
withAnimation { themes[index] = newTheme }
saveThemes()
}
@MainActor
func importTheme(name: String, data: Data?) {
guard let data else { return }

View File

@@ -23,3 +23,39 @@ struct foregroundColorStyle: ViewModifier {
}
}
}
struct scrollPaging: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 17, *) {
content.scrollTargetBehavior(.paging)
} else {
content
}
}
}
struct scrollTargetLayoutt: ViewModifier {
func body(content: Content) -> some View {
if #available(iOS 17, *) {
content.scrollTargetLayout()
} else {
content
}
}
}
struct contentMarginss: ViewModifier {
var length: CGFloat
init(length: CGFloat) {
self.length = length
}
func body(content: Content) -> some View {
if #available(iOS 17, *) {
content.contentMargins(length)
} else {
content
}
}
}

View File

@@ -14,6 +14,10 @@ struct ThemeManagerView: View {
@State var importURL: String = ""
@State var toImportName: String = ""
@State var showRenameAlert: Bool = false
@State var themeToRename: Theme?
@State var rename: String = ""
let grid: GridItem = GridItem(
.fixed(90),
spacing: 8,
@@ -27,10 +31,32 @@ struct ThemeManagerView: View {
LazyHGrid(rows: [grid, grid], alignment: .center, spacing: 8) {
ForEach(hostsManager.themes) { theme in
ThemePreview(theme: theme)
.contextMenu {
Button() {
themeToRename = theme
rename = theme.name
showRenameAlert.toggle()
} label: {
Label("Rename", systemImage: "pencil")
}
Button(role: .destructive) {
} label: {
Label("Delete", systemImage: "trash")
}
}
}
}
.alert("", isPresented: $showRenameAlert) {
TextField("", text: $rename)
Button("OK") {
hostsManager.renameTheme(themeToRename, to: rename)
rename = ""
}
}
.padding(.horizontal, 8)
}
.frame(height: 160)
.fixedSize(horizontal: false, vertical: true)
.scrollIndicators(.hidden)
.alert("Enter URL", isPresented: $showAlert) {
TextField("", text: $importURL, prompt: Text("URL"))
@@ -42,15 +68,6 @@ struct ThemeManagerView: View {
}
}
.toolbar {
ToolbarItem(placement: .confirmationAction) {
Button() {
if let pasteboard = UIPasteboard().string {
hostsManager.importTheme(name: toImportName, data: pasteboard.data(using: .utf8))
}
} label: {
Label("Import", systemImage: "plus")
}
}
ToolbarItem() {
Button() {
UIApplication.shared.open(URL(string: "https://iterm2colorschemes.com")!)