Skip to content

Add OSD preview rulers #4567

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 12 commits into
base: master
Choose a base branch
from
Open

Conversation

haslinghuis
Copy link
Member

@haslinghuis haslinghuis commented Aug 11, 2025

image image

Summary by CodeRabbit

  • New Features

    • Added a ruler overlay for the preview with an on/off toggle (enabled by default). Overlay repaints after preview updates and on window resize.
  • UI Improvements

    • Updated preview layout to occupy full height and support the overlay.
    • Adjusted preview margins and increased bottom bar spacing.
    • Standardized progress control appearance across browsers.
  • Localization

    • Added a "Rulers" label for the new toggle.

Copy link
Contributor

coderabbitai bot commented Aug 11, 2025

Walkthrough

Adds a canvas-based ruler overlay to the OSD preview with a default-enabled toggle, updates preview DOM and styles to host the overlay, implements ruler drawing and lifecycle (resize, toggle, render hook, cleanup), and adds a localization entry for the toggle.

Changes

Cohort / File(s) Summary
UI markup & overlay scaffolding
src/tabs/osd.html
Adds a default-checked rulers toggle (.osd-preview-rulers-selector), changes zoom label markup, and wraps the .preview in .preview-container containing a hidden canvas.ruler-overlay.
Ruler overlay feature & lifecycle
src/js/tabs/osd.js
Adds OSD.rulerConfig, initialization (initializeRulers, setupRulerContext), geometry helpers (_colCenterX, _rowCenterY), axis draw routines (_drawTopAxis, _drawBottomAxis, _drawLeftAxis, _drawRightAxis), OSD.drawRulers() orchestration, toggle handling, resize listener (resize.osd-rulers) with rAF/throttle, render-hook invocation and cleanup; removes TL.grow entry in positionConfigs.
Styles for preview and rulers
src/css/tabs/osd.less
Standardizes appearance on progress, sets .display-layout { height: 100% }, adds .preview-container and .ruler-overlay scaffolding, adjusts .preview margins, increases .gui_box_bottombar margin-top, and adds spacing classes for ruler controls.
Localization
locales/en/messages.json
Adds osdSetupPreviewCheckRulers ("Rulers") message for the new toggle label.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant OSD UI
  participant OSD.drawRulers
  participant Canvas

  User->>OSD UI: Toggle "Rulers" checkbox
  OSD UI->>OSD.drawRulers: invoke (enabled)
  OSD.drawRulers->>Canvas: measure preview, size canvas, draw axes/ticks/labels
Loading
sequenceDiagram
  participant Window
  participant OSD UI
  participant OSD.drawRulers
  participant Canvas

  Window-->>OSD UI: resize.osd-rulers event
  OSD UI->>OSD.drawRulers: redraw (throttled/requestAnimationFrame)
  OSD.drawRulers->>Canvas: recompute bounds, resize canvas, redraw rulers
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Assessment against linked issues

