@@ -249,7 +249,7 @@ define(function (require, exports, module) {
249249 panelShownAtStartup = true ;
250250 $icon . toggleClass ( "active" ) ;
251251 panel . show ( ) ;
252- _loadPreview ( true ) ;
252+ _loadPreview ( true , true ) ;
253253 _showCustomServerBannerIfNeeded ( ) ;
254254 } else {
255255 $icon . toggleClass ( "active" ) ;
@@ -575,7 +575,7 @@ define(function (require, exports, module) {
575575 }
576576 }
577577
578- let startupFilesLoadHandled = false , livePreviewStartCalled = false ;
578+ let startupFilesLoadHandled = false ;
579579 async function _projectOpened ( ) {
580580 customLivePreviewBannerShown = false ;
581581 $panel . find ( ".live-preview-custom-banner" ) . addClass ( "forced-hidden" ) ;
@@ -590,7 +590,6 @@ define(function (require, exports, module) {
590590 _togglePinUrl ( ) ;
591591 }
592592 $iframe . attr ( 'src' , StaticServer . getNoPreviewURL ( ) ) ;
593- livePreviewStartCalled = true ;
594593 if ( ! panelShownAtStartup && ! isBrowser && ProjectManager . isStartupFilesLoaded ( ) ) {
595594 // we dont do this in browser as the virtual server may not yet be started on app start
596595 // project open and a 404 page will briefly flash in the browser!
@@ -616,10 +615,6 @@ define(function (require, exports, module) {
616615 // and EVENT_PROJECT_OPEN. if _projectOpened has already shown the live preview panel when it saw that
617616 // ProjectManager.isStartupFilesLoaded() is true, we should not call project opened again at boot.
618617 }
619- if ( ! livePreviewStartCalled ) {
620- _projectOpened ( ) ;
621- return ;
622- }
623618 if ( ! panelShownAtStartup && ! isBrowser && ProjectManager . isStartupFilesLoaded ( ) ) {
624619 // we dont do this in browser as the virtual server may not yet be started on app start
625620 // project open and a 404 page will briefly flash in the browser!
@@ -629,6 +624,7 @@ define(function (require, exports, module) {
629624 const isPreviewable = currentFile ? utils . isPreviewableFile ( currentFile . fullPath ) : false ;
630625 if ( isPreviewable ) {
631626 _setPanelVisibility ( true ) ;
627+ _loadPreview ( true , true ) ;
632628 }
633629 }
634630 }
@@ -690,7 +686,7 @@ define(function (require, exports, module) {
690686 if ( changedFile && ( utils . isPreviewableFile ( fullPath ) ||
691687 utils . isServerRenderedFile ( fullPath ) ) ) {
692688 _loadPreview ( ) ;
693- if ( ! panelShownAtStartup ) {
689+ if ( ! panelShownAtStartup && ProjectManager . isStartupFilesLoaded ( ) ) {
694690 let previewDetails = await StaticServer . getPreviewDetails ( ) ;
695691 if ( previewDetails && ! previewDetails . isNoPreview ) {
696692 _setPanelVisibility ( true ) ;
@@ -786,11 +782,32 @@ define(function (require, exports, module) {
786782 StaticServer . init ( ) ;
787783 LiveDevServerManager . registerServer ( { create : _createStaticServer } , 5 ) ;
788784 ProjectManager . on ( ProjectManager . EVENT_PROJECT_FILE_CHANGED , _projectFileChanges ) ;
789- MainViewManager . on ( "currentFileChange" , _currentFileChanged ) ;
790785 ProjectManager . on ( ProjectManager . EVENT_PROJECT_OPEN , _projectOpened ) ;
791786 ProjectManager . on ( ProjectManager . EVENT_PROJECT_CLOSE , _projectClosed ) ;
792787 EditorManager . on ( "activeEditorChange" , _activeDocChanged ) ;
793788 ProjectManager . on ( ProjectManager . EVENT_AFTER_STARTUP_FILES_LOADED , _startupFilesLoaded ) ;
789+ let fileChangeListenerStartDelay = 0 ;
790+ if ( Phoenix . isNativeApp && Phoenix . platform === "mac" ) {
791+ // in mac, if we do the `open with Phoenix Code` from finder, then, the open with events come as events
792+ // after app start. This causes a problem where if we opens a txt file with open with, and and html file was
793+ // open previously, then currentFileChange listener will see the html file at first and open the live
794+ // preview panel, and immediately, the txt file event will be sent by os resulting in a no preview page.
795+ // we should not show a no preview page for opening txt / non-previewable files. So, we dont attach the
796+ // change listener in macos for a few seconds, and attach the listener if the user explicitly clicks on a
797+ // file.
798+ fileChangeListenerStartDelay = 500 ;
799+ ProjectManager . on ( ProjectManager . EVENT_FILE_CLICKED_SIDEBAR , ( ) => {
800+ MainViewManager . off ( "currentFileChange" , _currentFileChanged ) ;
801+ MainViewManager . on ( "currentFileChange" , _currentFileChanged ) ;
802+ } ) ;
803+ }
804+ setTimeout ( ( ) => {
805+ MainViewManager . off ( "currentFileChange" , _currentFileChanged ) ;
806+ MainViewManager . on ( "currentFileChange" , _currentFileChanged ) ;
807+ if ( Phoenix . isNativeApp && Phoenix . platform === "mac" && MainViewManager . getCurrentlyViewedFile ( ) ) {
808+ _currentFileChanged ( null , MainViewManager . getCurrentlyViewedFile ( ) ) ;
809+ }
810+ } , fileChangeListenerStartDelay ) ;
794811 CommandManager . register ( Strings . CMD_LIVE_FILE_PREVIEW , Commands . FILE_LIVE_FILE_PREVIEW , function ( ) {
795812 _toggleVisibilityOnClick ( ) ;
796813 } ) ;
@@ -826,7 +843,10 @@ define(function (require, exports, module) {
826843 // only show if there is some file to preview and not the default no-preview preview on startup
827844 _setPanelVisibility ( true ) ;
828845 }
829- _loadPreview ( true ) ;
846+ // in browsers, the static server is not reset on every call to open live preview, so its safe to reload
847+ // we need to reload once as
848+ const shouldReload = ! Phoenix . isNativeApp ;
849+ _loadPreview ( true , shouldReload ) ;
830850 } ) ;
831851 }
832852
0 commit comments