Skip to content

New ContextMenu API + example #4013

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

Merged
merged 9 commits into from
Jan 23, 2025

Conversation

leaanthony
Copy link
Member

@leaanthony leaanthony commented Jan 15, 2025

This PR introduces a simplified API for context menu creation and handling.

Example:

// Create context menu with a name
contextMenu := application.NewContextMenu("test")

// Construct context like a normal menu
clickMe := contextMenu.Add("Click Me")
contextDataMenuItem := contextMenu.Add("No Context Data")
clickMe.OnClick(func(data *application.Context) {
  app.Logger.Info("Context menu", "context data", data.ContextMenuData())

  // Update context menu like a normal menu
  contextDataMenuItem.SetLabel("My context data: " + data.ContextMenuData().(string))

  // Update context menu to apply changes, like a normal menu
  contextMenu.Update()
})

Fixes: #4012

Summary by CodeRabbit

  • New Features

    • Introduced a new context menu implementation with localized menu handling.
    • Added a new menu guide documentation.
    • Created a window menu example for Windows.
  • Changes

    • Updated ContextMenuData to return a string type.
    • Refactored context menu management to use a global approach.
    • Simplified context menu registration and handling.
  • Documentation

    • Added comprehensive menus guide.
    • Created README and example files for window menu functionality.
  • Bug Fixes

    • Improved error handling for context menu data retrieval.

Copy link
Contributor

coderabbitai bot commented Jan 15, 2025

Warning

Rate limit exceeded

@leaanthony has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 18 minutes and 14 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between b6a043d and 940e6cb.

📒 Files selected for processing (2)
  • docs/src/content/docs/changelog.mdx (2 hunks)
  • v3/examples/window-menu/README.md (1 hunks)

Walkthrough

The pull request introduces a comprehensive overhaul of the context menu system in Wails v3. The changes involve creating a new ContextMenu type, modifying how context menus are registered and managed, and updating the related methods across multiple files. The implementation shifts from a global context menu approach to a more localized and dynamic context menu handling strategy, with improved type safety and error handling.

Changes

File Change Summary
v3/pkg/application/menu.go Added new ContextMenu struct with Update() and Destroy() methods
v3/pkg/application/application.go Updated context menu handling methods and map type
v3/pkg/application/webview_window.go Removed instance-specific context menu management
v3/pkg/application/context.go Modified ContextMenuData() to return string
v3/pkg/application/messageprocessor_contextmenu.go Changed ContextMenuData struct to use string type
v3/examples/contextmenus/main.go Refactored context menu implementation
docs/src/content/docs/changelog.mdx Updated changelog to reflect changes in context menu handling
docs/src/content/docs/guides/menus.mdx Added new guide on creating and customizing menus
v3/examples/window-menu/README.md Added documentation for window menu example
v3/examples/window-menu/assets/about.html Introduced demo page for window menu functionality
v3/examples/window-menu/assets/index.html Created structured layout for window menu demo
v3/examples/window-menu/assets/style.css Added CSS for styling the window menu demo
v3/examples/window-menu/main.go Implemented a Wails application demonstrating window menu functionalities

Assessment against linked issues

Objective Addressed Explanation
Support dynamic context menu updates
Runtime context menu modification

Possibly related PRs

  • [v3-Windows] New DIP system for Enhanced High DPI Monitor Support #3665: The changes in this PR involve a new Device Independent Pixels (DIP) system that enhances support for high DPI monitors, which relates to the context menu overhaul in the main PR as both involve significant changes to how UI elements are managed and displayed in response to user interactions.
  • [windows] Add support for click-through overlay window #3667: This PR introduces methods for managing mouse events in webview windows, which is relevant to the main PR's changes in context menu interactions, as both involve user interface event handling and management.
  • Refactored Events #3731: The refactoring of the event handling API in this PR aligns with the changes in the main PR, as both involve modifications to how events are processed and handled within the application, enhancing the overall user interaction experience.

Poem

🐰 A rabbit's tale of menus new,
Context dancing, code so true,
Flexible and light as air,
Wails v3 beyond compare!
Hop, hop, hooray! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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. (Beta)
  • @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

cloudflare-workers-and-pages bot commented Jan 15, 2025

