Skip to content

Conversation

@rafa-rrayes
Copy link

@rafa-rrayes rafa-rrayes commented Nov 13, 2025

Add TextFieldMenuItem: Editable text fields in menu bar dropdowns

Overview

This PR adds a new TextFieldMenuItem class that allows editable text fields to appear directly inside menu bar dropdowns, following the same pattern as the existing SliderMenuItem.

Motivation

Menu bar apps often need to collect simple text input from users (searches, API keys, usernames, etc.). Currently, this requires opening separate windows or alerts. With TextFieldMenuItem, users can type directly into the menu, providing a more streamlined and native macOS experience.

Features

  • Native NSTextField integration - Built around macOS NSTextField for native look and feel
  • Modern styling - Rounded corners with proper bezeling, matching macOS design standards
  • Customizable appearance - Configurable dimensions and margins for flexible layouts
  • Decorator support - @text_field decorator for declarative menu creation
  • Callback system - Triggers callback when text changes or Enter is pressed
  • Property access - Get/set text value and placeholder programmatically

Usage

Direct instantiation:

import rumps

class MyApp(rumps.App):
    def __init__(self):
        super(MyApp, self).__init__("My App")
        
        self.search_field = rumps.TextFieldMenuItem(
            placeholder='Search...',
            callback=self.on_search,
            dimensions=(200, 24),
            margins=(15, 5, 15, 5)
        )
        
        self.menu = ['Search', self.search_field]
    
    def on_search(self, sender):
        print(f"Search: {sender.value}")

Using the decorator:

@rumps.text_field('Settings', 'Username', value='admin', placeholder='Enter username')
def on_username_changed(self, sender):
    print(f"Username: {sender.value}")

Implementation Details

  • Follows the exact same architecture as SliderMenuItem for consistency
  • Uses NSView container with NSTextField child
  • Integrates with existing callback mechanism via NSApp._ns_to_py_and_callback
  • Default dimensions: 200×24 pixels
  • Default margins: 15px left/right, 5px top/bottom
  • Applies NSTextFieldRoundedBezel style for modern appearance

API

TextFieldMenuItem

Parameters:

  • value (str): Initial text value (default: '')
  • placeholder (str): Placeholder text (default: '')
  • callback (callable): Function called on text change (default: None)
  • dimensions (tuple): Width and height in pixels (default: (200, 24))
  • margins (tuple): Left, top, right, bottom margins (default: (15, 5, 15, 5))

Properties:

  • value - Get/set current text
  • placeholder - Get/set placeholder text
  • callback - Get current callback function

Methods:

  • set_callback(callback) - Set or change callback function

Files Changed

  • rumps.py - Added TextFieldMenuItem class and text_field decorator
  • init.py - Exported new class and decorator
  • example_text_field.py - Complete working example with various configurations
  • TextFieldMenuItem.md - Full API documentation

Testing

Tested on macOS with PyObjC. The feature integrates seamlessly with existing rumps apps and follows the same patterns as other menu item types.

Backwards Compatibility

This is a purely additive change with no breaking modifications to existing functionality.

…owns

- Add TextFieldMenuItem class with NSTextField support
- Add @text_field decorator for declarative menu creation
- Support for rounded corners and customizable margins
- Include value, placeholder, and callback properties
- Add example and documentation
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