Skip to content

Commit 6ead779

Browse files
committed
fix: in macs live preview panel shown when opening text files on start
1 parent 981ff13 commit 6ead779

File tree

5 files changed

+63
-13
lines changed

5 files changed

+63
-13
lines changed

src/document/DocumentCommandHandlers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,9 @@ define(function (require, exports, module) {
21892189
Phoenix.app.setSingleInstanceCLIArgsHandler(_singleInstanceHandler);
21902190
_openFilesPassedInFromCLI()
21912191
.finally(()=>{
2192+
// in mac, this is not exactly correct. This event will get triggered on startup, but mac will only
2193+
// raise events in the background and there is no way for us to know when the mac open with events
2194+
// come. Use this event carefully in mac.
21922195
ProjectManager.trigger(ProjectManager.EVENT_AFTER_STARTUP_FILES_LOADED);
21932196
});
21942197
});

src/extensionsIntegrated/Phoenix-live-preview/main.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/project/FileTreeView.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ define(function (require, exports, module) {
3838
FileTreeViewModel = require("project/FileTreeViewModel"),
3939
ViewUtils = require("utils/ViewUtils"),
4040
KeyEvent = require("utils/KeyEvent"),
41-
PreferencesManager = require("preferences/PreferencesManager");
41+
EventDispatcher = require("utils/EventDispatcher");
42+
43+
EventDispatcher.makeEventDispatcher(exports);
44+
45+
const EVENT_FILE_NODE_CLICKED_IN_TREE = "fileNodeClickedInTree";
4246

4347
var DOM = Preact.DOM;
4448

@@ -584,6 +588,7 @@ define(function (require, exports, module) {
584588
}
585589
this.props.actions.setSelected(this.myPath(), doNotOpen);
586590
this.render();
591+
exports.trigger(EVENT_FILE_NODE_CLICKED_IN_TREE);
587592
},
588593

589594
/**
@@ -1331,4 +1336,7 @@ define(function (require, exports, module) {
13311336
exports.addIconProvider = addIconProvider;
13321337
exports.addClassesProvider = addClassesProvider;
13331338
exports.render = render;
1339+
1340+
// private events
1341+
exports._EVENT_FILE_NODE_CLICKED_IN_TREE = EVENT_FILE_NODE_CLICKED_IN_TREE;
13341342
});

src/project/ProjectManager.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ define(function (require, exports, module) {
9393
EVENT_PROJECT_REFRESH = "projectRefresh",
9494
EVENT_CONTENT_CHANGED = "contentChanged",
9595
EVENT_PROJECT_FILE_CHANGED = "projectFileChanged",
96-
EVENT_PROJECT_FILE_RENAMED = "projectFileRenamed";
96+
EVENT_PROJECT_FILE_RENAMED = "projectFileRenamed",
97+
EVENT_FILE_CLICKED_SIDEBAR = "fileClickedSidebar";
9798

9899
EventDispatcher.setLeakThresholdForEvent(EVENT_PROJECT_OPEN, 25);
99100

@@ -2215,6 +2216,14 @@ define(function (require, exports, module) {
22152216
_renderTree(true);
22162217
}
22172218

2219+
FileTreeView.on(FileTreeView._EVENT_FILE_NODE_CLICKED_IN_TREE, ()=>{
2220+
exports.trigger(EVENT_FILE_CLICKED_SIDEBAR);
2221+
});
2222+
2223+
WorkingSetView.on(WorkingSetView._EVENT_FILE_NODE_CLICKED_WORKING_SET, ()=>{
2224+
exports.trigger(EVENT_FILE_CLICKED_SIDEBAR);
2225+
});
2226+
22182227

22192228
// Private API helpful in testing
22202229
exports._actionCreator = actionCreator;
@@ -2272,4 +2281,5 @@ define(function (require, exports, module) {
22722281
exports.EVENT_PROJECT_FILE_CHANGED = EVENT_PROJECT_FILE_CHANGED;
22732282
exports.EVENT_PROJECT_FILE_RENAMED = EVENT_PROJECT_FILE_RENAMED;
22742283
exports.EVENT_PROJECT_OPEN_FAILED = EVENT_PROJECT_OPEN_FAILED;
2284+
exports.EVENT_FILE_CLICKED_SIDEBAR = EVENT_FILE_CLICKED_SIDEBAR;
22752285
});

src/project/WorkingSetView.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ define(function (require, exports, module) {
4343
paneListTemplate = require("text!htmlContent/working-set.html"),
4444
Strings = require("strings"),
4545
_ = require("thirdparty/lodash"),
46-
Mustache = require("thirdparty/mustache/mustache");
46+
Mustache = require("thirdparty/mustache/mustache"),
47+
EventDispatcher = require("utils/EventDispatcher");
48+
49+
EventDispatcher.makeEventDispatcher(exports);
50+
51+
const EVENT_FILE_NODE_CLICKED_WORKING_SET = "fileNodeClickedWorkingSet";
4752

4853
/**
4954
* Open view dictionary
@@ -276,6 +281,7 @@ define(function (require, exports, module) {
276281

277282
// The mouse down handler pretty much handles everything
278283
$el.mousedown(function (e) {
284+
exports.trigger(EVENT_FILE_NODE_CLICKED_WORKING_SET);
279285
var scrollDir = 0,
280286
dragged = false,
281287
startPageY = e.pageY,
@@ -1581,4 +1587,7 @@ define(function (require, exports, module) {
15811587
// API to be used only by default extensions
15821588
exports.useIconProviders = useIconProviders;
15831589
exports.useClassProviders = useClassProviders;
1590+
1591+
// private events
1592+
exports._EVENT_FILE_NODE_CLICKED_WORKING_SET = EVENT_FILE_NODE_CLICKED_WORKING_SET;
15841593
});

0 commit comments

Comments
 (0)