mirror of
https://github.com/neon443/ShhShell.git
synced 2026-03-11 13:26:16 +00:00
added a functional theme editor
added a long ass subscript to get/set ansi as swiftui.color from a subscript added an init from rgb, and init from SwiftUI.Color to colorcodable added a computed suiColor property to colorcodable more guardrails to swifterm.Color.init(_ colorCodable) themecodable colorcodable conform to hashable and equatable
This commit is contained in:
@@ -9,7 +9,7 @@ import Foundation
|
|||||||
import SwiftTerm
|
import SwiftTerm
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct ColorCodable: Codable {
|
struct ColorCodable: Codable, Hashable, Equatable {
|
||||||
var red: Double
|
var red: Double
|
||||||
var green: Double
|
var green: Double
|
||||||
var blue: Double
|
var blue: Double
|
||||||
@@ -19,12 +19,36 @@ struct ColorCodable: Codable {
|
|||||||
case green = "Green Component"
|
case green = "Green Component"
|
||||||
case blue = "Blue Component"
|
case blue = "Blue Component"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init(red: Double, green: Double, blue: Double) {
|
||||||
|
self.red = red
|
||||||
|
self.green = green
|
||||||
|
self.blue = blue
|
||||||
|
}
|
||||||
|
|
||||||
|
init(color: SwiftUI.Color) {
|
||||||
|
let uiColor = UIColor(color)
|
||||||
|
var r: CGFloat = 0
|
||||||
|
var g: CGFloat = 0
|
||||||
|
var b: CGFloat = 0
|
||||||
|
uiColor.getRed(&r, green: &g, blue: &b, alpha: nil)
|
||||||
|
self.red = r
|
||||||
|
self.green = g
|
||||||
|
self.blue = b
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ColorCodable {
|
extension ColorCodable {
|
||||||
var stColor: SwiftTerm.Color {
|
var stColor: SwiftTerm.Color {
|
||||||
return SwiftTerm.Color(self)
|
return SwiftTerm.Color(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var suiColor: SwiftUI.Color {
|
||||||
|
let red = CGFloat(self.red)/65535
|
||||||
|
let green = CGFloat(self.green)/65535
|
||||||
|
let blue = CGFloat(self.blue)/65535
|
||||||
|
return Color(UIColor(red: red, green: green, blue: blue, alpha: 1))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension SwiftTerm.Color {
|
extension SwiftTerm.Color {
|
||||||
@@ -37,9 +61,17 @@ extension SwiftTerm.Color {
|
|||||||
}
|
}
|
||||||
|
|
||||||
convenience init(_ colorCodable: ColorCodable) {
|
convenience init(_ colorCodable: ColorCodable) {
|
||||||
let red = UInt16(colorCodable.red * 65535)
|
var cc = colorCodable
|
||||||
let green = UInt16(colorCodable.green * 65535)
|
if cc.red < 0 { cc.red.negate() }
|
||||||
let blue = UInt16(colorCodable.blue * 65535)
|
if cc.green < 0 { cc.green.negate() }
|
||||||
|
if cc.blue < 0 { cc.blue.negate() }
|
||||||
|
if cc.red > 1 { cc.red = 1 }
|
||||||
|
if cc.green > 1 { cc.green = 1 }
|
||||||
|
if cc.blue > 1 { cc.blue = 1 }
|
||||||
|
|
||||||
|
let red = UInt16(cc.red * 65535)
|
||||||
|
let green = UInt16(cc.green * 65535)
|
||||||
|
let blue = UInt16(cc.blue * 65535)
|
||||||
self.init(red: red, green: green, blue: blue)
|
self.init(red: red, green: green, blue: blue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,9 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import SwiftTerm
|
import SwiftTerm
|
||||||
|
import SwiftUI
|
||||||
|
|
||||||
struct ThemeCodable: Codable {
|
struct ThemeCodable: Codable, Hashable, Equatable {
|
||||||
var id: String?
|
var id: String?
|
||||||
var name: String?
|
var name: String?
|
||||||
var ansi0: ColorCodable
|
var ansi0: ColorCodable
|
||||||
@@ -87,3 +88,50 @@ extension ThemeCodable {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension ThemeCodable {
|
||||||
|
subscript(ansiIndex index: Int) -> SwiftUI.Color {
|
||||||
|
get {
|
||||||
|
switch index {
|
||||||
|
case 0: return ansi0.stColor.suiColor
|
||||||
|
case 1: return ansi1.stColor.suiColor
|
||||||
|
case 2: return ansi2.stColor.suiColor
|
||||||
|
case 3: return ansi3.stColor.suiColor
|
||||||
|
case 4: return ansi4.stColor.suiColor
|
||||||
|
case 5: return ansi5.stColor.suiColor
|
||||||
|
case 6: return ansi6.stColor.suiColor
|
||||||
|
case 7: return ansi7.stColor.suiColor
|
||||||
|
case 8: return ansi8.stColor.suiColor
|
||||||
|
case 9: return ansi9.stColor.suiColor
|
||||||
|
case 10: return ansi10.stColor.suiColor
|
||||||
|
case 11: return ansi11.stColor.suiColor
|
||||||
|
case 12: return ansi12.stColor.suiColor
|
||||||
|
case 13: return ansi13.stColor.suiColor
|
||||||
|
case 14: return ansi14.stColor.suiColor
|
||||||
|
case 15: return ansi15.stColor.suiColor
|
||||||
|
default: fatalError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
switch index {
|
||||||
|
case 0: ansi0 = ColorCodable(color: newValue)
|
||||||
|
case 1: ansi1 = ColorCodable(color: newValue)
|
||||||
|
case 2: ansi2 = ColorCodable(color: newValue)
|
||||||
|
case 3: ansi3 = ColorCodable(color: newValue)
|
||||||
|
case 4: ansi4 = ColorCodable(color: newValue)
|
||||||
|
case 5: ansi5 = ColorCodable(color: newValue)
|
||||||
|
case 6: ansi6 = ColorCodable(color: newValue)
|
||||||
|
case 7: ansi7 = ColorCodable(color: newValue)
|
||||||
|
case 8: ansi8 = ColorCodable(color: newValue)
|
||||||
|
case 9: ansi9 = ColorCodable(color: newValue)
|
||||||
|
case 10: ansi10 = ColorCodable(color: newValue)
|
||||||
|
case 11: ansi11 = ColorCodable(color: newValue)
|
||||||
|
case 12: ansi12 = ColorCodable(color: newValue)
|
||||||
|
case 13: ansi13 = ColorCodable(color: newValue)
|
||||||
|
case 14: ansi14 = ColorCodable(color: newValue)
|
||||||
|
case 15: ansi15 = ColorCodable(color: newValue)
|
||||||
|
default: fatalError()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,27 +9,45 @@ import SwiftUI
|
|||||||
import SwiftTerm
|
import SwiftTerm
|
||||||
|
|
||||||
struct ThemeEditorView: View {
|
struct ThemeEditorView: View {
|
||||||
@State var theme: Theme
|
// @State var theme: Theme
|
||||||
|
@State var themeCodable: ThemeCodable
|
||||||
|
|
||||||
|
init(theme: Theme) {
|
||||||
|
// self.theme = theme
|
||||||
|
self.themeCodable = theme.themeCodable
|
||||||
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationStack {
|
NavigationStack {
|
||||||
// List {
|
// List {
|
||||||
|
// TextField("Name", text: $themeCodable.name)
|
||||||
|
|
||||||
|
ThemePreview(hostsManager: HostsManager(), theme: themeCodable.toTheme(), canModify: false)
|
||||||
|
.id(themeCodable)
|
||||||
|
|
||||||
|
Group {
|
||||||
|
Rectangle()
|
||||||
|
.fill(themeCodable.foreground.stColor.suiColor)
|
||||||
|
Rectangle()
|
||||||
|
.fill(themeCodable.background.stColor.suiColor)
|
||||||
|
Rectangle()
|
||||||
|
.fill(themeCodable.bold.stColor.suiColor)
|
||||||
|
Rectangle()
|
||||||
|
.fill(themeCodable.cursor.stColor.suiColor)
|
||||||
|
Rectangle()
|
||||||
|
.fill(themeCodable.cursorText.stColor.suiColor)
|
||||||
|
Rectangle()
|
||||||
|
.fill(themeCodable.selection.stColor.suiColor)
|
||||||
|
Rectangle()
|
||||||
|
.fill(themeCodable.selectedText.stColor.suiColor)
|
||||||
|
}
|
||||||
|
.frame(width: 100)
|
||||||
|
|
||||||
ForEach(0...1, id: \.self) { row in
|
ForEach(0...1, id: \.self) { row in
|
||||||
HStack {
|
HStack {
|
||||||
ForEach(1...8, id: \.self) { col in
|
ForEach(1...8, id: \.self) { col in
|
||||||
let index = (col + (row * 8)) - 1
|
let index = (col + (row * 8)) - 1
|
||||||
ColorPicker(
|
ColorPicker("Ansi \(index+1)", selection: $themeCodable[ansiIndex: index])
|
||||||
selection: Binding(
|
|
||||||
get: { theme.ansi[index].suiColor },
|
|
||||||
set: { newValue in
|
|
||||||
let cc = SwiftTerm.Color(newValue).colorCodable
|
|
||||||
theme.ansi[index] = cc.stColor
|
|
||||||
}
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
RoundedRectangle(cornerRadius: 5)
|
|
||||||
.fill(theme.ansi[index].suiColor)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user