Skip to content

Commit

Permalink
Another attempt at rendering items in Ice Bar with correct aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed Jun 14, 2024
1 parent 4dd2e5b commit 1a25292
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions Ice/IceBar/IceBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ class IceBarPanel: NSPanel {
// MARK: - IceBarImageCache

private class IceBarImageCache: ObservableObject {
@Published private var images = [MenuBarItemInfo: CGImage]()
@Published private var images = [MenuBarItemInfo: NSImage]()

func image(for info: MenuBarItemInfo) -> CGImage? {
func image(for info: MenuBarItemInfo) -> NSImage? {
images[info]
}

func cache(image: CGImage, for info: MenuBarItemInfo) {
func cache(image: NSImage, for info: MenuBarItemInfo) {
DispatchQueue.main.async {
self.images[info] = image
}
Expand Down Expand Up @@ -196,14 +196,15 @@ private struct IceBarItemView: View {
let item: MenuBarItem
let closePanel: () -> Void

private var image: CGImage? {
private var image: NSImage? {
let info = item.info
if let image = imageCache.image(for: info) {
return image
}
if let image = Bridging.captureWindow(item.windowID, option: .boundsIgnoreFraming) {
imageCache.cache(image: image, for: info)
return image
let nsImage = NSImage(cgImage: image, size: CGSize(width: image.width, height: image.height))
imageCache.cache(image: nsImage, for: info)
return nsImage
}
return nil
}
Expand All @@ -215,7 +216,7 @@ private struct IceBarItemView: View {

var body: some View {
if let image, let size {
Image(nsImage: NSImage(cgImage: image, size: size))
Image(nsImage: image)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(
Expand Down

0 comments on commit 1a25292

Please sign in to comment.