From 9980ec62fda16fdea700db4379fc87f762aee0eb Mon Sep 17 00:00:00 2001 From: lwouis Date: Fri, 11 Oct 2024 11:59:16 +0200 Subject: [PATCH] fix: avoid crashing in rare cases --- docs/Contributors.md | 2 ++ .../PopupButtonLikeSystemSettings.swift | 26 +++++++++++-------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/docs/Contributors.md b/docs/Contributors.md index 61e4b68f..bcfb3126 100644 --- a/docs/Contributors.md +++ b/docs/Contributors.md @@ -138,6 +138,7 @@ They helped [localize the app](https://poeditor.com/join/project/8AOEZ0eAZE): * Lcwhhh * Lester * Loïc +* Loic. peron * LostInCompilation * Lumaxis * lwouis @@ -197,6 +198,7 @@ They helped [localize the app](https://poeditor.com/join/project/8AOEZ0eAZE): * shlomo * Sippawit Thammawiset * Sjur Moshagen +* Sjur.n. moshagen * Stan Smits * Stefan * Svetoslav Stefanov diff --git a/src/ui/generic-components/PopupButtonLikeSystemSettings.swift b/src/ui/generic-components/PopupButtonLikeSystemSettings.swift index 1223bff8..fbc9ff94 100644 --- a/src/ui/generic-components/PopupButtonLikeSystemSettings.swift +++ b/src/ui/generic-components/PopupButtonLikeSystemSettings.swift @@ -15,16 +15,20 @@ class PopupButtonLikeSystemSettings: NSPopUpButton { } override var intrinsicContentSize: NSSize { - let fakePopUpButton = NSPopUpButton() - fakePopUpButton.addItem(withTitle: title) - fakePopUpButton.selectedItem!.image = selectedItem!.image - let fakeCell = fakePopUpButton.cell! as! NSPopUpButtonCell - let currentCell = cell! as! NSPopUpButtonCell - fakeCell.bezelStyle = currentCell.bezelStyle - fakeCell.arrowPosition = currentCell.arrowPosition - fakeCell.imagePosition = currentCell.imagePosition - fakePopUpButton.showsBorderOnlyWhileMouseInside = showsBorderOnlyWhileMouseInside - fakePopUpButton.sizeToFit() - return fakePopUpButton.intrinsicContentSize + if let selectedItem = selectedItem { + let fakePopUpButton = NSPopUpButton() + fakePopUpButton.addItem(withTitle: title) + fakePopUpButton.item(at: 0)!.image = selectedItem.image + let fakeCell = fakePopUpButton.cell! as! NSPopUpButtonCell + let currentCell = cell! as! NSPopUpButtonCell + fakeCell.bezelStyle = currentCell.bezelStyle + fakeCell.arrowPosition = currentCell.arrowPosition + fakeCell.imagePosition = currentCell.imagePosition + fakePopUpButton.showsBorderOnlyWhileMouseInside = showsBorderOnlyWhileMouseInside + fakePopUpButton.sizeToFit() + return fakePopUpButton.intrinsicContentSize + } else { + return super.intrinsicContentSize + } } }