Skip to content

Fix text color handling in custom themes #519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Kartikayy007
Copy link

@Kartikayy007 Kartikayy007 commented Mar 9, 2025

Fixes #518

Fix Text Color Handling in Custom Themes

Description

This PR addresses issues with text color handling in custom themes, particularly focusing on text annotations and their visibility against different backgrounds.

Changes Made

  • Fixed text color handling in custom themes to ensure proper visibility
  • Removed duplicate color assignments in getCanvasColors()
  • Separated text-related CSS variables to prevent unintended color effects
  • Improved type safety in theme handling functions

Context

Previously, text annotations were not changing color correctly in custom themes despite working properly in automatic themes. This was causing visibility issues when users applied custom themes.

Technical Details

  • Modified customThemeAbstraction.js to properly handle text-related CSS variables
  • Updated themer.js to remove redundant color assignments and improve theme application
  • Ensured proper separation between text colors and background colors

Testing

To test these changes:

  1. Apply different themes (both automatic and custom)
  2. Verify text annotations are visible in all themes
  3. Check that all text elements update properly when changing themes

Related

CircuitVerse/CircuitVerse#4460 (comment)
Fixes #5487

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • Code quality improvement
  • Type system enhancement

Screenshots (if appropriate)

Screen.Recording.2025-03-10.at.3.38.27.AM.mov

Copy link
Contributor

coderabbitai bot commented Mar 9, 2025

Walkthrough

The pull request refines the theming system by restructuring the changeCustomTheme function in the Vue component and updating the theme abstraction and style management across simulator modules. The changes streamline code readability by caching DOM elements and separating value extraction. They also introduce explicit handling for text color properties, adjust reference arrays in the theme abstraction, and force a canvas refresh after updating theme styles.

Changes

File(s) Summary of Changes
src/components/DialogBox/Themes/ApplyThemes.vue Refactored changeCustomTheme: caches the event target, extracts name/value into variables, adds explicit handling for the 'Text' theme by setting --text, updates customThemesList, and introduces a timeout to force a canvas re-render.
v0/src/simulator/src/themer/customThemeAbstraction.js
v1/src/simulator/src/themer/customThemeAbstraction.js
Updated CreateAbstraction function: modified the text color source (v0: from themeOptions['--text-lite'] to themeOptions['--text']) and adjusted the ref array for text properties (v0: now ['--text', '--text-panel', '--input-text']; v1: expanded to include --text with original values).
v0/src/simulator/src/themer/themer.js
v1/src/simulator/src/themer/themer.js
Enhanced updateThemeForStyle: simplified the selectedTheme check (using a falsy check), replaced document.getElementsByTagName('html')[0] with document.documentElement, implemented explicit handling for the --text CSS variable, and added a timeout mechanism to force the canvas refresh (v1 also adds colors['text'] in getCanvasColors).

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant D as DialogBox
    participant TM as Theme Manager
    participant T as Themer Module
    participant C as Canvas

    U->>D: Trigger input event
    D->>D: Execute changeCustomTheme(e) 
    D->>TM: Update customTheme in list
    TM->>T: Call updateThemeForStyle(themeName)
    T->>document: Update HTML CSS properties (incl. --text)
    T->>C: Set timeout to call renderCanvas()
    C-->>T: Canvas refresh complete
Loading

Suggested reviewers

  • JoshVarga

Poem

In a code field, I hopped along,
Refactoring functions into a song,
Setting themes with care and might,
Updating text to shine so bright,
A bunny's delight in every byte!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Mar 9, 2025

Deploy Preview for circuitverse ready!

Name Link
🔨 Latest commit a7eeece
🔍 Latest deploy log https://app.netlify.com/sites/circuitverse/deploys/67ce10d6234e4e0008be2432
😎 Deploy Preview https://deploy-preview-519--circuitverse.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 33
Accessibility: 72
Best Practices: 92
SEO: 80
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
v0/src/simulator/src/themer/themer.js (1)

104-109: Consider using optional chaining for window.globalScope

The forced canvas refresh ensures that text elements update immediately with the new colors. However, there's a potential to simplify the conditional check.

