fix(mac): size context bar to menu
parent
3bb33bdeed
commit
5e51107711
|
|
@ -4,7 +4,7 @@ import SwiftUI
|
|||
struct ContextUsageBar: View {
|
||||
let usedTokens: Int
|
||||
let contextTokens: Int
|
||||
var width: CGFloat = 220
|
||||
var width: CGFloat?
|
||||
var height: CGFloat = 6
|
||||
|
||||
private var clampedFractionUsed: Double {
|
||||
|
|
@ -28,14 +28,30 @@ struct ContextUsageBar: View {
|
|||
var body: some View {
|
||||
// SwiftUI menus (MenuBarExtraStyle.menu) drop certain view types (including ProgressView/Canvas).
|
||||
// Render the bar as an image to reliably display inside the menu.
|
||||
Image(nsImage: Self.renderBar(
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
fractionUsed: self.clampedFractionUsed,
|
||||
percentUsed: self.percentUsed))
|
||||
.resizable()
|
||||
.interpolation(.none)
|
||||
.frame(width: self.width, height: self.height)
|
||||
Group {
|
||||
if let width = self.width, width > 0 {
|
||||
Image(nsImage: Self.renderBar(
|
||||
width: width,
|
||||
height: self.height,
|
||||
fractionUsed: self.clampedFractionUsed,
|
||||
percentUsed: self.percentUsed))
|
||||
.resizable()
|
||||
.interpolation(.none)
|
||||
.frame(width: width, height: self.height)
|
||||
} else {
|
||||
GeometryReader { proxy in
|
||||
Image(nsImage: Self.renderBar(
|
||||
width: proxy.size.width,
|
||||
height: self.height,
|
||||
fractionUsed: self.clampedFractionUsed,
|
||||
percentUsed: self.percentUsed))
|
||||
.resizable()
|
||||
.interpolation(.none)
|
||||
.frame(width: proxy.size.width, height: self.height)
|
||||
}
|
||||
.frame(height: self.height)
|
||||
}
|
||||
}
|
||||
.accessibilityLabel("Context usage")
|
||||
.accessibilityValue(self.accessibilityValue)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ struct MenuContent: View {
|
|||
@State private var loadingMics = false
|
||||
@State private var sessionMenu: [SessionRow] = []
|
||||
@State private var mainSessionRow: SessionRow?
|
||||
@State private var mainSessionContextWidth: CGFloat = 0
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
|
|
@ -266,9 +267,15 @@ struct MenuContent: View {
|
|||
ContextUsageBar(
|
||||
usedTokens: row.tokens.total,
|
||||
contextTokens: row.tokens.contextTokens,
|
||||
width: 220)
|
||||
width: self.mainSessionContextWidth > 0 ? self.mainSessionContextWidth : nil)
|
||||
}
|
||||
.padding(.vertical, 2)
|
||||
.onWidthChange { width in
|
||||
let next = max(120, width)
|
||||
if abs(next - self.mainSessionContextWidth) > 1 {
|
||||
self.mainSessionContextWidth = next
|
||||
}
|
||||
}
|
||||
} else {
|
||||
HStack(spacing: 8) {
|
||||
Text("Context (main)")
|
||||
|
|
|
|||
|
|
@ -154,7 +154,7 @@ struct SessionsSettings: View {
|
|||
ContextUsageBar(
|
||||
usedTokens: row.tokens.total,
|
||||
contextTokens: row.tokens.contextTokens,
|
||||
width: 260)
|
||||
width: nil)
|
||||
}
|
||||
|
||||
HStack(spacing: 10) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
import SwiftUI
|
||||
|
||||
private struct ViewWidthPreferenceKey: PreferenceKey {
|
||||
static let defaultValue: CGFloat = 0
|
||||
|
||||
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
|
||||
value = max(value, nextValue())
|
||||
}
|
||||
}
|
||||
|
||||
extension View {
|
||||
func onWidthChange(_ onChange: @escaping (CGFloat) -> Void) -> some View {
|
||||
self.background(
|
||||
GeometryReader { proxy in
|
||||
Color.clear.preference(key: ViewWidthPreferenceKey.self, value: proxy.size.width)
|
||||
})
|
||||
.onPreferenceChange(ViewWidthPreferenceKey.self, perform: onChange)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue