System-Color-Picker icon indicating copy to clipboard operation
System-Color-Picker copied to clipboard

Improve text field input

Open W1W1-M opened this issue 3 years ago β€’ 6 comments

Hi @sindresorhus This PR is a first draft for issue #3 Validate text fields

I first tried a border color for the hex text field but it interfered with blue border when the text field is focused. Instead I added a SF symbol image that switches depending if the field is valid.

Capture d’écran 2022-08-17 aΜ€ 14 32 19 Capture d’écran 2022-08-17 aΜ€ 14 36 32

The implemented colorChecker class uses ObservedObject and the onChange modifier to run on user input, it also trims unwanted characters.

The code still needs to be implemented for hsl, rgb & lch text inputs. Also needs cleaning, optimizing & testing but I wanted an initial πŸ‘ before going ahead.

Regards

PS : I have more experience with iOS app development, this is helping me get to grips with Mac app development πŸ˜„

W1W1-M avatar Aug 17 '22 12:08 W1W1-M

Thanks for working on this. I don't think this is the right approach though.

Here's what I'm thinking:

  1. Abstract all of the hexColorView type views into a single (general) view that can be reused for each of the color inputs.
  2. The validation logic is already implemented, if let newColor = NSColor(hexString: hexColor.trimmingCharacters(in: .whitespaces)) succeeds, it means the color is valid. If not, you can show the invalid state.

I'm also not a big fan of the symbols. We don't need a success indication, just a failure one. So I think just coloring the text red if it's invalid is enough.

sindresorhus avatar Aug 17 '22 20:08 sindresorhus

  1. Abstract all of the hexColorView type views into a single (general) view that can be reused for each of the color inputs.

Ok, I'll work on that and update accordingly

  1. The validation logic is already implemented, if let newColor = NSColor(hexString: hexColor.trimmingCharacters(in: .whitespaces)) succeeds, it means the color is valid. If not, you can show the invalid state.

I didn't realize all the logic was already there πŸ˜… Thanks for pointing this out !

I'm also not a big fan of the symbols. We don't need a success indication, just a failure one. So I think just coloring the text red if it's invalid is enough.

Ok, simple and straightforward is good πŸ˜„ I'll update accordingly

W1W1-M avatar Aug 17 '22 20:08 W1W1-M

So this is still WIP, but some feedback to know if this is more inline with the proposed approach would be nice πŸ˜ƒ

As for hex codes: they are limited to a small set of characters and also in length, I've added some code to filter and trim hex inputs (ex: #123456, FFFFFF, etc). This helps users input only valid hex codes but also makes the input rigid and removes the need to indicate (in red) an invalid input. Maybe a compromise of just trimming excess characters without filtering is a good balance ?

W1W1-M avatar Aug 18 '22 20:08 W1W1-M

So this is still WIP, but some feedback to know if this is more inline with the proposed approach would be nice πŸ˜ƒ

Yes, it's the correct direction.

sindresorhus avatar Aug 19 '22 13:08 sindresorhus

This helps users input only valid hex codes but also makes the input rigid

Trimming whitespace is ok, but I don't think we should prevent some characters. Users will not understand that it's because it's not valid Hex. I think it's better to just make it red in that case.

sindresorhus avatar Aug 19 '22 13:08 sindresorhus

On user color input, updateColorsFromPanel method needs to be called to update other color codes. This method is currently in ColorPickerScreen. In order to be able to call it from each ColorInputView I believe it needs to be moved to AppState EnvironmentObject or some other class, along with required properties (as @Published properties) ? Or maybe there is smarter way to call it ? Or does it need a bigger rethink as the existing comment indicates:

TODO: Find a better way to handle this.

What's your opinion on this @sindresorhus ?

W1W1-M avatar Aug 19 '22 21:08 W1W1-M

Closing for lack of activity.

sindresorhus avatar Jun 20 '23 12:06 sindresorhus