Deploying wails with  Cloudflare Pages  Cloudflare Pages

Latest commit: 940e6cb
Status:⚡️  Build in progress...

View logs

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

🧹 Nitpick comments (3)
v3/examples/contextmenus/main.go (1)

Line range hint 27-36: Consider cross-platform window options.

The window options are Mac-specific. Consider providing platform-agnostic examples or documenting platform-specific behavior.

docs/src/content/docs/guides/menus.mdx (2)

59-138: Consider adding platform-specific behavior notes.

The documentation of menu item properties and states is comprehensive. Consider adding notes about platform-specific behavior differences, particularly for properties like tooltips and accelerators.


300-491: Consider adding best practices for context menu cleanup.

While the context menu documentation is comprehensive, consider adding a section about best practices for cleaning up context menus when components are unmounted or destroyed, particularly in single-page applications where components may be frequently mounted and unmounted.

🧰 Tools
🪛 LanguageTool

[uncategorized] ~340-~340: Did you mean “precedent”? (As in ‘set an example’)
Context: ...e most specific (closest) setting takes precedence 3. The auto value can be used to rese...

(PRECEDENT_PRECEDENCE_CONFUSION)


[style] ~418-~418: Consider shortening or rephrasing this to strengthen your wording.
Context: ...nd #### Context Menu Management After making changes to a context menu, call the Update() met...

(MAKE_CHANGES)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6713061 and d212f24.

