In my app running in the menu bar, I used a ColorPicker to change the color, but when I click on the ColorPicker, it doesn't open, and I can't select a color.
Post
Replies
Boosts
Views
Activity
First of all, thank you for your response. Since I have just started with SwiftUI, please don't mind my beginner mistakes regarding the color selection. I have shared all my codes
import SwiftUI
import AppKit
struct RenkSecimi: View {
@Binding var renk1: Color
@Binding var renk2: Color
@ObservedObject var short : TimerModel
@State private var renkGoster = false
var body: some View {
Form {
VStack(alignment: .center, spacing: 20) {
HStack(alignment: .center) {
ColorPicker("Color 1",selection: $renk2)
.padding()
.background(.gray)
.clipShape(RoundedRectangle(cornerRadius: 10))
.shadow(radius: 2)
.onTapGesture {
renkGoster.toggle()
}
ColorPicker("Color 2", selection: $renk1)
.padding()
.background(.gray)
.clipShape(RoundedRectangle(cornerRadius: 10))
.shadow(radius: 2)
.onTapGesture {
renkGoster.toggle()
}
}
VStack(spacing: 10){
Picker("Short Break", selection: $short.shortBreakDuration) {
ForEach(5...11, id: \.self) { i in
Text("\(i) Dk").tag(i * 60)
}
.onChange(of: short.shortBreakDuration) { oldValue, newValue in
short.ispresend = false
}
}
.pickerStyle(.menu)
.frame(width: 150)
Picker("Long Break", selection: $short.longBreakDuration) {
ForEach(10..<21) { i in
Text("\(i) Dk").tag(i * 60)
}
.onChange(of: short.longBreakDuration) { oldValue, newValue in
short.ispresend = false
}
}
.pickerStyle(.menu)
.frame(width: 150)
Picker("Pomodoro ", selection: $short.workDuration) {
ForEach(25..<61) { i in
Text("\(i) Dk").tag(i * 60)
}
.onChange(of: short.workDuration) { oldValue, newValue in
short.ispresend = false
}
}
.pickerStyle(.menu)
.frame(width: 150)
Button(action: {
NSApplication.shared.terminate(nil)
}) {
Text("QUIT")
.fontWeight(.semibold)
.padding(8)
.frame(maxWidth: 100)
.background(Color.blue)
.foregroundColor(.white)
.clipShape(RoundedRectangle(cornerRadius: 10))
}
.padding(.bottom)
.buttonStyle(PlainButtonStyle())
}
}
.padding()
.popover(isPresented: $renkGoster) {
Text(short.timerString(from: 0 * 60))
.font(.system(size: 90))
.padding()
.foregroundStyle(LinearGradient(colors: [renk1, renk2], startPoint: .bottomTrailing, endPoint: .topLeading))
}
}
.formStyle(.columns)
}
}
extension Color {
func toHex() -> String? {
let nsColor = NSColor(self)
guard let rgbColor = nsColor.usingColorSpace(.deviceRGB) else { return nil }
let r = rgbColor.redComponent
let g = rgbColor.greenComponent
let b = rgbColor.blueComponent
return String(format: "#%02X%02X%02X", Int(r * 255), Int(g * 255), Int(b * 255))
}
static func fromHex(_ hex: String) -> Color? {
var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")
var rgb: UInt64 = 0
guard Scanner(string: hexSanitized).scanHexInt64(&rgb) else {
print("Invalid hex string.")
return nil
}
let r = Double((rgb >> 16) & 0xFF) / 255.0
let g = Double((rgb >> 8) & 0xFF) / 255.0
let b = Double(rgb & 0xFF) / 255.0
return Color(red: r, green: g, blue: b)
}
}
#Preview {
RenkSecimi(renk1: .constant(.black), renk2: .constant(.red), short: .init())
}