Objective Addressed Explanation
Provide visual guides/rulers on OSD tab to aid alignment [#3536]
Allow measuring/counting pixels and reference to center for placement [#3536]

Assessment against linked issues: Out-of-scope changes

Code Change Explanation
Removal of grow: { x: 0, y: 1 } from positionConfigs (src/js/tabs/osd.js) Position config tweak not required by the rulers objective; appears unrelated to guides feature.
Replace -webkit-appearance with appearance on progress (src/css/tabs/osd.less) CSS normalization change unrelated to the rulers functionality.
Increase .gui_box_bottombar margin-top to 30px (src/css/tabs/osd.less) Spacing/layout adjustment not related to adding ruler guides.

Possibly related PRs

Suggested reviewers

  • nerdCopter
  • VitroidFPV

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b9c362c and 654d71e.

📒 Files selected for processing (2)
  • src/js/tabs/osd.js (4 hunks)
  • src/tabs/osd.html (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/js/tabs/osd.js
  • src/tabs/osd.html
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.
    • Explain this complex logic.
    • 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 explain this code block.
  • 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 explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests 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.

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
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: 6

🧹 Nitpick comments (1)
src/css/tabs/osd.less (1)

211-212: Consider using CSS variables for margin values

The hard-coded 20px margins might not scale well across different screen sizes or zoom levels. Consider using CSS variables or relative units.

-            margin-top: 20px;
-            margin-left: 20px;
+            margin-top: var(--ruler-margin, 20px);
+            margin-left: var(--ruler-margin, 20px);
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a80b170 and 8f83dd8.

📒 Files selected for processing (3)
  • src/css/tabs/osd.less (5 hunks)
  • src/js/tabs/osd.js (4 hunks)
  • src/tabs/osd.html (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-06-14T13:42:41.217Z
Learnt from: syahmizul
PR: betaflight/betaflight-configurator#4516
File: src/js/tabs/osd.js:3544-3559
Timestamp: 2025-06-14T13:42:41.217Z
Learning: In OSD positioning systems, width/height calculations like `limits.maxX - limits.minX` work in conjunction with centering coordinate formulas like `Math.floor((OSD.data.displaySize.x - w) / 2)`. Adding +1 to width calculations breaks centering by making elements off-center by half a character position. The entire positioning system must be considered holistically when making changes.

Applied to files:

  • src/js/tabs/osd.js
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: deploy
🔇 Additional comments (4)
src/css/tabs/osd.less (2)

31-31: Good: Using standard CSS property

Removing the vendor prefix in favor of the standard appearance property improves cross-browser compatibility.


192-206: LGTM: Well-structured overlay implementation

The preview container and ruler overlay CSS is properly structured with appropriate positioning and pointer-events handling.

src/js/tabs/osd.js (1)

4230-4243: LGTM: Proper resize throttling implementation

The window resize listener properly uses requestAnimationFrame for throttling and includes a timeout to ensure the preview has resized first. Good implementation.

src/tabs/osd.html (1)

63-67: LGTM: Well-structured DOM for overlay

The preview container structure with the overlay canvas is properly implemented. The initial hidden state and DOM hierarchy support the overlay rendering approach.

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: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8f83dd8 and 3e4a4b9.

📒 Files selected for processing (2)
  • locales/en/messages.json (1 hunks)
  • src/js/tabs/osd.js (5 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-06-14T13:43:10.231Z
Learnt from: syahmizul
PR: betaflight/betaflight-configurator#4516
File: src/js/tabs/osd.js:3544-3559
Timestamp: 2025-06-14T13:43:10.231Z
Learning: In the Betaflight Configurator OSD positioning feature (src/js/tabs/osd.js), the elementWidth and elementHeight calculations use limits.maxX - limits.minX and limits.maxY - limits.minY without adding 1. While adding 1 would be mathematically correct for inclusive bounds, it causes visual centering issues where elements appear off-center. The current calculation produces better visual centering results, so the +1 correction should be avoided to maintain proper visual alignment.

Applied to files:

  • src/js/tabs/osd.js
📚 Learning: 2025-06-14T13:42:41.217Z
Learnt from: syahmizul
PR: betaflight/betaflight-configurator#4516
File: src/js/tabs/osd.js:3544-3559
Timestamp: 2025-06-14T13:42:41.217Z
Learning: In OSD positioning systems, width/height calculations like `limits.maxX - limits.minX` work in conjunction with centering coordinate formulas like `Math.floor((OSD.data.displaySize.x - w) / 2)`. Adding +1 to width calculations breaks centering by making elements off-center by half a character position. The entire positioning system must be considered holistically when making changes.

Applied to files:

  • src/js/tabs/osd.js
🔇 Additional comments (9)
src/js/tabs/osd.js (9)

1-36: LGTM! Well-organized constants with clear documentation

The ruler constants are properly organized at the file level with clear naming and helpful comments. This follows the feedback from previous reviews to extract magic numbers into named constants.


2363-2383: Good consolidation of ruler configuration

The OSD.rulerConfig object effectively consolidates all ruler-related configuration in one place, making it easy to maintain and modify. The backwards compatibility comment for labelPadding is helpful.


2408-2457: Robust context setup with proper error handling

The setupRulerContext function properly validates DOM elements and handles edge cases like empty element arrays. The canvas resizing logic and context configuration are appropriate.


2459-2466: Simple and effective coordinate helpers

The column and row center calculation helpers are concise and correctly handle the coordinate transformations using getBoundingClientRect().


2468-2656: Well-structured axis drawing functions

The four axis drawing functions (_drawTopAxis, _drawBottomAxis, _drawLeftAxis, _drawRightAxis) follow a consistent pattern and properly handle:

  • Major/minor tick distinctions
  • Center line highlighting
  • Label positioning with edge constraints
  • Text stroke effects for visibility

The logic for determining major ticks and label positioning is appropriate for each axis orientation.


2658-2670: Clean coordinator function with proper error handling

The main drawRulers function acts as a clean coordinator that:

  • Uses the configuration object
  • Handles initialization failures gracefully
  • Orchestrates the four axis drawing functions
  • Maintains a clear separation of concerns

4281-4282: Proper integration with preview rendering lifecycle

The ruler drawing is correctly placed after the preview is rendered, ensuring the DOM elements are available for measurement and positioning.


4317-4336: Well-implemented event handling with proper throttling

The event handlers are properly implemented:

  • Change handler for the rulers toggle
  • Resize handler with proper throttling using requestAnimationFrame
  • Cleanup to prevent memory leaks
  • Timeout to allow preview to resize before redrawing rulers

The throttling mechanism prevents excessive redraw calls during window resizing.


4458-4458: Proper cleanup prevents memory leaks

The cleanup properly unbinds the resize event handler with the correct namespace, preventing memory leaks when the tab is closed.

@haslinghuis haslinghuis changed the title Add OSD meters Add OSD preview rulers Aug 11, 2025
Copy link

Copy link
Contributor

Preview URL: https://654d71e0.betaflight-configurator.pages.dev

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

Successfully merging this pull request may close these issues.

Request guides on OSD tab to help with aligning OSD elements (screenshot of idea)
1 participant