📒 Files selected for processing (7)
  • docs/src/content/docs/changelog.mdx (2 hunks)
  • docs/src/content/docs/guides/menus.mdx (1 hunks)
  • v3/examples/contextmenus/main.go (2 hunks)
  • v3/pkg/application/context.go (1 hunks)
  • v3/pkg/application/menuitem.go (1 hunks)
  • v3/pkg/application/messageprocessor_contextmenu.go (1 hunks)
  • v3/pkg/application/roles.go (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • v3/pkg/application/roles.go
🧰 Additional context used
🪛 LanguageTool
docs/src/content/docs/guides/menus.mdx

[grammar] ~34-~34: This phrase is duplicated. You should probably use “Radio Groups” only once.
Context: ... // true = initially checked ``` #### Radio Groups Radio groups allow users to select one option from a...

(PHRASE_REPETITION)


[duplication] ~43-~43: Possible typo: you repeated a word.
Context: ...u.AddRadio("Option 3", false) ``` #### Separators Separators are horizontal lines that help organise...

(ENGLISH_WORD_REPEAT_RULE)


[duplication] ~50-~50: Possible typo: you repeated a word.
Context: ...s: go menu.AddSeparator() #### Submenus Submenus are nested menus that appear when hover...

(ENGLISH_WORD_REPEAT_RULE)


[style] ~229-~229: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ...at the top of the screen (macOS). ### Application Menu Behaviour When you set an applica...

(ENGLISH_WORD_REPEAT_BEGINNING_RULE)


[uncategorized] ~340-~340: Did you mean “precedent”? (As in ‘set an example’)
Context: ...e most specific (closest) setting takes precedence 3. The auto value can be used to rese...

(PRECEDENT_PRECEDENCE_CONFUSION)


[style] ~418-~418: Consider shortening or rephrasing this to strengthen your wording.
Context: ...nd #### Context Menu Management After making changes to a context menu, call the Update() met...

(MAKE_CHANGES)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Run Go Tests (ubuntu-latest, 1.22)
  • GitHub Check: Run Go Tests (windows-latest, 1.22)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (5)
v3/pkg/application/messageprocessor_contextmenu.go (1)

11-11: Breaking change: Data field type constraint.

The change from any to string for the Data field improves type safety but may break existing implementations that use non-string data types.

Let's check for existing usages that might be affected:

v3/pkg/application/menuitem.go (1)

181-181: Breaking change: Role enum value renamed.

The change from 'Minimize' to 'Minimise' maintains consistency with British English spelling but may break existing code.

Let's check for existing usages:

docs/src/content/docs/guides/menus.mdx (2)

1-58: LGTM! Clear and comprehensive documentation of menu creation and basic items.

The documentation effectively covers menu creation and different types of menu items with clear examples.

🧰 Tools
🪛 LanguageTool

[grammar] ~34-~34: This phrase is duplicated. You should probably use “Radio Groups” only once.
Context: ... // true = initially checked ``` #### Radio Groups Radio groups allow users to select one option from a...

(PHRASE_REPETITION)


[duplication] ~43-~43: Possible typo: you repeated a word.
Context: ...u.AddRadio("Option 3", false) ``` #### Separators Separators are horizontal lines that help organise...

(ENGLISH_WORD_REPEAT_RULE)


[duplication] ~50-~50: Possible typo: you repeated a word.
Context: ...s: go menu.AddSeparator() #### Submenus Submenus are nested menus that appear when hover...

(ENGLISH_WORD_REPEAT_RULE)


139-223: LGTM! Comprehensive coverage of event handling and menu roles.

The documentation effectively explains event handling and provides a clear overview of available menu roles with platform-specific notes.

docs/src/content/docs/changelog.mdx (1)

33-33: LGTM! Clear and well-formatted changelog entries.

The changelog entries follow the Keep a Changelog format and accurately document the changes:

  1. Addition of the new menu guide
  2. Change in ContextMenuData return type from any to string

Also applies to: 43-43

leaanthony and others added 3 commits January 17, 2025 16:50
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

🧹 Nitpick comments (2)
v3/examples/window-menu/main.go (1)

29-33: Consider renaming "MenuBar" submenu to "View".

The "MenuBar" submenu name is unconventional. Consider moving the menu bar visibility controls under a more standard "View" menu, which is a common practice in desktop applications.

-editMenu := menu.AddSubmenu("MenuBar")
-editMenu.Add("Hide MenuBar").OnClick(func(ctx *application.Context) {
+viewMenu := menu.AddSubmenu("View")
+viewMenu.Add("Hide Menu Bar").OnClick(func(ctx *application.Context) {
v3/examples/window-menu/README.md (1)

3-3: Fix markdown formatting.

Remove spaces inside the emphasis markers.

-*** Windows Only ***
+***Windows Only***
🧰 Tools
🪛 Markdownlint (0.37.0)

3-3: null
Spaces inside emphasis markers

(MD037, no-space-in-emphasis)


3-3: null
Spaces inside emphasis markers

(MD037, no-space-in-emphasis)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9a4cb53 and 144329a.

📒 Files selected for processing (6)
  • docs/src/content/docs/changelog.mdx (2 hunks)
  • v3/examples/window-menu/README.md (1 hunks)
  • v3/examples/window-menu/assets/about.html (1 hunks)
  • v3/examples/window-menu/assets/index.html (1 hunks)
  • v3/examples/window-menu/assets/style.css (1 hunks)
  • v3/examples/window-menu/main.go (1 hunks)
✅ Files skipped from review due to trivial changes (3)
  • v3/examples/window-menu/assets/index.html
  • v3/examples/window-menu/assets/about.html
  • v3/examples/window-menu/assets/style.css
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/src/content/docs/changelog.mdx
🧰 Additional context used
🪛 Markdownlint (0.37.0)
v3/examples/window-menu/README.md

3-3: null
Spaces inside emphasis markers

(MD037, no-space-in-emphasis)


3-3: null
Spaces inside emphasis markers

(MD037, no-space-in-emphasis)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (3)
v3/examples/window-menu/main.go (3)

3-12: LGTM! Clean imports and proper asset embedding.

The imports are appropriate and the embed directive is correctly used to bundle assets.


13-20: LGTM! Clean application initialization.

The application setup follows best practices with proper configuration of name, description, and asset handling.


60-64: LGTM! Appropriate error handling for an example.

The error handling is simple but sufficient for demonstration purposes.

Comment on lines 47 to 57
KeyBindings: map[string]func(window *application.WebviewWindow){
"F1": func(window *application.WebviewWindow) {
window.ToggleMenuBar()
},
"F2": func(window *application.WebviewWindow) {
window.ShowMenuBar()
},
"F3": func(window *application.WebviewWindow) {
window.HideMenuBar()
},
},
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

Resolve documentation-code mismatch for key bindings.

The README mentions F10 for menu bar toggling, but the code implements F1, F2, and F3 keys. This inconsistency should be resolved either by:

  1. Updating the README to reflect the actual F1, F2, F3 key bindings, or
  2. Updating the code to use F10 as documented

Comment on lines 22 to 23
The example creates a window with a default menu and binds the F10 key to toggle the menu bar's visibility. The menu bar will hide when F10 is pressed and show when F10 is released.

Copy link
Contributor

@coderabbitai coderabbitai bot Jan 17, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update key binding documentation.

The documentation incorrectly states that F10 is used for toggling the menu bar. The actual implementation uses:

  • F1 to toggle the menu bar
  • F2 to show the menu bar
  • F3 to hide the menu bar

Copy link
Member Author

Choose a reason for hiding this comment

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

Make a committable suggestion

Copy link
Contributor

Choose a reason for hiding this comment

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

@leaanthony Here's the corrected documentation:

# Window Menu Example

*** Windows Only ***

This example demonstrates how to create a window with a menu bar that can be toggled using the window.ToggleMenuBar() method.

## Features

- Default menu bar with File, Edit, and Help menus
- F1 key to toggle menu bar visibility 
- F2 key to show menu bar
- F3 key to hide menu bar
- Simple HTML interface with instructions

## Running the Example

```bash
cd v3/examples/window-menu
go run .

How it Works

The example creates a window with a default menu and binds function keys for menu bar control:

  • F1 toggles the menu bar visibility
  • F2 shows the menu bar
  • F3 hides the menu bar

<!-- This is an auto-generated reply by CodeRabbit -->

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

♻️ Duplicate comments (1)
v3/examples/window-menu/README.md (1)

22-22: ⚠️ Potential issue

Fix inconsistent key binding documentation.

The documentation mentions both F1 and F10 for toggling the menu bar. Based on the previous review discussion, F10 is incorrect:

-The example creates a window with a default menu and binds the F1 key to toggle the menu bar's visibility. The menu bar will hide when F10 is pressed and show when F10 is released.
+The example creates a window with a default menu and binds function keys for menu bar control. The menu bar visibility can be toggled with F1, shown with F2, and hidden with F3.
🧹 Nitpick comments (1)
v3/examples/window-menu/README.md (1)

3-3: Fix markdown formatting.

Remove spaces inside emphasis markers:

-*** Windows Only ***
+***Windows Only***
🧰 Tools
🪛 Markdownlint (0.37.0)

3-3: null
Spaces inside emphasis markers

(MD037, no-space-in-emphasis)


3-3: null
Spaces inside emphasis markers

(MD037, no-space-in-emphasis)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 144329a and b6a043d.

📒 Files selected for processing (1)
  • v3/examples/window-menu/README.md (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.37.0)
v3/examples/window-menu/README.md

3-3: null
Spaces inside emphasis markers

(MD037, no-space-in-emphasis)


3-3: null
Spaces inside emphasis markers

(MD037, no-space-in-emphasis)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Analyze (go)
  • GitHub Check: Cloudflare Pages
🔇 Additional comments (2)
v3/examples/window-menu/README.md (2)

24-24: ⚠️ Potential issue

Update platform-specific note.

The note about F10 should be updated to reflect the correct key bindings:

-Note: The menu bar toggling functionality only works on Windows. On other platforms, the F10 key binding will have no effect.
+Note: The menu bar toggling functionality only works on Windows. On other platforms, the function key bindings (F1, F2, F3) will have no effect.

Likely invalid or redundant comment.


9-11: ⚠️ Potential issue

Complete the features list.

Based on the previous review discussion, the features list is missing F2 and F3 key bindings:

 - Default menu bar with File, Edit, and Help menus
 - F1 key to toggle menu bar visibility 
+- F2 key to show menu bar
+- F3 key to hide menu bar
 - Simple HTML interface with instructions

Likely invalid or redundant comment.

@leaanthony leaanthony merged commit 631c8a1 into v3-alpha Jan 23, 2025
12 of 14 checks passed
@leaanthony leaanthony deleted the v3-alpha-bugfix/4012-dynamic-context-menu branch January 23, 2025 11:10
@coderabbitai coderabbitai bot mentioned this pull request Jan 25, 2025
15 tasks
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.

1 participant