Skip to content

Pass theme colours to Widget Preview on querystring in the same format as DevTools #5702

Closed

Description

@DanTup

To ensure the widget preview fits in whether it's in the sidebar or editor tab, we should pass the bg/fg colours on the querystring. They should be in exactly the same format as embedded DevTools pages (which I think is 6-digit hex code, with the hash being optional - since VS Code's URI class has encoding issues).

Activity

  1. added this to the v3.120.0 milestone on Sep 16, 2025
  2. added
    is enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.
    in flutterRelates to running Flutter apps
    on Sep 16, 2025
  3. DanTup commented on Sep 17, 2025

    @DanTup
    MemberAuthor

    @bkonyi for DevTools, we support sending updated theme info via postMessage so that if the VS Code theme changes, we can update DevTools without refreshing and losing any state. Should we do the same here?

    eg. we pass on QS for initial load, but then postMessage for updates:

    function sendTheme() {
    	const devToolsFrame = document.getElementById('devToolsFrame');
    	const theme = getTheme();
    	devToolsFrame.contentWindow.postMessage({
    		method: 'editor.themeChanged',
    		params: {
    			kind: 'themeChanged',
    			theme: theme,
    		}
    	}, "*");
    }
    
    function getTheme() {
    	const isDarkMode = !document.body.classList.contains('vscode-light');
    	const backgroundColor = currentBackgroundColor = getComputedStyle(document.documentElement).getPropertyValue('--vscode-editor-background');
    	const foregroundColor = getComputedStyle(document.documentElement).getPropertyValue('--vscode-editor-foreground');
    
    	return {
    		isDarkMode: isDarkMode,
    		backgroundColor: backgroundColor,
    		foregroundColor: foregroundColor,
    	};
    }

    And the initial querystring looks like:

    const theme = getTheme();
    const themeKind = theme.isDarkMode ? 'dark' : 'light';
    // Don't include # in colors
    // https://github.com/flutter/flutter/issues/155992
    let url = \`\${message.urls.viewUrl}&theme=\${themeKind}&backgroundColor=\${encodeURIComponent(theme.backgroundColor?.replace('#', ''))}&foregroundColor=\${encodeURIComponent(theme.foregroundColor?.replace('#', ''))}\`;
  4. DanTup commented on Sep 22, 2025

    @DanTup
    MemberAuthor

    Looks like there's no explicit background on the previewer, so the background from the sidebar already passes through.

    theme.mp4

    However I'm still making the change noted above, so we'll pass bg/fg colors + theme kind (dark/light) on the querystring initially, and then postMessage any changes to the frame.

  5. bkonyi commented on Sep 22, 2025

    @bkonyi

    Ah, good catch! I didn't realize that. Either way, it'll still be good to have these colors to use for determining theming of the previewer elements.

  6. DanTup commented on Sep 22, 2025

    @DanTup
    MemberAuthor

    It might also be useful for the user to be able to override the dark/light theme from the editor temporarily so they can review both themes without having to change their VS Code theme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in flutterRelates to running Flutter appsin flutter widget previewRelates to the embedded Flutter widget previewis enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions