1
+ import { is_structure_file } from '$lib/io/parse'
1
2
import type { ThemeName } from '$lib/theme'
2
3
import { AUTO_THEME , COLOR_THEMES , is_valid_theme_mode } from '$lib/theme'
3
4
import { is_trajectory_file } from '$lib/trajectory/parse'
@@ -17,7 +18,7 @@ interface WebviewData {
17
18
theme : ThemeName
18
19
}
19
20
20
- interface MessageData {
21
+ export interface MessageData {
21
22
command : string
22
23
text ?: string
23
24
filename ?: string
@@ -28,6 +29,12 @@ interface MessageData {
28
29
// Track active file watchers by file path
29
30
const active_watchers = new Map < string , vscode . FileSystemWatcher > ( )
30
31
32
+ // Check if a file should be auto-rendered
33
+ export const should_auto_render = ( filename : string ) : boolean => {
34
+ if ( ! filename || typeof filename !== `string` ) return false
35
+ return is_structure_file ( filename ) || is_trajectory_file ( filename )
36
+ }
37
+
31
38
// Read file from filesystem
32
39
export const read_file = ( file_path : string ) : FileData => {
33
40
const filename = path . basename ( file_path )
@@ -242,70 +249,77 @@ function stop_watching_file(file_path: string): void {
242
249
}
243
250
}
244
251
245
- // Enhanced render function with file watching
246
- export const render = ( context : vscode . ExtensionContext , uri ?: vscode . Uri ) => {
247
- try {
248
- const file = get_file ( uri )
249
- const file_path = uri ?. fsPath ||
250
- vscode . window . activeTextEditor ?. document . fileName
251
-
252
- const panel = vscode . window . createWebviewPanel (
253
- `matterviz` ,
254
- `MatterViz - ${ file . filename } ` ,
255
- vscode . ViewColumn . Beside ,
256
- {
257
- enableScripts : true ,
258
- retainContextWhenHidden : true ,
259
- localResourceRoots : [
260
- vscode . Uri . joinPath ( context . extensionUri , `dist` ) ,
261
- vscode . Uri . joinPath ( context . extensionUri , `../../static` ) ,
262
- ] ,
263
- } ,
264
- )
252
+ // Create webview panel with common setup
253
+ function create_webview_panel (
254
+ context : vscode . ExtensionContext ,
255
+ file_data : FileData ,
256
+ file_path ?: string ,
257
+ view_column : vscode . ViewColumn = vscode . ViewColumn . Beside ,
258
+ ) : vscode . WebviewPanel {
259
+ const panel = vscode . window . createWebviewPanel (
260
+ `matterviz` ,
261
+ `MatterViz - ${ file_data . filename } ` ,
262
+ view_column ,
263
+ {
264
+ enableScripts : true ,
265
+ retainContextWhenHidden : true ,
266
+ localResourceRoots : [
267
+ vscode . Uri . joinPath ( context . extensionUri , `dist` ) ,
268
+ vscode . Uri . joinPath ( context . extensionUri , `../../static` ) ,
269
+ ] ,
270
+ } ,
271
+ )
265
272
266
- if ( file_path ) start_watching_file ( file_path , panel . webview )
273
+ if ( file_path ) start_watching_file ( file_path , panel . webview )
267
274
268
- panel . webview . html = create_html ( panel . webview , context , {
269
- type : is_trajectory_file ( file . filename ) ? `trajectory` : `structure` ,
270
- data : file ,
271
- theme : get_theme ( ) ,
272
- } )
275
+ panel . webview . html = create_html ( panel . webview , context , {
276
+ type : is_trajectory_file ( file_data . filename ) ? `trajectory` : `structure` ,
277
+ data : file_data ,
278
+ theme : get_theme ( ) ,
279
+ } )
273
280
274
- panel . webview . onDidReceiveMessage (
275
- ( msg : MessageData ) => handle_msg ( msg , panel . webview ) ,
276
- undefined ,
277
- context . subscriptions ,
278
- )
281
+ panel . webview . onDidReceiveMessage (
282
+ ( msg : MessageData ) => handle_msg ( msg , panel . webview ) ,
283
+ undefined ,
284
+ context . subscriptions ,
285
+ )
279
286
280
- // Listen for theme changes and update webview
281
- const update_theme = ( ) => {
282
- if ( panel . visible ) {
283
- const current_file = file_path ? read_file ( file_path ) : file
284
- panel . webview . html = create_html ( panel . webview , context , {
285
- type : is_trajectory_file ( file . filename ) ? `trajectory` : `structure` ,
286
- data : current_file ,
287
- theme : get_theme ( ) ,
288
- } )
289
- }
287
+ // Theme change handling
288
+ const update_theme = ( ) => {
289
+ if ( panel . visible ) {
290
+ const current_file = file_path ? read_file ( file_path ) : file_data
291
+ panel . webview . html = create_html ( panel . webview , context , {
292
+ type : is_trajectory_file ( file_data . filename ) ? `trajectory` : `structure` ,
293
+ data : current_file ,
294
+ theme : get_theme ( ) ,
295
+ } )
290
296
}
297
+ }
291
298
292
- const theme_change_listener = vscode . window . onDidChangeActiveColorTheme (
293
- update_theme ,
294
- )
295
- const config_change_listener = vscode . workspace . onDidChangeConfiguration (
296
- ( event : vscode . ConfigurationChangeEvent ) => {
297
- if ( event . affectsConfiguration ( `matterviz.theme` ) ) update_theme ( )
298
- } ,
299
- )
299
+ const theme_listener = vscode . window . onDidChangeActiveColorTheme ( update_theme )
300
+ const config_listener = vscode . workspace . onDidChangeConfiguration (
301
+ ( event : vscode . ConfigurationChangeEvent ) => {
302
+ if ( event . affectsConfiguration ( `matterviz.theme` ) ) update_theme ( )
303
+ } ,
304
+ )
300
305
301
- // Dispose listeners when panel is closed
302
- panel . onDidDispose ( ( ) => {
303
- theme_change_listener . dispose ( )
304
- config_change_listener . dispose ( )
306
+ panel . onDidDispose ( ( ) => {
307
+ theme_listener . dispose ( )
308
+ config_listener . dispose ( )
309
+ if ( file_path ) stop_watching_file ( file_path )
310
+ } )
305
311
306
- // Clean up file watcher
307
- if ( file_path ) stop_watching_file ( file_path )
308
- } )
312
+ return panel
313
+ }
314
+
315
+ // Enhanced render function with file watching
316
+ export const render = ( context : vscode . ExtensionContext , uri ?: vscode . Uri ) => {
317
+ try {
318
+ const file = get_file ( uri )
319
+ const file_path = uri ?. fsPath ||
320
+ vscode . window . activeTextEditor ?. document . fileName
321
+
322
+ create_webview_panel ( context , file , file_path )
309
323
} catch ( error : unknown ) {
310
324
const message = error instanceof Error ? error . message : String ( error )
311
325
vscode . window . showErrorMessage ( `Failed: ${ message } ` )
@@ -414,6 +428,36 @@ export const activate = (context: vscode.ExtensionContext): void => {
414
428
new Provider ( context ) ,
415
429
{ webviewOptions : { retainContextWhenHidden : true } } ,
416
430
) ,
431
+ vscode . workspace . onDidOpenTextDocument ( ( document : vscode . TextDocument ) => {
432
+ if (
433
+ document . uri . scheme === `file` &&
434
+ should_auto_render ( path . basename ( document . uri . fsPath ) )
435
+ ) {
436
+ setTimeout ( ( ) => {
437
+ try {
438
+ if (
439
+ ! vscode . workspace . getConfiguration ( `matterviz` ) . get < boolean > (
440
+ `autoRender` ,
441
+ true ,
442
+ )
443
+ ) return
444
+ const file_data = read_file ( document . uri . fsPath )
445
+ create_webview_panel (
446
+ context ,
447
+ file_data ,
448
+ document . uri . fsPath ,
449
+ vscode . ViewColumn . One ,
450
+ )
451
+ } catch ( error : unknown ) {
452
+ vscode . window . showErrorMessage (
453
+ `MatterViz auto-render failed: ${
454
+ error instanceof Error ? error . message : String ( error )
455
+ } `,
456
+ )
457
+ }
458
+ } , 100 )
459
+ }
460
+ } ) ,
417
461
)
418
462
}
419
463
0 commit comments