@@ -176,7 +176,7 @@ public final class AppKitBackend: AppBackend {
176
176
}
177
177
}
178
178
179
- public func computeRootEnvironment( defaultEnvironment: Environment ) -> Environment {
179
+ public func computeRootEnvironment( defaultEnvironment: EnvironmentValues ) -> EnvironmentValues {
180
180
let isDark = UserDefaults . standard. string ( forKey: " AppleInterfaceStyle " ) == " Dark "
181
181
let font = Font . system (
182
182
size: Int ( NSFont . systemFont ( ofSize: 0.0 ) . pointSize. rounded ( . awayFromZero) )
@@ -358,7 +358,7 @@ public final class AppKitBackend: AppBackend {
358
358
of text: String ,
359
359
whenDisplayedIn textView: Widget ,
360
360
proposedFrame: SIMD2 < Int > ? ,
361
- environment: Environment
361
+ environment: EnvironmentValues
362
362
) -> SIMD2 < Int > {
363
363
if let proposedFrame, proposedFrame. x == 0 {
364
364
// We want the text to have the same height as it would have if it were
@@ -400,7 +400,11 @@ public final class AppKitBackend: AppBackend {
400
400
return field
401
401
}
402
402
403
- public func updateTextView( _ textView: Widget , content: String , environment: Environment ) {
403
+ public func updateTextView(
404
+ _ textView: Widget ,
405
+ content: String ,
406
+ environment: EnvironmentValues
407
+ ) {
404
408
let field = textView as! NSTextField
405
409
field. attributedStringValue = Self . attributedString ( for: content, in: environment)
406
410
}
@@ -413,7 +417,7 @@ public final class AppKitBackend: AppBackend {
413
417
_ button: Widget ,
414
418
label: String ,
415
419
action: @escaping ( ) -> Void ,
416
- environment: Environment
420
+ environment: EnvironmentValues
417
421
) {
418
422
let button = button as! NSButton
419
423
button. attributedTitle = Self . attributedString ( for: label, in: environment)
@@ -494,7 +498,7 @@ public final class AppKitBackend: AppBackend {
494
498
public func updatePicker(
495
499
_ picker: Widget ,
496
500
options: [ String ] ,
497
- environment: Environment ,
501
+ environment: EnvironmentValues ,
498
502
onChange: @escaping ( Int ? ) -> Void
499
503
) {
500
504
let picker = picker as! NSPopUpButton
@@ -528,7 +532,7 @@ public final class AppKitBackend: AppBackend {
528
532
public func updateTextField(
529
533
_ textField: Widget ,
530
534
placeholder: String ,
531
- environment: Environment ,
535
+ environment: EnvironmentValues ,
532
536
onChange: @escaping ( String ) -> Void
533
537
) {
534
538
let textField = textField as! NSObservableTextField
@@ -696,7 +700,7 @@ public final class AppKitBackend: AppBackend {
696
700
public func setColumnLabels(
697
701
ofTable table: Widget ,
698
702
to labels: [ String ] ,
699
- environment: Environment
703
+ environment: EnvironmentValues
700
704
) {
701
705
let table = ( table as! NSScrollView ) . documentView as! NSCustomTableView
702
706
var columnIndices : [ ObjectIdentifier : Int ] = [ : ]
@@ -735,7 +739,7 @@ public final class AppKitBackend: AppBackend {
735
739
736
740
private static func attributedString(
737
741
for text: String ,
738
- in environment: Environment
742
+ in environment: EnvironmentValues
739
743
) -> NSAttributedString {
740
744
NSAttributedString (
741
745
string: text,
@@ -744,7 +748,7 @@ public final class AppKitBackend: AppBackend {
744
748
}
745
749
746
750
private static func attributes(
747
- forTextIn environment: Environment
751
+ forTextIn environment: EnvironmentValues
748
752
) -> [ NSAttributedString . Key : Any ] {
749
753
let paragraphStyle = NSMutableParagraphStyle ( )
750
754
paragraphStyle. alignment =
@@ -763,7 +767,7 @@ public final class AppKitBackend: AppBackend {
763
767
]
764
768
}
765
769
766
- private static func font( for environment: Environment ) -> NSFont {
770
+ private static func font( for environment: EnvironmentValues ) -> NSFont {
767
771
switch environment. font {
768
772
case . system( let size, let weight, let design) :
769
773
switch design {
@@ -823,7 +827,7 @@ public final class AppKitBackend: AppBackend {
823
827
public func updateProgressBar(
824
828
_ widget: Widget ,
825
829
progressFraction: Double ? ,
826
- environment: Environment
830
+ environment: EnvironmentValues
827
831
) {
828
832
let progressBar = widget as! NSProgressIndicator
829
833
progressBar. doubleValue = progressFraction ?? 0
@@ -847,7 +851,7 @@ public final class AppKitBackend: AppBackend {
847
851
public func updatePopoverMenu(
848
852
_ menu: Menu ,
849
853
content: ResolvedMenu ,
850
- environment: Environment
854
+ environment: EnvironmentValues
851
855
) {
852
856
menu. appearance = environment. colorScheme. nsAppearance
853
857
menu. items = Self . renderMenuItems ( content. items)
@@ -876,7 +880,7 @@ public final class AppKitBackend: AppBackend {
876
880
_ alert: Alert ,
877
881
title: String ,
878
882
actionLabels: [ String ] ,
879
- environment: Environment
883
+ environment: EnvironmentValues
880
884
) {
881
885
alert. messageText = title
882
886
for label in actionLabels {
@@ -908,6 +912,69 @@ public final class AppKitBackend: AppBackend {
908
912
public func dismissAlert( _ alert: Alert , window: Window ) {
909
913
window. endSheet ( alert. window)
910
914
}
915
+
916
+ public func showOpenDialog(
917
+ fileDialogOptions: FileDialogOptions ,
918
+ openDialogOptions: OpenDialogOptions ,
919
+ window: Window ,
920
+ resultHandler handleResult: @escaping ( DialogResult < [ URL ] > ) -> Void
921
+ ) {
922
+ let panel = NSOpenPanel ( )
923
+ panel. message = fileDialogOptions. title
924
+ panel. prompt = fileDialogOptions. defaultButtonLabel
925
+ panel. directoryURL = fileDialogOptions. initialDirectory
926
+ panel. showsHiddenFiles = fileDialogOptions. showHiddenFiles
927
+ panel. allowsOtherFileTypes = fileDialogOptions. allowOtherContentTypes
928
+
929
+ // TODO: allowedContentTypes
930
+
931
+ panel. allowsMultipleSelection = openDialogOptions. allowMultipleSelections
932
+ panel. canChooseFiles = openDialogOptions. allowSelectingFiles
933
+ panel. canChooseDirectories = openDialogOptions. allowSelectingDirectories
934
+
935
+ panel. beginSheetModal ( for: window) { response in
936
+ guard response != . continue else {
937
+ return
938
+ }
939
+
940
+ if response == . OK {
941
+ handleResult ( . success( panel. urls) )
942
+ } else {
943
+ handleResult ( . cancelled)
944
+ }
945
+ }
946
+ }
947
+
948
+ public func showSaveDialog(
949
+ fileDialogOptions: FileDialogOptions ,
950
+ saveDialogOptions: SaveDialogOptions ,
951
+ window: Window ,
952
+ resultHandler handleResult: @escaping ( DialogResult < URL > ) -> Void
953
+ ) {
954
+ let panel = NSSavePanel ( )
955
+ panel. message = fileDialogOptions. title
956
+ panel. prompt = fileDialogOptions. defaultButtonLabel
957
+ panel. directoryURL = fileDialogOptions. initialDirectory
958
+ panel. showsHiddenFiles = fileDialogOptions. showHiddenFiles
959
+ panel. allowsOtherFileTypes = fileDialogOptions. allowOtherContentTypes
960
+
961
+ // TODO: allowedContentTypes
962
+
963
+ panel. nameFieldLabel = saveDialogOptions. nameFieldLabel ?? panel. nameFieldLabel
964
+ panel. nameFieldStringValue = saveDialogOptions. defaultFileName ?? " "
965
+
966
+ panel. beginSheetModal ( for: window) { response in
967
+ guard response != . continue else {
968
+ return
969
+ }
970
+
971
+ if response == . OK {
972
+ handleResult ( . success( panel. url!) )
973
+ } else {
974
+ handleResult ( . cancelled)
975
+ }
976
+ }
977
+ }
911
978
}
912
979
913
980
final class NSCustomMenuItem : NSMenuItem {
0 commit comments