@@ -18,6 +18,7 @@ import {
1818import * as services from "@/store/services" ;
1919import { PLATFORM , PlatformMacOS } from "@/util/platformutil" ;
2020import { base64ToArray , fireAndForget } from "@/util/util" ;
21+ import { CanvasAddon } from "@xterm/addon-canvas" ;
2122import { SearchAddon } from "@xterm/addon-search" ;
2223import { SerializeAddon } from "@xterm/addon-serialize" ;
2324import { WebLinksAddon } from "@xterm/addon-web-links" ;
@@ -86,6 +87,7 @@ export class TermWrap {
8687 onSearchResultsDidChange ?: ( result : { resultIndex : number ; resultCount : number } ) => void ;
8788 private toDispose : TermTypes . IDisposable [ ] = [ ] ;
8889 webglAddon : WebglAddon | null = null ;
90+ canvasAddon : CanvasAddon | null = null ;
8991 webglEnabledAtom : jotai . PrimitiveAtom < boolean > ;
9092 pasteActive : boolean = false ;
9193 lastUpdated : number ;
@@ -182,9 +184,7 @@ export class TermWrap {
182184 }
183185 )
184186 ) ;
185- if ( WebGLSupported && waveOptions . useWebGl ) {
186- this . loadWebGlAddon ( ) ;
187- }
187+ this . setTermRenderer ( WebGLSupported && waveOptions . useWebGl ? "webgl" : "canvas" ) ;
188188 // Register OSC handlers
189189 this . terminal . parser . registerOscHandler ( 7 , ( data : string ) => {
190190 return handleOsc7Command ( data , this . blockId , this . loaded ) ;
@@ -300,42 +300,57 @@ export class TermWrap {
300300 this . terminal . options . cursorBlink = cursorBlink ?? false ;
301301 }
302302
303- loadWebGlAddon ( ) {
304- const addon = new WebglAddon ( ) ;
305- this . toDispose . push (
306- addon . onContextLoss ( ( ) => {
307- if ( addon === this . webglAddon ) {
308- this . disableWebGl ( ) ;
309- }
310- } )
311- ) ;
312- this . terminal . loadAddon ( addon ) ;
313- this . webglAddon = addon ;
314- globalStore . set ( this . webglEnabledAtom , true ) ;
315- if ( ! loggedWebGL ) {
316- console . log ( "loaded webgl!" ) ;
317- loggedWebGL = true ;
303+ setTermRenderer ( renderer : "webgl" | "canvas" ) {
304+ if ( renderer === "webgl" ) {
305+ if ( this . webglAddon != null ) {
306+ return ;
307+ }
308+ if ( ! WebGLSupported ) {
309+ renderer = "canvas" ;
310+ }
311+ } else {
312+ if ( this . canvasAddon != null ) {
313+ return ;
314+ }
315+ }
316+ if ( this . webglAddon != null ) {
317+ this . webglAddon . dispose ( ) ;
318+ this . webglAddon = null ;
319+ globalStore . set ( this . webglEnabledAtom , false ) ;
320+ }
321+ if ( this . canvasAddon != null ) {
322+ this . canvasAddon . dispose ( ) ;
323+ this . canvasAddon = null ;
324+ }
325+ if ( renderer === "webgl" ) {
326+ const addon = new WebglAddon ( ) ;
327+ this . toDispose . push (
328+ addon . onContextLoss ( ( ) => {
329+ if ( addon === this . webglAddon ) {
330+ this . setTermRenderer ( "canvas" ) ;
331+ }
332+ } )
333+ ) ;
334+ this . terminal . loadAddon ( addon ) ;
335+ this . webglAddon = addon ;
336+ globalStore . set ( this . webglEnabledAtom , true ) ;
337+ if ( ! loggedWebGL ) {
338+ console . log ( "loaded webgl!" ) ;
339+ loggedWebGL = true ;
340+ }
341+ } else {
342+ const addon = new CanvasAddon ( ) ;
343+ this . terminal . loadAddon ( addon ) ;
344+ this . canvasAddon = addon ;
318345 }
319346 }
320347
321- isWebGlEnabled ( ) : boolean {
322- return this . webglAddon != null ;
323- }
324-
325- enableWebGl ( ) {
326- if ( ! WebGLSupported || this . webglAddon != null ) {
327- return ;
328- }
329- this . loadWebGlAddon ( ) ;
348+ getTermRenderer ( ) : "webgl" | "canvas" {
349+ return this . webglAddon != null ? "webgl" : "canvas" ;
330350 }
331351
332- disableWebGl ( ) {
333- if ( this . webglAddon == null ) {
334- return ;
335- }
336- this . webglAddon . dispose ( ) ;
337- this . webglAddon = null ;
338- globalStore . set ( this . webglEnabledAtom , false ) ;
352+ isWebGlEnabled ( ) : boolean {
353+ return this . webglAddon != null ;
339354 }
340355
341356 resetCompositionState ( ) {
0 commit comments