-    if (window.globalScope && window.globalScope.renderCanvas) {
+    if (window.globalScope?.renderCanvas) {
        setTimeout(() => {
            window.globalScope.renderCanvas()
        }, 10)
    }
🧰 Tools
🪛 Biome (1.9.4)

[error] 105-105: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

v1/src/simulator/src/themer/themer.js (1)

108-111: Force canvas redraw to apply theme changes immediately

Adding the canvas redraw ensures that theme changes are immediately visible to users without requiring a page refresh. This improves the user experience when switching between themes.

Consider using optional chaining for safer object property access:

- if (window.globalScope && window.globalScope.renderCanvas) {
+ if (window.globalScope?.renderCanvas) {
    setTimeout(() => window.globalScope.renderCanvas(), 10)
}
🧰 Tools
🪛 Biome (1.9.4)

[error] 109-109: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2a9f309 and a7eeece.

📒 Files selected for processing (5)
  • src/components/DialogBox/Themes/ApplyThemes.vue (1 hunks)
  • v0/src/simulator/src/themer/customThemeAbstraction.js (1 hunks)
  • v0/src/simulator/src/themer/themer.js (1 hunks)
  • v1/src/simulator/src/themer/customThemeAbstraction.js (1 hunks)
  • v1/src/simulator/src/themer/themer.js (2 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
v0/src/simulator/src/themer/themer.js

[error] 105-105: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

v1/src/simulator/src/themer/themer.js

[error] 109-109: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (7)
v1/src/simulator/src/themer/customThemeAbstraction.js (1)

36-36: Good addition of --text to the reference list

Adding the --text variable to the ref array ensures that text color settings properly propagate to all relevant CSS variables, which helps fix the visibility issues mentioned in the PR objectives.

v0/src/simulator/src/themer/customThemeAbstraction.js (1)

34-36: LGTM: Improved text color handling

The changes from --text-lite to --text as the source property and updating the reference array to include --text and --input-text establish a more consistent approach to text color handling across the application.

src/components/DialogBox/Themes/ApplyThemes.vue (1)

183-221: Well-structured refactoring of custom theme handling

The restructuring of the changeCustomTheme function significantly improves code quality by:

  1. Caching the DOM element and separating value extraction
  2. Adding explicit handling for text color properties
  3. Forcing canvas refresh to ensure changes are visible

This directly addresses the issue of text annotations not changing color appropriately in custom themes.

v0/src/simulator/src/themer/themer.js (2)

87-90: Good simplification of condition check and element access

Changing from if (selectedTheme === undefined) to if (!selectedTheme) and using document.documentElement instead of document.getElementsByTagName('html')[0] improves code readability and robustness.


96-99: Great explicit handling for text color in custom themes

This special case handling ensures that the --text property is properly set for custom themes, which directly addresses the core visibility issue this PR aims to fix.

v1/src/simulator/src/themer/themer.js (2)

90-93: Code improvement: More concise condition and variable declarations

The condition has been simplified from explicitly checking for undefined to a more general falsy check, which is more concise and handles more edge cases. Additionally, using document.documentElement is a more direct approach than document.getElementsByTagName('html')[0].


99-103: Explicitly handle text color in themes

This special handling for the --text property is crucial for ensuring text color visibility across themes, which addresses the core issue mentioned in the PR objectives. By explicitly setting the text color when available, it ensures that text annotations will render with proper contrast against various backgrounds.

Comment on lines +72 to +74
colors['text'] = getComputedStyle(
document.documentElement
).getPropertyValue('--text')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove duplicate assignment to colors['text']

The colors['text'] property is already assigned at lines 51-53 with the same value. This duplicate assignment should be removed to avoid redundancy and potential confusion.

- colors['text'] = getComputedStyle(
-     document.documentElement
- ).getPropertyValue('--text')
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
colors['text'] = getComputedStyle(
document.documentElement
).getPropertyValue('--text')

@Kartikayy007
Copy link
Author

@JoshVarga please review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐞 Bug: Text annotations not changing text color in custom themes.
1 participant