I've needed a field for a project that shows hint text and have pretty much got there but there are a couple of very minor snags.
The snags are minor and this works very well, but would be nice to make it perfect
Project available here: https://github.com/stam66/skPlaceholderField
This includes a small UI as a control panel to modify the field's custom props. The stack is set up as a palette and can be used as a plugin (recommended) - you can adjust the 'prototype' field and copy it to your stack - or if you already have an skPlaceholderField in your stack you want to edit, select it in the IDE and click the arrow button top left.
The functionality I required
- If field is 'empty' it shows the hint text in a different style to 'normal' text entry (eg italic and light grey)
- On entering field nothing changes but on typing or pasting text, the hint text disappears and the text style changes (eg black, plain)
- If text is deleted the placeholder text reappears before exiting the field
- For fields/texts that are within the size of the field it enters the text vertically - but if the formatted text exceeds the height of the field, the text should be vertically aligned to top instead.
- Option to resize text as a function of field height (i.e. as a percentage of field height)
- Placeholder and normal text style changes based on whether the field background is light or dark
- Configurable properties - not hardcoded in handlers
The 2 minor issues
1. The way my code handles entering text is if the he placeholder text is showing, it removes it when the user types so that only the user's text (in normal text style) shows. To do this, I need to be able to check for presence of the placeholder text and remove it - which works well. But if the user clicks in the middle of the placeholder text that inserted text there which makes it very difficult to do this, so I set the selection to the start of the field which then works seamlessly.
Weirdly, putting code in openField did not help, the best I can do is with
Code: Select all
on selectionChanged if fieldIsEmpty() then select before me end selectionChanged
This works - but while the mouse is down, the cursor is inserted at the mouseLoc and on mouseUp jumps to the start of field.
On click this is isn't that noticeable but on persistent mouseDown it really is... it's a cosmetic issue but it's bugging me
Question: How can I immediately move the selection to the start of the text on mouseDown (mouseDown and openField don't work...)
2. Setting the text programmatically: Because it isn't possible to create a setProp-style handler for the field's text I can't trigger a textStyle change when setting text programmatically, but since this is triggered on field events this works:
Code: Select all
set the text of field "skPlaceholderField" to "some text here" send "exitField" to field "skPlaceholderField" -- triggers text formatting
Not sure if there is a way around these minor snags, but very grateful for any suggestions...
I'll post the code in a separate post to make this more readable