11using System ;
2- using System . Collections . Generic ;
32using System . IO ;
43using System . Net . Http ;
5- using System . Text ;
64using System . Text . Json ;
75using System . Threading . Tasks ;
86
@@ -75,79 +73,6 @@ public static AppBuilder BuildAvaloniaApp()
7573 #endregion
7674
7775 #region Utility Functions
78- public static Task ShowDialog ( object data , Window owner = null )
79- {
80- if ( owner == null )
81- {
82- if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow : { } mainWindow } )
83- owner = mainWindow ;
84- else
85- return null ;
86- }
87-
88- if ( data is Views . ChromelessWindow window )
89- return window . ShowDialog ( owner ) ;
90-
91- window = Views . ControlExtensions . CreateFromViewModels ( data ) as Views . ChromelessWindow ;
92- if ( window != null )
93- {
94- window . DataContext = data ;
95- return window . ShowDialog ( owner ) ;
96- }
97-
98- return null ;
99- }
100-
101- public static void ShowWindow ( object data )
102- {
103- if ( data is not Views . ChromelessWindow window )
104- {
105- window = Views . ControlExtensions . CreateFromViewModels ( data ) as Views . ChromelessWindow ;
106- if ( window == null )
107- return ;
108-
109- window . DataContext = data ;
110- }
111-
112- do
113- {
114- if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { Windows : { Count : > 0 } windows } )
115- {
116- // Try to find the actived window (fall back to `MainWindow`)
117- Window actived = windows [ 0 ] ;
118- if ( ! actived . IsActive )
119- {
120- for ( var i = 1 ; i < windows . Count ; i ++ )
121- {
122- var test = windows [ i ] ;
123- if ( test . IsActive )
124- {
125- actived = test ;
126- break ;
127- }
128- }
129- }
130-
131- // Get the screen where current window locates.
132- var screen = actived . Screens . ScreenFromWindow ( actived ) ?? actived . Screens . Primary ;
133- if ( screen == null )
134- break ;
135-
136- // Calculate the startup position (Center Screen Mode) of target window
137- var rect = new PixelRect ( PixelSize . FromSize ( window . ClientSize , actived . DesktopScaling ) ) ;
138- var centeredRect = screen . WorkingArea . CenterRect ( rect ) ;
139- if ( actived . Screens . ScreenFromPoint ( centeredRect . Position ) == null )
140- break ;
141-
142- // Use the startup position
143- window . WindowStartupLocation = WindowStartupLocation . Manual ;
144- window . Position = centeredRect . Position ;
145- }
146- } while ( false ) ;
147-
148- window . Show ( ) ;
149- }
150-
15176 public static async Task < bool > AskConfirmAsync ( string message , Models . ConfirmButtonType buttonType = Models . ConfirmButtonType . OkCancel )
15277 {
15378 if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow : { } owner } )
@@ -253,8 +178,8 @@ public static void SetFonts(string defaultFont, string monospaceFont)
253178 app . _fontsOverrides = null ;
254179 }
255180
256- defaultFont = app . FixFontFamilyName ( defaultFont ) ;
257- monospaceFont = app . FixFontFamilyName ( monospaceFont ) ;
181+ defaultFont = StringExtensions . FormatFontNames ( defaultFont ) ;
182+ monospaceFont = StringExtensions . FormatFontNames ( monospaceFont ) ;
258183
259184 var resDic = new ResourceDictionary ( ) ;
260185 if ( ! string . IsNullOrEmpty ( defaultFont ) )
@@ -303,15 +228,9 @@ public static ViewModels.Launcher GetLauncher()
303228 public static void Quit ( int exitCode )
304229 {
305230 if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop )
306- {
307- desktop . ShutdownMode = ShutdownMode . OnExplicitShutdown ;
308- desktop . MainWindow ? . Close ( ) ;
309231 desktop . Shutdown ( exitCode ) ;
310- }
311232 else
312- {
313233 Environment . Exit ( exitCode ) ;
314- }
315234 }
316235 #endregion
317236
@@ -547,10 +466,27 @@ private void TryLaunchAsNormal(IClassicDesktopStyleApplicationLifetime desktop)
547466
548467 var pref = ViewModels . Preferences . Instance ;
549468 pref . SetCanModify ( ) ;
469+ pref . UpdateAvailableAIModels ( ) ;
550470
551471 _launcher = new ViewModels . Launcher ( startupRepo ) ;
552472 desktop . MainWindow = new Views . Launcher ( ) { DataContext = _launcher } ;
553- desktop . ShutdownMode = ShutdownMode . OnMainWindowClose ;
473+ desktop . ShutdownMode = ShutdownMode . OnExplicitShutdown ;
474+
475+ // Fix macOS crash when quiting from Dock
476+ if ( OperatingSystem . IsMacOS ( ) )
477+ {
478+ desktop . ShutdownRequested += ( _ , e ) =>
479+ {
480+ e . Cancel = true ;
481+ Dispatcher . UIThread . Post ( ( ) => Quit ( 0 ) ) ;
482+ } ;
483+ }
484+
485+ desktop . Exit += ( _ , _ ) =>
486+ {
487+ _ipcChannel ? . Dispose ( ) ;
488+ _ipcChannel = null ;
489+ } ;
554490
555491 _ipcChannel . MessageReceived += repo =>
556492 {
@@ -562,8 +498,6 @@ private void TryLaunchAsNormal(IClassicDesktopStyleApplicationLifetime desktop)
562498 } ) ;
563499 } ;
564500
565- desktop . Exit += ( _ , _ ) => _ipcChannel . Dispose ( ) ;
566-
567501#if ! DISABLE_UPDATE_DETECTION
568502 if ( pref . ShouldCheck4UpdateOnStartup ( ) )
569503 Check4Update ( ) ;
@@ -619,7 +553,12 @@ private void ShowSelfUpdateResult(object data)
619553 {
620554 Dispatcher . UIThread . Invoke ( async ( ) =>
621555 {
622- await ShowDialog ( new ViewModels . SelfUpdate { Data = data } ) ;
556+ if ( Current ? . ApplicationLifetime is IClassicDesktopStyleApplicationLifetime { MainWindow : { } owner } )
557+ {
558+ var ctx = new ViewModels . SelfUpdate { Data = data } ;
559+ var dialog = new Views . SelfUpdate ( ) { DataContext = ctx } ;
560+ await dialog . ShowDialog ( owner ) ;
561+ }
623562 } ) ;
624563 }
625564 catch
@@ -629,47 +568,6 @@ private void ShowSelfUpdateResult(object data)
629568 }
630569 #endregion
631570
632- private string FixFontFamilyName ( string input )
633- {
634- if ( string . IsNullOrEmpty ( input ) )
635- return string . Empty ;
636-
637- var parts = input . Split ( ',' ) ;
638- var trimmed = new List < string > ( ) ;
639-
640- foreach ( var part in parts )
641- {
642- var t = part . Trim ( ) ;
643- if ( string . IsNullOrEmpty ( t ) )
644- continue ;
645-
646- var sb = new StringBuilder ( ) ;
647- var prevChar = '\0 ' ;
648-
649- foreach ( var c in t )
650- {
651- if ( c == ' ' && prevChar == ' ' )
652- continue ;
653- sb . Append ( c ) ;
654- prevChar = c ;
655- }
656-
657- var name = sb . ToString ( ) ;
658- try
659- {
660- var fontFamily = FontFamily . Parse ( name ) ;
661- if ( fontFamily . FamilyTypefaces . Count > 0 )
662- trimmed . Add ( name ) ;
663- }
664- catch
665- {
666- // Ignore exceptions.
667- }
668- }
669-
670- return trimmed . Count > 0 ? string . Join ( ',' , trimmed ) : string . Empty ;
671- }
672-
673571 private Models . IpcChannel _ipcChannel = null ;
674572 private ViewModels . Launcher _launcher = null ;
675573 private ResourceDictionary _activeLocale = null ;
0 commit comments